""" 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("""
Powered by IDM-VTON - State-of-the-Art AI
⚡ Photorealistic Results • 🎨 AI-Powered • ✅ Actually Works!
This uses IDM-VTON model (CC BY-NC-SA 4.0 License)
Unlike simple image overlays, this uses real AI technology: