Commit
·
5f7473f
1
Parent(s):
ccd3af2
make it load data_uris
Browse files- handler.py +30 -25
handler.py
CHANGED
@@ -2,7 +2,7 @@ from typing import Dict, List, Any
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
-
import
|
6 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DDIMScheduler
|
7 |
|
8 |
# set device
|
@@ -36,33 +36,38 @@ class EndpointHandler():
|
|
36 |
prompt = data.pop("inputs", data)
|
37 |
url = data.pop("url", data)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
|
43 |
|
44 |
-
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
+
from urllib import request
|
6 |
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, DDIMScheduler
|
7 |
|
8 |
# set device
|
|
|
36 |
prompt = data.pop("inputs", data)
|
37 |
url = data.pop("url", data)
|
38 |
|
39 |
+
with request.urlopen(data_uri) as response:
|
40 |
+
data = response.read()
|
41 |
+
init_image = Image.open(BytesIO(data)).convert("RGB")
|
42 |
+
init_image = Image.open(url)
|
43 |
+
init_image.thumbnail((512, 512))
|
44 |
|
45 |
|
46 |
+
params = data.pop("parameters", data)
|
47 |
|
48 |
+
# hyperparamters
|
49 |
+
num_inference_steps = params.pop("num_inference_steps", 25)
|
50 |
+
guidance_scale = params.pop("guidance_scale", 7.5)
|
51 |
+
negative_prompt = params.pop("negative_prompt", None)
|
52 |
+
prompt = params.pop("prompt", None)
|
53 |
+
height = params.pop("height", None)
|
54 |
+
width = params.pop("width", None)
|
55 |
+
manual_seed = params.pop("manual_seed", -1)
|
56 |
|
57 |
+
out = None
|
58 |
|
59 |
+
generator = torch.Generator(device='cuda')
|
60 |
+
generator.manual_seed(manual_seed)
|
61 |
+
# run img2img pipeline
|
62 |
+
out = self.imgPipe(prompt,
|
63 |
+
image=init_image,
|
64 |
+
num_inference_steps=num_inference_steps,
|
65 |
+
guidance_scale=guidance_scale,
|
66 |
+
num_images_per_prompt=1,
|
67 |
+
negative_prompt=negative_prompt,
|
68 |
+
height=height,
|
69 |
+
width=width
|
70 |
+
)
|
71 |
|
72 |
+
# return first generated PIL image
|
73 |
+
return out.images[0]
|