Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -44,6 +44,18 @@ def load_models():
|
|
| 44 |
device='cuda'
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
automasker = AutoMasker(
|
| 48 |
densepose_ckpt=os.path.join(repo_path, "DensePose"),
|
| 49 |
schp_ckpt=os.path.join(repo_path, "SCHP"),
|
|
@@ -53,21 +65,20 @@ def load_models():
|
|
| 53 |
print("β
Models loaded!", file=sys.stderr)
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
-
print(f"β Error: {e}", file=sys.stderr)
|
| 57 |
traceback.print_exc()
|
| 58 |
raise
|
| 59 |
|
| 60 |
def generate_tryon(person_img, cloth_img):
|
|
|
|
|
|
|
| 61 |
print("="*50, file=sys.stderr)
|
| 62 |
-
print(f"
|
| 63 |
|
| 64 |
if person_img is None or cloth_img is None:
|
| 65 |
raise gr.Error("Both images required!")
|
| 66 |
|
| 67 |
try:
|
| 68 |
-
# person_img and cloth_img are already PIL Images, no conversion needed
|
| 69 |
-
print("Images received as PIL", file=sys.stderr)
|
| 70 |
-
|
| 71 |
load_models()
|
| 72 |
|
| 73 |
target_height = 1024
|
|
@@ -78,6 +89,7 @@ def generate_tryon(person_img, cloth_img):
|
|
| 78 |
mask = automasker(person_img, "upper")['mask']
|
| 79 |
gc.collect()
|
| 80 |
|
|
|
|
| 81 |
result = pipeline(
|
| 82 |
image=person_img,
|
| 83 |
condition_image=cloth_img,
|
|
@@ -89,7 +101,7 @@ def generate_tryon(person_img, cloth_img):
|
|
| 89 |
width=target_width
|
| 90 |
)[0]
|
| 91 |
|
| 92 |
-
print("β
Success!", file=sys.stderr)
|
| 93 |
return result
|
| 94 |
|
| 95 |
except Exception as e:
|
|
@@ -97,14 +109,13 @@ def generate_tryon(person_img, cloth_img):
|
|
| 97 |
traceback.print_exc()
|
| 98 |
raise gr.Error(str(e))
|
| 99 |
|
| 100 |
-
# CRITICAL: Use gr.Interface instead of gr.Blocks for better API support
|
| 101 |
demo = gr.Interface(
|
| 102 |
fn=generate_tryon,
|
| 103 |
inputs=[
|
| 104 |
-
gr.Image(label="Person Image", type="pil"),
|
| 105 |
-
gr.Image(label="Garment Image", type="pil")
|
| 106 |
],
|
| 107 |
-
outputs=gr.Image(label="Result", type="pil"),
|
| 108 |
title="Try-Space Virtual Try-On",
|
| 109 |
description="Upload person and garment images. Processing takes 2-3 minutes on GPU T4.",
|
| 110 |
api_name="generate_tryon",
|
|
|
|
| 44 |
device='cuda'
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# CRITICAL FIX: Disable NSFW filter
|
| 48 |
+
if hasattr(pipeline, 'safety_checker'):
|
| 49 |
+
pipeline.safety_checker = None
|
| 50 |
+
print("β
NSFW filter disabled", file=sys.stderr)
|
| 51 |
+
|
| 52 |
+
# Alternative: Replace safety checker with dummy function
|
| 53 |
+
if hasattr(pipeline, 'run_safety_checker'):
|
| 54 |
+
def dummy_safety_checker(images, *args, **kwargs):
|
| 55 |
+
return images, [False] * len(images)
|
| 56 |
+
pipeline.run_safety_checker = dummy_safety_checker
|
| 57 |
+
print("β
Safety checker replaced with dummy", file=sys.stderr)
|
| 58 |
+
|
| 59 |
automasker = AutoMasker(
|
| 60 |
densepose_ckpt=os.path.join(repo_path, "DensePose"),
|
| 61 |
schp_ckpt=os.path.join(repo_path, "SCHP"),
|
|
|
|
| 65 |
print("β
Models loaded!", file=sys.stderr)
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
+
print(f"β Error loading models: {e}", file=sys.stderr)
|
| 69 |
traceback.print_exc()
|
| 70 |
raise
|
| 71 |
|
| 72 |
def generate_tryon(person_img, cloth_img):
|
| 73 |
+
"""Generate virtual try-on"""
|
| 74 |
+
|
| 75 |
print("="*50, file=sys.stderr)
|
| 76 |
+
print(f"API CALL - Person: {type(person_img)}, Cloth: {type(cloth_img)}", file=sys.stderr)
|
| 77 |
|
| 78 |
if person_img is None or cloth_img is None:
|
| 79 |
raise gr.Error("Both images required!")
|
| 80 |
|
| 81 |
try:
|
|
|
|
|
|
|
|
|
|
| 82 |
load_models()
|
| 83 |
|
| 84 |
target_height = 1024
|
|
|
|
| 89 |
mask = automasker(person_img, "upper")['mask']
|
| 90 |
gc.collect()
|
| 91 |
|
| 92 |
+
print("Running pipeline...", file=sys.stderr)
|
| 93 |
result = pipeline(
|
| 94 |
image=person_img,
|
| 95 |
condition_image=cloth_img,
|
|
|
|
| 101 |
width=target_width
|
| 102 |
)[0]
|
| 103 |
|
| 104 |
+
print(f"β
Success! Result size: {result.size}", file=sys.stderr)
|
| 105 |
return result
|
| 106 |
|
| 107 |
except Exception as e:
|
|
|
|
| 109 |
traceback.print_exc()
|
| 110 |
raise gr.Error(str(e))
|
| 111 |
|
|
|
|
| 112 |
demo = gr.Interface(
|
| 113 |
fn=generate_tryon,
|
| 114 |
inputs=[
|
| 115 |
+
gr.Image(label="Person Image", type="pil"),
|
| 116 |
+
gr.Image(label="Garment Image", type="pil")
|
| 117 |
],
|
| 118 |
+
outputs=gr.Image(label="Result", type="pil"),
|
| 119 |
title="Try-Space Virtual Try-On",
|
| 120 |
description="Upload person and garment images. Processing takes 2-3 minutes on GPU T4.",
|
| 121 |
api_name="generate_tryon",
|