""" Logique ML pour les recommandations d'outfits Adapté de votre fichier recommendations.py """ import numpy as np import requests from sklearn.metrics.pairwise import cosine_similarity import json # Configuration météo OPENWEATHER_API_KEY = "a92f907ace22631f8af40374ae0b30b6" def get_weather(city: str): """Récupère la météo depuis OpenWeather API""" try: url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_API_KEY}&units=metric" response = requests.get(url, timeout=5) data = response.json() return { "temperature": data["main"]["temp"], "condition": data["weather"][0]["main"] } except: return {"temperature": 20, "condition": "Clear"} def get_season_from_weather(temp: float): """Détermine la saison selon la température""" if temp > 25: return "summer" elif temp > 17: return "spring" elif temp > 0: return "fall" return "winter" def recommend_outfit_ml(clothes_data: list, preference: str, city: str): """ Recommande un outfit complet basé sur ML Args: clothes_data: Liste des vêtements disponibles preference: Style préféré city: Ville pour la météo Returns: Dict avec l'outfit recommandé et l'explication """ # 1. Obtenir la météo weather = get_weather(city) season = get_season_from_weather(weather["temperature"]) # 2. Filtrer par style et saison pref_lower = preference.lower() def matches_season(item_season, target_season): if not item_season or item_season.lower() in ["all", "toutes"]: return True return item_season.lower() == target_season tops = [c for c in clothes_data if c.get("category") == "top" and c.get("style") == pref_lower and matches_season(c.get("season"), season)] bottoms = [c for c in clothes_data if c.get("category") == "bottom" and c.get("style") == pref_lower and matches_season(c.get("season"), season)] footwear = [c for c in clothes_data if c.get("category") in ["footwear", "shoes"] and c.get("style") == pref_lower and matches_season(c.get("season"), season)] # 3. Vérifier si on a assez de vêtements if not tops or not bottoms or not footwear: return { "success": False, "message": f"Pas assez de vêtements '{preference}' pour la saison '{season}'", "weather": weather, "season": season } # 4. Sélectionner le meilleur outfit (basé sur les scores) top = max(tops, key=lambda x: x.get("score", 0)) bottom = max(bottoms, key=lambda x: x.get("score", 0)) shoe = max(footwear, key=lambda x: x.get("score", 0)) return { "success": True, "outfit": { "top": top["id"], "bottom": bottom["id"], "footwear": shoe["id"] }, "explanation": { "top": { "reason": f"Best rated (Score: {top.get('score', 0):.2f})", "score": top.get("score", 0) }, "bottom": { "reason": f"Best match (Score: {bottom.get('score', 0):.2f})", "score": bottom.get("score", 0) }, "footwear": { "reason": f"Best match (Score: {shoe.get('score', 0):.2f})", "score": shoe.get("score", 0) } }, "weather": weather, "season": season, "preference": preference }