Stable Diffusion with LoRAs
Introduction
This guide demonstrates how to run Stable Diffusion with custom LoRAs.
View the Code
See the code for this example on Github.
Setup Remote Environment
The first thing we’ll do is setup an Image
with the Python packages required for this app.
Because this script will run remotely, we need to make sure our local Python interpreter doesn’t try to install these packages locally.
We’ll use the if env.is_remote()
flag to conditionally import the Python packages only when the script is running remotely on Beam.
Pre-Load Models
Next, we’ll set up a function to run once when the container first starts up. This allows us to cache the model in memory between requests and ensures we don’t unnecessarily re-load the model.
Inference Function
Here’s our inference function. By adding the @endpoint()
decorator to it, we can expose this function as a RESTful API.
There are a few things to take note of:
- an
image
with the Python requirements we defined above - an
on_start
function that runs once when the container first boots. The value fromon_start
(in this case, ourpipe
handler) is available in the inference function using thecontext
value:pipe = context.on_start_value
volumes
, which are used to store the downloaded LoRAs and model weights on Beamkeep_warm_seconds
, which tells Beam how long to keep the container running between requests
Saving Image Outputs
Notice the Output.from_pil_image(image).save()
method below.
This will generate a sharable URL to access the images created from the inference function:
Create a Preview Deployment
You can spin up a temporary REST API to test this endpoint on Beam, using the beam serve
command:
When you run this command, Beam will spin up a GPU-backed container to test your code on the cloud:
You can paste the curl
command in your shell to call the API.
The API will return a pre-signed URL with the image generated:
medieval rich kingpin sitting in a tavern, raw
Deploy to Production
The beam serve
command is used for temporary APIs. When you’re ready to move to production, deploy a persistent endpoint:
Was this page helpful?