SDXL Turbo
A variant of Stable Diffusion XL that generates images in as few as one inference step, enabling near real-time image creation. Learn more
Deploy SDXL Turbo behind an API endpoint in seconds.
Deploy modelExample usage
The model accepts a two main inputs:
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.num_steps
: Represents the number of iterations the model must cycle through before creating the final image. This value should be between 1-4.
The output JSON object contains a key called data
which represents the generated image as a base64 string.
The output JSON object contains a key called result
which represents the generated image as a base64 string.
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
12def b64_to_pil(b64_str):
13 return Image.open(BytesIO(base64.b64decode(b64_str.replace(BASE64_PREAMBLE, ""))))
14
15data = {
16 "prompt": "a picture of a rhino wearing a suit",
17 "num_steps": 1,
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()
29img_b64 = res.get("result")
30
31# Save the base64 string to a PNG
32img = b64_to_pil(img_b64)
33img.show()
34img.save("sdxl-output-1.png")
1{
2 "result": "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA..."
3}
Here is another example with the following prompt:
an illustration of a rocket taking off from an alien planet
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
12def b64_to_pil(b64_str):
13 return Image.open(BytesIO(base64.b64decode(b64_str.replace(BASE64_PREAMBLE, ""))))
14
15data = {
16 "prompt": "an illustration of a rocket taking off from an alien planet",
17 "num_steps": 1,
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()
29img_b64 = res.get("result")
30
31# Save the base64 string to a PNG
32img = b64_to_pil(img_b64)
33img.show()
34img.save("sdxl-output-2.png")
1{
2 "result": "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA..."
3}