Commit
·
ed4e013
1
Parent(s):
ad5410b
handler.pyを追加
Browse files- handler.py +104 -0
handler.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import DPMSolverMultistepScheduler, DiffusionPipeline, StableDiffusionImg2ImgPipeline, StableDiffusionInpaintPipelineLegacy
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import base64
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# set device
|
| 10 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 11 |
+
|
| 12 |
+
class EndpointHandler():
|
| 13 |
+
def __init__(self, path=""):
|
| 14 |
+
# load StableDiffusionInpaintPipeline pipeline
|
| 15 |
+
self.txt2img_pipe = DiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
|
| 16 |
+
# Set safety_checker
|
| 17 |
+
self.txt2img_pipe.safety_checker = None
|
| 18 |
+
# use DPMSolverMultistepScheduler
|
| 19 |
+
self.txt2img_pipe.scheduler = DPMSolverMultistepScheduler.from_config(self.txt2img_pipe.scheduler.config)
|
| 20 |
+
|
| 21 |
+
self.img2img_pipe = StableDiffusionImg2ImgPipeline(
|
| 22 |
+
vae=self.txt2img_pipe.vae,
|
| 23 |
+
text_encoder=self.txt2img_pipe.text_encoder,
|
| 24 |
+
tokenizer=self.txt2img_pipe.tokenizer,
|
| 25 |
+
unet=self.txt2img_pipe.unet,
|
| 26 |
+
scheduler=self.txt2img_pipe.scheduler,
|
| 27 |
+
safety_checker=self.txt2img_pipe.safety_checker,
|
| 28 |
+
feature_extractor=self.txt2img_pipe.feature_extractor,
|
| 29 |
+
).to(device)
|
| 30 |
+
self.inpaint_pipe = StableDiffusionInpaintPipelineLegacy(
|
| 31 |
+
vae=self.txt2img_pipe.vae,
|
| 32 |
+
text_encoder=self.txt2img_pipe.text_encoder,
|
| 33 |
+
tokenizer=self.txt2img_pipe.tokenizer,
|
| 34 |
+
unet=self.txt2img_pipe.unet,
|
| 35 |
+
scheduler=self.txt2img_pipe.scheduler,
|
| 36 |
+
safety_checker=self.txt2img_pipe.safety_checker,
|
| 37 |
+
feature_extractor=self.txt2img_pipe.feature_extractor,
|
| 38 |
+
).to(device)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
|
| 42 |
+
"""
|
| 43 |
+
:param data: A dictionary contains `inputs` and optional `image` field.
|
| 44 |
+
:return: A dictionary with `image` field contains image in base64.
|
| 45 |
+
"""
|
| 46 |
+
inputs = data.pop("inputs", data)
|
| 47 |
+
encoded_image = data.pop("image", None)
|
| 48 |
+
encoded_mask_image = data.pop("mask_image", None)
|
| 49 |
+
|
| 50 |
+
# hyperparamters
|
| 51 |
+
num_inference_steps = data.pop("num_inference_steps", 25)
|
| 52 |
+
guidance_scale = data.pop("guidance_scale", 7.5)
|
| 53 |
+
negative_prompt = data.pop("negative_prompt", None)
|
| 54 |
+
height = data.pop("height", 512)
|
| 55 |
+
width = data.pop("width", 512)
|
| 56 |
+
strength = data.pop("strength", 0.8)
|
| 57 |
+
|
| 58 |
+
# run inference pipeline
|
| 59 |
+
if encoded_image is not None and encoded_mask_image is not None:
|
| 60 |
+
image = self.decode_base64_image(encoded_image)
|
| 61 |
+
mask_image = self.decode_base64_image(encoded_mask_image)
|
| 62 |
+
|
| 63 |
+
out = self.inpaint_pipe(inputs,
|
| 64 |
+
init_image=image,
|
| 65 |
+
mask_image=mask_image,
|
| 66 |
+
strength=strength,
|
| 67 |
+
num_inference_steps=num_inference_steps,
|
| 68 |
+
guidance_scale=guidance_scale,
|
| 69 |
+
num_images_per_prompt=1,
|
| 70 |
+
negative_prompt=negative_prompt
|
| 71 |
+
)
|
| 72 |
+
return out.images[0]
|
| 73 |
+
|
| 74 |
+
elif encoded_image is not None:
|
| 75 |
+
image = self.decode_base64_image(encoded_image)
|
| 76 |
+
|
| 77 |
+
out = self.img2img_pipe(inputs,
|
| 78 |
+
init_image=image,
|
| 79 |
+
strength=strength,
|
| 80 |
+
num_inference_steps=num_inference_steps,
|
| 81 |
+
guidance_scale=guidance_scale,
|
| 82 |
+
num_images_per_prompt=1,
|
| 83 |
+
negative_prompt=negative_prompt
|
| 84 |
+
)
|
| 85 |
+
return out.images[0]
|
| 86 |
+
else:
|
| 87 |
+
out = self.txt2img_pipe(inputs,
|
| 88 |
+
num_inference_steps=num_inference_steps,
|
| 89 |
+
guidance_scale=guidance_scale,
|
| 90 |
+
num_images_per_prompt=1,
|
| 91 |
+
negative_prompt=negative_prompt,
|
| 92 |
+
height=height,
|
| 93 |
+
width=width
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# return first generate PIL image
|
| 97 |
+
return out.images[0]
|
| 98 |
+
|
| 99 |
+
# helper to decode input image
|
| 100 |
+
def decode_base64_image(self, image_string):
|
| 101 |
+
base64_image = base64.b64decode(image_string)
|
| 102 |
+
buffer = BytesIO(base64_image)
|
| 103 |
+
image = Image.open(buffer)
|
| 104 |
+
return image
|