salma-mahjoub commited on
Commit
bd20751
·
1 Parent(s): 987a859

Fix: Update Gradio 6.x API endpoint configuration

Browse files
Files changed (1) hide show
  1. app.py +74 -12
app.py CHANGED
@@ -1,6 +1,7 @@
1
  """
2
  Labasni Recommender Service - Hugging Face Space
3
  Interface Gradio pour les recommandations d'outfits
 
4
  """
5
 
6
  import gradio as gr
@@ -20,20 +21,79 @@ def recommend_outfit_api(clothes_json: str, preference: str, city: str = "Tunis"
20
  JSON avec l'outfit recommandé
21
  """
22
  try:
23
- # Parser le JSON
24
  clothes_data = json.loads(clothes_json)
25
-
26
- # Appeler le modèle ML
27
  result = recommend_outfit_ml(clothes_data, preference, city)
28
-
29
- # Retourner le résultat en JSON
30
  return json.dumps(result, indent=2)
31
  except Exception as e:
32
- # Retourner l'erreur en JSON
33
  return json.dumps({"success": False, "error": str(e)})
34
 
35
- # Interface Gradio avec api_name explicite
36
- # La variable DOIT s'appeler 'demo' pour Hugging Face Spaces
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  demo = gr.Interface(
38
  fn=recommend_outfit_api,
39
  inputs=[
@@ -49,7 +109,7 @@ demo = gr.Interface(
49
  ),
50
  gr.Textbox(label="City", value="Tunis")
51
  ],
52
- outputs=gr.Textbox(label="Recommended Outfit (JSON)", lines=15), # Removed show_copy_button
53
  title="🎽 Labasni Outfit Recommender",
54
  description="Recommandations d'outfits basées sur ML (TensorFlow + PyTorch)",
55
  examples=[
@@ -59,8 +119,10 @@ demo = gr.Interface(
59
  "Tunis"
60
  ]
61
  ],
62
- api_name="predict" # CRUCIAL pour exposer l'endpoint /api/predict
 
 
63
  )
64
 
65
- # Pour Hugging Face Spaces, l'interface est automatiquement détectée et lancée
66
- # Pas besoin d'appeler demo.launch()
 
1
  """
2
  Labasni Recommender Service - Hugging Face Space
3
  Interface Gradio pour les recommandations d'outfits
4
+ Version simplifiée avec gr.Interface
5
  """
6
 
7
  import gradio as gr
 
21
  JSON avec l'outfit recommandé
22
  """
23
  try:
 
24
  clothes_data = json.loads(clothes_json)
 
 
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({"success": False, "error": str(e)})
29
 
30
+ # Interface Gradio - gr.Interface avec api_name explicite pour Gradio 6.x
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=[
 
109
  ),
110
  gr.Textbox(label="City", value="Tunis")
111
  ],
112
+ outputs=gr.Textbox(label="Recommended Outfit (JSON)", lines=15),
113
  title="🎽 Labasni Outfit Recommender",
114
  description="Recommandations d'outfits basées sur ML (TensorFlow + PyTorch)",
115
  examples=[
 
119
  "Tunis"
120
  ]
121
  ],
122
+ # CRUCIAL : api_name est nécessaire pour Gradio 6.x
123
+ # Cela expose l'endpoint /api/predict
124
+ api_name="predict"
125
  )
126
 
127
+ # Pour Hugging Face Spaces, l'interface est automatiquement détectée et lancée
128
+ # La variable 'demo' est la convention standard pour Hugging Face Spaces