Nandha2017 commited on
Commit
1659459
Β·
verified Β·
1 Parent(s): 576cb44

Fix: .then() chaining for instant status, remove gr.File, robust error return

Browse files
Files changed (1) hide show
  1. app.py +19 -13
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
- raise gr.Error("Please upload both a person photo and a garment image.")
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
- path = os.path.join(OUTPUT_DIR, f"tryon_{timestamp}_{i}.png")
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\n"
107
- "> ⏳ After clicking, the button shows a **loading spinner** β€” that means it is working. \n"
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=380)
114
- garment_input = gr.Image(label="Garment Image", type="pil", height=380)
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
- gr.Markdown("*Click an image in the gallery then use its ⬇ icon to download.*")
129
- output_gallery = gr.Gallery(label="Result", columns=1, height=420, show_download_button=True)
 
 
 
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(