Spaces:
Running
Running
| """ | |
| 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 | |
| ) |