Introduction
Beam is a new way of quickly prototyping AI projects. In this example, we’ll show how to deploy a serverless API endpoint that generates images with stable diffusion.Setting up the environment
First, we’ll setup the environment to run Stable Diffusion. We’re going to define a few things:Appwith a unique nameRuntimewith CPU and memory requirements, and anA10GGPUImagewith Python packages required to run stable diffusionVolumeto mount a storage volume to cache the model weights
Inference function
You’ll write a simple function that takes a prompt passed from the user and returns an image generated using Stable Diffusion.Saving image outputs
Notice theimage.save() method below. We’re going to save our generated images to an Output file, by passing an outputs argument to our function:
app.py
Adding callbacks
If you supply acallback_url argument, Beam will make a POST request to your server whenever a task completes:
app.py
Deployment
In your teriminal, run:
Calling the API
In the dashboard, click Call API to view the API URL.
Querying the status of a job
You can use the/v1/task/{task_id}/status/ API to retrieve the status of a job. Using the task ID, here’s how you can get the output with the API.
url to the generated image in the outputs object.