|
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]]: |
|
|
|
data = data.get("json", data) |
|
prompt = data.get("inputs", None) |
|
parameters = data.get("parameters", {}) |
|
if not prompt: |
|
raise ValueError("Input prompt is missing.") |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
generator = torch.manual_seed(seed) |
|
|
|
|
|
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 |