|
from typing import Dict, List, Any |
|
from diffusers import AutoPipelineForText2Image |
|
import torch |
|
|
|
|
|
import numpy as np |
|
|
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
|
if device.type != 'cuda': |
|
raise ValueError("need to run on GPU") |
|
|
|
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16 |
|
|
|
|
|
class EndpointHandler(): |
|
def __init__(self, path=""): |
|
|
|
self.stable_diffusion_id = "runwayml/stable-diffusion-v1-5" |
|
self.pipe = AutoPipelineForText2Image.from_pretrained(self.stable_diffusion_id, |
|
torch_dtype=dtype, |
|
safety_checker=None) |
|
self.pipe.load_lora_weights("Oysiyl/sd-lora-android-google-toy", weights="pytorch_lora_weights.safetensors") |
|
self.pipe.enable_xformers_memory_efficient_attention() |
|
self.pipe.to(device) |
|
|
|
def __call__(self, data: Any) -> List[List[Dict[str, float]]]: |
|
""" |
|
:param data: A dictionary contains `inputs`. |
|
:return: A dictionary with `image` field contains image in base64. |
|
""" |
|
prompt = data.pop("inputs", None) |
|
seed = data.pop("seed", 42) |
|
|
|
|
|
if prompt is None: |
|
return {"error": "Please provide a prompt."} |
|
|
|
generator = torch.Generator(device=device).manual_seed(seed) |
|
|
|
|
|
num_inference_steps = data.pop("num_inference_steps", 50) |
|
guidance_scale = data.pop("guidance_scale", 7.5) |
|
temperature = data.pop("temperature", 1.0) |
|
|
|
|
|
|
|
out = self.pipe( |
|
prompt=prompt, |
|
num_inference_steps=num_inference_steps, |
|
guidance_scale=guidance_scale, |
|
temperature=temperature, |
|
num_images_per_prompt=1, |
|
seed=seed, |
|
generator=generator |
|
) |
|
|
|
|
|
|
|
return out.images[0] |
|
|