karimbenharrak commited on
Commit
744d735
1 Parent(s): 86c6ead

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +42 -5
handler.py CHANGED
@@ -1,10 +1,10 @@
1
  from typing import Dict, List, Any
2
  import torch
3
- from diffusers import DPMSolverMultistepScheduler, StableDiffusionInpaintPipeline, AutoPipelineForInpainting, AutoPipelineForImage2Image, StableDiffusionXLImg2ImgPipeline
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')
@@ -12,6 +12,16 @@ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
  if device.type != 'cuda':
13
  raise ValueError("need to run on GPU")
14
 
 
 
 
 
 
 
 
 
 
 
15
  class EndpointHandler():
16
  def __init__(self, path=""):
17
 
@@ -24,7 +34,19 @@ class EndpointHandler():
24
  # )
25
  # self.smooth_pipe.to("cuda")
26
 
27
-
 
 
 
 
 
 
 
 
 
 
 
 
28
  # load StableDiffusionInpaintPipeline pipeline
29
  self.pipe = AutoPipelineForInpainting.from_pretrained(
30
  "runwayml/stable-diffusion-inpainting",
@@ -50,7 +72,7 @@ class EndpointHandler():
50
  self.pipe3 = AutoPipelineForImage2Image.from_pipe(self.pipe2)
51
  #self.pipe3.enable_model_cpu_offload()
52
  self.pipe3.enable_xformers_memory_efficient_attention()
53
-
54
 
55
 
56
  def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
@@ -102,7 +124,7 @@ class EndpointHandler():
102
  """
103
 
104
  #pipe = AutoPipelineForInpainting.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype=torch.float16, variant="fp16").to("cuda")
105
-
106
  # run inference pipeline
107
  out = self.pipe(prompt=prompt, negative_prompt=negative_prompt, image=image, mask_image=mask_image)
108
 
@@ -137,6 +159,21 @@ class EndpointHandler():
137
 
138
  # return first generate PIL image
139
  return image2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
  # helper to decode input image
142
  def decode_base64_image(self, image_string):
 
1
  from typing import Dict, List, Any
2
  import torch
3
+ from diffusers import DPMSolverMultistepScheduler, StableDiffusionInpaintPipeline, AutoPipelineForInpainting, AutoPipelineForImage2Image, StableDiffusionXLImg2ImgPipeline, StableDiffusionControlNetInpaintPipeline, ControlNetModel
4
  from PIL import Image
5
  import base64
6
  from io import BytesIO
7
+ import numpy as np
8
 
9
  # set device
10
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
12
  if device.type != 'cuda':
13
  raise ValueError("need to run on GPU")
14
 
15
+ def make_inpaint_condition(image, image_mask):
16
+ image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
17
+ image_mask = np.array(image_mask.convert("L")).astype(np.float32) / 255.0
18
+
19
+ assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
20
+ image[image_mask > 0.5] = -1.0 # set as masked pixel
21
+ image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
22
+ image = torch.from_numpy(image)
23
+ return image
24
+
25
  class EndpointHandler():
26
  def __init__(self, path=""):
27
 
 
34
  # )
35
  # self.smooth_pipe.to("cuda")
36
 
37
+ self.controlnet = ControlNetModel.from_pretrained(
38
+ "lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16
39
+ )
40
+
41
+ self.pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
42
+ "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
43
+ )
44
+
45
+ self.pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
46
+ self.pipe.enable_model_cpu_offload()
47
+ self.pipe.enable_xformers_memory_efficient_attention()
48
+
49
+ """
50
  # load StableDiffusionInpaintPipeline pipeline
51
  self.pipe = AutoPipelineForInpainting.from_pretrained(
52
  "runwayml/stable-diffusion-inpainting",
 
72
  self.pipe3 = AutoPipelineForImage2Image.from_pipe(self.pipe2)
73
  #self.pipe3.enable_model_cpu_offload()
74
  self.pipe3.enable_xformers_memory_efficient_attention()
75
+ """
76
 
77
 
78
  def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
 
124
  """
125
 
126
  #pipe = AutoPipelineForInpainting.from_pretrained("diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype=torch.float16, variant="fp16").to("cuda")
127
+ """
128
  # run inference pipeline
129
  out = self.pipe(prompt=prompt, negative_prompt=negative_prompt, image=image, mask_image=mask_image)
130
 
 
159
 
160
  # return first generate PIL image
161
  return image2
162
+ """
163
+
164
+ control_image = make_inpaint_condition(image, mask_image)
165
+
166
+ # generate image
167
+ image = pipe(
168
+ prompt,
169
+ num_inference_steps=num_inference_steps,
170
+ eta=1.0,
171
+ image=image,
172
+ mask_image=mask_image,
173
+ control_image=control_image,
174
+ ).images[0]
175
+
176
+ return image
177
 
178
  # helper to decode input image
179
  def decode_base64_image(self, image_string):