hackoak/Stable_diffusion_3.5_custom_endpoint_handler
03
Stable Diffusion 3.5 Custom Handler
This is a custom handler for deploying the stabilityai/stable-diffusion-3.5-large-turbo model on Hugging Face Inference Endpoints.
Files
handler.py: The main custom handler implementationrequirements.txt: Required dependenciestest_handler.py: Test script to verify the handler worksREADME.md: This file
Handler Features
The custom handler supports:
- Text-to-image generation using Stable Diffusion 3.5 Large Turbo
- Configurable parameters:
num_inference_steps: Number of inference steps (default: 1)guidance_scale: Guidance scale (default: 0.0)width: Image width (default: 1024)height: Image height (default: 1024)seed: Random seed for reproducible results
Deployment Steps
1. Create a Hugging Face Repository
- Go to Hugging Face and create a new repository
- Name it something like
your-username/stable-diffusion-3.5-handler
2. Upload the Handler Files
Upload these files to your repository:
handler.pyrequirements.txt
3. Deploy as Inference Endpoint
- Go to Hugging Face Inference Endpoints
- Click "New Endpoint"
- Select your repository
- Choose your cloud provider and region
- Select an appropriate instance type (GPU recommended)
- Set the task to "Custom"
- Deploy the endpoint
API Usage
Once deployed, you can call the endpoint with:
import requests
url = "https://your-endpoint-url"
headers = {"Authorization": "Bearer YOUR_TOKEN"}
payload = {
"inputs": "A beautiful sunset over mountains, digital art",
"num_inference_steps": 1,
"guidance_scale": 0.0,
"width": 1024,
"height": 1024,
"seed": 42
}
response = requests.post(url, headers=headers, json=payload)
result = response.json()
# The result contains a base64-encoded image
image_data = result["image"]Response Format
The handler returns a JSON object with:
{
"image": "base64_encoded_image_string",
"prompt": "original_prompt",
"parameters": {
"num_inference_steps": 1,
"guidance_scale": 0.0,
"width": 1024,
"height": 1024,
"seed": 42
}
}Error Handling
If an error occurs, the response will contain:
{
"error": "Error message describing what went wrong"
}Testing Locally
To test the handler locally:
pip install -r requirements.txt
python test_handler.pyNotes
- The handler uses
torch.float16for memory efficiency - GPU acceleration is automatically enabled if available
- Memory efficient attention is enabled if xformers is available
- The model is loaded once during initialization for optimal performance
