Hemil Ghori commited on
Commit
5d8d06b
Β·
1 Parent(s): 43375a7

change ui

Browse files
Files changed (2) hide show
  1. app.py +227 -123
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,161 +1,265 @@
1
  import os
2
- import subprocess
3
  import threading
4
- import atexit
5
- import asyncio
6
  from typing import Optional
7
 
8
  import gradio as gr
9
  import numpy as np
10
- import gc
11
  import torch
 
12
  from PIL import Image
13
 
14
- from fashn_vton import TryOnPipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- # Auto-download weights for Hugging Face Spaces
17
- if not os.path.exists("./weights/model.safetensors"):
18
- print("Downloading weights...")
19
- subprocess.run(["python", "scripts/download_weights.py", "--weights-dir", "./weights"], check=True)
20
 
21
- # Thread-safe lazy pipeline loader
22
  _pipeline_lock = threading.Lock()
23
- _pipeline: Optional[TryOnPipeline] = None
24
- _pipeline_device: Optional[str] = None
25
 
26
 
27
- def get_pipeline(weights_dir: str = "./weights", device: Optional[str] = None) -> TryOnPipeline:
28
- global _pipeline, _pipeline_device
 
29
  with _pipeline_lock:
30
- # Normalize device string
31
- dev = device
32
- if dev is not None and dev.lower() == "auto":
33
- dev = None
34
-
35
- # If a pipeline exists and device is changing, delete it and free CUDA memory
36
- if _pipeline is not None and (_pipeline_device is not None and dev != _pipeline_device):
37
- try:
38
- del _pipeline
39
- except Exception:
40
- pass
41
- _pipeline = None
42
- # free python-side references and CUDA caches
43
- gc.collect()
44
- try:
45
- if torch.cuda.is_available():
46
- torch.cuda.empty_cache()
47
- except Exception:
48
- pass
49
-
50
- # Recreate pipeline if device changed or not created
51
  if _pipeline is None:
52
- _pipeline = TryOnPipeline(weights_dir=weights_dir, device=dev)
53
- _pipeline_device = dev
54
- return _pipeline
55
-
56
-
57
- def pil_from_input(x):
58
- if x is None:
59
- return None
60
- if isinstance(x, Image.Image):
61
- return x.convert("RGB")
62
- if isinstance(x, np.ndarray):
63
- return Image.fromarray(x).convert("RGB")
64
- # If it's a filepath
65
- try:
66
- return Image.open(x).convert("RGB")
67
- except Exception:
68
- return None
69
 
70
 
71
- def run_tryon(
72
- person,
73
- garment,
74
- category,
 
75
  num_timesteps: int,
76
- num_samples: int,
77
  guidance_scale: float,
 
78
  segmentation_free: bool,
79
- weights_dir: str = "./weights",
80
  ):
81
- person_img = pil_from_input(person)
82
- garment_img = pil_from_input(garment)
83
- if person_img is None or garment_img is None:
84
- return None, "Invalid input images"
 
85
 
86
- try:
87
- # Force CPU-only execution in the Space
88
- pipeline = get_pipeline(weights_dir=weights_dir, device="cpu")
89
- except Exception as e:
90
- return None, f"Failed to load pipeline: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  try:
93
  result = pipeline(
94
  person_image=person_img,
95
  garment_image=garment_img,
96
  category=category,
97
- num_samples=num_samples,
 
98
  num_timesteps=num_timesteps,
99
  guidance_scale=guidance_scale,
 
100
  segmentation_free=segmentation_free,
101
  )
102
- images = result.images
103
- return images, "Done"
104
  except Exception as e:
105
- return None, f"Inference error: {e}"
106
-
107
-
108
- def build_ui():
109
- with gr.Blocks(title="FASHN VTON - TryOn demo") as demo:
110
- gr.Markdown("# FASHN VTON β€” Virtual Try-On (demo)")
111
-
112
- with gr.Row():
113
- with gr.Column(scale=1):
114
- person_in = gr.Image(label="Person image", type="pil")
115
- garment_in = gr.Image(label="Garment image", type="pil")
116
- category = gr.Dropdown(choices=["tops", "bottoms", "one-pieces"], value="tops", label="Category")
117
- weights_dir = gr.Textbox(value="./weights", visible=False)
118
- segmentation_free = gr.Checkbox(label="Segmentation-free (recommended)", value=True)
119
- num_timesteps = gr.Slider(minimum=10, maximum=50, step=1, value=30, label="Num timesteps")
120
- num_samples = gr.Slider(minimum=1, maximum=3, step=1, value=1, label="Num samples")
121
- guidance_scale = gr.Slider(minimum=0.0, maximum=5.0, step=0.1, value=1.5, label="Guidance scale")
122
- run_btn = gr.Button("Run Try-On")
123
-
124
- with gr.Column(scale=1):
125
- status = gr.Textbox(value="Ready", label="Status", interactive=False)
126
- gallery = gr.Gallery(label="Outputs", show_label=True, elem_id="outputs_gallery")
127
-
128
- def _on_click(person, garment, category, num_timesteps, num_samples, guidance_scale, segmentation_free, weights_dir):
129
- images, msg = run_tryon(person, garment, category, int(num_timesteps), int(num_samples), float(guidance_scale), segmentation_free, weights_dir)
130
- if images is None:
131
- return gr.update(value=[]), msg
132
- return images, msg
133
-
134
- run_btn.click(
135
- fn=_on_click,
136
- inputs=[person_in, garment_in, category, num_timesteps, num_samples, guidance_scale, segmentation_free, weights_dir],
137
- outputs=[gallery, status],
138
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
- return demo
 
 
 
 
 
 
 
 
 
 
141
 
 
142
 
143
  if __name__ == "__main__":
144
- demo = build_ui()
145
-
146
- # Register cleanup to close asyncio loop at process exit. This suppresses the
147
- # "Invalid file descriptor" errors raised during interpreter shutdown in
148
- # some container runtimes.
149
- def _close_asyncio_loop():
150
- try:
151
- loop = asyncio.get_event_loop()
152
- if loop.is_running():
153
- loop.stop()
154
- if not loop.is_closed():
155
- loop.close()
156
- except Exception:
157
- pass
158
-
159
- atexit.register(_close_asyncio_loop)
160
-
161
- demo.launch(server_name="0.0.0.0", share=False, ssr_mode=False)
 
1
  import os
2
+ import gc
3
  import threading
 
 
4
  from typing import Optional
5
 
6
  import gradio as gr
7
  import numpy as np
 
8
  import torch
9
+ from huggingface_hub import hf_hub_download
10
  from PIL import Image
11
 
12
+ # ─────────────────────────── CONFIG ──────────────────────────── #
13
+
14
+ SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
15
+ WEIGHTS_DIR = os.path.join(SCRIPT_DIR, "weights")
16
+ EXAMPLES_DIR = os.path.join(SCRIPT_DIR, "examples", "data")
17
+
18
+ CATEGORIES = ["tops", "bottoms", "one-pieces"]
19
+ GARMENT_PHOTO_TYPES = ["model", "flat-lay"]
20
+
21
+ # ──────────────────────── WEIGHT DOWNLOAD ────────────────────── #
22
+
23
+ def download_weights():
24
+ """Download model weights from HuggingFace Hub (skips if already present)."""
25
+ os.makedirs(WEIGHTS_DIR, exist_ok=True)
26
+ dwpose_dir = os.path.join(WEIGHTS_DIR, "dwpose")
27
+ os.makedirs(dwpose_dir, exist_ok=True)
28
+
29
+ tryon_path = os.path.join(WEIGHTS_DIR, "model.safetensors")
30
+ if not os.path.exists(tryon_path):
31
+ print("Downloading TryOnModel weights...")
32
+ hf_hub_download(
33
+ repo_id="fashn-ai/fashn-vton-1.5",
34
+ filename="model.safetensors",
35
+ local_dir=WEIGHTS_DIR,
36
+ )
37
+
38
+ for filename in ["yolox_l.onnx", "dw-ll_ucoco_384.onnx"]:
39
+ filepath = os.path.join(dwpose_dir, filename)
40
+ if not os.path.exists(filepath):
41
+ print(f"Downloading DWPose/{filename}...")
42
+ hf_hub_download(
43
+ repo_id="fashn-ai/DWPose",
44
+ filename=filename,
45
+ local_dir=dwpose_dir,
46
+ )
47
+
48
+ print("All weights ready!")
49
 
50
+ # Download weights at startup
51
+ download_weights()
52
+
53
+ # ──────────────────────── PIPELINE LOADER ────────────────────── #
54
 
 
55
  _pipeline_lock = threading.Lock()
56
+ _pipeline: Optional[object] = None
 
57
 
58
 
59
+ def get_pipeline():
60
+ """Thread-safe lazy pipeline loader (CPU mode)."""
61
+ global _pipeline
62
  with _pipeline_lock:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  if _pipeline is None:
64
+ from fashn_vton import TryOnPipeline
65
+ print("Loading pipeline on CPU...")
66
+ _pipeline = TryOnPipeline(weights_dir=WEIGHTS_DIR, device="cpu")
67
+ print("Pipeline ready!")
68
+ return _pipeline
69
+
70
+
71
+ # ─────────────────────────── INFERENCE ───────────────────────── #
 
 
 
 
 
 
 
 
 
72
 
73
 
74
+ def try_on(
75
+ person_image,
76
+ garment_image,
77
+ category: str,
78
+ garment_photo_type: str,
79
  num_timesteps: int,
 
80
  guidance_scale: float,
81
+ seed: int,
82
  segmentation_free: bool,
 
83
  ):
84
+ """Run virtual try-on inference."""
85
+ if person_image is None:
86
+ raise gr.Error("Please upload a person image.")
87
+ if garment_image is None:
88
+ raise gr.Error("Please upload a garment image.")
89
 
90
+ # Normalise seed
91
+ if seed is None or seed < 0:
92
+ seed = 42
93
+ seed = int(seed)
94
+
95
+ # Ensure PIL RGB
96
+ def to_pil(x):
97
+ if isinstance(x, np.ndarray):
98
+ x = Image.fromarray(x)
99
+ if isinstance(x, Image.Image):
100
+ return x.convert("RGB")
101
+ return Image.open(x).convert("RGB")
102
+
103
+ person_img = to_pil(person_image)
104
+ garment_img = to_pil(garment_image)
105
+
106
+ pipeline = get_pipeline()
107
 
108
  try:
109
  result = pipeline(
110
  person_image=person_img,
111
  garment_image=garment_img,
112
  category=category,
113
+ garment_photo_type=garment_photo_type,
114
+ num_samples=1,
115
  num_timesteps=num_timesteps,
116
  guidance_scale=guidance_scale,
117
+ seed=seed,
118
  segmentation_free=segmentation_free,
119
  )
120
+ return result.images[0], "βœ… Done!"
 
121
  except Exception as e:
122
+ return None, f"❌ Error: {e}"
123
+
124
+
125
+ # ─────────────────────────── GRADIO UI ───────────────────────── #
126
+
127
+ CUSTOM_CSS = """
128
+ body { font-family: 'Inter', sans-serif; }
129
+
130
+ .contain img {
131
+ object-fit: contain !important;
132
+ max-height: 520px !important;
133
+ }
134
+
135
+ #run-btn {
136
+ background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%) !important;
137
+ border: none !important;
138
+ color: white !important;
139
+ font-size: 1.1rem !important;
140
+ font-weight: 600 !important;
141
+ padding: 0.75rem !important;
142
+ border-radius: 12px !important;
143
+ transition: opacity 0.2s;
144
+ }
145
+ #run-btn:hover { opacity: 0.85; }
146
+
147
+ .status-box textarea {
148
+ font-size: 0.9rem !important;
149
+ color: #a3e635 !important;
150
+ background: #1e1e2e !important;
151
+ border-radius: 8px !important;
152
+ }
153
+
154
+ .gr-accordion { border-radius: 10px !important; }
155
+ """
156
+
157
+ BANNER_MD = """
158
+ # πŸ‘— FASHN VTON β€” Virtual Try-On
159
+ Upload a **person image** and a **garment image**, choose the garment category and hit **Try On**!
160
+ > ⚠️ Running on **CPU** β€” inference may take a few minutes. Reduce *Sampling Steps* for faster results.
161
+ """
162
+
163
+ person_example = os.path.join(EXAMPLES_DIR, "model.webp")
164
+ garment_example = os.path.join(EXAMPLES_DIR, "garment.webp")
165
+
166
+ with gr.Blocks(css=CUSTOM_CSS, title="FASHN VTON β€” Virtual Try-On") as demo:
167
+
168
+ gr.Markdown(BANNER_MD)
169
+
170
+ with gr.Row(equal_height=False):
171
+
172
+ # ── Column 1 : Person ──────────────────────────────────
173
+ with gr.Column(scale=1):
174
+ person_in = gr.Image(
175
+ label="Person Image",
176
+ type="pil",
177
+ sources=["upload", "clipboard"],
178
+ elem_classes=["contain"],
179
+ )
180
+ if os.path.exists(person_example):
181
+ gr.Examples(
182
+ examples=[person_example],
183
+ inputs=person_in,
184
+ label="Person Example",
185
+ )
186
+
187
+ # ── Column 2 : Garment ─────────────────────────────────
188
+ with gr.Column(scale=1):
189
+ garment_in = gr.Image(
190
+ label="Garment Image",
191
+ type="pil",
192
+ sources=["upload", "clipboard"],
193
+ elem_classes=["contain"],
194
+ )
195
+ with gr.Row():
196
+ category = gr.Dropdown(
197
+ choices=CATEGORIES,
198
+ value="tops",
199
+ label="Category",
200
+ )
201
+ garment_photo_type = gr.Dropdown(
202
+ choices=GARMENT_PHOTO_TYPES,
203
+ value="model",
204
+ label="Photo Type",
205
+ )
206
+ if os.path.exists(garment_example):
207
+ gr.Examples(
208
+ examples=[garment_example],
209
+ inputs=garment_in,
210
+ label="Garment Example",
211
+ )
212
+
213
+ # ── Column 3 : Result ──────────────────────────────────
214
+ with gr.Column(scale=1):
215
+ result_img = gr.Image(
216
+ label="Try-On Result",
217
+ type="pil",
218
+ interactive=False,
219
+ elem_classes=["contain"],
220
+ )
221
+ status = gr.Textbox(
222
+ value="Ready",
223
+ label="Status",
224
+ interactive=False,
225
+ elem_classes=["status-box"],
226
+ )
227
+ run_btn = gr.Button("πŸ‘— Try On", variant="primary", elem_id="run-btn")
228
+
229
+ with gr.Accordion("βš™οΈ Advanced Settings", open=False):
230
+ num_timesteps = gr.Slider(
231
+ minimum=10, maximum=50, value=30, step=5,
232
+ label="Sampling Steps",
233
+ info="Higher = better quality but slower. 30 is a good balance.",
234
+ )
235
+ guidance_scale = gr.Slider(
236
+ minimum=1.0, maximum=3.0, value=1.5, step=0.1,
237
+ label="Guidance Scale",
238
+ info="How closely to follow the garment details. 1.5 recommended.",
239
+ )
240
+ seed = gr.Number(
241
+ value=42, label="Seed", precision=0,
242
+ info="Change seed to get a different variation of the result.",
243
+ )
244
+ segmentation_free = gr.Checkbox(
245
+ value=True,
246
+ label="Segmentation-Free (Recommended)",
247
+ info="Preserves body features and allows unconstrained garment volume.",
248
+ )
249
 
250
+ # ── Event ──────────────────────────────────────────────────
251
+ run_btn.click(
252
+ fn=try_on,
253
+ inputs=[
254
+ person_in, garment_in,
255
+ category, garment_photo_type,
256
+ num_timesteps, guidance_scale,
257
+ seed, segmentation_free,
258
+ ],
259
+ outputs=[result_img, status],
260
+ )
261
 
262
+ demo.queue(default_concurrency_limit=1, max_size=10)
263
 
264
  if __name__ == "__main__":
265
+ demo.launch(server_name="0.0.0.0", share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -6,4 +6,5 @@ pillow
6
  opencv-python
7
  gradio
8
  onnxruntime
 
9
  git+https://github.com/fashn-AI/fashn-vton-1.5.git
 
6
  opencv-python
7
  gradio
8
  onnxruntime
9
+ huggingface_hub
10
  git+https://github.com/fashn-AI/fashn-vton-1.5.git