Nandha2017 commited on
Commit
ceeb6a1
·
verified ·
1 Parent(s): bd4b456

Fix: switch to PaintByExamplePipeline (CatVTON had no model_index.json)

Browse files
Files changed (1) hide show
  1. app.py +14 -46
app.py CHANGED
@@ -1,52 +1,30 @@
1
  """
2
- Virtual Try-On — CatVTON + Hugging Face ZeroGPU
3
- No local GPU or model storage needed. Generated images download to your device.
 
4
  """
5
 
6
  import datetime
7
  import os
8
- import sys
9
 
10
  import gradio as gr
11
- import numpy as np
12
  import spaces
13
  import torch
14
- from huggingface_hub import snapshot_download
15
  from PIL import Image, ImageDraw
16
 
17
  # ---------------------------------------------------------------------------
18
  # Persistent storage (/data on ZeroGPU Spaces, /tmp fallback)
19
  # ---------------------------------------------------------------------------
20
  DATA_DIR = "/data" if os.path.exists("/data") else "/tmp"
21
- MODELS_DIR = os.path.join(DATA_DIR, "catvton_models")
22
  OUTPUT_DIR = os.path.join(DATA_DIR, "outputs")
23
- os.makedirs(MODELS_DIR, exist_ok=True)
24
  os.makedirs(OUTPUT_DIR, exist_ok=True)
25
 
 
26
  os.environ["HF_HOME"] = os.path.join(DATA_DIR, "hf_cache")
27
  os.environ["HUGGINGFACE_HUB_CACHE"] = os.path.join(DATA_DIR, "hf_cache", "hub")
28
 
29
  # ---------------------------------------------------------------------------
30
- # Model download runs once at Space startup on HF servers (not locally)
31
- # ---------------------------------------------------------------------------
32
- CATVTON_REPO = "zhengchong/CatVTON"
33
- CATVTON_LOCAL = os.path.join(MODELS_DIR, "CatVTON")
34
-
35
- def download_models():
36
- if os.path.exists(os.path.join(CATVTON_LOCAL, "model_index.json")):
37
- print("CatVTON already cached.")
38
- return
39
- print("Downloading CatVTON (~4 GB) to HF persistent storage…")
40
- snapshot_download(
41
- repo_id=CATVTON_REPO,
42
- local_dir=CATVTON_LOCAL,
43
- local_dir_use_symlinks=False,
44
- ignore_patterns=["*.md", "*.txt", "*.py"],
45
- )
46
- print("CatVTON ready.")
47
-
48
- # ---------------------------------------------------------------------------
49
- # Pipeline (loaded lazily inside @spaces.GPU)
50
  # ---------------------------------------------------------------------------
51
  _pipe = None
52
 
@@ -54,15 +32,14 @@ def _get_pipe():
54
  global _pipe
55
  if _pipe is not None:
56
  return _pipe
57
- from diffusers import StableDiffusionInpaintPipeline
58
- _pipe = StableDiffusionInpaintPipeline.from_pretrained(
59
- CATVTON_LOCAL,
 
60
  torch_dtype=torch.float16,
61
- safety_checker=None,
62
- requires_safety_checker=False,
63
  ).to("cuda")
64
  _pipe.set_progress_bar_config(disable=True)
65
- print("Pipeline loaded on CUDA.")
66
  return _pipe
67
 
68
  # ---------------------------------------------------------------------------
@@ -112,17 +89,10 @@ def run_tryon(
112
  rng = torch.Generator(device="cuda")
113
  rng.manual_seed(int(seed) if seed != -1 else torch.randint(0, 2**32, (1,)).item())
114
 
115
- prompt = (
116
- "a person wearing the garment in the reference image, "
117
- "photorealistic, high quality, natural lighting"
118
- )
119
- negative = "blurry, distorted, deformed, low quality, artifacts"
120
-
121
  result = pipe(
122
- prompt=prompt,
123
- negative_prompt=negative,
124
  image=person,
125
  mask_image=mask,
 
126
  num_inference_steps=num_steps,
127
  guidance_scale=guidance_scale,
128
  generator=rng,
@@ -146,7 +116,8 @@ with gr.Blocks(title="Virtual Try-On", theme=gr.themes.Soft()) as demo:
146
  "# 👗 Virtual Try-On\n"
147
  "Upload a **person photo** and a **garment image**, select the type, then click **Try On**.\n\n"
148
  "> Runs entirely on **Hugging Face ZeroGPU** (free A10G) — no local GPU needed. \n"
149
- "> Models download once to HF persistent storage. Images save to your device via the Download button."
 
150
  )
151
 
152
  with gr.Row():
@@ -182,12 +153,9 @@ with gr.Blocks(title="Virtual Try-On", theme=gr.themes.Soft()) as demo:
182
  gr.Markdown(
183
  "---\n"
184
  "**Tips:** front-facing photo · garment on white/neutral background · upper body for shirts\n\n"
185
- "First run: ~2-5 min (model download). Subsequent runs: ~15-30s.\n\n"
186
- "Built with [CatVTON](https://github.com/zhengchong/CatVTON) · "
187
  "[Gradio](https://gradio.app) · [ZeroGPU](https://huggingface.co/docs/hub/spaces-zerogpu)"
188
  )
189
 
190
- download_models()
191
-
192
  if __name__ == "__main__":
193
  demo.launch()
 
1
  """
2
+ Virtual Try-On — Paint-by-Example + Hugging Face ZeroGPU
3
+ Uses an exemplar garment image to guide inpainting on a person photo.
4
+ No local GPU or model storage needed.
5
  """
6
 
7
  import datetime
8
  import os
 
9
 
10
  import gradio as gr
 
11
  import spaces
12
  import torch
 
13
  from PIL import Image, ImageDraw
14
 
15
  # ---------------------------------------------------------------------------
16
  # Persistent storage (/data on ZeroGPU Spaces, /tmp fallback)
17
  # ---------------------------------------------------------------------------
18
  DATA_DIR = "/data" if os.path.exists("/data") else "/tmp"
 
19
  OUTPUT_DIR = os.path.join(DATA_DIR, "outputs")
 
20
  os.makedirs(OUTPUT_DIR, exist_ok=True)
21
 
22
+ # Point HF cache to persistent storage so model downloads survive restarts
23
  os.environ["HF_HOME"] = os.path.join(DATA_DIR, "hf_cache")
24
  os.environ["HUGGINGFACE_HUB_CACHE"] = os.path.join(DATA_DIR, "hf_cache", "hub")
25
 
26
  # ---------------------------------------------------------------------------
27
+ # Pipeline (loaded lazily inside @spaces.GPU to avoid wasting GPU quota)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  # ---------------------------------------------------------------------------
29
  _pipe = None
30
 
 
32
  global _pipe
33
  if _pipe is not None:
34
  return _pipe
35
+ from diffusers import PaintByExamplePipeline
36
+ print("Loading Paint-by-Example pipeline (~5 GB, first run only)…")
37
+ _pipe = PaintByExamplePipeline.from_pretrained(
38
+ "Fantasy-Studio/Paint-by-Example",
39
  torch_dtype=torch.float16,
 
 
40
  ).to("cuda")
41
  _pipe.set_progress_bar_config(disable=True)
42
+ print("Pipeline ready on CUDA.")
43
  return _pipe
44
 
45
  # ---------------------------------------------------------------------------
 
89
  rng = torch.Generator(device="cuda")
90
  rng.manual_seed(int(seed) if seed != -1 else torch.randint(0, 2**32, (1,)).item())
91
 
 
 
 
 
 
 
92
  result = pipe(
 
 
93
  image=person,
94
  mask_image=mask,
95
+ example_image=garment,
96
  num_inference_steps=num_steps,
97
  guidance_scale=guidance_scale,
98
  generator=rng,
 
116
  "# 👗 Virtual Try-On\n"
117
  "Upload a **person photo** and a **garment image**, select the type, then click **Try On**.\n\n"
118
  "> Runs entirely on **Hugging Face ZeroGPU** (free A10G) — no local GPU needed. \n"
119
+ "> Models download once to HF persistent storage. Images save to your device via the Download button.\n\n"
120
+ "> **First run:** ~2-3 min (model download). **Subsequent runs:** ~15-30s."
121
  )
122
 
123
  with gr.Row():
 
153
  gr.Markdown(
154
  "---\n"
155
  "**Tips:** front-facing photo · garment on white/neutral background · upper body for shirts\n\n"
156
+ "Built with [Paint-by-Example](https://github.com/Fantasy-Studio/Paint-by-Example) · "
 
157
  "[Gradio](https://gradio.app) · [ZeroGPU](https://huggingface.co/docs/hub/spaces-zerogpu)"
158
  )
159
 
 
 
160
  if __name__ == "__main__":
161
  demo.launch()