Define the environment

First, create a file with your Beam App definition. You can name this whatever you want. In this example, name it
import beam

# The environment your code will run on
app = beam.App(
    python_packages=["diffusers", "transformers", "torch", "pillow"],

# Deploys function as async webhook
    inputs={"prompt": beam.Types.String()},

# File to store image outputs
app.Output.File(path="output.png", name="myimage")

# Persistent volume to store cached model
app.Mount.PersistentVolume(app_path="./cached_models", name="cached_model")

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 method below. You defined a file path called output.png in, and that’s where your images will be saved.
import os

# Specify the HuggingFace model cache before the diffusers library is imported
os.environ["TRANSFORMERS_CACHE"] = "/workspace/cached_models"
os.environ["HF_HOME"] = "/workspace/cached_models"

import torch
from torch import autocast
from diffusers import StableDiffusionPipeline
from PIL import Image

def generate_image(**inputs):
    pipe = StableDiffusionPipeline.from_pretrained(
        # Add your own access token from Huggingface

    prompt = inputs["prompt"]

    with autocast("cuda"):
        image = pipe(prompt, guidance_scale=7.5).images[0]

Deploying the API

In your teriminal, run:

beam deploy

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.

  curl -X POST --compressed "" \
   -H 'Accept: */*' \
   -H 'Accept-Encoding: gzip, deflate' \
   -H 'Authorization: Basic [YOUR_AUTH_TOKEN]' \
   -H 'Connection: keep-alive' \
   -H 'Content-Type: application/json' \
   -d '{"prompt": "a renaissance style photo of steve jobs"}'

The API returns a Task ID.

{ "task_id": "edbcf7ff-e8ce-4199-8661-8e15ed880481" }

Querying the status of a job

You will use the /task API to retrieve the status of a job, passing in the Task ID.

curl -X POST --compressed "" \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Authorization: Basic [YOUR_AUTH_TOKEN]' \
  -H 'Content-Type: application/json' \
  -d '{
	"action": "retrieve",
	"task_id": "edbcf7ff-e8ce-4199-8661-8e15ed880481"

This returns the generated image in the outputs dictionary, as a pre-signed URL.

  "outputs": {
    "myoutput": ""
  "started_at": "2022-11-04T19:43:25.668303Z",
  "ended_at": "2022-11-04T19:43:26.017401Z",
  "task_id": "edbcf7ff-e8ce-4199-8661-8e15ed880481"