File size: 1,417 Bytes
379b9e6 c8ef0f4 379b9e6 c8ef0f4 379b9e6 561a904 379b9e6 561a904 379b9e6 561a904 379b9e6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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 |