aryanprooo's picture
Update app.py
82aad57 verified
"""
Virtual Try-On using IDM-VTON via Gradio Client
THIS ACTUALLY WORKS!
Note: For personal/non-commercial use only (IDM-VTON license)
"""
import gradio as gr
from gradio_client import Client, handle_file
from PIL import Image
import time
import os
class WorkingVirtualTryOn:
"""Actually working virtual try-on using IDM-VTON"""
def __init__(self):
self.client = None
self.backup_clients = [
"yisol/IDM-VTON",
"levihsu/OOTDiffusion"
]
self.current_model = None
print("πŸ”„ Initializing Virtual Try-On...")
def connect_to_model(self, model_id):
"""Connect to model"""
try:
print(f"πŸ”„ Connecting to {model_id}...")
client = Client(model_id)
print(f"βœ… Connected to {model_id}")
return client
except Exception as e:
print(f"❌ Failed to connect to {model_id}: {e}")
return None
def process_with_idm_vton(self, person_img, garment_img, garment_desc, progress=gr.Progress()):
"""Process using IDM-VTON"""
try:
progress(0.2, desc="πŸ”— Connecting to IDM-VTON...")
client = self.connect_to_model("yisol/IDM-VTON")
if not client:
return None, "❌ Failed to connect to IDM-VTON"
progress(0.4, desc="πŸ“€ Uploading images...")
# Call IDM-VTON
result = client.predict(
dict={
"background": handle_file(person_img),
"layers": [],
"composite": None
},
garm_img=handle_file(garment_img),
garment_des=garment_desc,
is_checked=True,
is_checked_crop=False,
denoise_steps=30,
seed=42,
api_name="/tryon"
)
progress(0.9, desc="βœ… Processing complete!")
if result and len(result) > 0:
return result[0], "βœ… Success with IDM-VTON!"
return None, "❌ No result from model"
except Exception as e:
error = str(e)
if "queue" in error.lower():
return None, "⏳ Model is busy. Please wait 1-2 minutes and try again."
elif "connection" in error.lower():
return None, "❌ Connection failed. Model may be temporarily down."
else:
return None, f"❌ Error: {error}"
def process_with_ootd(self, person_img, garment_img, progress=gr.Progress()):
"""Fallback: OOTDiffusion"""
try:
progress(0.3, desc="πŸ”— Trying OOTDiffusion...")
client = self.connect_to_model("levihsu/OOTDiffusion")
if not client:
return None, "❌ Failed to connect"
progress(0.5, desc="πŸ“€ Processing...")
result = client.predict(
vton_img=handle_file(person_img),
garm_img=handle_file(garment_img),
n_samples=1,
n_steps=20,
image_scale=1.0,
seed=-1,
api_name="/process_dc"
)
if result:
return result, "βœ… Success with OOTDiffusion!"
return None, "❌ No result"
except Exception as e:
return None, f"❌ Error: {str(e)}"
def process_tryon(self, person_img, garment_img, garment_type, progress=gr.Progress()):
"""Main process with fallback"""
if not person_img or not garment_img:
return None, "❌ Please upload both images!"
progress(0.1, desc="πŸš€ Starting...")
# Description based on type
descriptions = {
"Upper Body": "upper body garment, shirt or top",
"Lower Body": "lower body garment, pants or trousers",
"Dress": "full body dress or outfit"
}
garment_desc = descriptions.get(garment_type, "garment")
# Try IDM-VTON first
result, status = self.process_with_idm_vton(
person_img,
garment_img,
garment_desc,
progress
)
if result:
final_status = f"""βœ… Virtual Try-On Successful!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
πŸ“Š Generation Details:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
πŸ€– Model: IDM-VTON (State-of-the-art)
πŸ‘• Garment Type: {garment_type}
✨ Quality: Photorealistic
⚑ Method: AI-powered virtual try-on
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠️ Important License Information:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
πŸ“œ License: CC BY-NC-SA 4.0
βœ… Allowed: Personal use, Education, Research
❌ NOT Allowed: Commercial use, Business
For commercial use, contact IDM-VTON authors
or use commercial virtual try-on APIs.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
πŸ’‘ This app works by calling the public
IDM-VTON Gradio space. Quality is excellent!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
"""
return result, final_status
# Try OOTDiffusion as backup
progress(0.5, desc="πŸ”„ Trying backup model...")
result, status = self.process_with_ootd(person_img, garment_img, progress)
if result:
return result, f"βœ… Success with backup model!\n\n{status}"
# Both failed
return None, """❌ All models are currently busy or unavailable.
πŸ”§ What to do:
1️⃣ **Wait 2-3 minutes** and try again
- Public models get busy during peak hours
- Usually works within a few retries
2️⃣ **Try during off-peak hours**
- Early morning or late night (UTC)
- Weekdays usually less busy
3️⃣ **Check model status**
- Visit: https://huggingface.co/spaces/yisol/IDM-VTON
- See if it's online and working
4️⃣ **Alternative for commercial use**
- Use paid virtual try-on APIs
- Examples: Replicate, Scenario, etc.
πŸ’‘ This app depends on free public models,
so occasional busy times are normal!
"""
# Initialize
print("="*70)
print("πŸš€ Virtual Try-On - IDM-VTON Edition")
print("βœ… Actually works with real AI!")
print("="*70)
vton = WorkingVirtualTryOn()
# CSS
css = """
.container {max-width: 1400px; margin: auto;}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 50px 30px;
text-align: center;
border-radius: 20px;
margin-bottom: 30px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.header h1 {
font-size: 3.5em;
margin: 0;
font-weight: 800;
}
.warning-box {
background: #fff3cd;
border: 2px solid #ffc107;
border-left: 5px solid #ff6b6b;
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.success-box {
background: #d4edda;
border: 2px solid #28a745;
border-left: 5px solid #28a745;
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.upload-box {
border: 3px dashed #667eea !important;
border-radius: 15px !important;
padding: 25px;
background: #f8f9ff !important;
}
.btn-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
color: white !important;
font-size: 20px !important;
font-weight: 700 !important;
padding: 18px 50px !important;
border-radius: 12px !important;
}
"""
# Gradio Interface
with gr.Blocks(css=css, theme=gr.themes.Soft(), title="Virtual Try-On - IDM-VTON") as demo:
gr.HTML("""
<div class="header">
<h1>πŸ‘” Virtual Try-On System</h1>
<p style='font-size: 1.4em; margin-top: 15px;'>
Powered by IDM-VTON - State-of-the-Art AI
</p>
<p style='font-size: 1em; margin-top: 10px; opacity: 0.9;'>
⚑ Photorealistic Results β€’ 🎨 AI-Powered β€’ βœ… Actually Works!
</p>
</div>
""")
gr.HTML("""
<div class="warning-box">
<h3 style='margin-top: 0; color: #856404;'>⚠️ License Notice</h3>
<p style='margin: 10px 0; color: #856404; font-size: 1.05em;'>
<strong>This uses IDM-VTON model (CC BY-NC-SA 4.0 License)</strong>
</p>
<ul style='color: #856404; line-height: 1.8;'>
<li>βœ… <strong>Allowed:</strong> Personal use, Education, Research, Learning</li>
<li>❌ <strong>NOT Allowed:</strong> Commercial use, Business applications, Selling</li>
<li>πŸ’‘ For commercial use, contact model authors or use paid APIs</li>
</ul>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## πŸ“Έ Upload Images")
person_img = gr.Image(
label="Your Photo (Full body, front-facing recommended)",
type="filepath",
elem_classes="upload-box",
height=300
)
garment_img = gr.Image(
label="Garment Photo (Clear, plain background)",
type="filepath",
elem_classes="upload-box",
height=300
)
gr.Markdown("## βš™οΈ Settings")
garment_type = gr.Radio(
["Upper Body", "Lower Body", "Dress"],
value="Upper Body",
label="Garment Type"
)
gr.Markdown("## πŸš€ Generate")
btn = gr.Button(
"✨ Generate Virtual Try-On",
variant="primary",
size="lg",
elem_classes="btn-primary"
)
gr.Markdown("""
### ⏱️ Processing Time
- Usually: 30-60 seconds
- If busy: May take 1-2 minutes
- First time: Might queue
""")
with gr.Column(scale=1):
gr.Markdown("## 🎨 Result")
output = gr.Image(
label="Virtual Try-On Result",
type="filepath",
height=600
)
status = gr.Textbox(
label="Status & Information",
lines=25,
interactive=False
)
gr.HTML("""
<div class="success-box">
<h3 style='margin-top: 0; color: #155724;'>βœ… Why This Actually Works</h3>
<p style='color: #155724; line-height: 1.8;'>
Unlike simple image overlays, this uses <strong>real AI technology</strong>:
</p>
<ul style='color: #155724; line-height: 1.8;'>
<li>πŸ€– <strong>IDM-VTON:</strong> State-of-the-art virtual try-on model</li>
<li>🎨 <strong>Photorealistic:</strong> Actual AI-generated results, not overlays</li>
<li>✨ <strong>Smart Fitting:</strong> Understands body shape and garment draping</li>
<li>πŸ”— <strong>Via Gradio Client:</strong> Calls the official IDM-VTON space</li>
</ul>
</div>
""")
gr.Markdown("""
---
## πŸ’‘ Tips for Best Results
### πŸ“Έ Person Photo:
- βœ… Full body or upper body shot
- βœ… Front-facing, standing straight
- βœ… Good lighting, minimal shadows
- βœ… Plain background (white/neutral)
- βœ… Arms slightly away from body
- βœ… High resolution (at least 512px width)
### πŸ‘• Garment Photo:
- βœ… Clear, centered clothing item
- βœ… Plain white or light background
- βœ… Flat lay or on mannequin
- βœ… No wrinkles or folds
- βœ… Good lighting, no shadows
- βœ… High resolution for details
### βš™οΈ If Model is Busy:
- ⏳ Wait 2-3 minutes and retry
- πŸŒ™ Try during off-peak hours
- πŸ”„ Refresh page if stuck
- βœ… Usually works within 2-3 tries
---
## πŸ” How This Works
**Technical Details:**
1. Your images are sent to the IDM-VTON public Gradio space
2. IDM-VTON AI model processes the virtual try-on
3. Photorealistic result is generated and returned
4. No local AI model needed - uses the official space
**Advantages:**
- βœ… Real AI quality (not image processing)
- βœ… No model download (5GB+ saved)
- βœ… Always up-to-date with latest model
- βœ… No GPU required on your end
**Limitations:**
- ⏳ Depends on public model availability
- πŸ”’ Non-commercial license only
- ⏱️ May queue during busy times
---
## πŸ“Š Comparison
| Method | Quality | Speed | Commercial? | This App |
|--------|---------|-------|-------------|----------|
| **IDM-VTON (this)** | ⭐⭐⭐⭐⭐ | 30-60s | ❌ No | βœ… Yes |
| **Image Overlay** | ⭐⭐ | Instant | βœ… Yes | ❌ No |
| **Commercial APIs** | ⭐⭐⭐⭐⭐ | 10-30s | βœ… Yes | ❌ No |
---
## ⚠️ For Commercial Use
If you need virtual try-on for business:
1. **Contact IDM-VTON authors** for commercial license
2. **Use commercial APIs:**
- Replicate.com (pay per use)
- Scenario.com (subscription)
- Custom model training
3. **Self-host with license** (requires agreement)
---
## 🎯 Use Cases (Non-Commercial)
Perfect for:
- βœ… Personal fashion experiments
- βœ… Learning and education
- βœ… Research projects
- βœ… Portfolio demos
- βœ… Testing outfits before buying
- βœ… Fashion design education
---
**Powered by IDM-VTON | For Personal Use Only**
""")
# Event
btn.click(
fn=vton.process_tryon,
inputs=[person_img, garment_img, garment_type],
outputs=[output, status]
)
if __name__ == "__main__":
print("\n" + "="*70)
print("πŸŽ‰ App Ready!")
print("βœ… Using real IDM-VTON AI")
print("⚠️ Non-commercial use only")
print("="*70 + "\n")
demo.queue(max_size=10)
demo.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True
)