Jacobmadwed commited on
Commit
f73bdec
·
verified ·
1 Parent(s): d477ee0

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +25 -35
handler.py CHANGED
@@ -101,7 +101,7 @@ class EndpointHandler:
101
  self.pipe.image_proj_model.to("cuda")
102
  self.pipe.unet.to("cuda")
103
 
104
- d def __call__(self, data):
105
 
106
  def convert_from_cv2_to_image(img: np.ndarray) -> Image:
107
  return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
@@ -146,41 +146,20 @@ class EndpointHandler:
146
  p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
147
  return p.replace("{prompt}", positive), n + " " + negative
148
 
149
- def load_image_from_base64(base64_str):
150
- image_data = base64.b64decode(base64_str)
151
- image = Image.open(io.BytesIO(image_data))
152
- return image
153
-
154
- # Debug: Print incoming data
155
- print("Incoming data:", data)
156
-
157
- face_image_base64 = data.pop("face_image_base64", None)
158
- if face_image_base64 is not None:
159
- print("Using provided base64 image")
160
- face_image = load_image_from_base64(face_image_base64)
161
- else:
162
- print("No base64 image provided, using default path or URL")
163
- face_image_path = data.pop("face_image_path", "raw/input.jpg")
164
- face_image = load_image(face_image_path)
165
-
166
- # Debug: Print the used face image
167
- face_image.show(title="Debug Face Image")
168
-
169
- pose_image_path = data.pop("pose_image_path", None)
170
  style_name = data.pop("style_name", DEFAULT_STYLE_NAME)
171
  prompt = data.pop("inputs", "a man flying in the sky in Mars")
172
  negative_prompt = data.pop("negative_prompt", "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green")
173
- identitynet_strength_ratio = data.pop("identitynet_strength_ratio", 0.8)
174
- adapter_strength_ratio = data.pop("adapter_strength_ratio", 0.8)
175
- pose_strength = data.pop("pose_strength", 0.5)
176
- canny_strength = data.pop("canny_strength", 0.3)
177
- num_steps = data.pop("num_steps", 20)
178
- guidance_scale = data.pop("guidance_scale", 5.0)
179
- controlnet_selection = data.pop("controlnet_selection", ["pose", "canny"])
180
- scheduler = data.pop("scheduler", "EulerDiscreteScheduler")
181
- enable_fast_inference = data.pop("enable_fast_inference", False)
182
- enhance_non_face_region = data.pop("enhance_non_face_region", False)
183
- seed = data.pop("seed", 42)
184
 
185
  self.pipe.disable_lora()
186
  scheduler_class_name = scheduler.split("-")[0]
@@ -196,6 +175,7 @@ class EndpointHandler:
196
  # apply the style template
197
  prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
198
 
 
199
  face_image = resize_img(face_image, max_side=1024)
200
  face_image_cv2 = convert_from_image_to_cv2(face_image)
201
  height, width, _ = face_image_cv2.shape
@@ -203,6 +183,11 @@ class EndpointHandler:
203
  # Extract face features
204
  face_info = self.app.get(face_image_cv2)
205
 
 
 
 
 
 
206
  face_info = sorted(
207
  face_info,
208
  key=lambda x: (x["bbox"][2] - x["bbox"][0]) * x["bbox"][3] - x["bbox"][1],
@@ -220,6 +205,11 @@ class EndpointHandler:
220
 
221
  face_info = self.app.get(pose_image_cv2)
222
 
 
 
 
 
 
223
  face_info = face_info[-1]
224
  face_kps = draw_kps(pose_image, face_info["kps"])
225
 
@@ -247,7 +237,7 @@ class EndpointHandler:
247
  for s in controlnet_selection
248
  ]
249
 
250
- generator = torch.Generator(device=device).manual_seed(seed)
251
 
252
  print("Start inference...")
253
  print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
@@ -272,4 +262,4 @@ class EndpointHandler:
272
  images[0].save(buffered, format="JPEG")
273
  img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
274
 
275
- return {"generated_image_base64": img_str}
 
101
  self.pipe.image_proj_model.to("cuda")
102
  self.pipe.unet.to("cuda")
103
 
104
+ def __call__(self, data):
105
 
106
  def convert_from_cv2_to_image(img: np.ndarray) -> Image:
107
  return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
 
146
  p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
147
  return p.replace("{prompt}", positive), n + " " + negative
148
 
149
+ face_image_path = data.pop("face_image_path", "https://i.ibb.co/GQzm527/examples-musk-resize.jpg")
150
+ pose_image_path = data.pop("pose_image_path", "https://i.ibb.co/TRCK4MS/examples-poses-pose2.jpg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  style_name = data.pop("style_name", DEFAULT_STYLE_NAME)
152
  prompt = data.pop("inputs", "a man flying in the sky in Mars")
153
  negative_prompt = data.pop("negative_prompt", "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green")
154
+
155
+ identitynet_strength_ratio = 0.8
156
+ adapter_strength_ratio = 0.8
157
+ pose_strength = 0.5
158
+ canny_strength = 0.3
159
+ num_steps = 20
160
+ guidance_scale = 5.0
161
+ controlnet_selection = ["pose", "canny"]
162
+ scheduler = "EulerDiscreteScheduler"
 
 
163
 
164
  self.pipe.disable_lora()
165
  scheduler_class_name = scheduler.split("-")[0]
 
175
  # apply the style template
176
  prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
177
 
178
+ face_image = load_image(face_image_path)
179
  face_image = resize_img(face_image, max_side=1024)
180
  face_image_cv2 = convert_from_image_to_cv2(face_image)
181
  height, width, _ = face_image_cv2.shape
 
183
  # Extract face features
184
  face_info = self.app.get(face_image_cv2)
185
 
186
+ # if len(face_info) == 0:
187
+ # raise gr.Error(
188
+ # f"Unable to detect a face in the image. Please upload a different photo with a clear face."
189
+ # )
190
+
191
  face_info = sorted(
192
  face_info,
193
  key=lambda x: (x["bbox"][2] - x["bbox"][0]) * x["bbox"][3] - x["bbox"][1],
 
205
 
206
  face_info = self.app.get(pose_image_cv2)
207
 
208
+ # if len(face_info) == 0:
209
+ # raise gr.Error(
210
+ # f"Cannot find any face in the reference image! Please upload another person image"
211
+ # )
212
+
213
  face_info = face_info[-1]
214
  face_kps = draw_kps(pose_image, face_info["kps"])
215
 
 
237
  for s in controlnet_selection
238
  ]
239
 
240
+ generator = torch.Generator(device=device).manual_seed(42)
241
 
242
  print("Start inference...")
243
  print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
 
262
  images[0].save(buffered, format="JPEG")
263
  img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
264
 
265
+ return {"generated_image_base64": img_str}