Stability AI logoStable Diffusion XL

A text to image foundation model that generates detailed images from short prompts. Learn more

Deploy Stable Diffusion XL behind an API endpoint in seconds.

Deploy model

Example usage

The model accepts a few main inputs:

  1. prompt: This is text describing the image you want to generate. The output images tend to get better as you add more descriptive words to the prompt.

  2. width: Width of the image in pixels. Default is 1024.

  3. height: Height of the image in pixels. Default is 1024.

The output JSON object contains a key called data which represents the generated image as a base64 string.

Input
1import requests
2import os
3import base64
4from PIL import Image
5from io import BytesIO
6
7# Replace the empty string with your model id below
8model_id = ""
9baseten_api_key = os.environ["BASETEN_API_KEY"]
10BASE64_PREAMBLE = "data:image/png;base64,"
11
12# Function used to convert a base64 string to a PIL image
13def b64_to_pil(b64_str):
14    return Image.open(BytesIO(base64.b64decode(b64_str.replace(BASE64_PREAMBLE, ""))))
15
16data = {
17  "prompt": "a little boy looking through a large magical portal, the boy sees a futuristic human civilization in that portal, extremely detailed, trending on artstation, 8k"
18}
19
20# Call model endpoint
21res = requests.post(
22    f"https://model-{model_id}.api.baseten.co/production/predict",
23    headers={"Authorization": f"Api-Key {baseten_api_key}"},
24    json=data
25)
26
27# Get output image
28res = res.json()
29output = res.get("data")
30
31# Convert the base64 model output to an image
32img = b64_to_pil(output)
33img.save("output_image.png")
34os.system("open output_image.png")
JSON output
1{
2    "status": "success",
3    "data": "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA...",
4    "time": 7.62
5}
Preview

You can also provide negative_prompt as in input to steer the model away from things you don't want to generate.

The num_inference_steps parameter is the number of iterations the model does before creating the final image.

The output JSON object contains a key called data which represents the generated image as a base64 string.

Input
1import requests
2import os
3import base64
4from PIL import Image
5from io import BytesIO
6
7# Replace the empty string with your model id below
8model_id = ""
9baseten_api_key = os.environ["BASETEN_API_KEY"]
10BASE64_PREAMBLE = "data:image/png;base64,"
11
12# Function used to convert a base64 string to a PIL image
13def b64_to_pil(b64_str):
14    return Image.open(BytesIO(base64.b64decode(b64_str.replace(BASE64_PREAMBLE, ""))))
15
16data = {
17  "prompt": "Extremely detailed and intricate scene of baby phoenix hatchling cuddled up resting on a pile of ashes surrounded by fire and smoke, rays of sunlight shine on the phoenix, in the background is a dense dark forest, settings: f/8 aperture, full shot, hyper realistic, 4k",
18  "negative_prompt": "worst quality, low quality",
19  "width": 1248,
20  "height": 832,
21  "num_inference_steps": 35,
22  "guidance_scale": 14
23}
24
25# Call model endpoint
26res = requests.post(
27    f"https://model-{model_id}.api.baseten.co/production/predict",
28    headers={"Authorization": f"Api-Key {baseten_api_key}"},
29    json=data
30)
31
32# Get output image
33res = res.json()
34output = res.get("data")
35
36# Convert the base64 model output to an image
37img = b64_to_pil(output)
38img.save("output_image.png")
39os.system("open output_image.png")
JSON output
1{
2    "status": "success",
3    "data": "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA...",
4    "time": 7.62
5}
Preview

Deploy any model in just a few commands

Avoid getting tangled in complex deployment processes. Deploy best-in-class open-source models and take advantage of optimized serving for your own models.

$

truss init -- example stable-diffusion-2-1-base ./my-sd-truss

$

cd ./my-sd-truss

$

export BASETEN_API_KEY=MdNmOCXc.YBtEZD0WFOYKso2A6NEQkRqTe

$

truss push

INFO

Serializing Stable Diffusion 2.1 truss.

INFO

Making contact with Baseten 👋 👽

INFO

🚀 Uploading model to Baseten 🚀

Upload progress: 0% | | 0.00G/2.39G