mr2along commited on
Commit
44d6fa0
·
verified ·
1 Parent(s): 982bdcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -99
app.py CHANGED
@@ -233,27 +233,6 @@ MODEL_REPOS = {
233
 
234
  # ============================================================
235
  # MODEL DOWNLOAD
236
- #
237
- # IMPORTANT FIX:
238
- #
239
- # huggingface_hub can preserve the directory contained
240
- # inside filename.
241
- #
242
- # Example:
243
- #
244
- # filename =
245
- # facerestore_models/GPEN-BFR-512.onnx
246
- #
247
- # with local_dir=/app/models/facerestore_models
248
- #
249
- # can result in:
250
- #
251
- # /app/models/facerestore_models/
252
- # facerestore_models/
253
- # GPEN-BFR-512.onnx
254
- #
255
- # This function downloads to a temporary directory and then
256
- # copies ONLY the actual model file to the expected path.
257
  # ============================================================
258
 
259
  def download_model(model_name):
@@ -279,10 +258,6 @@ def download_model(model_name):
279
  basename
280
  )
281
 
282
- # --------------------------------------------------------
283
- # EXACT EXPECTED PATH ALREADY EXISTS
284
- # --------------------------------------------------------
285
-
286
  if os.path.isfile(
287
  local_path
288
  ):
@@ -301,17 +276,6 @@ def download_model(model_name):
301
 
302
  # --------------------------------------------------------
303
  # MIGRATE OLD BROKEN NESTED PATH
304
- #
305
- # Old broken path:
306
- #
307
- # /app/models/facerestore_models/
308
- # facerestore_models/
309
- # GPEN-BFR-512.onnx
310
- #
311
- # Move it to:
312
- #
313
- # /app/models/facerestore_models/
314
- # GPEN-BFR-512.onnx
315
  # --------------------------------------------------------
316
 
317
  nested_path = os.path.join(
@@ -385,10 +349,6 @@ def download_model(model_name):
385
  + str(downloaded_path)
386
  )
387
 
388
- # ----------------------------------------------------
389
- # COPY ACTUAL FILE TO EXPECTED PATH
390
- # ----------------------------------------------------
391
-
392
  shutil.copy2(
393
  downloaded_path,
394
  local_path
@@ -1060,12 +1020,6 @@ def get_detection_image(
1060
 
1061
  # ============================================================
1062
  # FACE CROP
1063
- #
1064
- # NEW:
1065
- # Source Face Review / Target Face Review show individual
1066
- # face crops instead of the whole image.
1067
- #
1068
- # Padding is used so the crop is not too tight.
1069
  # ============================================================
1070
 
1071
  def crop_face_from_image(
@@ -1210,9 +1164,6 @@ def make_face_review_gallery(
1210
 
1211
  # ============================================================
1212
  # FACE REVIEW FOR MULTIPLE FILES
1213
- #
1214
- # NEW:
1215
- # Every face gets its own crop.
1216
  # ============================================================
1217
 
1218
  def make_multi_face_review(
@@ -1249,8 +1200,6 @@ def make_multi_face_review(
1249
 
1250
  # ============================================================
1251
  # ANNOTATED FACE PREVIEW
1252
- #
1253
- # Kept for compatibility / debugging.
1254
  # ============================================================
1255
 
1256
  def make_annotated_preview(
@@ -1421,10 +1370,6 @@ def make_annotated_preview(
1421
 
1422
  # ============================================================
1423
  # MULTI FILE PREVIEW
1424
- #
1425
- # CHANGED:
1426
- # Source gallery = face crops
1427
- # Target gallery = face crops
1428
  # ============================================================
1429
 
1430
  def preview_source_target(
@@ -1471,8 +1416,6 @@ def preview_source_target(
1471
  faces
1472
  )
1473
 
1474
- # NEW:
1475
- # Crop each source face separately.
1476
  crops = make_face_review_gallery(
1477
  path,
1478
  f"SOURCE {source_index}"
@@ -1522,8 +1465,6 @@ def preview_source_target(
1522
  faces
1523
  )
1524
 
1525
- # NEW:
1526
- # Crop each target face separately.
1527
  crops = make_face_review_gallery(
1528
  path,
1529
  f"TARGET {target_index}"
@@ -2627,17 +2568,6 @@ def reactor_swap(
2627
  else "none"
2628
  )
2629
 
2630
- # ========================================================
2631
- # GPEN
2632
- #
2633
- # ensure_model() now guarantees:
2634
- #
2635
- # /app/models/facerestore_models/
2636
- # GPEN-BFR-512.onnx
2637
- #
2638
- # ReActor receives model NAME, not the broken nested path.
2639
- # ========================================================
2640
-
2641
  if restore_name != "none":
2642
 
2643
  restore_path = ensure_model(
@@ -2699,10 +2629,6 @@ def reactor_swap(
2699
  "source_image": source_image
2700
  }
2701
 
2702
- # ========================================================
2703
- # FIRST ATTEMPT
2704
- # ========================================================
2705
-
2706
  try:
2707
 
2708
  result = reactorfaceswap.execute(
@@ -2802,10 +2728,6 @@ def swap_one_image(
2802
  target_image_override=None
2803
  ):
2804
 
2805
- # ========================================================
2806
- # SOURCE
2807
- # ========================================================
2808
-
2809
  source_tensor = load_source_cached(
2810
  source_path
2811
  )
@@ -2814,10 +2736,6 @@ def swap_one_image(
2814
  source_tensor
2815
  )
2816
 
2817
- # ========================================================
2818
- # TARGET
2819
- # ========================================================
2820
-
2821
  if target_image_override is None:
2822
 
2823
  target_tensor = comfy_load_image(
@@ -2842,10 +2760,6 @@ def swap_one_image(
2842
  "Target tensor is None."
2843
  )
2844
 
2845
- # ========================================================
2846
- # DEBUG
2847
- # ========================================================
2848
-
2849
  log(
2850
  "[SWAP] Source type:",
2851
  type(source_tensor),
@@ -2868,10 +2782,6 @@ def swap_one_image(
2868
  )
2869
  )
2870
 
2871
- # ========================================================
2872
- # INFERENCE
2873
- # ========================================================
2874
-
2875
  with INFERENCE_SEMAPHORE:
2876
 
2877
  result_image = reactor_swap(
@@ -3857,8 +3767,7 @@ def auto_mapping(
3857
 
3858
  f"SOURCE "
3859
  f"{source_index} "
3860
- f"FACE "
3861
- f"{source_face_index} "
3862
  f"-> TARGET 0 FACE "
3863
  f"{target_face_index}"
3864
  )
@@ -3914,6 +3823,33 @@ def status_text(
3914
  f"Workers: {MAX_CONCURRENT_JOBS}"
3915
  )
3916
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3917
  # ============================================================
3918
  # UI
3919
  # ============================================================
@@ -3982,9 +3918,6 @@ with gr.Blocks(
3982
 
3983
  # ========================================================
3984
  # SOURCE FACE REVIEW
3985
- #
3986
- # NEW:
3987
- # Each detected source face is shown as an individual crop.
3988
  # ========================================================
3989
 
3990
  source_gallery = gr.Gallery(
@@ -4004,9 +3937,6 @@ with gr.Blocks(
4004
 
4005
  # ========================================================
4006
  # TARGET FACE REVIEW
4007
- #
4008
- # NEW:
4009
- # Each detected target face is shown as an individual crop.
4010
  # ========================================================
4011
 
4012
  target_gallery = gr.Gallery(
@@ -4261,6 +4191,32 @@ with gr.Blocks(
4261
  interactive=False
4262
  )
4263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4264
  output_gallery = gr.Gallery(
4265
 
4266
  label="Output Preview",
@@ -4273,7 +4229,9 @@ with gr.Blocks(
4273
 
4274
  object_fit="contain",
4275
 
4276
- preview=True
 
 
4277
  )
4278
 
4279
  # ========================================================
@@ -4515,6 +4473,9 @@ with gr.Blocks(
4515
 
4516
  # ========================================================
4517
  # GENERATE
 
 
 
4518
  # ========================================================
4519
 
4520
  generate_button.click(
@@ -4560,6 +4521,31 @@ with gr.Blocks(
4560
  concurrency_limit=2
4561
  )
4562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4563
  # ========================================================
4564
  # OUTPUT -> TARGET
4565
  # ========================================================
@@ -4761,6 +4747,14 @@ log(
4761
  "[APP] GPEN Path Fix: ENABLED"
4762
  )
4763
 
 
 
 
 
 
 
 
 
4764
  log("=" * 70)
4765
 
4766
  # ============================================================
 
233
 
234
  # ============================================================
235
  # MODEL DOWNLOAD
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  # ============================================================
237
 
238
  def download_model(model_name):
 
258
  basename
259
  )
260
 
 
 
 
 
261
  if os.path.isfile(
262
  local_path
263
  ):
 
276
 
277
  # --------------------------------------------------------
278
  # MIGRATE OLD BROKEN NESTED PATH
 
 
 
 
 
 
 
 
 
 
 
279
  # --------------------------------------------------------
280
 
281
  nested_path = os.path.join(
 
349
  + str(downloaded_path)
350
  )
351
 
 
 
 
 
352
  shutil.copy2(
353
  downloaded_path,
354
  local_path
 
1020
 
1021
  # ============================================================
1022
  # FACE CROP
 
 
 
 
 
 
1023
  # ============================================================
1024
 
1025
  def crop_face_from_image(
 
1164
 
1165
  # ============================================================
1166
  # FACE REVIEW FOR MULTIPLE FILES
 
 
 
1167
  # ============================================================
1168
 
1169
  def make_multi_face_review(
 
1200
 
1201
  # ============================================================
1202
  # ANNOTATED FACE PREVIEW
 
 
1203
  # ============================================================
1204
 
1205
  def make_annotated_preview(
 
1370
 
1371
  # ============================================================
1372
  # MULTI FILE PREVIEW
 
 
 
 
1373
  # ============================================================
1374
 
1375
  def preview_source_target(
 
1416
  faces
1417
  )
1418
 
 
 
1419
  crops = make_face_review_gallery(
1420
  path,
1421
  f"SOURCE {source_index}"
 
1465
  faces
1466
  )
1467
 
 
 
1468
  crops = make_face_review_gallery(
1469
  path,
1470
  f"TARGET {target_index}"
 
2568
  else "none"
2569
  )
2570
 
 
 
 
 
 
 
 
 
 
 
 
2571
  if restore_name != "none":
2572
 
2573
  restore_path = ensure_model(
 
2629
  "source_image": source_image
2630
  }
2631
 
 
 
 
 
2632
  try:
2633
 
2634
  result = reactorfaceswap.execute(
 
2728
  target_image_override=None
2729
  ):
2730
 
 
 
 
 
2731
  source_tensor = load_source_cached(
2732
  source_path
2733
  )
 
2736
  source_tensor
2737
  )
2738
 
 
 
 
 
2739
  if target_image_override is None:
2740
 
2741
  target_tensor = comfy_load_image(
 
2760
  "Target tensor is None."
2761
  )
2762
 
 
 
 
 
2763
  log(
2764
  "[SWAP] Source type:",
2765
  type(source_tensor),
 
2782
  )
2783
  )
2784
 
 
 
 
 
2785
  with INFERENCE_SEMAPHORE:
2786
 
2787
  result_image = reactor_swap(
 
3767
 
3768
  f"SOURCE "
3769
  f"{source_index} "
3770
+ f"FACE {source_face_index} "
 
3771
  f"-> TARGET 0 FACE "
3772
  f"{target_face_index}"
3773
  )
 
3823
  f"Workers: {MAX_CONCURRENT_JOBS}"
3824
  )
3825
 
3826
+ # ============================================================
3827
+ # OUTPUT PREVIEW VISIBILITY
3828
+ # ============================================================
3829
+
3830
+ def toggle_output_preview(
3831
+ is_visible
3832
+ ):
3833
+
3834
+ new_visible = not bool(
3835
+ is_visible
3836
+ )
3837
+
3838
+ return (
3839
+
3840
+ gr.update(
3841
+ visible=new_visible
3842
+ ),
3843
+
3844
+ new_visible,
3845
+
3846
+ (
3847
+ "Hide Output Preview"
3848
+ if new_visible
3849
+ else "Show Output Preview"
3850
+ )
3851
+ )
3852
+
3853
  # ============================================================
3854
  # UI
3855
  # ============================================================
 
3918
 
3919
  # ========================================================
3920
  # SOURCE FACE REVIEW
 
 
 
3921
  # ========================================================
3922
 
3923
  source_gallery = gr.Gallery(
 
3937
 
3938
  # ========================================================
3939
  # TARGET FACE REVIEW
 
 
 
3940
  # ========================================================
3941
 
3942
  target_gallery = gr.Gallery(
 
4191
  interactive=False
4192
  )
4193
 
4194
+ # ========================================================
4195
+ # OUTPUT PREVIEW STATE
4196
+ #
4197
+ # IMPORTANT:
4198
+ # DEFAULT = False
4199
+ # ========================================================
4200
+
4201
+ output_preview_visible = gr.State(
4202
+ False
4203
+ )
4204
+
4205
+ # ========================================================
4206
+ # OUTPUT PREVIEW TOGGLE BUTTON
4207
+ # ========================================================
4208
+
4209
+ toggle_output_preview_button = gr.Button(
4210
+ "Show Output Preview"
4211
+ )
4212
+
4213
+ # ========================================================
4214
+ # OUTPUT PREVIEW
4215
+ #
4216
+ # IMPORTANT:
4217
+ # DEFAULT HIDDEN
4218
+ # ========================================================
4219
+
4220
  output_gallery = gr.Gallery(
4221
 
4222
  label="Output Preview",
 
4229
 
4230
  object_fit="contain",
4231
 
4232
+ preview=True,
4233
+
4234
+ visible=False
4235
  )
4236
 
4237
  # ========================================================
 
4473
 
4474
  # ========================================================
4475
  # GENERATE
4476
+ #
4477
+ # Output Preview stays hidden after generation.
4478
+ # Generated files are still populated normally.
4479
  # ========================================================
4480
 
4481
  generate_button.click(
 
4521
  concurrency_limit=2
4522
  )
4523
 
4524
+ # ========================================================
4525
+ # TOGGLE OUTPUT PREVIEW
4526
+ # ========================================================
4527
+
4528
+ toggle_output_preview_button.click(
4529
+
4530
+ fn=toggle_output_preview,
4531
+
4532
+ inputs=[
4533
+
4534
+ output_preview_visible
4535
+ ],
4536
+
4537
+ outputs=[
4538
+
4539
+ output_gallery,
4540
+
4541
+ output_preview_visible,
4542
+
4543
+ toggle_output_preview_button
4544
+ ],
4545
+
4546
+ concurrency_limit=2
4547
+ )
4548
+
4549
  # ========================================================
4550
  # OUTPUT -> TARGET
4551
  # ========================================================
 
4747
  "[APP] GPEN Path Fix: ENABLED"
4748
  )
4749
 
4750
+ log(
4751
+ "[APP] Output Preview Toggle: ENABLED"
4752
+ )
4753
+
4754
+ log(
4755
+ "[APP] Output Preview Default: HIDDEN"
4756
+ )
4757
+
4758
  log("=" * 70)
4759
 
4760
  # ============================================================