al-livefront's picture
Read inputs from base64
f040cb4
raw
history blame
1.08 kB
from typing import Dict, List, Any
from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
import torch
import base64
from PIL import Image
import io
class EndpointHandler():
def __init__(self, path=""):
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(path, subfolder="scheduler")
self.pipeline = DiffusionPipeline.from_pretrained(path, scheduler=scheduler, torch_dtype=torch.float32)
if torch.cuda.is_available():
self.pipeline.to("cuda")
def __call__(self, data: Dict[str, any]) -> List[Dict[str, Any]]:
mask_bytes = base64.b64decode(data["inputs"]["mask"])
mask = Image.open(io.BytesIO(mask_bytes)).convert('RGB')
image_bytes = base64.b64decode(data["inputs"]["image"])
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
prompt = "a picture of a person with a nice haircut"
new_image = self.pipeline(prompt=prompt, image=image, mask_image=mask, height=512, width=512, num_inference_steps=20).images[0]
return [{"image": new_image}]