File size: 2,140 Bytes
6889e88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28f02e0
 
300f57b
b431f15
 
6889e88
 
 
 
 
 
 
9a48ac5
6889e88
 
 
 
9a48ac5
d751390
6889e88
 
 
 
 
28f02e0
6889e88
 
 
 
 
 
 
 
9a48ac5
 
6889e88
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from typing import  Dict, List, Any
from diffusers import AutoPipelineForText2Image
import torch


import numpy as np

# set device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if device.type != 'cuda':
    raise ValueError("need to run on GPU")
# set mixed precision dtype
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] == 8 else torch.float16


class EndpointHandler():
    def __init__(self, path=""):
        # Load StableDiffusionPipeline 
        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)
        
        # Check if prompt is not provided
        if prompt is None:
            return {"error": "Please provide a prompt."}

        generator = torch.Generator(device=device).manual_seed(seed)
        
        # hyperparamters
        num_inference_steps = data.pop("num_inference_steps", 50)
        guidance_scale = data.pop("guidance_scale", 7.5)
        temperature = data.pop("temperature", 1.0)

        
        # run inference pipeline
        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 first generate PIL image
        return out.images[0]