Spaces:
Paused
Paused
File size: 16,418 Bytes
9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a 5e89af2 9b6249a aa9a482 |
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
"""
Comprehensive Tag System for Personalized Outfit Recommendations
This module defines the expanded tag system that captures user preferences
and influences outfit recommendations with fine-grained control.
"""
from typing import Dict, List, Optional, Set, Any
from enum import Enum
class OccasionTag(str, Enum):
"""Occasion tags matching API Literal types"""
CASUAL = "casual"
BUSINESS = "business"
FORMAL = "formal"
SEMI_FORMAL = "semi_formal"
BUSINESS_CASUAL = "business_casual"
COCKTAIL = "cocktail"
WEDDING = "wedding"
PARTY = "party"
DATE = "date"
SPORT = "sport"
WORKOUT = "workout"
TRAVEL = "travel"
BEACH = "beach"
OUTDOOR = "outdoor"
NIGHT_OUT = "night_out"
BRUNCH = "brunch"
DINNER = "dinner"
MEETING = "meeting"
INTERVIEW = "interview"
CULTURAL = "cultural"
TRADITIONAL = "traditional"
class WeatherTag(str, Enum):
"""Weather tags matching API Literal types"""
ANY = "any"
HOT = "hot"
WARM = "warm"
MILD = "mild"
COOL = "cool"
COLD = "cold"
FREEZING = "freezing"
RAIN = "rain"
SNOW = "snow"
WINDY = "windy"
HUMID = "humid"
SUNNY = "sunny"
CLOUDY = "cloudy"
class StyleTag(str, Enum):
"""Style tags matching API Literal types (outfit_style)"""
CASUAL = "casual"
SMART_CASUAL = "smart_casual"
FORMAL = "formal"
SPORTY = "sporty"
ATHLETIC = "athletic"
STREETWEAR = "streetwear"
MINIMALIST = "minimalist"
CLASSIC = "classic"
MODERN = "modern"
ELEGANT = "elegant"
SOPHISTICATED = "sophisticated"
TRADITIONAL = "traditional"
ETHNIC = "ethnic"
class ColorPreferenceTag(str, Enum):
"""Color preference tags matching API Literal types"""
NEUTRAL = "neutral"
MONOCHROMATIC = "monochromatic"
COMPLEMENTARY = "complementary"
BOLD = "bold"
SUBTLE = "subtle"
BRIGHT = "bright"
MUTED = "muted"
PASTEL = "pastel"
DARK = "dark"
LIGHT = "light"
EARTH_TONES = "earth_tones"
JEWEL_TONES = "jewel_tones"
BLACK_WHITE = "black_white"
NAVY_WHITE = "navy_white"
COLORFUL = "colorful"
MINIMAL_COLOR = "minimal_color"
class FitPreferenceTag(str, Enum):
"""Fit preference tags matching API Literal types"""
FITTED = "fitted"
LOOSE = "loose"
OVERSIZED = "oversized"
RELAXED = "relaxed"
COMFORTABLE = "comfortable"
STRUCTURED = "structured"
FLOWY = "flowy"
TAILORED = "tailored"
ATHLETIC_FIT = "athletic_fit"
REGULAR_FIT = "regular_fit"
class MaterialPreferenceTag(str, Enum):
"""Material preference tags matching API Literal types"""
COTTON = "cotton"
LINEN = "linen"
SILK = "silk"
WOOL = "wool"
CASHMERE = "cashmere"
DENIM = "denim"
LEATHER = "leather"
BREATHABLE = "breathable"
WATERPROOF = "waterproof"
MOISTURE_WICKING = "moisture_wicking"
SUSTAINABLE = "sustainable"
class BudgetTag(str, Enum):
"""Budget preference tags matching API Literal types"""
LUXURY = "luxury"
PREMIUM = "premium"
MID_RANGE = "mid_range"
AFFORDABLE = "affordable"
BUDGET = "budget"
VALUE = "value"
class AgeGroupTag(str, Enum):
"""Age group tags for age-appropriate styling"""
TEEN = "teen"
YOUNG_ADULT = "young_adult"
ADULT = "adult"
MATURE = "mature"
SENIOR = "senior"
ALL_AGES = "all_ages"
class GenderTag(str, Enum):
"""Gender tags for gender-specific recommendations"""
MALE = "male"
FEMALE = "female"
NON_BINARY = "non_binary"
UNISEX = "unisex"
ANY = "any"
class SeasonTag(str, Enum):
"""Season tags for seasonal styling"""
SPRING = "spring"
SUMMER = "summer"
FALL = "fall"
AUTUMN = "autumn"
WINTER = "winter"
YEAR_ROUND = "year_round"
TRANSITIONAL = "transitional"
class TimeOfDayTag(str, Enum):
"""Time of day tags for time-appropriate styling"""
MORNING = "morning"
AFTERNOON = "afternoon"
EVENING = "evening"
NIGHT = "night"
ALL_DAY = "all_day"
class PersonalStyleTag(str, Enum):
"""Personal style preference tags"""
CONSERVATIVE = "conservative"
MODERATE = "moderate"
BOLD = "bold"
EXPERIMENTAL = "experimental"
TRADITIONAL = "traditional"
TRENDY = "trendy"
TIMELESS = "timeless"
FASHION_FORWARD = "fashion_forward"
CLASSIC = "classic"
ECLECTIC = "eclectic"
class TagProcessor:
"""
Processes and prioritizes tags to influence outfit recommendations.
Handles tag conflicts, weights, and generates context-aware preferences.
"""
def __init__(self):
self.tag_weights = self._initialize_tag_weights()
self.tag_conflicts = self._initialize_tag_conflicts()
self.tag_synergies = self._initialize_tag_synergies()
def _initialize_tag_weights(self) -> Dict[str, float]:
"""Initialize default weights for different tag categories"""
return {
"occasion": 1.0, # Highest priority
"weather": 0.9, # Very high priority
"style": 0.8, # High priority
"color_preference": 0.7, # Medium-high priority
"fit_preference": 0.6, # Medium priority
"material_preference": 0.5, # Medium priority
"season": 0.4, # Lower priority
"time_of_day": 0.3, # Lower priority
"budget": 0.2, # Lower priority
"age_group": 0.1, # Lower priority
"gender": 0.1, # Lower priority
"personal_style": 0.5, # Medium priority
}
def _initialize_tag_conflicts(self) -> Dict[str, List[str]]:
"""Define conflicting tags that should not be used together"""
return {
"hot": ["cold", "freezing", "snow"],
"cold": ["hot", "warm"],
"formal": ["casual", "sporty"],
"sporty": ["formal", "elegant"],
"budget": ["luxury"],
"loose": ["fitted", "tight"],
"tight": ["loose", "oversized"],
}
def _initialize_tag_synergies(self) -> Dict[str, List[str]]:
"""Define tag synergies that work well together"""
return {
"formal": ["elegant", "sophisticated", "tailored"],
"casual": ["comfortable", "relaxed", "practical"],
"sporty": ["athletic", "comfortable", "moisture_wicking"],
"hot": ["breathable", "cotton", "linen", "light"],
"cold": ["wool", "cashmere", "warm", "structured"],
"rain": ["waterproof", "practical", "boot"],
"business": ["professional", "tailored", "neutral"],
"date": ["elegant", "romantic", "fitted"],
"party": ["glamorous", "bold", "colorful"],
}
def process_tags(self, tags: Dict[str, Any]) -> Dict[str, Any]:
"""
Process and prioritize tags, resolving conflicts and applying synergies.
Args:
tags: Dictionary of tag categories and their values
Returns:
Processed tags with weights, resolved conflicts, and applied synergies
"""
processed = {
"primary_tags": {},
"secondary_tags": {},
"weights": {},
"preferences": {},
"constraints": {}
}
# Extract and validate tags (support both "style" and "outfit_style" for backward compatibility)
occasion = tags.get("occasion", "casual")
weather = tags.get("weather", "any")
outfit_style = tags.get("outfit_style") or tags.get("style", "casual") # Support both keys
color_preference = tags.get("color_preference", None)
fit_preference = tags.get("fit_preference", None)
material_preference = tags.get("material_preference", None)
season = tags.get("season", None)
time_of_day = tags.get("time_of_day", None)
budget = tags.get("budget", None)
age_group = tags.get("age_group", None)
gender = tags.get("gender", None)
personal_style = tags.get("personal_style", None)
# Primary tags (high influence)
processed["primary_tags"] = {
"occasion": occasion,
"weather": weather,
"outfit_style": outfit_style # Use outfit_style consistently
}
# Secondary tags (medium influence)
secondary = {}
if color_preference:
secondary["color_preference"] = color_preference
if fit_preference:
secondary["fit_preference"] = fit_preference
if material_preference:
secondary["material_preference"] = material_preference
if personal_style:
secondary["personal_style"] = personal_style
processed["secondary_tags"] = secondary
# Tertiary tags (lower influence)
tertiary = {}
if season:
tertiary["season"] = season
if time_of_day:
tertiary["time_of_day"] = time_of_day
if budget:
tertiary["budget"] = budget
if age_group:
tertiary["age_group"] = age_group
if gender:
tertiary["gender"] = gender
processed["tertiary_tags"] = tertiary
# Resolve conflicts
processed["resolved_tags"] = self._resolve_conflicts(
processed["primary_tags"], processed["secondary_tags"]
)
# Apply synergies
processed["synergies"] = self._apply_synergies(
processed["resolved_tags"]
)
# Calculate weights
processed["weights"] = self._calculate_weights(
processed["primary_tags"],
processed["secondary_tags"],
processed["tertiary_tags"]
)
# Generate preferences
processed["preferences"] = self._generate_preferences(
processed["resolved_tags"],
processed["synergies"]
)
# Generate constraints
processed["constraints"] = self._generate_constraints(
processed["resolved_tags"]
)
return processed
def _resolve_conflicts(self, primary: Dict, secondary: Dict) -> Dict:
"""Resolve tag conflicts by prioritizing primary tags"""
resolved = {**primary, **secondary}
for tag, conflicts in self.tag_conflicts.items():
if tag in resolved.values():
for conflict in conflicts:
if conflict in resolved.values():
# Remove conflict from secondary tags, keep primary
for key, value in list(resolved.items()):
if value == conflict and key not in primary:
del resolved[key]
return resolved
def _apply_synergies(self, tags: Dict) -> List[str]:
"""Apply tag synergies to enhance recommendations"""
synergies = []
tag_values = list(tags.values())
for tag, synergy_list in self.tag_synergies.items():
if tag in tag_values:
synergies.extend(synergy_list)
return list(set(synergies)) # Remove duplicates
def _calculate_weights(self, primary: Dict, secondary: Dict, tertiary: Dict) -> Dict:
"""Calculate weights for different tag categories"""
weights = {}
for category in primary:
weights[category] = self.tag_weights.get(category, 1.0)
for category in secondary:
weights[category] = self.tag_weights.get(category, 0.5)
for category in tertiary:
weights[category] = self.tag_weights.get(category, 0.2)
return weights
def _generate_preferences(self, tags: Dict, synergies: List[str]) -> Dict:
"""Generate preference dictionary for recommendation engine"""
preferences = {
"preferred_categories": [],
"color_palette": [],
"material_preferences": [],
"fit_preferences": [],
"style_keywords": []
}
# Map tags to preferences (support both "style" and "outfit_style")
style_value = tags.get("outfit_style") or tags.get("style", "casual")
style_mapping = {
"formal": ["blazer", "jacket", "dress shirt", "dress pant", "oxford"],
"casual": ["tshirt", "jean", "sneaker", "hoodie"],
"sporty": ["athletic shirt", "jogger", "running shoe", "tank"],
"athletic": ["athletic shirt", "jogger", "running shoe", "tank"],
"business": ["shirt", "pants", "loafer", "blazer"],
"smart_casual": ["shirt", "chino", "loafer", "blazer"],
"traditional": ["kameez", "kurta", "shalwar", "peshawari"],
"ethnic": ["kameez", "kurta", "shalwar", "peshawari"],
"elegant": ["blazer", "dress shirt", "dress pant", "oxford"],
"sophisticated": ["blazer", "dress shirt", "dress pant", "oxford"],
"minimalist": ["tshirt", "jean", "sneaker", "hoodie"],
"classic": ["shirt", "pants", "loafer", "blazer"],
"modern": ["tshirt", "jean", "sneaker", "hoodie"],
"streetwear": ["tshirt", "jean", "sneaker", "hoodie"]
}
if style_value in style_mapping:
preferences["preferred_categories"].extend(style_mapping[style_value])
# Add synergies as style keywords
preferences["style_keywords"].extend(synergies)
return preferences
def _generate_constraints(self, tags: Dict) -> Dict:
"""Generate constraints based on tags"""
constraints = {
"min_items": 2,
"max_items": 6,
"accessory_limit": 3,
"requires_outerwear": False,
"requires_formal": False
}
# Occasion-based constraints
if tags.get("occasion") == "formal":
constraints["min_items"] = 4
constraints["max_items"] = 5
constraints["requires_outerwear"] = True
constraints["requires_formal"] = True
constraints["accessory_limit"] = 4
elif tags.get("occasion") == "business":
constraints["min_items"] = 3
constraints["max_items"] = 4
constraints["accessory_limit"] = 3
elif tags.get("occasion") == "sport":
constraints["min_items"] = 2
constraints["max_items"] = 3
constraints["accessory_limit"] = 1
# Weather-based constraints
if tags.get("weather") in ["cold", "freezing"]:
constraints["requires_outerwear"] = True
return constraints
def get_all_tag_options() -> Dict[str, List[str]]:
"""Get all available tag options for UI/API"""
return {
"occasion": [tag.value for tag in OccasionTag],
"weather": [tag.value for tag in WeatherTag],
"outfit_style": [tag.value for tag in StyleTag], # Use outfit_style to match API
"style": [tag.value for tag in StyleTag], # Keep for backward compatibility
"color_preference": [tag.value for tag in ColorPreferenceTag],
"fit_preference": [tag.value for tag in FitPreferenceTag],
"material_preference": [tag.value for tag in MaterialPreferenceTag],
"season": [tag.value for tag in SeasonTag],
"time_of_day": [tag.value for tag in TimeOfDayTag],
"budget": [tag.value for tag in BudgetTag],
"age_group": [tag.value for tag in AgeGroupTag],
"gender": [tag.value for tag in GenderTag],
"personal_style": [tag.value for tag in PersonalStyleTag],
}
def validate_tags(tags: Dict[str, Any]) -> tuple[bool, List[str]]:
"""Validate tags and return (is_valid, errors)"""
errors = []
all_options = get_all_tag_options()
for category, value in tags.items():
if category not in all_options:
errors.append(f"Unknown tag category: {category}")
continue
if value not in all_options[category]:
errors.append(f"Invalid value '{value}' for category '{category}'. Valid options: {all_options[category]}")
return len(errors) == 0, errors
|