mikitona commited on
Commit
4ab4571
·
verified ·
1 Parent(s): 3c8bf9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -44
app.py CHANGED
@@ -217,48 +217,55 @@ def start_tryon(
217
  # 最初にマスク画像を一度だけ出力
218
  yield output_images.value, mask_gray
219
 
220
- for i in range(int(num_images)):
221
- current_seed = seed + i if seed is not None and seed != -1 else None
222
- generator = (
223
- torch.Generator(device).manual_seed(int(current_seed)) if current_seed is not None else None
224
- )
 
225
 
226
- with torch.no_grad():
227
- with torch.cuda.amp.autocast():
228
- images = pipe(
229
- prompt_embeds=prompt_embeds.to(device, torch.float16),
230
- negative_prompt_embeds=negative_prompt_embeds.to(device, torch.float16),
231
- pooled_prompt_embeds=pooled_prompt_embeds.to(device, torch.float16),
232
- negative_pooled_prompt_embeds=negative_pooled_prompt_embeds.to(
233
- device, torch.float16
234
- ),
235
- num_inference_steps=denoise_steps,
236
- generator=generator,
237
- strength=1.0,
238
- pose_img=pose_img_tensor.to(device, torch.float16),
239
- text_embeds_cloth=prompt_embeds_c.to(device, torch.float16),
240
- cloth=garm_tensor.to(device, torch.float16),
241
- mask_image=mask,
242
- image=human_img,
243
- height=1024,
244
- width=768,
245
- ip_adapter_image=garm_img.resize((768, 1024)),
246
- guidance_scale=2.0,
247
- )[0]
248
-
249
- if is_checked_crop:
250
- out_img = images[0].resize(crop_size)
251
- human_img_copy = human_img_orig.copy()
252
- human_img_copy.paste(out_img, (int(left), int(top)))
253
- new_image = human_img_copy
254
- else:
255
- new_image = images[0]
256
-
257
- # 画像を追加
258
- output_images.value.append(new_image)
259
-
260
- # ギャラリーを更新しつつmasked_imgを保持
261
- yield output_images.value, mask_gray # mask_grayを再度渡す
 
 
 
 
 
 
262
 
263
  # 最終的な結果を返す
264
  return output_images.value, mask_gray
@@ -316,7 +323,9 @@ with image_blocks as demo:
316
  label="Masked image output", elem_id="masked-img", show_share_button=False
317
  )
318
  with gr.Column():
319
- image_gallery = gr.Gallery(label="Generated Images", elem_id="output-gallery", show_label=True)
 
 
320
 
321
  with gr.Column():
322
  try_button = gr.Button(value="Try-on")
@@ -327,7 +336,7 @@ with image_blocks as demo:
327
  )
328
  seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
329
  num_images = gr.Slider(
330
- label="Number of Images", minimum=1, maximum=10, step=1, value=1
331
  )
332
 
333
  try_button.click(
@@ -346,4 +355,4 @@ with image_blocks as demo:
346
  api_name='tryon',
347
  )
348
 
349
- image_blocks.launch(show_error=True)
 
217
  # 最初にマスク画像を一度だけ出力
218
  yield output_images.value, mask_gray
219
 
220
+ try:
221
+ for i in range(int(num_images)):
222
+ current_seed = seed + i if seed is not None and seed != -1 else None
223
+ generator = (
224
+ torch.Generator(device).manual_seed(int(current_seed)) if current_seed is not None else None
225
+ )
226
 
227
+ with torch.no_grad():
228
+ with torch.cuda.amp.autocast():
229
+ images = pipe(
230
+ prompt_embeds=prompt_embeds.to(device, torch.float16),
231
+ negative_prompt_embeds=negative_prompt_embeds.to(device, torch.float16),
232
+ pooled_prompt_embeds=pooled_prompt_embeds.to(device, torch.float16),
233
+ negative_pooled_prompt_embeds=negative_pooled_prompt_embeds.to(
234
+ device, torch.float16
235
+ ),
236
+ num_inference_steps=denoise_steps,
237
+ generator=generator,
238
+ strength=1.0,
239
+ pose_img=pose_img_tensor.to(device, torch.float16),
240
+ text_embeds_cloth=prompt_embeds_c.to(device, torch.float16),
241
+ cloth=garm_tensor.to(device, torch.float16),
242
+ mask_image=mask,
243
+ image=human_img,
244
+ height=1024,
245
+ width=768,
246
+ ip_adapter_image=garm_img.resize((768, 1024)),
247
+ guidance_scale=2.0,
248
+ )[0]
249
+
250
+ if is_checked_crop:
251
+ out_img = images[0].resize(crop_size)
252
+ human_img_copy = human_img_orig.copy()
253
+ human_img_copy.paste(out_img, (int(left), int(top)))
254
+ new_image = human_img_copy
255
+ else:
256
+ new_image = images[0]
257
+
258
+ # 画像を追加
259
+ output_images.value.append(new_image)
260
+
261
+ # ギャラリーを更新しつつmasked_imgを保持
262
+ yield output_images.value, mask_gray # mask_grayを再度渡す
263
+
264
+ except Exception as e:
265
+ # タイムアウトなどの例外が発生した場合でも、生成済みの画像を返却
266
+ yield output_images.value, mask_gray
267
+ # エラーを再度発生させてエラーメッセージを表示
268
+ raise e
269
 
270
  # 最終的な結果を返す
271
  return output_images.value, mask_gray
 
323
  label="Masked image output", elem_id="masked-img", show_share_button=False
324
  )
325
  with gr.Column():
326
+ image_gallery = gr.Gallery(
327
+ label="Generated Images", elem_id="output-gallery", show_label=True
328
+ )
329
 
330
  with gr.Column():
331
  try_button = gr.Button(value="Try-on")
 
336
  )
337
  seed = gr.Number(label="Seed", minimum=-1, maximum=2147483647, step=1, value=42)
338
  num_images = gr.Slider(
339
+ label="Number of Images", minimum=1, maximum=10, step=1, value=1 # 最大値を10に変更
340
  )
341
 
342
  try_button.click(
 
355
  api_name='tryon',
356
  )
357
 
358
+ image_blocks.launch(show_error=True)