Spaces:
Running
A newer version of the Gradio SDK is available: 6.14.0
StyleWell Backend Implementation README
This document explains how the StyleWell backend works from the frontend request all the way down to classification, outfit scoring, product scraping, caching, and fallbacks. It is intentionally implementation-focused: it names the actual files, functions, payloads, and decision paths used in this codebase.
1. System Shape
StyleWell is split into three related layers:
- React frontend in
src/. - FastAPI backend in
StyleWellBackend/. - Supabase database, storage, auth, migrations, and older Edge Function logic in
supabase/.
The most important thing to understand is that the frontend does not use only one backend surface. Wardrobe persistence is usually handled through Supabase from the frontend, while AI-heavy work is sent to the FastAPI backend.
Current practical flow:
- Authentication: frontend uses Supabase Auth through
src/lib/supabase.ts. - Wardrobe images and garment rows: frontend stores images in Supabase Storage and metadata in the
garment_itemstable when Supabase env vars are configured. - Classification: frontend calls FastAPI
/classifybefore saving the classified garment to Supabase. - Outfit matching: frontend reads wardrobe items from Supabase, normalizes them, then sends them to FastAPI
/ai/recommend-outfits. - Shopping suggestions: frontend sends the natural-language shopping request to FastAPI
/scraper/recommend. - Backend-local SQLite: FastAPI also has its own SQLite store for local uploads, backend-side item CRUD, feedback, and persistent matching cache.
This means StyleWell can run in a hybrid mode: Supabase owns user-facing data, and FastAPI owns AI inference, scoring, scraper planning, scraping, and fallback intelligence.
2. Backend Directory Map
StyleWellBackend/app.py
Main FastAPI application. It defines all production routes, CORS, startup lifecycle, NVIDIA model inference helpers, classification, outfit grid scoring, fallback scoring orchestration, scraper planning, scraper UI, image proxying, and error translation.
StyleWellBackend/db.py
SQLite persistence layer. It creates items, outfit_feedback, and search_cache, exposes CRUD helpers, and stores cache entries used by matching.
StyleWellBackend/scoring.py
Deterministic rule-based outfit scoring engine. Used whenever AI grid scoring or multimodal ranking is unavailable, and also used inside scraper product-to-wardrobe match explanations.
StyleWellBackend/scraper.py
Nike scraper and URL builder. It can be run as a separate FastAPI scraper API, but the main backend imports its Nike URL and product extraction functions.
StyleWellBackend/zalando_scraper.py
Zalando URL builder and scraper. It can use Apify first and HTML scraping as fallback. It also has query enrichment helpers for underspecified Zalando searches.
StyleWellBackend/fashion_ai/
Local multimodal recommendation subsystem:
encoder.py: encodes garment text and images into vectors.retriever.py: splits wardrobe by slot and retrieves candidate garments.ranker.py: scores candidate outfits with a trained transformer if available, otherwise zero-shot vector scoring.classifier.py: model-backed or metadata-backed item classifier and item matcher.service.py: orchestrates encoder, retriever, ranker, and classifier.schemas.py: dataclasses for encoded items, context, weather, and candidates.
3. Frontend to Backend Connection
Base URL Resolution
Frontend API calls go through src/api/http.ts:
requestJson()resolves a path into a full URL.resolveRequestUrl()callsgetFlaskApiUrl()fromsrc/lib/supabase.ts.getFlaskApiUrl()checks:VITE_FLASK_API_URLorVITE_API_BASE_URL.- Supabase
app_configrow wherekey = 'flask_api_url'. - Local fallback
http://127.0.0.1:5000.
The base URL is normalized by src/config/api.ts:
normalizeBaseUrl()trims trailing slashes.- It fixes a common Hugging Face Space typo by converting
<owner>-<space>.spaceinto<owner>-<space>.hf.space. buildApiUrl(path, baseUrl)joins the backend base URL with endpoint paths.
API Mode
src/config/api.ts defines API_MODE:
- Explicit
VITE_API_MODEwins. - Otherwise frontend uses
liveif Supabase or API base env vars exist. - Otherwise it uses
mock.
Several frontend functions branch on isLiveApiMode():
- In live mode, matching and scraper calls go to FastAPI.
- In mock mode, frontend returns rule-based or provider-search fallback suggestions.
Route Fallbacks
The frontend tries multiple route shapes for compatibility with different deployments:
Matching in src/api/matching.ts tries:
/ai/recommend-outfits/api/ai/recommend-outfits/recommend-outfits/api/recommend-outfits
Suggestions in src/api/client.ts tries:
/suggestions/api/suggestions
Scraper recommendation in src/api/client.ts tries:
/scraper/recommend/api/scraper/recommend
It only advances to the next route when the error looks like a 404. Network failures and server errors are surfaced instead of silently trying unrelated routes.
4. Wardrobe Upload and Classification Flow
The wardrobe upload UI lives in src/components/UploadModal.tsx.
Flow:
- User picks an image.
classifyImage(file)insrc/api/client.tssends a multipartPOST /classifyrequest to FastAPI.- FastAPI
classify()inapp.pyreads the file into a PIL image. - The image is converted to RGB and thumbnailed to
512x512. run_nvidia_inference(pil_image, CLASSIFICATION_PROMPT)sends the image and classification prompt to NVIDIA chat completions.parse_json_from_text()extracts JSON from the model output.normalize_specs()guarantees the response has:typecategorycolorpatternfabricfitoccasionseason
- The frontend lets the user edit the classification.
uploadClothingItem(file, classificationOverride)saves the final garment.
In the current frontend, when Supabase env vars exist, saving goes through saveGarmentWithClassification() in src/lib/supabase.ts:
- Requires an authenticated Supabase user.
- Builds a storage path like
{user.id}/{timestamp}_{safe_filename}.{ext}. - Uploads to the first working bucket among:
VITE_SUPABASE_STORAGE_BUCKETclothing-imagesgarments
- Inserts a row into
garment_items. - Tries insert with
user_id; if schema does not supportuser_id, it falls back to inserting without it. - Resolves a signed URL for the stored image.
If Supabase is not available, uploadClothingItem() can fall back to FastAPI /upload in live mode or a local object URL in mock mode.
FastAPI /upload does its own classification and then stores a normalized item in SQLite. It uses memory://{item_id} as the image URL because the FastAPI backend does not persist the uploaded binary image itself.
5. FastAPI Classification Internals
Classification settings in app.py:
NVIDIA_INVOKE_URL, defaulthttps://integrate.api.nvidia.com/v1/chat/completionsNVIDIA_MODEL_ID, defaultqwen/qwen3.5-122b-a10bNVIDIA_FALLBACK_MODEL_IDS, comma-separated fallback model IDs.NVIDIA_MAX_TOKENSNVIDIA_REASONING_MAX_TOKENSNVIDIA_TEMPERATURENVIDIA_TOP_PNVIDIA_TIMEOUT_SECONDSNVIDIA_MAX_RETRIESNVIDIA_RETRY_BACKOFF_SECONDSNVIDIA_ENABLE_THINKINGNVIDIA_IMAGE_MAX_DIM
run_nvidia_inference() is used for image+text inference. It:
- Checks
NVIDIA_API_KEY. - Tries
NVIDIA_MODEL_ID, then every configured fallback model fromNVIDIA_FALLBACK_MODEL_IDS. - Sends an SSE streaming chat completion request.
- Extracts streamed text with
_extract_streamed_nvidia_text(). - Retries transient status codes
429,500,502,503, and504. - Applies retry backoff.
- If the response hits a token limit, it can retry with a larger token budget up to
NVIDIA_REASONING_MAX_TOKENS. - If a provider says a model is degraded, it tries the next model.
The model response parser is defensive:
_extract_text_from_nvidia_content()accepts string, list, or dict content shapes._extract_nvidia_text()can readcontent,reasoning_content, orreasoning.parse_json_from_text()first tries full JSON, then extracts the first{...}block.normalize_specs()fills missing fields with"Unknown".
If every model/provider path fails, _raise_http_error() converts errors into HTTP responses:
- Missing NVIDIA key:
503. - NVIDIA gateway error: configured status, usually
502or503. - Bad model payload:
502. - Unknown server problem:
500.
Frontend error messages are normalized by toUserFacingApiMessage() in src/api/http.ts.
6. Wardrobe Item Normalization
Backend scoring expects a compact normalized shape. _normalize_wardrobe_item() in app.py accepts raw records from SQLite, Supabase, or frontend payloads and returns:
idimage_urltypecategorycolorpatternfabricfitseasonstyleoccasiondescription
Important logic:
- If
categoryis missing, it reads fromdescription.categoryordescription.type. - If
typeis missing, it calls_infer_type(category). _infer_type()maps shirt, tee, top, kurta, blouse, hoodie, sweater, blazer, jacket, polo totopwear.- It maps jean, pant, trouser, short, skirt, jogger, palazzo, chino to
bottomwear. - Everything else becomes
others. stylecomes fromitem.style,description.occasion,description.style, or falls back tocasual.
Frontend has a parallel slot-normalization helper in src/lib/wardrobeSlots.ts, and API normalizers call it so the UI can group items into topwear, bottomwear, and others.
7. SQLite Storage
db.py chooses the database path with _resolve_db_path():
DB_PATHif provided./data/wardrobe.dbif running on Hugging Face Spaces with persistent storage../wardrobe.dbfor local development.
_conn() opens SQLite with:
check_same_thread=Falserow_factory=sqlite3.Row- WAL journal mode
- foreign keys enabled
- automatic commit/rollback
init_db() creates:
items
Stores backend-local wardrobe items with scalar metadata and JSON description.
Columns:
idimage_urlcategorycolorpatternfabricfitseasonstyletypedescriptioncreated_at
outfit_feedback
Stores feedback for a top/bottom pair.
Columns:
idtop_idbottom_idoccasionaction, constrained towear,skip, orsavescorecreated_at
search_cache
Generic JSON cache table.
Columns:
cache_keypayloadcreated_atexpires_at
cache_get() deletes expired rows on read. cache_set() upserts payloads with TTL.
8. Outfit Matching: Frontend Flow
The matching page is src/pages/Matching.tsx.
User controls:
- Select occasion.
- Optionally lock a top.
- Optionally lock a bottom.
- Optionally lock an
othersitem. - Click
Find Outfits.
State lives in src/store/outfitStore.ts.
When the page loads:
fetchWardrobe()callsgetWardrobeItems().- Items are normalized into frontend
ClothingItemobjects. - Slots are inferred with
inferWardrobeSlot().
When the user finds outfits:
findOutfits()computes lock state.- It resolves a matching cache category:
- locked other ->
others - locked bottom only ->
bottomwear - locked top ->
topwear - default ->
topwear
- locked other ->
- It builds a lock signature like
top:{id|-}|bottom:{id|-}|other:{id|-}. - It gets the full wardrobe context.
- It computes a wardrobe hash:
- global hash when locked selections exist.
- category hash for prefetch/default category caching.
- It checks local cache in
outfitMatchingCache.v1inlocalStorage. - If no cache hit, it calls
fetchWardrobeOutfits()insrc/api/matching.ts.
fetchWardrobeOutfits() sends this payload to FastAPI:
{
"occasion": "casual",
"top_k": 5,
"top_selected": null,
"bottom_selected": null,
"other_selected": null,
"wardrobe_items": [],
"user_id": "optional",
"cache_category": "topwear",
"wardrobe_hash": "optional",
"lock_signature": "optional"
}
The backend response is normalized by normalizeMatchingResponse() before it is rendered in OutfitCard.
9. Outfit Matching: Backend Route
The main route is POST /ai/recommend-outfits in app.py.
It accepts:
occasionwardrobe_itemstop_selectedbottom_selectedother_selectedweatheruser_profileregiontop_kcandidate_pooldiversity_lambdacache_categorywardrobe_hashlock_signatureuser_id
Validation and normalization:
wardrobe_itemsmust be a list.- If
wardrobe_itemsis empty, backend reads from SQLiteitem_get_all(). - Every item is passed through
_normalize_wardrobe_item(). top_kis clamped to1..20.candidate_poolis clamped to4..64.diversity_lambdais clamped to0..0.95.
Case labels:
- Case
A: no top or bottom locked. - Case
B: top locked. - Case
C: bottom locked. - Case
D: top and bottom locked. - Case
E: standaloneothersitem.
Special handling:
- If a locked
other_selecteditem is anothersitem, the backend returns standalone recommendations using fallback scoring, becauseothersmay be a dress, kurta, saree, jumpsuit, or another single-piece outfit. - If there are no tops or no bottoms but there are
others, it returns standaloneothersoutfits. - If there are no usable items, it returns an empty response with a notice.
10. Matching Cache
There are two caches:
Frontend cache
src/store/cacheUtils.ts stores matching responses in localStorage under outfitMatchingCache.v1.
Cache key:
{category}|{occasion}|{wardrobeHash}|{lockSignature}
buildWardrobeHash() hashes only items in a category. buildGlobalWardrobeHash() hashes all wardrobe items. The hash includes item type, id, and updated/created timestamp, so changes invalidate cached outfits.
Backend cache
app.py stores matching responses in:
- In-memory
MATCHING_RESULT_CACHE. - SQLite
search_cacheunder keys prefixed bymatching:.
Backend matching cache key:
{user_id}|{cache_category}|{occasion}|{wardrobe_hash}|{lock_signature}
The backend cache is used only when both cache_category and wardrobe_hash are supplied. TTL is controlled by MATCHING_RESULT_CACHE_TTL_SECONDS, default 86400.
11. AI Grid Outfit Scoring
The preferred backend scoring path is _recommend_outfits_with_ai_grid().
It works like this:
_resolve_outfit_grid_sources()splits wardrobe into top, bottom, and others pools.- If a top is locked, only that top is used and bottoms are ranked against it.
- If a bottom is locked, only that bottom is used and tops are ranked against it.
- If both are locked, exactly that pair is evaluated.
- If
other_selectedis provided, it is included as a locked Row 3 candidate. - The candidate pools may be reduced before image grid scoring:
OUTFIT_GRID_MAX_TOP_ITEMS, default4.OUTFIT_GRID_MAX_BOTTOM_ITEMS, default4.
- If
OUTFIT_TEXT_PRESELECT_ENABLED=true,_select_grid_candidates_with_text_ai()asks NVIDIA text inference to pick the strongest candidates before grid scoring. _build_outfit_grid_session()creates a composite image:- Row 1: topwear.
- Row 2: bottomwear.
- Row 3: optional others.
- Every cell gets a coordinate like
1:1,2:3,3:1.
- The composite image is saved to
OUTFIT_GRID_SESSION_DIR. _grid_scoring_prompt()builds a prompt containing:- occasion
- weather
- user profile
- region
- anchor mode
- locked cell indices
- metadata map
- combination count
- requested
top_k
run_nvidia_inference()sends the grid image plus prompt to the vision model.parse_json_from_text()extracts JSON._normalize_ai_outfit_payload()maps returned grid coordinates back to actual wardrobe items.
The model must return recommendations with:
top_indexbottom_indexother_indexscorebreakdownreasontip
The backend then returns:
recommendationsfor normal cases.selected_outfit_scoreplusimproved_recommendationsfor CaseD.engine_version: ai-grid-v1.
If the AI grid path fails, the backend does not fail the user. It falls back.
12. Multimodal Fallback Recommendation Service
If AI grid scoring fails, _current_fallback_recommendations() first tries fashion_ai:
get_recommendation_service().recommend(...)
That service is created once as a singleton in fashion_ai/service.py.
Encoder
FashionItemEncoder in encoder.py builds garment embeddings.
Item prompt example:
Fashion product photo of a navy solid cotton shirt, regular fit, casual style, suitable for all-season, worn as top.
It encodes both text and image when possible:
- Text is encoded with the configured model.
- Image URL is fetched and encoded if it is HTTP(S) or a local file.
- Text and image vectors are averaged.
Backends:
open_clipwhen model id starts withmarqo/and OpenCLIP is installed.- Hugging Face Transformers model, default
patrickjohncyh/fashion-clip. - Deterministic hash embedding fallback if model loading fails.
The fallback hash embedding means the system stays usable even without model weights, but the quality is lower.
Retriever
OutfitCandidateRetriever in retriever.py:
- Encodes every wardrobe item.
- Splits items into slots:
topbottomshoesaccessoryunknown
- Uses context vector similarity to rank each slot.
- Uses MMR diversification to avoid near-duplicate candidates.
- Honors locked top, bottom, and other items.
Ranker
NeuralOutfitScorer in ranker.py:
- Loads a trained transformer checkpoint from
FASHION_RANKER_CHECKPOINTif available. - If no checkpoint exists, uses zero-shot geometric scoring.
Transformer scoring input shape:
[CONTEXT, USER, TOP, BOTTOM, SHOES, ACCESSORY]
Zero-shot scoring blends:
- context alignment
- user alignment
- pairwise cohesion
- slot coverage
It returns:
scorebreakdownreasontip
Service Cases
MultimodalOutfitRecommendationService.recommend() uses cases:
A: no locked top/bottom.B: locked top.C: locked bottom.D: locked top and bottom.
For Case D, it scores the selected outfit separately and returns improvement suggestions in improved_recommendations.
13. Deterministic Rule-Based Scoring
If AI grid and fashion_ai both fail, the backend uses scoring.py.
The main entry points are:
compute_score(top, bottom, occasion, other=None)score_pair_full(top, bottom, occasion, other=None)recommend_outfits(tops, bottoms, occasion, others, locked_top, locked_bottom, locked_other)
Runtime output details:
score_pair_full()returnsengine_version: scoring-v2.recommend_outfits()returns up toTOP_K = 6combinations.
Weights
WEIGHTS:
- color:
0.30 - style:
0.25 - occasion:
0.20 - fit:
0.13 - pattern:
0.12
Color Score
_color_score() extracts base colors and scores:
- Known complementary pairs: high score, usually
90. - Neutral with neutral:
82unless same neutral, then50. - Neutral with non-neutral:
80. - Analogous colors:
60. - Same non-neutral color:
45. - Unknown/missing:
60.
Complementary examples:
- blue + beige
- black + white
- navy + khaki
- olive + tan
- burgundy + grey
- mustard + navy
Style Score
_style_score() maps style or occasion to:
- casual
- formal
- streetwear
- party
- sports
Then _STYLE_MATRIX scores pair compatibility. Examples:
- formal + formal:
90 - casual + casual:
85 - streetwear + streetwear:
88 - sports + formal:
28 - formal + streetwear:
48
Occasion Score
_occasion_score() maps styles to valid occasions:
- casual: casual, everyday, weekend, college, brunch
- formal: formal, work, interview, business, office, wedding, meeting
- party: party, festive, ethnic, diwali, celebration, date
- sports: sports, gym, active, outdoor, trekking
- streetwear: casual, streetwear, everyday, college
Formal occasions are stricter:
- Both pieces fit:
90. - One piece fits:
60for formal,70otherwise. - Neither fits:
25for formal,35otherwise.
Fit Score
_fit_score() uses _FIT_MATRIX.
Examples:
- oversized top + slim bottom:
92. - regular + regular:
80. - oversized + oversized:
55. - slim + regular:
82.
Pattern Score
_pattern_score():
- Both patterned:
55. - One patterned, one solid:
88. - Both solid/plain:
75.
Season and Fabric Penalties
_season_penalty() subtracts points:
- Heavy fabrics in summer:
18. - Very light fabrics in winter:
12.
Heavy fabrics:
- wool
- leather
- velvet
- tweed
- corduroy
- fleece
Light fabrics:
- linen
- cotton
- silk
- chiffon
- georgette
Veto Caps
After weighted scoring, fatal flaws cap the final score:
- color score
<= 50: cap final score at68. - style score
<= 48: cap final score at58. - occasion score
<= 40: cap final score at52. - both patterned plus weak color: cap final score at
72.
Other Items
When an other item is included, the backend blends top-bottom scores with top-other and bottom-other scores:
- primary top-bottom breakdown contributes
65%. - extra pair average contributes
35%.
This lets accessories, footwear, or outerwear influence the outfit without overpowering the main top/bottom pair.
Explanations
build_reason() creates user-facing text from the strongest and weakest scoring dimensions. build_tip() gives the styling advice shown in the UI.
Diversity Penalty
recommend_outfits() applies _apply_diversity_penalty() before final ranking:
- Near-duplicate looks (same top color, bottom color, and other-item color) are penalized.
- Penalty is
-10per prior similar outfit. - Outfits are re-sorted after penalty and then truncated to top
TOP_K.
14. Standalone Others
The code treats others specially because many garments are complete outfits by themselves:
- dresses
- kurtas
- sarees
- lehengas
- jumpsuits
- rompers
- gowns
- co-ord sets
In fashion_ai/encoder.py, standalone outfit keywords become slot unknown.
In app.py:
_fallback_rule_recommendations()can scoreothersby pairing the item with itself._occasion_prefers_standalone_others()boosts standaloneothersfor wedding, festive, ethnic, ceremony, engagement, reception, sangeet, haldi, and mehndi occasions._merge_standalone_others_for_priority_occasions()merges boosted standalone outfits into normal recommendations for those occasions.
This prevents ethnic or single-piece outfits from being ignored just because they are not topwear or bottomwear.
15. Score Outfit Endpoint
POST /ai/score-outfit scores one outfit.
Required:
topbottom
Optional:
otheroccasionweatheruser_profileregion
Scoring path:
- Try AI grid scoring with only the supplied items.
- If that fails, try
fashion_aiservicescore_outfit(). - If that fails, use
score_pair_full().
Response fields:
scorecolor_scorestyle_scoreoccasion_scorefit_scorepattern_scoreseason_scorereasontipengine_version
16. Gap Analysis
POST /ai/gap-analysis uses _gap_suggestions().
It is deterministic, not model-generated:
- If no topwear exists, suggest adding topwear.
- If no bottomwear exists, suggest adding bottomwear.
- If one category is much larger than the other by more than 2 items, suggest balancing the wardrobe.
- Otherwise suggest adding one occasion-specific versatile piece.
17. Feedback
POST /feedback records preference signals into SQLite.
Required:
top_idbottom_idaction
action must be:
wearskipsave
Optional:
occasionscore
The backend validates that both item IDs exist before writing feedback.
18. Shopping Suggestions: Frontend Flow
The shopping UI is src/pages/Suggestions.tsx.
User provides:
- natural-language request
- store selection: default, Nike, or Zalando
- gender: men, women, unisex
handleGenerate() calls getGemmaScraperRecommendations() in src/api/client.ts. The function name says Gemma, but the current backend planner uses the configured NVIDIA model through NVIDIA-compatible inference.
Before the scraper call, frontend:
- Reads wardrobe count with
getWardrobeItems(). - Shows how many total, topwear, bottomwear, and others items are loaded.
- Checks backend
/healthand verifiesnvidia_api_configuredis truthy in live mode.
Then it posts:
{
"user_prompt": "Need a formal office shirt...",
"occasion": "auto",
"gender": "men",
"target_category": "both",
"filters": {},
"preferences": "",
"store": "nike"
}
The response contains:
query_plansearch_urlsproduct_urlsproductsintermediate_stepsplan_sourceplan_errorscrape_errorsaved_json_path
The UI renders products directly from products.
19. Scraper Recommendation Route
The main route is POST /scraper/recommend.
It performs these steps:
- Read
user_prompt. - Infer structured intent from the prompt with
_infer_structured_request_from_prompt(). - Merge explicit payload fields with inferred fields.
- Merge inferred colors, include keywords, and exclude keywords into filters.
- Build a scraper query cache key.
- Return cached result when available.
- Otherwise call the scraper planner function in
app.py. - Store the result in in-memory scraper cache.
The scraper cache key is:
md5(user_prompt.lower().strip()|store|gender|target_category)
TTL is SCRAPER_QUERY_CACHE_TTL_SECONDS, default 15 days.
20. Prompt Intent Inference
_infer_structured_request_from_prompt() reads the user prompt and extracts:
target_categoryoccasiongenderpreferred_colorsinclude_keywordsexclude_keywords
Target category hints:
- topwear tokens: top, shirt, blazer, jacket, polo, tee, t-shirt, kurta, upper
- bottomwear tokens: bottom, trouser, trousers, pants, jeans, shorts, joggers, lower
Occasion buckets:
- formal: formal, interview, office, work, business, meeting, wedding
- party: party, festive, diwali, celebration, date, ethnic
- sports: sports, gym, workout, training, running, active
- casual: casual, daily, everyday, weekend, outing
Color terms include:
- black
- white
- navy
- blue
- grey
- beige
- olive
- green
- brown
- khaki
- cream
- maroon
- charcoal
- tan
Include keywords currently recognized:
- formal
- structured
- minimal
- smart
- elegant
- tailored
Exclude keywords are recognized only when phrased like avoid hoodie, no hoodie, or without hoodie.
21. NVIDIA Model Shopping Planner
The scraper planner function in app.py is the heart of shopping suggestions. The implementation currently has a legacy function name, but the runtime behavior is NVIDIA-model-driven.
It builds:
- wardrobe snapshot from SQLite via
_wardrobe_metadata_snapshot() - requested target category
- safe filters
- planning context from
_build_scraper_planning_context() - prompt from
_build_scraper_plan_prompt()
Wardrobe Snapshot
_wardrobe_metadata_snapshot() reads backend SQLite items and returns:
total_items- item metadata list
- counts by
{type}|{occasion}
Important limitation: if your frontend wardrobe is stored only in Supabase and not mirrored into FastAPI SQLite, the shopping planner's backend SQLite wardrobe snapshot may be empty or incomplete. The frontend currently sends wardrobe data for /suggestions, but /scraper/recommend builds its snapshot from backend SQLite.
Planning Context
_build_scraper_planning_context() computes:
- requested target category
- resolved target category
- occasion bucket
- gender preference
- allowed categories
- color shortlist
- color resonance scores
- style direction
- reference slot
- reference item IDs
- dominant categories/colors by slot
If user asks for both, _resolve_target_category() chooses the category that is underrepresented:
- If top count is less than or equal to bottom count, recommend topwear.
- Otherwise recommend bottomwear.
Allowed categories come from SCRAPER_CATEGORY_POLICY.
For topwear:
- formal: shirt, polo, jacket
- party: shirt, jacket, polo
- sports: jersey, t-shirt, hoodie
- casual: shirt, t-shirt, polo, jacket, hoodie
For bottomwear:
- formal: trousers, pants
- party: trousers, pants, jeans
- sports: joggers, shorts, tights, leggings
- casual: jeans, pants, shorts, joggers, trousers
Formal disallowed terms are blocked:
- hoodie
- sweatshirt
- joggers
- shorts
- tank top
- tights
- leggings
Color Resonance
_rank_color_resonance() scores candidate colors using:
- colors from the reference slot
- colors across topwear and bottomwear
- user preferred colors
- occasion boost
The reference slot is the opposite of the target:
- If recommending topwear, reference bottomwear colors.
- If recommending bottomwear, reference topwear colors.
Score formula:
(reference_count * 3) + global_count + preferred_bonus + occasion_bonus
Preferred bonus is 2. Occasion bonus is 1 for formal-friendly colors or sports-friendly colors.
The planner uses the top ranked colors as color_shortlist.
Prompt Contract
The NVIDIA model planner prompt requires strict JSON:
{
"target_category": "topwear|bottomwear",
"color": "string from color_shortlist",
"category": "string from allowed_categories post-vetting",
"gender": "men|women|unisex",
"style_direction": "formal-smart|business-casual|casual-polished|etc",
"reference_item_ids": [],
"query": "commerce-ready search string",
"wardrobe_grounding": "specific evidence from wardrobe_snapshot",
"reason": "concise strategic justification"
}
_recover_scraper_plan_from_text() makes the planner robust:
- It first tries normal JSON parsing.
- If parsing fails, it scans model text for allowed categories, colors, gender, and a quoted
query. - If enough fields can be recovered, it builds a valid plan.
If the NVIDIA model planner fails and strict planner mode is disabled, _fallback_scraper_plan() builds a deterministic plan using:
- first allowed category
- first color shortlist entry, or black
- normalized gender
- style direction from planning context
The plan source becomes fallback.
22. Search URL Formation
After a query plan is normalized, the scraper planner creates a ScraperRecommendation:
ScraperRecommendation(
color=color,
category=category,
gender=plan_gender,
)
Then it calls _build_store_search_urls_from_query().
Nike URL Formation
Nike functions come from scraper.py.
build_search_urls_from_query(query, store='nike', gender=None):
- If gender is provided, prefixes the query with gender if it is not already present.
- Returns one Nike URL:
https://www.nike.com/w?q={encoded_query}&vst={encoded_query}
- If gender is not provided, returns three URLs:
- men + query
- women + query
- unmodified query
build_nike_search_url(color, category, gender) uses:
CATEGORY_ALIASESto normalize category words.- query parts: gender plural, color, category.
urlencode({"q": query, "vst": query}).
Example:
https://www.nike.com/w?q=mens+navy+shirt&vst=mens+navy+shirt
Zalando URL Formation
Zalando functions come from zalando_scraper.py.
build_zalando_search_url(query, gender):
- Normalizes gender to
men,women, orunisex. - Picks a path with
_pick_category_path(). - URL-encodes
q. - Returns:
https://www.zalando.co.uk/{path}?q={encoded_query}
Path examples:
- men clothing:
mens-clothing - women clothing:
womens-clothing - unisex clothing:
clothing - women dresses:
womens-clothing-dresses - men shoes:
mens-shoes
build_zalando_search_urls_from_request() can enrich underspecified queries, compose a final search query, and return URLs plus enrichment metadata.
In the main backend app, the completion function is passed as None for this enrichment step because the NVIDIA model planner already produced the query.
23. Product Scraping
After URLs are generated, the scraper planner loops through each URL and calls _extract_store_product_summaries().
Nike Scraping
extract_product_summaries() in scraper.py:
- Downloads the page using
requestswith a desktop browser user agent. - Parses with BeautifulSoup and
lxml. - Looks for
div.product-card__body. - Inside each card, finds
a.product-card__link-overlay. - Extracts:
- product link
- title
- price
- image URL
- Deduplicates by link.
If Nike markup changes and no summaries are found:
- It falls back to
extract_product_urls(). - That scans anchors with product URL patterns like
/t/. - Products get
N/Afor title/price and empty image.
Zalando Scraping
extract_product_summaries() in zalando_scraper.py:
- Validates search URL.
- Uses Apify if
use_apify=TrueandAPIFY_API_TOKENis configured. - If Apify returns no products or errors, falls back to HTML scraping.
- Optionally runs postprocess if products are missing important fields.
- If both Apify and HTML fail, raises
requests.RequestException.
Apify path:
_scrape_with_apify()calls the actor endpoint.- It tries two payload variants:
startUrlsas string arraystartUrlsas object array
- It caps result count to
APIFY_MAX_RESULTS, default20. - If sync dataset call fails or returns empty, it tries
_scrape_with_apify_run_dataset_fallback().
Apify run-dataset fallback:
- Starts actor run through
/acts/{actor_id}/runs. - Waits for finish using
waitForFinish. - Reads the actor default dataset.
- Normalizes the dataset items.
HTML fallback:
- Requests the Zalando search page.
- Selects product-like cards using
article,div[data-testid*="product"], andli[data-testid*="product"]. - Finds product links with
/p/. - Extracts name, price, image, and link.
Zalando normalization handles different Apify payload shapes:
name,title,productName,product_nameprice,currentPrice,displayPrice,priceLabelpromotionalPrice,originalPrice,discountPercentbrand,brandNameimage,imageUrl,image_url,thumbnailurl,productUrl,item_link,link
24. Product Relevance Filtering
After scraping, the scraper planner filters products with _is_relevant_scraped_product().
It rejects products when:
- Product text is empty.
- Product text contains excluded categories such as socks, underwear, swimwear, belts, hats, wallets, bags, watches, shoes, sneakers, boots, sandals, or slippers.
- Product text does not contain planned category keywords.
- Target is topwear but no topwear terms appear.
- Target is bottomwear but no bottomwear terms appear.
- Occasion is formal and product text contains blocked casual/sport terms.
If strict filtering removes everything but there are unfiltered products available, backend uses scrape_fallback:
- It returns the first unfiltered products up to the scrape limit.
- It records an intermediate step explaining that strict relevance filtering returned no products.
25. Product to Wardrobe Matching
Each accepted product is enriched by _enrich_scraper_products_with_matches().
For each product, _build_product_match_context():
- Determines product slot:
- target topwear -> product is topwear.
- target bottomwear -> product is bottomwear.
- Determines the complementary wardrobe slot:
- topwear product matches existing bottomwear.
- bottomwear product matches existing topwear.
- Builds a product stub using:
- planned category
- planned color
- planned style direction
- occasion bucket
- Scores that stub against each complementary wardrobe item using
score_pair_full(). - Sorts matches by score.
- Adds top 3 matched garments.
- Creates a reason that explicitly says it only evaluates against the complementary slot.
Returned product enrichment:
reasonmatch_scorematched_with_slotmatched_garments
This prevents bad explanations like matching a recommended shirt against existing shirts.
26. Suggestions Endpoint
POST /suggestions and POST /api/suggestions wrap the scraper recommendation flow for the older suggestions UI/API shape.
Input:
occasiontarget_categorygender_preferencefiltersmax_resultsstore
It calls _build_shopping_suggestions_from_scraper():
- Converts filters into a
preferencesstring. - Calls the scraper planner function.
- Maps products into
suggestions. - Assigns a basic match score:
max(65, 95 - index * 4). - Includes product URL, image, reason, category, color, query, scrape status, gender, and matched garments.
The frontend getSuggestions() has additional fallback behavior:
- It times out after
VITE_SCRAPER_TIMEOUT_MS, default20000. - It throttles requests by
VITE_SCRAPER_MIN_INTERVAL_MS, default1200. - It logs scrape events.
- It builds fallback suggestions if live scraping times out, fails, returns malformed data, or returns too few results.
27. Frontend Shopping Fallbacks
Fallback suggestion logic lives in src/api/client.ts.
If live scraper data fails, frontend creates suggestions from templates and provider search URLs.
Provider fallback behavior:
buildFallbackSuggestions()produces ranked fallback cards using wardrobe profile and request filters.createProviderFallbackSuggestions()appends external provider search links when live results are belowSCRAPER_MIN_RESULTS, default4.- Fallback URLs often point to Google searches, for example:
https://www.google.com/search?q={encoded_search_query}
The frontend labels these with:
scrape_status: "fallback"scrape_errorexplaining why fallback was used.
Live suggestions get a small ranking boost over fallback suggestions.
Filtering and ranking:
computeFilterBoost()gives boosts for preferred colors, patterns, fabrics, fits, styles, seasons, and include keywords.- Exclude keywords reject a candidate.
- Gender filtering removes mismatched products unless gender is
anyorunisex. - Final ranking uses match score + filter boost + live boost.
28. Image Proxy
GET /image-proxy?url=... fetches a remote image through the backend.
It:
- Accepts only HTTP(S).
- Uses browser-like headers.
- Returns the original content type or
image/jpeg. - Adds
Cache-Control: public, max-age=3600.
This is useful when browser CORS or hotlinking blocks direct product image rendering.
29. Health Endpoint
GET /health returns:
statusclassification_providermodelnvidia_api_configurednvidia_invoke_urlengine_versionoutfit_matching_provider
The frontend uses nvidia_api_configured before scraper recommendation requests. If it is false, it shows a configuration error rather than waiting for a planner call that cannot work.
30. Supabase Edge Functions
The repo still contains Supabase Edge Functions:
supabase/functions/wardrobe/index.tssupabase/functions/matching/index.tssupabase/functions/recommendations/index.ts- shared scoring in
supabase/functions/_shared/scoring.ts
These functions implement an earlier/alternate backend path:
- Authenticated wardrobe CRUD.
- Mock image classification and embedding generation.
- Rule-based matching with an AI placeholder.
- Occasion recommendation with outfit history writes.
The current React API clients mostly call Supabase directly for storage/data and FastAPI for AI work. The Edge Functions are useful historical context or a possible serverless deployment path, but they are not the main implementation path for the current matching and scraper UI.
31. Environment Variables
Important FastAPI variables:
DB_PATH: override SQLite file path.NVIDIA_API_KEY: required for image classification, NVIDIA model planning, and AI grid scoring.NVIDIA_INVOKE_URL: NVIDIA-compatible chat completions endpoint.NVIDIA_MODEL_ID: primary vision/text model.NVIDIA_FALLBACK_MODEL_IDS: comma-separated fallback model IDs.NVIDIA_MAX_TOKENSNVIDIA_REASONING_MAX_TOKENSNVIDIA_TEMPERATURENVIDIA_TOP_PNVIDIA_TIMEOUT_SECONDSNVIDIA_MAX_RETRIESNVIDIA_RETRY_BACKOFF_SECONDSNVIDIA_ENABLE_THINKINGNVIDIA_IMAGE_MAX_DIM- NVIDIA planner model ID: shopping planner model setting used by the backend.
- NVIDIA planner max tokens: planner token budget used by the backend.
SCRAPER_DEFAULT_STORE: defaultnike.SCRAPER_QUERY_CACHE_TTL_SECONDS: default 15 days.MATCHING_RESULT_CACHE_MAX: default500.MATCHING_RESULT_CACHE_TTL_SECONDS: default 1 day.APIFY_API_TOKEN: enables Zalando Apify scraping.APIFY_ACTOR_ENDPOINTAPIFY_MIN_TIMEOUT_SECONDSAPIFY_WAIT_FOR_FINISH_SECONDSZALANDO_HTML_TIMEOUT_SECONDSFASHION_ENCODER_MODEL_IDFASHION_EMBEDDING_DIMFASHION_IMAGE_TIMEOUT_SECONDSFASHION_EMBEDDING_CACHE_SIZEFASHION_RANKER_CHECKPOINTFASHION_CLASSIFIER_NVIDIA_MODELFASHION_CLASSIFIER_HF_MODELFASHION_CLASSIFIER_CACHE_SIZEOUTFIT_GRID_CELL_SIZEOUTFIT_GRID_LABEL_HEIGHTOUTFIT_GRID_PADDINGOUTFIT_GRID_FETCH_TIMEOUT_SECONDSOUTFIT_GRID_SESSION_TTL_SECONDSOUTFIT_GRID_SESSION_DIROUTFIT_GRID_MAX_TOP_ITEMSOUTFIT_GRID_MAX_BOTTOM_ITEMSOUTFIT_ANCHOR_MIN_SCOREOUTFIT_TEXT_PRESELECT_ENABLEDOUTFIT_TEXT_SELECTOR_MAX_TOKENSOUTFIT_AI_MAX_TOKENS
Important frontend variables:
VITE_API_MODEVITE_API_BASE_URLVITE_FLASK_API_URLVITE_SUPABASE_URLVITE_SUPABASE_ANON_KEYVITE_SUPABASE_STORAGE_BUCKETVITE_SCRAPER_TIMEOUT_MSVITE_SCRAPER_MIN_INTERVAL_MSVITE_SCRAPER_MIN_RESULTS
32. End-to-End Feature Flow Summary
Add Wardrobe Item
UploadModal
-> classifyImage()
-> FastAPI POST /classify
-> NVIDIA vision classification
-> frontend editable classification form
-> uploadClothingItem()
-> Supabase Storage + garment_items
-> wardrobe store updates
-> matching cache prefetch may run
Find Outfits
Matching page
-> outfitStore.findOutfits()
-> frontend cache check
-> fetchWardrobeOutfits()
-> FastAPI POST /ai/recommend-outfits
-> backend cache check
-> AI grid scoring
-> fashion_ai fallback
-> scoring.py fallback
-> frontend response normalizer
-> OutfitCard rendering
Score One Outfit
POST /ai/score-outfit
-> normalize top/bottom/other
-> AI grid scoring
-> fashion_ai score_outfit fallback
-> scoring.py fallback
-> score/breakdown/reason/tip response
Generate Shopping Suggestions
Suggestions page
-> getGemmaScraperRecommendations()
-> backend health check
-> FastAPI POST /scraper/recommend
-> prompt intent inference
-> wardrobe snapshot and planning context
-> NVIDIA model query planner
-> deterministic planner fallback if needed
-> Nike/Zalando URL generation
-> product scraping
-> strict relevance filtering
-> scrape fallback if strict filter removed everything
-> product-to-wardrobe match enrichment
-> saved JSON payload
-> product cards in frontend
33. Known Implementation Nuances
getGemmaScraperRecommendations()is named after Gemma, but the current backend route uses the configured NVIDIA model through the NVIDIA-compatible inference path.- The existing
StyleWellBackend/README.mdmentions Hugging Face primary and NVIDIA fallback, but the currentapp.pypath relies heavily on NVIDIA-compatible chat completions for classification, NVIDIA model planning, and AI grid scoring. /scraper/recommendbuilds wardrobe context from backend SQLite. If the active wardrobe is only in Supabase, planner grounding may not reflect the user's actual Supabase wardrobe unless items are mirrored or this route is updated to accept wardrobe payloads./uploadin FastAPI storesmemory://image URLs, so it is useful for backend-local tests but not equivalent to Supabase image persistence.- The Supabase Edge Functions contain useful earlier scoring and CRUD logic, but the main frontend path now uses Supabase client APIs plus FastAPI.
- There are many fallback layers by design. A failed AI model should degrade to multimodal scoring, deterministic scoring, provider search links, or readable user errors rather than leaving the UI blank.
34. Local Run
From StyleWellBackend/:
pip install -r requirements.txt
python app.py
The backend starts on:
http://0.0.0.0:7860
Health check:
curl http://127.0.0.1:7860/health
Classification:
curl -X POST http://127.0.0.1:7860/classify -F "image=@/path/to/garment.jpg"
Outfit recommendation:
curl -X POST http://127.0.0.1:7860/ai/recommend-outfits \
-H "Content-Type: application/json" \
-d '{"occasion":"casual","wardrobe_items":[]}'
Scraper recommendation:
curl -X POST http://127.0.0.1:7860/scraper/recommend \
-H "Content-Type: application/json" \
-d '{"user_prompt":"Need a formal navy shirt","gender":"men","store":"nike","max_products":6}'