custom-sd3.5-handler / handler.py
davidberenstein1957's picture
Update handler.py (#1)
561a904 verified
from diffusers import DiffusionPipeline
from typing import Any, Dict, List
import torch
class EndpointHandler:
def __init__(self, path=""):
self.pipeline = DiffusionPipeline.from_pretrained(
path, torch_dtype=torch.bfloat16
).to("cuda")
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
# Extract data
data = data.get("json", data)
prompt = data.get("inputs", None)
parameters = data.get("parameters", {})
if not prompt:
raise ValueError("Input prompt is missing.")
# Extract parameters with defaults
negative_prompt = parameters.get("negative_prompt", "bad quality, worse quality, deformed")
height = parameters.get("height", 512)
width = parameters.get("width", 512)
guidance_scale = parameters.get("guidance_scale", 4.5)
num_inference_steps = parameters.get("num_inference_steps", 28)
seed = parameters.get("seed", 0)
# Seed generator
generator = torch.manual_seed(seed)
# Generate prediction
prediction = self.pipeline(
prompt,
negative_prompt=negative_prompt,
height=height,
width=width,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
generator=generator
).images[0]
return prediction