Example usage
The model accepts a single input, prompt, and returns a base64 string of the image as the key result
.
This implementation uses the 4-step UNet checkpoint to balance speed and quality. You can deploy your own version with either 2 steps for even faster results or 8 steps for even higher quality.
import base64
import requests
import os
# Replace the empty string with your model id below
model_id = ""
baseten_api_key = os.environ["BASETEN_API_KEY"]
BASE64_PREAMBLE = "data:image/png;base64,"
data = {
"prompt": "a picture of a rhino wearing a suit",
}
# Call model endpoint
res = requests.post(
f"https://model-{model_id}.api.baseten.co/production/predict",
headers={"Authorization": f"Api-Key {baseten_api_key}"},
json=data
)
# Get output image
res = res.json()
img_b64 = res.get("result")
img = base64.b64decode(img_b64)
# Save the base64 string to a PNG
img_file = open("sdxl-output-1.png", "wb")
img_file.write(img)
img_file.close()
os.system("open sdxl-output-1.png")
JSON Output
{
"result": "iVBORw0KGgoAAAANSUhEUgAABAAAAAQACAIAAA..."
}