""" 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 )