CoolFace
Modelpublic

hackoak/Stable_diffusion_3.5_custom_endpoint_handler

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes3downloads
Model Card

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 implementation
  • —requirements.txt: Required dependencies
  • —test_handler.py: Test script to verify the handler works
  • —README.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

  1. 1.Go to Hugging Face and create a new repository
  2. 2.Name it something like your-username/stable-diffusion-3.5-handler

2. Upload the Handler Files

Upload these files to your repository:

  • —handler.py
  • —requirements.txt

3. Deploy as Inference Endpoint

  1. 1.Go to Hugging Face Inference Endpoints
  2. 2.Click "New Endpoint"
  3. 3.Select your repository
  4. 4.Choose your cloud provider and region
  5. 5.Select an appropriate instance type (GPU recommended)
  6. 6.Set the task to "Custom"
  7. 7.Deploy the endpoint

API Usage

Once deployed, you can call the endpoint with:

python
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:

json
{
    "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:

json
{
    "error": "Error message describing what went wrong"
}

Testing Locally

To test the handler locally:

bash
pip install -r requirements.txt
python test_handler.py

Notes

  • —The handler uses torch.float16 for 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