Overview
Stable_Diffusion_Handler is a TorchServe handler for serving Stable Diffusion models via the Hugging Face diffusers library. It extends BaseHandler and uses diffusers.DiffusionPipeline to generate 768x768 images from text prompts with configurable guidance scale and inference steps.
Description
The DiffusersHandler class bridges TorchServe with the Hugging Face Diffusers library for text-to-image generation. It extracts a model archive containing the diffusion pipeline, loads it via DiffusionPipeline.from_pretrained(), and runs inference with configurable parameters including guidance scale, number of steps, and output resolution.
Key Responsibilities
- Model Loading: Extracts a ZIP archive and loads the Stable Diffusion pipeline via
diffusers.DiffusionPipeline
- Text Prompt Extraction: Parses text prompts from incoming request data
- Image Generation: Runs the diffusion pipeline with
guidance_scale=7.5, 50 inference steps, and 768x768 output resolution
- Output Conversion: Converts PIL Image outputs to numpy arrays for serialization
Dependencies
| Library |
Usage
|
diffusers.DiffusionPipeline |
Core diffusion model pipeline
|
PIL |
Image handling (output from pipeline)
|
numpy |
Final output conversion
|
Code Reference
Source Location
| File |
Lines |
Repository
|
examples/diffusers/stable_diffusion_handler.py |
L1-95 |
pytorch/serve
|
Key Class
class DiffusersHandler(BaseHandler):
"""
Stable Diffusion handler extending BaseHandler.
Lines 16-95.
"""
def initialize(self, context):
"""
Extract ZIP archive and load DiffusionPipeline.
Extracts the model ZIP into the model directory, then
loads the pipeline via DiffusionPipeline.from_pretrained().
Parameters:
context: TorchServe context with system_properties and manifest.
"""
...
def preprocess(self, data):
"""
Extract text prompts from request data.
Parses the incoming request to retrieve the text prompt
string used for image generation.
Parameters:
data (list): List of request input dicts.
Returns:
list: Text prompt strings.
"""
...
def inference(self, data, *args, **kwargs):
"""
Run the Stable Diffusion pipeline.
Executes the DiffusionPipeline with:
- guidance_scale=7.5
- num_inference_steps=50
- height=768, width=768
Parameters:
data (list): Text prompts from preprocess.
Returns:
PIL.Image: Generated image(s) from the pipeline.
"""
...
def postprocess(self, data):
"""
Convert PIL images to numpy arrays.
Parameters:
data (PIL.Image): Generated images from inference.
Returns:
list: Numpy array representations of the images.
"""
...
Import
from ts.torch_handler.base_handler import BaseHandler
from diffusers import DiffusionPipeline
I/O Contract
| Method |
Input |
Output |
Notes
|
initialize(context) |
Context with system_properties, manifest |
None (sets self.pipeline) |
Extracts ZIP, loads DiffusionPipeline
|
preprocess(data) |
list of request dicts with text prompts |
list of prompt strings |
Extracts text from request body
|
inference(data) |
list of prompt strings |
PIL.Image |
Pipeline with guidance_scale=7.5, 50 steps, 768x768
|
postprocess(data) |
PIL.Image |
list of numpy arrays |
Converts PIL to numpy for serialization
|
Pipeline Configuration
| Parameter |
Value |
Description
|
guidance_scale |
7.5 |
Classifier-free guidance strength
|
num_inference_steps |
50 |
Number of denoising steps
|
height |
768 |
Output image height in pixels
|
width |
768 |
Output image width in pixels
|
Usage Examples
Example 1: Handler Initialization
# The handler extracts the model ZIP and loads the pipeline
handler = DiffusersHandler()
handler.initialize(context)
# self.pipeline is now a loaded DiffusionPipeline instance
Example 2: Text-to-Image Generation
# Request with a text prompt
data = [{"body": "A photograph of a cat wearing a space suit on Mars"}]
# Pipeline execution:
prompts = handler.preprocess(data)
images = handler.inference(prompts)
numpy_results = handler.postprocess(images)
# numpy_results contains the generated image as numpy array
Example 3: curl Request
curl -X POST http://localhost:8080/predictions/stable_diffusion \
-H "Content-Type: application/json" \
-d '{"data": "A futuristic cityscape at sunset, digital art"}'
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.