Stable Diffusion API
Building an API to generate images with stable diffusion
Define the environment
First, create a file with your Beam App definition. You can name this whatever
you want. In this example, name it app.py
.
Inference Function
Write a simple function that takes a prompt passed from the user, and returns an image generated using Stable Diffusion.
You need an access token from Huggingface to run this example. You can sign up for Huggingface and access your token on the settings page, and store it in the Beam Secrets Manager.
Notice the image.save()
method below. You defined a file path called output.png
in app.py
, and that’s where your images will be saved.
Deploying the API
In your teriminal, run:
You’ll see the deployment appear in the dashboard.
Generating images
In the dashboard, click Call API to view the API URL.
Paste the code into your terminal to make a request.
The API returns a Task ID.
Querying the status of a job
You will use the /task
API to retrieve the status of a job, passing in the Task ID.
This returns the generated image in the outputs
dictionary, as a pre-signed URL.