Spaces:
Sleeping
Sleeping
File size: 3,570 Bytes
ee432f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | """
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
}
|