File size: 2,948 Bytes
bc6a2a5
 
e71d0a2
bc6a2a5
 
 
 
 
 
 
e71d0a2
bc6a2a5
 
e71d0a2
bc6a2a5
 
e71d0a2
bc6a2a5
 
 
e71d0a2
bc6a2a5
 
e71d0a2
 
 
f54a0c9
e71d0a2
bc6a2a5
e71d0a2
 
 
bc6a2a5
e71d0a2
 
 
 
 
 
 
 
 
 
 
 
 
 
bc6a2a5
e71d0a2
 
 
 
 
 
bc6a2a5
e71d0a2
 
bc6a2a5
 
e71d0a2
 
 
 
 
 
bc6a2a5
 
e71d0a2
 
 
bc6a2a5
 
e71d0a2
 
 
 
bc6a2a5
 
 
e71d0a2
 
 
 
f54a0c9
e71d0a2
f54a0c9
bc6a2a5
 
 
e71d0a2
 
 
 
 
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
"""
Labasni VTO Service - Hugging Face Space
✅ Compatible avec Gradio Client pour WebSocket temps réel
"""

import gradio as gr
import json
import base64
from vto_model import process_frame_vto

def vto_api(frame_base64: str, clothes_json: str):
    """
    API endpoint pour le Virtual Try-On
    ✅ CORRIGÉ : Accepte directement le base64
    
    Args:
        frame_base64: Image encodée en base64 (STRING)
        clothes_json: JSON des vêtements à essayer
    
    Returns:
        JSON string avec {success: true, frame: "base64..."}
    """
    try:
        print(f"🔍 VTO Request received")
        print(f"   Frame length: {len(frame_base64)} chars")
        print(f"   Clothes JSON length: {len(clothes_json)} chars")
        
        # Parser le JSON des vêtements
        clothes_data = json.loads(clothes_json)
        print(f"   ✅ Parsed {len(clothes_data)} clothing item(s)")
        
        # Appeler le modèle VTO
        result = process_frame_vto(frame_base64, clothes_data)
        
        print(f"   ✅ VTO processing: {result.get('success', False)}")
        
        # Retourner en JSON string (pour Gradio Client)
        return json.dumps(result, ensure_ascii=False)
        
    except json.JSONDecodeError as e:
        error_response = {
            "success": False,
            "error": f"Invalid JSON: {str(e)}"
        }
        print(f"   ❌ JSON Error: {str(e)}")
        return json.dumps(error_response)
        
    except Exception as e:
        error_response = {
            "success": False,
            "error": str(e)
        }
        print(f"   ❌ Error: {str(e)}")
        return json.dumps(error_response)

# ✅ Interface Gradio (pour tests manuels ET Gradio Client)
demo = gr.Interface(
    fn=vto_api,
    inputs=[
        gr.Textbox(
            label="Frame (Base64)",
            placeholder="data:image/jpeg;base64,/9j/4AAQ...",
            lines=3,
            max_lines=5
        ),
        gr.Textbox(
            label="Clothes to Try (JSON)",
            placeholder='[{"imageURL":"https://...","category":"top"}]',
            lines=5,
            value='[{"imageURL":"https://res.cloudinary.com/dechk1ohr/image/upload/v1765128119/istockphoto-483960103-612x612-removebg-preview_fnh1r4.png","category":"top"}]'
        )
    ],
    outputs=gr.Textbox(
        label="VTO Result (JSON)",
        lines=10
    ),
    title="👕 Labasni Virtual Try-On",
    description="Essayez des vêtements virtuellement avec MediaPipe + OpenCV",
    examples=[
        [
            "",  # Laissez vide pour test manuel
            '[{"imageURL":"https://res.cloudinary.com/dechk1ohr/image/upload/v1765128119/istockphoto-483960103-612x612-removebg-preview_fnh1r4.png","category":"top"}]'
        ]
    ],
    api_name="predict",
    cache_examples=False
)

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )