Spaces:
Sleeping
Sleeping
Commit ·
cfc357c
1
Parent(s): a619495
fix: Update to Gradio 5.x compatible API
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Labasni Recommender Service - Hugging Face Space
|
| 3 |
-
|
| 4 |
-
Version simplifiée avec gr.Interface
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
|
@@ -25,104 +24,52 @@ def recommend_outfit_api(clothes_json: str, preference: str, city: str = "Tunis"
|
|
| 25 |
result = recommend_outfit_ml(clothes_data, preference, city)
|
| 26 |
return json.dumps(result, indent=2)
|
| 27 |
except Exception as e:
|
| 28 |
-
return json.dumps({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
# ✅ Interface Gradio
|
| 31 |
iface = gr.Interface(
|
| 32 |
fn=recommend_outfit_api,
|
| 33 |
inputs=[
|
| 34 |
gr.Textbox(
|
| 35 |
label="Clothes Data (JSON)",
|
| 36 |
-
placeholder='[{"id":"top1","category":"top","style":"casual",.
|
| 37 |
lines=10
|
| 38 |
),
|
| 39 |
gr.Dropdown(
|
| 40 |
-
choices=["casual", "formal", "sport", "chic"],
|
| 41 |
label="Preference",
|
| 42 |
value="casual"
|
| 43 |
),
|
| 44 |
-
gr.Textbox(label="City", value="Tunis")
|
| 45 |
-
],
|
| 46 |
-
outputs=gr.Textbox(label="Recommended Outfit (JSON)", lines=15),
|
| 47 |
-
title="🎽 Labasni Outfit Recommender",
|
| 48 |
-
description="Recommandations d'outfits basées sur ML (TensorFlow + PyTorch)",
|
| 49 |
-
examples=[
|
| 50 |
-
[
|
| 51 |
-
'[{"id":"top1","category":"top","style":"casual","color":"white","season":"summer","score":0.8,"imageURL":"https://example.com/top.jpg"},{"id":"bot1","category":"bottom","style":"casual","color":"blue","season":"summer","score":0.7,"imageURL":"https://example.com/bottom.jpg"},{"id":"shoe1","category":"footwear","style":"casual","color":"black","season":"summer","score":0.9,"imageURL":"https://example.com/shoes.jpg"}]',
|
| 52 |
-
"casual",
|
| 53 |
-
"Tunis"
|
| 54 |
-
]
|
| 55 |
-
],
|
| 56 |
-
# ✅ CRUCIAL : api_name est nécessaire pour Gradio 6.x
|
| 57 |
-
# Cela expose l'endpoint /api/predict
|
| 58 |
-
api_name="predict"
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
# ✅ Lancement de l'interface
|
| 62 |
-
# Hugging Face Spaces détecte automatiquement l'interface Gradio
|
| 63 |
-
iface.launch()
|
| 64 |
-
example.com
|
| 65 |
-
Amine
|
| 66 |
-
"""
|
| 67 |
-
Labasni Recommender Service - Hugging Face Space
|
| 68 |
-
Interface Gradio pour les recommandations d'outfits
|
| 69 |
-
Version simplifiée avec gr.Interface
|
| 70 |
-
"""
|
| 71 |
-
|
| 72 |
-
import gradio as gr
|
| 73 |
-
import json
|
| 74 |
-
from recommender_model import recommend_outfit_ml
|
| 75 |
-
|
| 76 |
-
def recommend_outfit_api(clothes_json: str, preference: str, city: str = "Tunis"):
|
| 77 |
-
"""
|
| 78 |
-
API endpoint pour les recommandations d'outfit
|
| 79 |
-
|
| 80 |
-
Args:
|
| 81 |
-
clothes_json: JSON string contenant la liste des vêtements
|
| 82 |
-
preference: Style préféré (casual, formal, sport)
|
| 83 |
-
city: Ville pour la météo
|
| 84 |
-
|
| 85 |
-
Returns:
|
| 86 |
-
JSON avec l'outfit recommandé
|
| 87 |
-
"""
|
| 88 |
-
try:
|
| 89 |
-
clothes_data = json.loads(clothes_json)
|
| 90 |
-
result = recommend_outfit_ml(clothes_data, preference, city)
|
| 91 |
-
return json.dumps(result, indent=2)
|
| 92 |
-
except Exception as e:
|
| 93 |
-
return json.dumps({"success": False, "error": str(e)})
|
| 94 |
-
|
| 95 |
-
# ✅ Interface Gradio - gr.Interface avec api_name explicite pour Gradio 6.x
|
| 96 |
-
# Hugging Face Spaces détecte automatiquement la variable 'demo'
|
| 97 |
-
demo = gr.Interface(
|
| 98 |
-
fn=recommend_outfit_api,
|
| 99 |
-
inputs=[
|
| 100 |
gr.Textbox(
|
| 101 |
-
label="
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
),
|
| 105 |
-
gr.Dropdown(
|
| 106 |
-
choices=["casual", "formal", "sport", "chic"],
|
| 107 |
-
label="Preference",
|
| 108 |
-
value="casual"
|
| 109 |
-
),
|
| 110 |
-
gr.Textbox(label="City", value="Tunis")
|
| 111 |
],
|
| 112 |
-
outputs=gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 113 |
title="🎽 Labasni Outfit Recommender",
|
| 114 |
description="Recommandations d'outfits basées sur ML (TensorFlow + PyTorch)",
|
| 115 |
examples=[
|
| 116 |
[
|
| 117 |
-
'[{"id":"top1","category":"top","style":"casual","color":"white","season":"summer","score":0.8
|
| 118 |
"casual",
|
| 119 |
"Tunis"
|
| 120 |
]
|
| 121 |
],
|
| 122 |
-
# ✅ CRUCIAL :
|
| 123 |
-
#
|
| 124 |
-
api_name="predict"
|
| 125 |
)
|
| 126 |
|
| 127 |
-
|
| 128 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Labasni Recommender Service - Hugging Face Space
|
| 3 |
+
✅ Compatible Gradio 5.x avec API externe accessible
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
|
|
|
| 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": str(e),
|
| 30 |
+
"message": "Erreur lors de la recommandation"
|
| 31 |
+
}, indent=2)
|
| 32 |
|
| 33 |
+
# ✅ CORRECTION PRINCIPALE : Interface Gradio 5.x avec api_name explicite
|
| 34 |
iface = gr.Interface(
|
| 35 |
fn=recommend_outfit_api,
|
| 36 |
inputs=[
|
| 37 |
gr.Textbox(
|
| 38 |
label="Clothes Data (JSON)",
|
| 39 |
+
placeholder='[{"id":"top1","category":"top","style":"casual","color":"white","season":"summer","score":0.5}]',
|
| 40 |
lines=10
|
| 41 |
),
|
| 42 |
gr.Dropdown(
|
| 43 |
+
choices=["casual", "formal", "sport", "chic", "elegant"],
|
| 44 |
label="Preference",
|
| 45 |
value="casual"
|
| 46 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
gr.Textbox(
|
| 48 |
+
label="City",
|
| 49 |
+
value="Tunis"
|
| 50 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
],
|
| 52 |
+
outputs=gr.Textbox(
|
| 53 |
+
label="Recommended Outfit (JSON)",
|
| 54 |
+
lines=15
|
| 55 |
+
),
|
| 56 |
title="🎽 Labasni Outfit Recommender",
|
| 57 |
description="Recommandations d'outfits basées sur ML (TensorFlow + PyTorch)",
|
| 58 |
examples=[
|
| 59 |
[
|
| 60 |
+
'[{"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}]',
|
| 61 |
"casual",
|
| 62 |
"Tunis"
|
| 63 |
]
|
| 64 |
],
|
| 65 |
+
# ✅ CRUCIAL : Activer l'API externe
|
| 66 |
+
api_name="recommend_outfit" # Nom explicite pour l'endpoint
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
# ✅ Lancer avec accès API activé
|
| 71 |
+
iface.launch(
|
| 72 |
+
server_name="0.0.0.0",
|
| 73 |
+
server_port=7860,
|
| 74 |
+
share=False
|
| 75 |
+
)
|