Fix status updates: use gr.Progress instead of yield generator
Browse files18 unmodified lines1920212222232416 unmodified lines41424345444546474850495051523 unmodified lines5657586059606162636466656667689 unmodified lines78798082818283842 unmodified lines878889909192939495959697981 unmodified line1001011021031041051061071 unmodified line1091101111121101131141151162 unmodified lines11912012111912012112212312412512618 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 restarts
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 lines d.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 / dress
else:
d.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)
# GPU-decorated inference
# ---------------------------------------------------------------------------
_pipe = None
3 unmodified lines global _pipe
if _pipe is None:
from diffusers import PaintByExamplePipeline
print("Loading Paint-by-Example pipeline (~5 GB, first run only)…")
print("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 on CUDA.")
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 lines return result.images
# ---------------------------------------------------------------------------
# Outer generator — handles status streaming to Gradio UI
# Gradio inference — uses gr.Progress for live status updates
# ---------------------------------------------------------------------------
def run_tryon(
person_image: Image.Image,2 unmodified lines num_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.")
yield [], [], "⏳ Requesting GPU + loading model… (first run ~3 min, then ~30s)"
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 line
output_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 line img.save(path, format="PNG")
saved_paths.append(path)
progress(1.0, desc="✅ Done!")
yield output_images, saved_paths, "✅ Done! Download your result below."
return output_images, saved_paths, "✅ Done! Download your result below."
# ---------------------------------------------------------------------------
# Gradio UI2 unmodified lines gr.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."
"> 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