File size: 3,572 Bytes
ee432f8
 
8a0497c
ee432f8
 
 
 
 
 
edaff03
 
 
 
 
d1a3d0b
 
 
edaff03
d1a3d0b
 
edaff03
d1a3d0b
 
edaff03
d1a3d0b
 
 
 
 
 
 
 
edaff03
d1a3d0b
 
cfc357c
 
d1a3d0b
cfc357c
 
edaff03
8a0497c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eebb212
cfc357c
f7ae8f4
d1a3d0b
cfc357c
 
f7ae8f4
7e9fa5a
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
95
96
"""
Labasni Recommender Service - Hugging Face Space
✅ Compatible Gradio 6.x avec API REST forcée
"""

import gradio as gr
import json
from recommender_model import recommend_outfit_ml

def recommend_outfit_api(clothes_json: str, preference: str, city: str = "Tunis"):
    """
    API endpoint pour les recommandations d'outfit
    """
    try:
        print(f"🔍 Received request - Preference: {preference}, City: {city}")
        print(f"📦 Clothes data length: {len(clothes_json)} chars")
        
        clothes_data = json.loads(clothes_json)
        print(f"✅ Parsed {len(clothes_data)} clothes items")
        
        result = recommend_outfit_ml(clothes_data, preference, city)
        print(f"✅ Recommendation generated: {result.get('success', False)}")
        
        return json.dumps(result, indent=2)
    except json.JSONDecodeError as e:
        error_msg = f"Invalid JSON format: {str(e)}"
        print(f"❌ JSON Error: {error_msg}")
        return json.dumps({
            "success": False,
            "error": error_msg,
            "message": "Le format JSON des vêtements est invalide"
        }, indent=2)
    except Exception as e:
        error_msg = str(e)
        print(f"❌ Error: {error_msg}")
        return json.dumps({
            "success": False,
            "error": error_msg,
            "message": "Erreur lors de la recommandation"
        }, indent=2)

# ✅ Version avec gr.Blocks pour plus de contrôle sur l'API
with gr.Blocks() as demo:
    gr.Markdown("# 🎽 Labasni Outfit Recommender")
    gr.Markdown("Recommandations d'outfits basées sur ML")
    
    with gr.Row():
        with gr.Column():
            clothes_input = gr.Textbox(
                label="Clothes Data (JSON)",
                placeholder='[{"id":"top1","category":"top",...}]',
                lines=10,
                value='[{"id":"top1","category":"top","style":"casual","color":"white","season":"all","score":0.8},{"id":"bottom1","category":"bottom","style":"casual","color":"blue","season":"all","score":0.7},{"id":"shoe1","category":"footwear","style":"casual","color":"black","season":"all","score":0.9}]'
            )
            preference_input = gr.Dropdown(
                choices=["casual", "formal", "sport", "chic", "elegant"],
                label="Preference",
                value="casual"
            )
            city_input = gr.Textbox(label="City", value="Tunis")
            submit_btn = gr.Button("Get Recommendation", variant="primary")
        
        with gr.Column():
            output = gr.Textbox(
                label="Recommended Outfit (JSON)",
                lines=15
            )
    
    # Exemples
    gr.Examples(
        examples=[
            [
                '[{"id":"top1","category":"top","style":"casual","color":"white","season":"all","score":0.8},{"id":"bottom1","category":"bottom","style":"casual","color":"blue","season":"all","score":0.7},{"id":"shoe1","category":"footwear","style":"casual","color":"black","season":"all","score":0.9}]',
                "casual",
                "Tunis"
            ]
        ],
        inputs=[clothes_input, preference_input, city_input]
    )
    
    # Event handler
    submit_btn.click(
        fn=recommend_outfit_api,
        inputs=[clothes_input, preference_input, city_input],
        outputs=output,
        api_name="predict"  # ✅ Force le nom de l'API
    )

if __name__ == "__main__":
    # ✅ Lancer avec l'API activée
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )