Fix status updates: use gr.Progress instead of yield generator
Browse files18 unmodified lines1920212223242516 unmodified lines4243444546474849505152533 unmodified lines575859606162636465666768699 unmodified lines798081828384852 unmodified lines88899091929394959697981 unmodified line1001011021031041051 unmodified line1071081091101111121132 unmodified lines11611711811912012112212312418 unmodified linesOUTPUT_DIR = os.path.join(DATA_DIR, "outputs")os.makedirs(OUTPUT_DIR, exist_ok=True)# Point HF cache to persistent storage so model downloads survive restartsos.environ["HF_HOME"] = os.path.join(DATA_DIR, "hf_cache")os.environ["HUGGINGFACE_HUB_CACHE"] = os.path.join(DATA_DIR, "hf_cache", "hub")16 unmodified linesd.rectangle([int(size*.10), int(size*.18), int(size*.90), int(size*.65)], fill=255)elif cloth_type == "lower":d.rectangle([int(size*.05), int(size*.55), int(size*.95), int(size*1.0)], fill=255)else: # overall / dressd.rectangle([int(size*.05), int(size*.15), int(size*.95), int(size*1.0)], fill=255)return mask# ---------------------------------------------------------------------------# GPU-decorated inference — plain return, no yield (spaces.GPU doesn't stream)# ---------------------------------------------------------------------------_pipe = None3 unmodified linesglobal _pipeif _pipe is None:from diffusers import PaintByExamplePipelineprint("Loading Paint-by-Example pipeline (~5 GB, first run only)…")_pipe = PaintByExamplePipeline.from_pretrained("Fantasy-Studio/Paint-by-Example",torch_dtype=torch.float16,).to("cuda")_pipe.set_progress_bar_config(disable=True)print("Pipeline ready on CUDA.")rng = torch.Generator(device="cuda")rng.manual_seed(int(seed) if seed != -1 else torch.randint(0, 2**32, (1,)).item())9 unmodified linesreturn result.images# ---------------------------------------------------------------------------# Outer generator — handles status streaming to Gradio UI# ---------------------------------------------------------------------------def run_tryon(person_image: Image.Image,2 unmodified linesnum_steps: int,guidance_scale: float,seed: int,):if person_image is None or garment_image is None:raise gr.Error("Please upload both a person photo and a garment image.")yield [], [], "⏳ Requesting GPU + loading model… (first run ~3 min, then ~30s)"person = _fit_to_square(person_image)garment = _fit_to_square(garment_image)1 unmodified lineoutput_images = _run_inference(person, garment, mask, num_steps, guidance_scale, seed)timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")saved_paths = []for i, img in enumerate(output_images):1 unmodified lineimg.save(path, format="PNG")saved_paths.append(path)yield output_images, saved_paths, "✅ Done! Download your result below."# ---------------------------------------------------------------------------# Gradio UI2 unmodified linesgr.Markdown("# 👗 Virtual Try-On\n""Upload a **person photo** and a **garment image**, select the type, then click **Try On**.\n\n""> Runs entirely on **Hugging Face ZeroGPU** (free A10G) — no local GPU needed. \n""> Models download once to HF persistent storage. Images save to your device via the Download button.\n\n""> **First run:** ~2-3 min (model download). **Subsequent runs:** ~15-30s.")with gr.Row():18 unmodified lines19202122232416 unmodified lines4142434445464748495051523 unmodified lines565758596061626364656667689 unmodified lines787980818283842 unmodified lines8788899091929394959697981 unmodified line1001011021031041051061071 unmodified line1091101111121131141151162 unmodified lines11912012112212312412512618 unmodified linesOUTPUT_DIR = os.path.join(DATA_DIR, "outputs")os.makedirs(OUTPUT_DIR, exist_ok=True)os.environ["HF_HOME"] = os.path.join(DATA_DIR, "hf_cache")os.environ["HUGGINGFACE_HUB_CACHE"] = os.path.join(DATA_DIR, "hf_cache", "hub")16 unmodified linesd.rectangle([int(size*.10), int(size*.18), int(size*.90), int(size*.65)], fill=255)elif cloth_type == "lower":d.rectangle([int(size*.05), int(size*.55), int(size*.95), int(size*1.0)], fill=255)else:d.rectangle([int(size*.05), int(size*.15), int(size*.95), int(size*1.0)], fill=255)return mask# ---------------------------------------------------------------------------# GPU-decorated inference# ---------------------------------------------------------------------------_pipe = None3 unmodified linesglobal _pipeif _pipe is None:from diffusers import PaintByExamplePipelineprint("Loading Paint-by-Example (~5 GB, first run only)…")_pipe = PaintByExamplePipeline.from_pretrained("Fantasy-Studio/Paint-by-Example",torch_dtype=torch.float16,).to("cuda")_pipe.set_progress_bar_config(disable=True)print("Pipeline ready.")rng = torch.Generator(device="cuda")rng.manual_seed(int(seed) if seed != -1 else torch.randint(0, 2**32, (1,)).item())9 unmodified linesreturn result.images# ---------------------------------------------------------------------------# Gradio inference — uses gr.Progress for live status updates# ---------------------------------------------------------------------------def run_tryon(person_image: Image.Image,2 unmodified linesnum_steps: int,guidance_scale: float,seed: int,progress=gr.Progress(track_tqdm=True),):if person_image is None or garment_image is None:raise gr.Error("Please upload both a person photo and a garment image.")progress(0, desc="⏳ Requesting GPU + loading model (first run ~3 min)…")person = _fit_to_square(person_image)garment = _fit_to_square(garment_image)1 unmodified lineoutput_images = _run_inference(person, garment, mask, num_steps, guidance_scale, seed)progress(0.9, desc="💾 Saving result…")timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")saved_paths = []for i, img in enumerate(output_images):1 unmodified lineimg.save(path, format="PNG")saved_paths.append(path)progress(1.0, desc="✅ Done!")return output_images, saved_paths, "✅ Done! Download your result below."# ---------------------------------------------------------------------------# Gradio UI2 unmodified linesgr.Markdown("# 👗 Virtual Try-On\n""Upload a **person photo** and a **garment image**, select the type, then click **Try On**.\n\n""> Runs on **Hugging Face ZeroGPU** (free A10G) — no local GPU needed. \n""> **First run:** ~2-3 min (model download ~5 GB). **Subsequent runs:** ~15-30s.")with gr.Row():
app.py+13-11