Spaces:
Sleeping
Sleeping
Commit ·
d1a3d0b
1
Parent(s): cfc357c
Gradio API
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Labasni Recommender Service - Hugging Face Space
|
| 3 |
-
✅ Compatible Gradio
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
|
@@ -20,55 +20,89 @@ def recommend_outfit_api(clothes_json: str, preference: str, city: str = "Tunis"
|
|
| 20 |
JSON avec l'outfit recommandé
|
| 21 |
"""
|
| 22 |
try:
|
|
|
|
|
|
|
|
|
|
| 23 |
clothes_data = json.loads(clothes_json)
|
|
|
|
|
|
|
| 24 |
result = recommend_outfit_ml(clothes_data, preference, city)
|
|
|
|
|
|
|
| 25 |
return json.dumps(result, indent=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
|
|
|
|
|
|
| 27 |
return json.dumps({
|
| 28 |
"success": False,
|
| 29 |
-
"error":
|
| 30 |
"message": "Erreur lors de la recommandation"
|
| 31 |
}, indent=2)
|
| 32 |
|
| 33 |
-
# ✅ CORRECTION
|
| 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 |
if __name__ == "__main__":
|
| 70 |
-
|
| 71 |
-
iface.launch(
|
| 72 |
server_name="0.0.0.0",
|
| 73 |
server_port=7860,
|
| 74 |
share=False
|
|
|
|
| 1 |
"""
|
| 2 |
Labasni Recommender Service - Hugging Face Space
|
| 3 |
+
✅ Compatible Gradio 6.x avec API externe accessible
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
|
|
|
| 20 |
JSON avec l'outfit recommandé
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
+
print(f"🔍 Received request - Preference: {preference}, City: {city}")
|
| 24 |
+
print(f"📦 Clothes data length: {len(clothes_json)} chars")
|
| 25 |
+
|
| 26 |
clothes_data = json.loads(clothes_json)
|
| 27 |
+
print(f"✅ Parsed {len(clothes_data)} clothes items")
|
| 28 |
+
|
| 29 |
result = recommend_outfit_ml(clothes_data, preference, city)
|
| 30 |
+
print(f"✅ Recommendation generated: {result.get('success', False)}")
|
| 31 |
+
|
| 32 |
return json.dumps(result, indent=2)
|
| 33 |
+
except json.JSONDecodeError as e:
|
| 34 |
+
error_msg = f"Invalid JSON format: {str(e)}"
|
| 35 |
+
print(f"❌ JSON Error: {error_msg}")
|
| 36 |
+
return json.dumps({
|
| 37 |
+
"success": False,
|
| 38 |
+
"error": error_msg,
|
| 39 |
+
"message": "Le format JSON des vêtements est invalide"
|
| 40 |
+
}, indent=2)
|
| 41 |
except Exception as e:
|
| 42 |
+
error_msg = str(e)
|
| 43 |
+
print(f"❌ Error: {error_msg}")
|
| 44 |
return json.dumps({
|
| 45 |
"success": False,
|
| 46 |
+
"error": error_msg,
|
| 47 |
"message": "Erreur lors de la recommandation"
|
| 48 |
}, indent=2)
|
| 49 |
|
| 50 |
+
# ✅ CORRECTION GRADIO 6.x : Utiliser gr.Blocks au lieu de gr.Interface
|
| 51 |
+
with gr.Blocks(title="🎽 Labasni Outfit Recommender") as demo:
|
| 52 |
+
gr.Markdown("""
|
| 53 |
+
# 🎽 Labasni Outfit Recommender
|
| 54 |
+
Recommandations d'outfits basées sur Machine Learning
|
| 55 |
+
""")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
with gr.Column():
|
| 59 |
+
clothes_input = gr.Textbox(
|
| 60 |
+
label="Clothes Data (JSON)",
|
| 61 |
+
placeholder='[{"id":"top1","category":"top","style":"casual","color":"white","season":"summer","score":0.5}]',
|
| 62 |
+
lines=10,
|
| 63 |
+
value='[{"id":"top1","category":"top","style":"casual","color":"white","season":"summer","score":0.8},{"id":"bottom1","category":"bottom","style":"casual","color":"blue","season":"summer","score":0.7},{"id":"shoe1","category":"footwear","style":"casual","color":"black","season":"summer","score":0.9}]'
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
preference_input = gr.Dropdown(
|
| 67 |
+
choices=["casual", "formal", "sport", "chic", "elegant"],
|
| 68 |
+
label="Preference",
|
| 69 |
+
value="casual"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
city_input = gr.Textbox(
|
| 73 |
+
label="City",
|
| 74 |
+
value="Tunis"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
submit_btn = gr.Button("🎯 Get Recommendation", variant="primary")
|
| 78 |
+
|
| 79 |
+
with gr.Column():
|
| 80 |
+
output = gr.Textbox(
|
| 81 |
+
label="Recommended Outfit (JSON)",
|
| 82 |
+
lines=15
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# ✅ Lier le bouton à la fonction
|
| 86 |
+
submit_btn.click(
|
| 87 |
+
fn=recommend_outfit_api,
|
| 88 |
+
inputs=[clothes_input, preference_input, city_input],
|
| 89 |
+
outputs=output
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
gr.Markdown("""
|
| 93 |
+
### 📋 Exemple de données
|
| 94 |
+
```json
|
| 95 |
+
[
|
| 96 |
+
{"id":"top1","category":"top","style":"casual","color":"white","season":"summer","score":0.8},
|
| 97 |
+
{"id":"bottom1","category":"bottom","style":"casual","color":"blue","season":"summer","score":0.7},
|
| 98 |
+
{"id":"shoe1","category":"footwear","style":"casual","color":"black","season":"summer","score":0.9}
|
| 99 |
+
]
|
| 100 |
+
```
|
| 101 |
+
""")
|
| 102 |
|
| 103 |
+
# ✅ CRUCIAL : Lancer avec l'API activée
|
| 104 |
if __name__ == "__main__":
|
| 105 |
+
demo.launch(
|
|
|
|
| 106 |
server_name="0.0.0.0",
|
| 107 |
server_port=7860,
|
| 108 |
share=False
|