Spaces:
Runtime error
Runtime error
File size: 10,244 Bytes
c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 0ae5112 c5d4941 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
import gradio as gr
import torch
from PIL import Image
import os
import sys
import gc
from huggingface_hub import snapshot_download
import numpy as np
# Add CatVTON to path
sys.path.insert(0, './CatVTON')
from model.pipeline import CatVTONPipeline
from model.cloth_masker import AutoMasker
from utils import init_weight_dtype, resize_and_crop, resize_and_padding
class CatVTONService:
def __init__(self):
# Auto-detect device
self.device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"π₯οΈ Using device: {self.device}")
self.pipeline = None
self.automasker = None
self.models_loaded = False
def load_models(self):
"""Load models once and cache them"""
if self.models_loaded:
return
print("π Loading CatVTON models (this happens once)...")
try:
# Download model weights from HuggingFace Hub - CACHED automatically
repo_path = snapshot_download(
repo_id="zhengchong/CatVTON",
cache_dir="./model_cache",
resume_download=True, # Resume if interrupted
local_files_only=False # Allow downloading
)
print(f"β
Models downloaded to: {repo_path}")
# Determine weight dtype based on device
weight_dtype = init_weight_dtype("fp16" if self.device == "cuda" else "fp32")
use_tf32 = self.device == "cuda" # Only use TF32 on CUDA
print(f"βοΈ Weight dtype: {weight_dtype}, TF32: {use_tf32}")
# Initialize pipeline
self.pipeline = CatVTONPipeline(
base_ckpt="booksforcharlie/stable-diffusion-inpainting",
attn_ckpt=repo_path,
attn_ckpt_version="mix",
weight_dtype=weight_dtype,
use_tf32=use_tf32,
device=self.device
)
# Initialize automasker
self.automasker = AutoMasker(
densepose_ckpt=os.path.join(repo_path, "DensePose"),
schp_ckpt=os.path.join(repo_path, "SCHP"),
device=self.device
)
self.models_loaded = True
print("β
CatVTON ready!")
except Exception as e:
print(f"β Error loading models: {e}")
raise
def generate_tryon(self, person_image, garment_image, progress=gr.Progress()):
"""Generate virtual try-on result"""
try:
# Load models if not already loaded
progress(0, desc="Loading models...")
self.load_models()
# Validate inputs
if person_image is None or garment_image is None:
return None, "β Please upload both person and garment images!"
progress(0.2, desc="Processing images...")
# Convert to PIL Images
if isinstance(person_image, np.ndarray):
person_img = Image.fromarray(person_image).convert("RGB")
else:
person_img = person_image.convert("RGB")
if isinstance(garment_image, np.ndarray):
garment_img = Image.fromarray(garment_image).convert("RGB")
else:
garment_img = garment_image.convert("RGB")
# Resize images
target_width = 768
target_height = 1024
person_img = resize_and_crop(person_img, (target_width, target_height))
garment_img = resize_and_padding(garment_img, (target_width, target_height))
progress(0.4, desc="Generating body mask...")
# Generate mask
mask = self.automasker(person_img, "upper")['mask']
# Clear memory
gc.collect()
if self.device == "cuda":
torch.cuda.empty_cache()
device_msg = "GPU - ~30-60 seconds" if self.device == "cuda" else "CPU - ~2-5 minutes"
progress(0.6, desc=f"Running virtual try-on on {device_msg}...")
# Run inference
result = self.pipeline(
image=person_img,
condition_image=garment_img,
mask=mask,
num_inference_steps=50,
guidance_scale=2.5,
seed=42,
height=target_height,
width=target_width
)[0]
# Clear memory after inference
gc.collect()
if self.device == "cuda":
torch.cuda.empty_cache()
progress(1.0, desc="Complete!")
return result, f"β
Virtual try-on generated successfully on {self.device.upper()}!"
except Exception as e:
import traceback
error_msg = f"β Error: {str(e)}\n\n{traceback.format_exc()}"
print(error_msg)
# Clear memory on error
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
return None, error_msg
# Initialize service
print("π Initializing CatVTON Service...")
service = CatVTONService()
# Preload models on startup (optional - comment out if you want lazy loading)
# try:
# service.load_models()
# except Exception as e:
# print(f"β οΈ Could not preload models: {e}")
# print("Models will be loaded on first request")
# Create Gradio Interface
def generate_tryon_interface(person_img, garment_img, progress=gr.Progress()):
"""Wrapper for Gradio"""
result, message = service.generate_tryon(person_img, garment_img, progress)
return result, message
# Build UI
with gr.Blocks(
title="CatVTON Virtual Try-On",
theme=gr.themes.Soft(),
css="""
.gradio-container {max-width: 1200px !important}
#title {text-align: center; margin-bottom: 1em}
#subtitle {text-align: center; color: #666; margin-bottom: 2em}
"""
) as demo:
device_info = "π₯οΈ GPU" if torch.cuda.is_available() else "π» CPU"
processing_time = "30-60 seconds" if torch.cuda.is_available() else "2-5 minutes"
gr.HTML(f"""
<div id="title">
<h1>π CatVTON - Virtual Try-On</h1>
</div>
<div id="subtitle">
<p>Upload a person image and a garment to see how it looks on them!</p>
<p><strong>Device:</strong> {device_info} | <strong>Processing Time:</strong> ~{processing_time}</p>
<p><em>First run downloads models (~5GB) - subsequent runs are faster!</em></p>
</div>
""")
with gr.Row():
with gr.Column():
gr.Markdown("### πΈ Step 1: Upload Images")
person_input = gr.Image(
label="π€ Person Image (full body, front-facing)",
type="pil",
sources=["upload", "clipboard"]
)
garment_input = gr.Image(
label="π Garment Image (upper body clothing)",
type="pil",
sources=["upload", "clipboard"]
)
generate_btn = gr.Button(
"π Generate Virtual Try-On",
variant="primary",
size="lg"
)
gr.Markdown("""
### π‘ Tips for Best Results:
- Use well-lit, clear images
- Person should face camera directly
- Garment on plain/white background
- Works best with shirts, jackets, tops
- Avoid images with multiple people
""")
with gr.Column():
gr.Markdown("### β¨ Result")
result_output = gr.Image(
label="Generated Try-On Result",
type="pil"
)
status_output = gr.Textbox(
label="Status",
lines=3,
show_label=True
)
# Examples (only show if examples directory exists)
if os.path.exists("examples"):
gr.Markdown("### π Example Images")
example_files = []
if os.path.exists("examples/person1.jpg") and os.path.exists("examples/garment1.jpg"):
example_files.append(["examples/person1.jpg", "examples/garment1.jpg"])
if os.path.exists("examples/person2.jpg") and os.path.exists("examples/garment2.jpg"):
example_files.append(["examples/person2.jpg", "examples/garment2.jpg"])
if example_files:
gr.Examples(
examples=example_files,
inputs=[person_input, garment_input],
label="Try these examples"
)
# Footer
gr.Markdown("""
---
### βΉοΈ About
This app uses **CatVTON** (Concatenation-based Attention Virtual Try-On) for realistic garment transfer.
- Model: [zhengchong/CatVTON](https://huggingface.co/zhengchong/CatVTON)
- Based on Stable Diffusion Inpainting
- Supports upper body garments (shirts, jackets, tops)
**Note:** Processing time depends on hardware. GPU is recommended for faster results.
""")
# Connect button
generate_btn.click(
fn=generate_tryon_interface,
inputs=[person_input, garment_input],
outputs=[result_output, status_output]
)
# Launch app
if __name__ == "__main__":
print("\n" + "="*60)
print("π Starting CatVTON Virtual Try-On Server")
print("="*60)
print(f"Device: {service.device}")
print(f"Server: http://0.0.0.0:7860")
print("="*60 + "\n")
demo.queue(
max_size=20, # Max queue size
default_concurrency_limit=2 # Limit concurrent requests
)
demo.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True,
share=False # Don't create public link on HF Spaces
) |