Spaces:
Running on Zero
Running on Zero
Fix: .then() chaining for instant status, remove gr.File, robust error return
Browse files
app.py
CHANGED
|
@@ -45,7 +45,7 @@ def _make_mask(size: int, cloth_type: str) -> Image.Image:
|
|
| 45 |
return mask
|
| 46 |
|
| 47 |
# ---------------------------------------------------------------------------
|
| 48 |
-
# GPU inference
|
| 49 |
# ---------------------------------------------------------------------------
|
| 50 |
_pipe = None
|
| 51 |
|
|
@@ -59,7 +59,7 @@ def run_tryon(
|
|
| 59 |
seed: int,
|
| 60 |
):
|
| 61 |
if person_image is None or garment_image is None:
|
| 62 |
-
|
| 63 |
|
| 64 |
global _pipe
|
| 65 |
if _pipe is None:
|
|
@@ -91,10 +91,9 @@ def run_tryon(
|
|
| 91 |
|
| 92 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 93 |
for i, img in enumerate(output_images):
|
| 94 |
-
|
| 95 |
-
img.save(path, format="PNG")
|
| 96 |
|
| 97 |
-
return output_images
|
| 98 |
|
| 99 |
# ---------------------------------------------------------------------------
|
| 100 |
# Gradio UI
|
|
@@ -103,15 +102,14 @@ with gr.Blocks(title="Virtual Try-On", theme=gr.themes.Soft()) as demo:
|
|
| 103 |
gr.Markdown(
|
| 104 |
"# π Virtual Try-On\n"
|
| 105 |
"Upload a **person photo** and a **garment image**, select the type, then click **Try On**.\n\n"
|
| 106 |
-
"> Runs on **Hugging Face ZeroGPU** (free A10G) β no local GPU needed.\n
|
| 107 |
-
">
|
| 108 |
-
"> **First run:** ~2-3 min (model download). **Subsequent runs:** ~15-30s."
|
| 109 |
)
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column():
|
| 113 |
-
person_input = gr.Image(label="Person Photo", type="pil", height=
|
| 114 |
-
garment_input = gr.Image(label="Garment Image", type="pil", height=
|
| 115 |
cloth_type = gr.Radio(
|
| 116 |
["upper", "lower", "overall"],
|
| 117 |
value="upper",
|
|
@@ -125,13 +123,21 @@ with gr.Blocks(title="Virtual Try-On", theme=gr.themes.Soft()) as demo:
|
|
| 125 |
try_btn = gr.Button("π Try On", variant="primary", size="lg")
|
| 126 |
|
| 127 |
with gr.Column():
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
| 130 |
|
|
|
|
| 131 |
try_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
fn=run_tryon,
|
| 133 |
inputs=[person_input, garment_input, cloth_type, num_steps, guidance, seed_input],
|
| 134 |
-
outputs=[output_gallery],
|
| 135 |
)
|
| 136 |
|
| 137 |
gr.Markdown(
|
|
|
|
| 45 |
return mask
|
| 46 |
|
| 47 |
# ---------------------------------------------------------------------------
|
| 48 |
+
# GPU inference β returns images + status string
|
| 49 |
# ---------------------------------------------------------------------------
|
| 50 |
_pipe = None
|
| 51 |
|
|
|
|
| 59 |
seed: int,
|
| 60 |
):
|
| 61 |
if person_image is None or garment_image is None:
|
| 62 |
+
return None, "β Please upload both a person photo and a garment image."
|
| 63 |
|
| 64 |
global _pipe
|
| 65 |
if _pipe is None:
|
|
|
|
| 91 |
|
| 92 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 93 |
for i, img in enumerate(output_images):
|
| 94 |
+
img.save(os.path.join(OUTPUT_DIR, f"tryon_{timestamp}_{i}.png"), format="PNG")
|
|
|
|
| 95 |
|
| 96 |
+
return output_images, "β
Done! Right-click an image in the gallery to save it."
|
| 97 |
|
| 98 |
# ---------------------------------------------------------------------------
|
| 99 |
# Gradio UI
|
|
|
|
| 102 |
gr.Markdown(
|
| 103 |
"# π Virtual Try-On\n"
|
| 104 |
"Upload a **person photo** and a **garment image**, select the type, then click **Try On**.\n\n"
|
| 105 |
+
"> Runs on **Hugging Face ZeroGPU** (free A10G) β no local GPU needed.\n"
|
| 106 |
+
"> **First run:** ~2-3 min (model download ~5 GB). **Subsequent runs:** ~15-30s."
|
|
|
|
| 107 |
)
|
| 108 |
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column():
|
| 111 |
+
person_input = gr.Image(label="Person Photo", type="pil", height=350)
|
| 112 |
+
garment_input = gr.Image(label="Garment Image", type="pil", height=350)
|
| 113 |
cloth_type = gr.Radio(
|
| 114 |
["upper", "lower", "overall"],
|
| 115 |
value="upper",
|
|
|
|
| 123 |
try_btn = gr.Button("π Try On", variant="primary", size="lg")
|
| 124 |
|
| 125 |
with gr.Column():
|
| 126 |
+
status_box = gr.Textbox(
|
| 127 |
+
label="Status", value="Ready β upload images and click Try On",
|
| 128 |
+
interactive=False, max_lines=2,
|
| 129 |
+
)
|
| 130 |
+
output_gallery = gr.Gallery(label="Result", columns=1, height=420)
|
| 131 |
|
| 132 |
+
# Chain: first update status immediately, then run inference
|
| 133 |
try_btn.click(
|
| 134 |
+
fn=lambda: "ⳠRequesting GPU + loading model⦠(first run ~3 min, please wait)",
|
| 135 |
+
inputs=None,
|
| 136 |
+
outputs=[status_box],
|
| 137 |
+
).then(
|
| 138 |
fn=run_tryon,
|
| 139 |
inputs=[person_input, garment_input, cloth_type, num_steps, guidance, seed_input],
|
| 140 |
+
outputs=[output_gallery, status_box],
|
| 141 |
)
|
| 142 |
|
| 143 |
gr.Markdown(
|