add handler
Browse files- __pycache__/handler.cpython-38.pyc +0 -0
- handler.py +68 -0
__pycache__/handler.cpython-38.pyc
DELETED
Binary file (2.88 kB)
|
|
handler.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any
|
2 |
+
import torch, base64
|
3 |
+
from PIL import Image
|
4 |
+
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, DDIMScheduler
|
5 |
+
from diffusers.utils import load_image
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
class EndpointHandler():
|
9 |
+
def __init__(self, path=""):
|
10 |
+
self.controlnet = ControlNetModel.from_pretrained("DionTimmer/controlnet_qrcode-control_v11p_sd21", torch_dtype=torch.float16)
|
11 |
+
self.pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", controlnet=self.controlnet, safety_checker=None, torch_dtype=torch.float16)
|
12 |
+
|
13 |
+
self.pipe.enable_xformers_memory_efficient_attention()
|
14 |
+
self.pipe.scheduler = DDIMScheduler.from_config(self.pipe.scheduler.config)
|
15 |
+
self.pipe.enable_model_cpu_offload()
|
16 |
+
|
17 |
+
def __call__(self, data):
|
18 |
+
"""
|
19 |
+
data args:
|
20 |
+
inputs (:obj: `str`)
|
21 |
+
date (:obj: `str`)
|
22 |
+
Return:
|
23 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
24 |
+
"""
|
25 |
+
# get inputs
|
26 |
+
inputs = data.pop("inputs", data)
|
27 |
+
params = data.pop("parameters", data)
|
28 |
+
prompt = params.get("prompt")
|
29 |
+
negative_prompt = params.get("negative_prompt")
|
30 |
+
|
31 |
+
def resize_image(input_image: Image, resolution: int):
|
32 |
+
input_image = input_image.convert("RGB")
|
33 |
+
W, H = input_image.size
|
34 |
+
k = float(resolution) / min(H, W)
|
35 |
+
H *= k
|
36 |
+
W *= k
|
37 |
+
H = int(round(H / 64.0)) * 64
|
38 |
+
W = int(round(W / 64.0)) * 64
|
39 |
+
img = input_image.resize((W, H), resample=Image.LANCZOS)
|
40 |
+
return img
|
41 |
+
|
42 |
+
orriginal_qr_code_image = load_image(inputs)
|
43 |
+
img_path = 'https://images.squarespace-cdn.com/content/v1/59413d96e6f2e1c6837c7ecd/1536503659130-R84NUPOY4QPQTEGCTSAI/15fe1e62172035.5a87280d713e4.png'
|
44 |
+
|
45 |
+
|
46 |
+
init_image = load_image(img_path)
|
47 |
+
condition_image = resize_image(orriginal_qr_code_image, 768)
|
48 |
+
init_image = resize_image(init_image, 768)
|
49 |
+
generator = torch.manual_seed(123121231)
|
50 |
+
image = self.pipe(prompt=prompt or "a bilboard in NYC with a qrcode",
|
51 |
+
negative_prompt=negative_prompt or "ugly, disfigured, low quality, blurry, nsfw, worst quality, illustration, drawing",
|
52 |
+
image=init_image,
|
53 |
+
control_image=condition_image,
|
54 |
+
width=768,
|
55 |
+
height=768,
|
56 |
+
guidance_scale=20,
|
57 |
+
controlnet_conditioning_scale=2.5,
|
58 |
+
generator=generator,
|
59 |
+
strength=0.9,
|
60 |
+
num_inference_steps=150,
|
61 |
+
)
|
62 |
+
|
63 |
+
image = image.images[0]
|
64 |
+
buffered = BytesIO()
|
65 |
+
image.save(buffered, format="JPEG")
|
66 |
+
img_str = base64.b64encode(buffered.getvalue())
|
67 |
+
|
68 |
+
return {"image": img_str.decode()}
|