""" 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("""

👔 Virtual Try-On System

Powered by IDM-VTON - State-of-the-Art AI

⚡ Photorealistic Results • 🎨 AI-Powered • ✅ Actually Works!

""") gr.HTML("""

⚠️ License Notice

This uses IDM-VTON model (CC BY-NC-SA 4.0 License)

""") 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("""

✅ Why This Actually Works

Unlike simple image overlays, this uses real AI technology:

""") 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 )