Article
image-generationstable-diffusionsdxl-turbopythondiffusersgenerative-aitext-to-imagereal-time
Real-Time Image Generation with SDXL Turbo
Generate high-quality images from text prompts in a single inference step. This Stable Diffusion XL Turbo Inference Script uses a distilled model for extreme speed, making it ideal for interactive applications where low latency is critical.
intermediate15 min4 steps
The play
- Set Up Your Python EnvironmentCreate a virtual environment and install the necessary libraries from Hugging Face. `diffusers` provides the pipeline, `transformers` handles text processing, and `accelerate` optimizes performance on your hardware (CPU or GPU).
- Load the SDXL Turbo PipelineImport the `AutoPipelineForText2Image` class and load the pretrained 'stabilityai/sdxl-turbo' model. We specify `torch.float16` for memory efficiency and move the pipeline to the GPU (`cuda`) for maximum speed.
- Define Your Prompt and ParametersCraft a text prompt describing the image you want to create. For the Stable Diffusion XL Turbo Inference Script, you must set `num_inference_steps` to 1 and `guidance_scale` to 0.0. This is what enables single-step, real-time generation.
- Generate and Save the ImageCall the pipeline with your prompt and the specific Turbo parameters. The model will generate an image object. You can then access the first image from the output list and save it to a file.
Starter code
import torch
from diffusers import AutoPipelineForText2Image
from datetime import datetime
# Check for CUDA GPU, fall back to CPU if not available
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
# 1. Load the pipeline
print("Loading SDXL Turbo pipeline...")
try:
pipe = AutoPipelineForText2Image.from_pretrained(
"stabilityai/sdxl-turbo",
torch_dtype=torch.float16,
variant="fp16"
)
pipe = pipe.to(device)
except Exception as e:
print(f"Failed to load FP16 variant, trying default: {e}")
pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo")
pipe = pipe.to(device)
# 2. Define the prompt and parameters
prompt = "A photorealistic portrait of a cat wearing a tiny wizard hat, magical, fantasy, detailed fur."
# 3. Generate the image
print(f"Generating image for prompt: '{prompt}'...")
# SDXL Turbo is optimized for a single inference step.
image = pipe(
prompt=prompt,
num_inference_steps=1,
guidance_scale=0.0
).images[0]
# 4. Save the image
output_filename = f"sdxl_turbo_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
image.save(output_filename)
print(f"Image successfully generated and saved as '{output_filename}'")