Add handler
Browse files- handler.py +28 -0
handler.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import AutoPipelineForImage2Image
|
2 |
+
import torch
|
3 |
+
from typing import Dict, Any
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
import base64
|
7 |
+
|
8 |
+
|
9 |
+
class EndpointHandler():
|
10 |
+
|
11 |
+
def __init__(self, path="."):
|
12 |
+
if torch.cuda.is_available():
|
13 |
+
device = "cuda"
|
14 |
+
else:
|
15 |
+
device = "cpu"
|
16 |
+
self._pipe = AutoPipelineForImage2Image.from_pretrained(path, torch_dtype=torch.float32) #.to(device)
|
17 |
+
|
18 |
+
def __call__(self, data: Dict[str, Any]) -> list[Dict[str, Any]]:
|
19 |
+
inputs = data.pop("inputs", data)
|
20 |
+
|
21 |
+
params = {"prompt": inputs.get("prompt", ""),
|
22 |
+
"image": Image.open(BytesIO(base64.b64decode(inputs['image']))),
|
23 |
+
"strength": inputs.get("strength", 0.3),
|
24 |
+
"guidance_scale": inputs.get("guidance_scale", 10),
|
25 |
+
"height": 768,
|
26 |
+
"width": 768}
|
27 |
+
|
28 |
+
return self._pipe(**params).images[0]
|