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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # StyleWell Backend Implementation README
2
+
3
+ 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.
4
+
5
+ ## 1. System Shape
6
+
7
+ StyleWell is split into three related layers:
8
+
9
+ 1. React frontend in `src/`.
10
+ 2. FastAPI backend in `StyleWellBackend/`.
11
+ 3. Supabase database, storage, auth, migrations, and older Edge Function logic in `supabase/`.
12
+
13
+ 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.
14
+
15
+ Current practical flow:
16
+
17
+ - Authentication: frontend uses Supabase Auth through `src/lib/supabase.ts`.
18
+ - Wardrobe images and garment rows: frontend stores images in Supabase Storage and metadata in the `garment_items` table when Supabase env vars are configured.
19
+ - Classification: frontend calls FastAPI `/classify` before saving the classified garment to Supabase.
20
+ - Outfit matching: frontend reads wardrobe items from Supabase, normalizes them, then sends them to FastAPI `/ai/recommend-outfits`.
21
+ - Shopping suggestions: frontend sends the natural-language shopping request to FastAPI `/scraper/recommend`.
22
+ - Backend-local SQLite: FastAPI also has its own SQLite store for local uploads, backend-side item CRUD, feedback, and persistent matching cache.
23
+
24
+ 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.
25
+
26
+ ## 2. Backend Directory Map
27
+
28
+ `StyleWellBackend/app.py`
29
+
30
+ 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.
31
+
32
+ `StyleWellBackend/db.py`
33
+
34
+ SQLite persistence layer. It creates `items`, `outfit_feedback`, and `search_cache`, exposes CRUD helpers, and stores cache entries used by matching.
35
+
36
+ `StyleWellBackend/scoring.py`
37
+
38
+ 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.
39
+
40
+ `StyleWellBackend/scraper.py`
41
+
42
+ 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.
43
+
44
+ `StyleWellBackend/zalando_scraper.py`
45
+
46
+ 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.
47
+
48
+ `StyleWellBackend/fashion_ai/`
49
+
50
+ Local multimodal recommendation subsystem:
51
+
52
+ - `encoder.py`: encodes garment text and images into vectors.
53
+ - `retriever.py`: splits wardrobe by slot and retrieves candidate garments.
54
+ - `ranker.py`: scores candidate outfits with a trained transformer if available, otherwise zero-shot vector scoring.
55
+ - `classifier.py`: model-backed or metadata-backed item classifier and item matcher.
56
+ - `service.py`: orchestrates encoder, retriever, ranker, and classifier.
57
+ - `schemas.py`: dataclasses for encoded items, context, weather, and candidates.
58
+
59
+ ## 3. Frontend to Backend Connection
60
+
61
+ ### Base URL Resolution
62
+
63
+ Frontend API calls go through `src/api/http.ts`:
64
+
65
+ - `requestJson()` resolves a path into a full URL.
66
+ - `resolveRequestUrl()` calls `getFlaskApiUrl()` from `src/lib/supabase.ts`.
67
+ - `getFlaskApiUrl()` checks:
68
+ 1. `VITE_FLASK_API_URL` or `VITE_API_BASE_URL`.
69
+ 2. Supabase `app_config` row where `key = 'flask_api_url'`.
70
+ 3. Local fallback `http://127.0.0.1:5000`.
71
+
72
+ The base URL is normalized by `src/config/api.ts`:
73
+
74
+ - `normalizeBaseUrl()` trims trailing slashes.
75
+ - It fixes a common Hugging Face Space typo by converting `<owner>-<space>.space` into `<owner>-<space>.hf.space`.
76
+ - `buildApiUrl(path, baseUrl)` joins the backend base URL with endpoint paths.
77
+
78
+ ### API Mode
79
+
80
+ `src/config/api.ts` defines `API_MODE`:
81
+
82
+ - Explicit `VITE_API_MODE` wins.
83
+ - Otherwise frontend uses `live` if Supabase or API base env vars exist.
84
+ - Otherwise it uses `mock`.
85
+
86
+ Several frontend functions branch on `isLiveApiMode()`:
87
+
88
+ - In live mode, matching and scraper calls go to FastAPI.
89
+ - In mock mode, frontend returns rule-based or provider-search fallback suggestions.
90
+
91
+ ### Route Fallbacks
92
+
93
+ The frontend tries multiple route shapes for compatibility with different deployments:
94
+
95
+ Matching in `src/api/matching.ts` tries:
96
+
97
+ - `/ai/recommend-outfits`
98
+ - `/api/ai/recommend-outfits`
99
+ - `/recommend-outfits`
100
+ - `/api/recommend-outfits`
101
+
102
+ Suggestions in `src/api/client.ts` tries:
103
+
104
+ - `/suggestions`
105
+ - `/api/suggestions`
106
+
107
+ Scraper recommendation in `src/api/client.ts` tries:
108
+
109
+ - `/scraper/recommend`
110
+ - `/api/scraper/recommend`
111
+
112
+ 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.
113
+
114
+ ## 4. Wardrobe Upload and Classification Flow
115
+
116
+ The wardrobe upload UI lives in `src/components/UploadModal.tsx`.
117
+
118
+ Flow:
119
+
120
+ 1. User picks an image.
121
+ 2. `classifyImage(file)` in `src/api/client.ts` sends a multipart `POST /classify` request to FastAPI.
122
+ 3. FastAPI `classify()` in `app.py` reads the file into a PIL image.
123
+ 4. The image is converted to RGB and thumbnailed to `512x512`.
124
+ 5. `run_nvidia_inference(pil_image, CLASSIFICATION_PROMPT)` sends the image and classification prompt to NVIDIA chat completions.
125
+ 6. `parse_json_from_text()` extracts JSON from the model output.
126
+ 7. `normalize_specs()` guarantees the response has:
127
+ - `type`
128
+ - `category`
129
+ - `color`
130
+ - `pattern`
131
+ - `fabric`
132
+ - `fit`
133
+ - `occasion`
134
+ - `season`
135
+ 8. The frontend lets the user edit the classification.
136
+ 9. `uploadClothingItem(file, classificationOverride)` saves the final garment.
137
+
138
+ In the current frontend, when Supabase env vars exist, saving goes through `saveGarmentWithClassification()` in `src/lib/supabase.ts`:
139
+
140
+ - Requires an authenticated Supabase user.
141
+ - Builds a storage path like `{user.id}/{timestamp}_{safe_filename}.{ext}`.
142
+ - Uploads to the first working bucket among:
143
+ - `VITE_SUPABASE_STORAGE_BUCKET`
144
+ - `clothing-images`
145
+ - `garments`
146
+ - Inserts a row into `garment_items`.
147
+ - Tries insert with `user_id`; if schema does not support `user_id`, it falls back to inserting without it.
148
+ - Resolves a signed URL for the stored image.
149
+
150
+ If Supabase is not available, `uploadClothingItem()` can fall back to FastAPI `/upload` in live mode or a local object URL in mock mode.
151
+
152
+ 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.
153
+
154
+ ## 5. FastAPI Classification Internals
155
+
156
+ Classification settings in `app.py`:
157
+
158
+ - `NVIDIA_INVOKE_URL`, default `https://integrate.api.nvidia.com/v1/chat/completions`
159
+ - `NVIDIA_MODEL_ID`, default `qwen/qwen3.5-122b-a10b`
160
+ - `NVIDIA_FALLBACK_MODEL_IDS`, comma-separated fallback model IDs.
161
+ - `NVIDIA_MAX_TOKENS`
162
+ - `NVIDIA_REASONING_MAX_TOKENS`
163
+ - `NVIDIA_TEMPERATURE`
164
+ - `NVIDIA_TOP_P`
165
+ - `NVIDIA_TIMEOUT_SECONDS`
166
+ - `NVIDIA_MAX_RETRIES`
167
+ - `NVIDIA_RETRY_BACKOFF_SECONDS`
168
+ - `NVIDIA_ENABLE_THINKING`
169
+ - `NVIDIA_IMAGE_MAX_DIM`
170
+
171
+ `run_nvidia_inference()` is used for image+text inference. It:
172
+
173
+ 1. Checks `NVIDIA_API_KEY`.
174
+ 2. Tries `NVIDIA_MODEL_ID`, then every configured fallback model from `NVIDIA_FALLBACK_MODEL_IDS`.
175
+ 3. Sends an SSE streaming chat completion request.
176
+ 4. Extracts streamed text with `_extract_streamed_nvidia_text()`.
177
+ 5. Retries transient status codes `429`, `500`, `502`, `503`, and `504`.
178
+ 6. Applies retry backoff.
179
+ 7. If the response hits a token limit, it can retry with a larger token budget up to `NVIDIA_REASONING_MAX_TOKENS`.
180
+ 8. If a provider says a model is degraded, it tries the next model.
181
+
182
+ The model response parser is defensive:
183
+
184
+ - `_extract_text_from_nvidia_content()` accepts string, list, or dict content shapes.
185
+ - `_extract_nvidia_text()` can read `content`, `reasoning_content`, or `reasoning`.
186
+ - `parse_json_from_text()` first tries full JSON, then extracts the first `{...}` block.
187
+ - `normalize_specs()` fills missing fields with `"Unknown"`.
188
+
189
+ If every model/provider path fails, `_raise_http_error()` converts errors into HTTP responses:
190
+
191
+ - Missing NVIDIA key: `503`.
192
+ - NVIDIA gateway error: configured status, usually `502` or `503`.
193
+ - Bad model payload: `502`.
194
+ - Unknown server problem: `500`.
195
+
196
+ Frontend error messages are normalized by `toUserFacingApiMessage()` in `src/api/http.ts`.
197
+
198
+ ## 6. Wardrobe Item Normalization
199
+
200
+ Backend scoring expects a compact normalized shape. `_normalize_wardrobe_item()` in `app.py` accepts raw records from SQLite, Supabase, or frontend payloads and returns:
201
+
202
+ - `id`
203
+ - `image_url`
204
+ - `type`
205
+ - `category`
206
+ - `color`
207
+ - `pattern`
208
+ - `fabric`
209
+ - `fit`
210
+ - `season`
211
+ - `style`
212
+ - `occasion`
213
+ - `description`
214
+
215
+ Important logic:
216
+
217
+ - If `category` is missing, it reads from `description.category` or `description.type`.
218
+ - If `type` is missing, it calls `_infer_type(category)`.
219
+ - `_infer_type()` maps shirt, tee, top, kurta, blouse, hoodie, sweater, blazer, jacket, polo to `topwear`.
220
+ - It maps jean, pant, trouser, short, skirt, jogger, palazzo, chino to `bottomwear`.
221
+ - Everything else becomes `others`.
222
+ - `style` comes from `item.style`, `description.occasion`, `description.style`, or falls back to `casual`.
223
+
224
+ 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`.
225
+
226
+ ## 7. SQLite Storage
227
+
228
+ `db.py` chooses the database path with `_resolve_db_path()`:
229
+
230
+ 1. `DB_PATH` if provided.
231
+ 2. `/data/wardrobe.db` if running on Hugging Face Spaces with persistent storage.
232
+ 3. `./wardrobe.db` for local development.
233
+
234
+ `_conn()` opens SQLite with:
235
+
236
+ - `check_same_thread=False`
237
+ - `row_factory=sqlite3.Row`
238
+ - WAL journal mode
239
+ - foreign keys enabled
240
+ - automatic commit/rollback
241
+
242
+ `init_db()` creates:
243
+
244
+ ### `items`
245
+
246
+ Stores backend-local wardrobe items with scalar metadata and JSON `description`.
247
+
248
+ Columns:
249
+
250
+ - `id`
251
+ - `image_url`
252
+ - `category`
253
+ - `color`
254
+ - `pattern`
255
+ - `fabric`
256
+ - `fit`
257
+ - `season`
258
+ - `style`
259
+ - `type`
260
+ - `description`
261
+ - `created_at`
262
+
263
+ ### `outfit_feedback`
264
+
265
+ Stores feedback for a top/bottom pair.
266
+
267
+ Columns:
268
+
269
+ - `id`
270
+ - `top_id`
271
+ - `bottom_id`
272
+ - `occasion`
273
+ - `action`, constrained to `wear`, `skip`, or `save`
274
+ - `score`
275
+ - `created_at`
276
+
277
+ ### `search_cache`
278
+
279
+ Generic JSON cache table.
280
+
281
+ Columns:
282
+
283
+ - `cache_key`
284
+ - `payload`
285
+ - `created_at`
286
+ - `expires_at`
287
+
288
+ `cache_get()` deletes expired rows on read. `cache_set()` upserts payloads with TTL.
289
+
290
+ ## 8. Outfit Matching: Frontend Flow
291
+
292
+ The matching page is `src/pages/Matching.tsx`.
293
+
294
+ User controls:
295
+
296
+ - Select occasion.
297
+ - Optionally lock a top.
298
+ - Optionally lock a bottom.
299
+ - Optionally lock an `others` item.
300
+ - Click `Find Outfits`.
301
+
302
+ State lives in `src/store/outfitStore.ts`.
303
+
304
+ When the page loads:
305
+
306
+ - `fetchWardrobe()` calls `getWardrobeItems()`.
307
+ - Items are normalized into frontend `ClothingItem` objects.
308
+ - Slots are inferred with `inferWardrobeSlot()`.
309
+
310
+ When the user finds outfits:
311
+
312
+ 1. `findOutfits()` computes lock state.
313
+ 2. It resolves a matching cache category:
314
+ - locked other -> `others`
315
+ - locked bottom only -> `bottomwear`
316
+ - locked top -> `topwear`
317
+ - default -> `topwear`
318
+ 3. It builds a lock signature like `top:{id|-}|bottom:{id|-}|other:{id|-}`.
319
+ 4. It gets the full wardrobe context.
320
+ 5. It computes a wardrobe hash:
321
+ - global hash when locked selections exist.
322
+ - category hash for prefetch/default category caching.
323
+ 6. It checks local cache in `outfitMatchingCache.v1` in `localStorage`.
324
+ 7. If no cache hit, it calls `fetchWardrobeOutfits()` in `src/api/matching.ts`.
325
+
326
+ `fetchWardrobeOutfits()` sends this payload to FastAPI:
327
+
328
+ ```json
329
+ {
330
+ "occasion": "casual",
331
+ "top_k": 5,
332
+ "top_selected": null,
333
+ "bottom_selected": null,
334
+ "other_selected": null,
335
+ "wardrobe_items": [],
336
+ "user_id": "optional",
337
+ "cache_category": "topwear",
338
+ "wardrobe_hash": "optional",
339
+ "lock_signature": "optional"
340
+ }
341
+ ```
342
+
343
+ The backend response is normalized by `normalizeMatchingResponse()` before it is rendered in `OutfitCard`.
344
+
345
+ ## 9. Outfit Matching: Backend Route
346
+
347
+ The main route is `POST /ai/recommend-outfits` in `app.py`.
348
+
349
+ It accepts:
350
+
351
+ - `occasion`
352
+ - `wardrobe_items`
353
+ - `top_selected`
354
+ - `bottom_selected`
355
+ - `other_selected`
356
+ - `weather`
357
+ - `user_profile`
358
+ - `region`
359
+ - `top_k`
360
+ - `candidate_pool`
361
+ - `diversity_lambda`
362
+ - `cache_category`
363
+ - `wardrobe_hash`
364
+ - `lock_signature`
365
+ - `user_id`
366
+
367
+ Validation and normalization:
368
+
369
+ - `wardrobe_items` must be a list.
370
+ - If `wardrobe_items` is empty, backend reads from SQLite `item_get_all()`.
371
+ - Every item is passed through `_normalize_wardrobe_item()`.
372
+ - `top_k` is clamped to `1..20`.
373
+ - `candidate_pool` is clamped to `4..64`.
374
+ - `diversity_lambda` is clamped to `0..0.95`.
375
+
376
+ Case labels:
377
+
378
+ - Case `A`: no top or bottom locked.
379
+ - Case `B`: top locked.
380
+ - Case `C`: bottom locked.
381
+ - Case `D`: top and bottom locked.
382
+ - Case `E`: standalone `others` item.
383
+
384
+ Special handling:
385
+
386
+ - If a locked `other_selected` item is an `others` item, the backend returns standalone recommendations using fallback scoring, because `others` may be a dress, kurta, saree, jumpsuit, or another single-piece outfit.
387
+ - If there are no tops or no bottoms but there are `others`, it returns standalone `others` outfits.
388
+ - If there are no usable items, it returns an empty response with a notice.
389
+
390
+ ## 10. Matching Cache
391
+
392
+ There are two caches:
393
+
394
+ ### Frontend cache
395
+
396
+ `src/store/cacheUtils.ts` stores matching responses in `localStorage` under `outfitMatchingCache.v1`.
397
+
398
+ Cache key:
399
+
400
+ ```text
401
+ {category}|{occasion}|{wardrobeHash}|{lockSignature}
402
+ ```
403
+
404
+ `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.
405
+
406
+ ### Backend cache
407
+
408
+ `app.py` stores matching responses in:
409
+
410
+ - In-memory `MATCHING_RESULT_CACHE`.
411
+ - SQLite `search_cache` under keys prefixed by `matching:`.
412
+
413
+ Backend matching cache key:
414
+
415
+ ```text
416
+ {user_id}|{cache_category}|{occasion}|{wardrobe_hash}|{lock_signature}
417
+ ```
418
+
419
+ 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`.
420
+
421
+ ## 11. AI Grid Outfit Scoring
422
+
423
+ The preferred backend scoring path is `_recommend_outfits_with_ai_grid()`.
424
+
425
+ It works like this:
426
+
427
+ 1. `_resolve_outfit_grid_sources()` splits wardrobe into top, bottom, and others pools.
428
+ 2. If a top is locked, only that top is used and bottoms are ranked against it.
429
+ 3. If a bottom is locked, only that bottom is used and tops are ranked against it.
430
+ 4. If both are locked, exactly that pair is evaluated.
431
+ 5. If `other_selected` is provided, it is included as a locked Row 3 candidate.
432
+ 6. The candidate pools may be reduced before image grid scoring:
433
+ - `OUTFIT_GRID_MAX_TOP_ITEMS`, default `4`.
434
+ - `OUTFIT_GRID_MAX_BOTTOM_ITEMS`, default `4`.
435
+ 7. If `OUTFIT_TEXT_PRESELECT_ENABLED=true`, `_select_grid_candidates_with_text_ai()` asks NVIDIA text inference to pick the strongest candidates before grid scoring.
436
+ 8. `_build_outfit_grid_session()` creates a composite image:
437
+ - Row 1: topwear.
438
+ - Row 2: bottomwear.
439
+ - Row 3: optional others.
440
+ - Every cell gets a coordinate like `1:1`, `2:3`, `3:1`.
441
+ 9. The composite image is saved to `OUTFIT_GRID_SESSION_DIR`.
442
+ 10. `_grid_scoring_prompt()` builds a prompt containing:
443
+ - occasion
444
+ - weather
445
+ - user profile
446
+ - region
447
+ - anchor mode
448
+ - locked cell indices
449
+ - metadata map
450
+ - combination count
451
+ - requested `top_k`
452
+ 11. `run_nvidia_inference()` sends the grid image plus prompt to the vision model.
453
+ 12. `parse_json_from_text()` extracts JSON.
454
+ 13. `_normalize_ai_outfit_payload()` maps returned grid coordinates back to actual wardrobe items.
455
+
456
+ The model must return recommendations with:
457
+
458
+ - `top_index`
459
+ - `bottom_index`
460
+ - `other_index`
461
+ - `score`
462
+ - `breakdown`
463
+ - `reason`
464
+ - `tip`
465
+
466
+ The backend then returns:
467
+
468
+ - `recommendations` for normal cases.
469
+ - `selected_outfit_score` plus `improved_recommendations` for Case `D`.
470
+ - `engine_version: ai-grid-v1`.
471
+
472
+ If the AI grid path fails, the backend does not fail the user. It falls back.
473
+
474
+ ## 12. Multimodal Fallback Recommendation Service
475
+
476
+ If AI grid scoring fails, `_current_fallback_recommendations()` first tries `fashion_ai`:
477
+
478
+ ```python
479
+ get_recommendation_service().recommend(...)
480
+ ```
481
+
482
+ That service is created once as a singleton in `fashion_ai/service.py`.
483
+
484
+ ### Encoder
485
+
486
+ `FashionItemEncoder` in `encoder.py` builds garment embeddings.
487
+
488
+ Item prompt example:
489
+
490
+ ```text
491
+ Fashion product photo of a navy solid cotton shirt, regular fit, casual style, suitable for all-season, worn as top.
492
+ ```
493
+
494
+ It encodes both text and image when possible:
495
+
496
+ - Text is encoded with the configured model.
497
+ - Image URL is fetched and encoded if it is HTTP(S) or a local file.
498
+ - Text and image vectors are averaged.
499
+
500
+ Backends:
501
+
502
+ - `open_clip` when model id starts with `marqo/` and OpenCLIP is installed.
503
+ - Hugging Face Transformers model, default `patrickjohncyh/fashion-clip`.
504
+ - Deterministic hash embedding fallback if model loading fails.
505
+
506
+ The fallback hash embedding means the system stays usable even without model weights, but the quality is lower.
507
+
508
+ ### Retriever
509
+
510
+ `OutfitCandidateRetriever` in `retriever.py`:
511
+
512
+ - Encodes every wardrobe item.
513
+ - Splits items into slots:
514
+ - `top`
515
+ - `bottom`
516
+ - `shoes`
517
+ - `accessory`
518
+ - `unknown`
519
+ - Uses context vector similarity to rank each slot.
520
+ - Uses MMR diversification to avoid near-duplicate candidates.
521
+ - Honors locked top, bottom, and other items.
522
+
523
+ ### Ranker
524
+
525
+ `NeuralOutfitScorer` in `ranker.py`:
526
+
527
+ - Loads a trained transformer checkpoint from `FASHION_RANKER_CHECKPOINT` if available.
528
+ - If no checkpoint exists, uses zero-shot geometric scoring.
529
+
530
+ Transformer scoring input shape:
531
+
532
+ ```text
533
+ [CONTEXT, USER, TOP, BOTTOM, SHOES, ACCESSORY]
534
+ ```
535
+
536
+ Zero-shot scoring blends:
537
+
538
+ - context alignment
539
+ - user alignment
540
+ - pairwise cohesion
541
+ - slot coverage
542
+
543
+ It returns:
544
+
545
+ - `score`
546
+ - `breakdown`
547
+ - `reason`
548
+ - `tip`
549
+
550
+ ### Service Cases
551
+
552
+ `MultimodalOutfitRecommendationService.recommend()` uses cases:
553
+
554
+ - `A`: no locked top/bottom.
555
+ - `B`: locked top.
556
+ - `C`: locked bottom.
557
+ - `D`: locked top and bottom.
558
+
559
+ For Case `D`, it scores the selected outfit separately and returns improvement suggestions in `improved_recommendations`.
560
+
561
+ ## 13. Deterministic Rule-Based Scoring
562
+
563
+ If AI grid and `fashion_ai` both fail, the backend uses `scoring.py`.
564
+
565
+ The main entry points are:
566
+
567
+ - `compute_score(top, bottom, occasion, other=None)`
568
+ - `score_pair_full(top, bottom, occasion, other=None)`
569
+ - `recommend_outfits(tops, bottoms, occasion, others, locked_top, locked_bottom, locked_other)`
570
+
571
+ ### Weights
572
+
573
+ `WEIGHTS`:
574
+
575
+ - color: `0.30`
576
+ - style: `0.25`
577
+ - occasion: `0.20`
578
+ - fit: `0.13`
579
+ - pattern: `0.12`
580
+
581
+ ### Color Score
582
+
583
+ `_color_score()` extracts base colors and scores:
584
+
585
+ - Known complementary pairs: high score, usually `90`.
586
+ - Neutral with neutral: `82` unless same neutral, then `50`.
587
+ - Neutral with non-neutral: `80`.
588
+ - Analogous colors: `60`.
589
+ - Same non-neutral color: `45`.
590
+ - Unknown/missing: `60`.
591
+
592
+ Complementary examples:
593
+
594
+ - blue + beige
595
+ - black + white
596
+ - navy + khaki
597
+ - olive + tan
598
+ - burgundy + grey
599
+ - mustard + navy
600
+
601
+ ### Style Score
602
+
603
+ `_style_score()` maps `style` or `occasion` to:
604
+
605
+ - casual
606
+ - formal
607
+ - streetwear
608
+ - party
609
+ - sports
610
+
611
+ Then `_STYLE_MATRIX` scores pair compatibility. Examples:
612
+
613
+ - formal + formal: `90`
614
+ - casual + casual: `85`
615
+ - streetwear + streetwear: `88`
616
+ - sports + formal: `28`
617
+ - formal + streetwear: `48`
618
+
619
+ ### Occasion Score
620
+
621
+ `_occasion_score()` maps styles to valid occasions:
622
+
623
+ - casual: casual, everyday, weekend, college, brunch
624
+ - formal: formal, work, interview, business, office, wedding, meeting
625
+ - party: party, festive, ethnic, diwali, celebration, date
626
+ - sports: sports, gym, active, outdoor, trekking
627
+ - streetwear: casual, streetwear, everyday, college
628
+
629
+ Formal occasions are stricter:
630
+
631
+ - Both pieces fit: `90`.
632
+ - One piece fits: `60` for formal, `70` otherwise.
633
+ - Neither fits: `25` for formal, `35` otherwise.
634
+
635
+ ### Fit Score
636
+
637
+ `_fit_score()` uses `_FIT_MATRIX`.
638
+
639
+ Examples:
640
+
641
+ - oversized top + slim bottom: `92`.
642
+ - regular + regular: `80`.
643
+ - oversized + oversized: `55`.
644
+ - slim + regular: `82`.
645
+
646
+ ### Pattern Score
647
+
648
+ `_pattern_score()`:
649
+
650
+ - Both patterned: `55`.
651
+ - One patterned, one solid: `88`.
652
+ - Both solid/plain: `75`.
653
+
654
+ ### Season and Fabric Penalties
655
+
656
+ `_season_penalty()` subtracts points:
657
+
658
+ - Heavy fabrics in summer: `18`.
659
+ - Very light fabrics in winter: `12`.
660
+
661
+ Heavy fabrics:
662
+
663
+ - wool
664
+ - leather
665
+ - velvet
666
+ - tweed
667
+ - corduroy
668
+ - fleece
669
+
670
+ Light fabrics:
671
+
672
+ - linen
673
+ - cotton
674
+ - silk
675
+ - chiffon
676
+ - georgette
677
+
678
+ ### Veto Caps
679
+
680
+ After weighted scoring, fatal flaws cap the final score:
681
+
682
+ - color score `<= 50`: cap final score at `68`.
683
+ - style score `<= 48`: cap final score at `58`.
684
+ - occasion score `<= 40`: cap final score at `52`.
685
+ - both patterned plus weak color: cap final score at `72`.
686
+
687
+ ### Other Items
688
+
689
+ When an `other` item is included, the backend blends top-bottom scores with top-other and bottom-other scores:
690
+
691
+ - primary top-bottom breakdown contributes `65%`.
692
+ - extra pair average contributes `35%`.
693
+
694
+ This lets accessories, footwear, or outerwear influence the outfit without overpowering the main top/bottom pair.
695
+
696
+ ### Explanations
697
+
698
+ `build_reason()` creates user-facing text from the strongest and weakest scoring dimensions. `build_tip()` gives the styling advice shown in the UI.
699
+
700
+ ## 14. Standalone Others
701
+
702
+ The code treats `others` specially because many garments are complete outfits by themselves:
703
+
704
+ - dresses
705
+ - kurtas
706
+ - sarees
707
+ - lehengas
708
+ - jumpsuits
709
+ - rompers
710
+ - gowns
711
+ - co-ord sets
712
+
713
+ In `fashion_ai/encoder.py`, standalone outfit keywords become slot `unknown`.
714
+
715
+ In `app.py`:
716
+
717
+ - `_fallback_rule_recommendations()` can score `others` by pairing the item with itself.
718
+ - `_occasion_prefers_standalone_others()` boosts standalone `others` for wedding, festive, ethnic, ceremony, engagement, reception, sangeet, haldi, and mehndi occasions.
719
+ - `_merge_standalone_others_for_priority_occasions()` merges boosted standalone outfits into normal recommendations for those occasions.
720
+
721
+ This prevents ethnic or single-piece outfits from being ignored just because they are not topwear or bottomwear.
722
+
723
+ ## 15. Score Outfit Endpoint
724
+
725
+ `POST /ai/score-outfit` scores one outfit.
726
+
727
+ Required:
728
+
729
+ - `top`
730
+ - `bottom`
731
+
732
+ Optional:
733
+
734
+ - `other`
735
+ - `occasion`
736
+ - `weather`
737
+ - `user_profile`
738
+ - `region`
739
+
740
+ Scoring path:
741
+
742
+ 1. Try AI grid scoring with only the supplied items.
743
+ 2. If that fails, try `fashion_ai` service `score_outfit()`.
744
+ 3. If that fails, use `score_pair_full()`.
745
+
746
+ Response fields:
747
+
748
+ - `score`
749
+ - `color_score`
750
+ - `style_score`
751
+ - `occasion_score`
752
+ - `fit_score`
753
+ - `pattern_score`
754
+ - `season_score`
755
+ - `reason`
756
+ - `tip`
757
+ - `engine_version`
758
+
759
+ ## 16. Gap Analysis
760
+
761
+ `POST /ai/gap-analysis` uses `_gap_suggestions()`.
762
+
763
+ It is deterministic, not model-generated:
764
+
765
+ - If no topwear exists, suggest adding topwear.
766
+ - If no bottomwear exists, suggest adding bottomwear.
767
+ - If one category is much larger than the other by more than 2 items, suggest balancing the wardrobe.
768
+ - Otherwise suggest adding one occasion-specific versatile piece.
769
+
770
+ ## 17. Feedback
771
+
772
+ `POST /feedback` records preference signals into SQLite.
773
+
774
+ Required:
775
+
776
+ - `top_id`
777
+ - `bottom_id`
778
+ - `action`
779
+
780
+ `action` must be:
781
+
782
+ - `wear`
783
+ - `skip`
784
+ - `save`
785
+
786
+ Optional:
787
+
788
+ - `occasion`
789
+ - `score`
790
+
791
+ The backend validates that both item IDs exist before writing feedback.
792
+
793
+ ## 18. Shopping Suggestions: Frontend Flow
794
+
795
+ The shopping UI is `src/pages/Suggestions.tsx`.
796
+
797
+ User provides:
798
+
799
+ - natural-language request
800
+ - store selection: default, Nike, or Zalando
801
+ - gender: men, women, unisex
802
+
803
+ `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.
804
+
805
+ Before the scraper call, frontend:
806
+
807
+ - Reads wardrobe count with `getWardrobeItems()`.
808
+ - Shows how many total, topwear, bottomwear, and others items are loaded.
809
+ - Checks backend `/health` and verifies `nvidia_api_configured` is truthy in live mode.
810
+
811
+ Then it posts:
812
+
813
+ ```json
814
+ {
815
+ "user_prompt": "Need a formal office shirt...",
816
+ "occasion": "auto",
817
+ "gender": "men",
818
+ "target_category": "both",
819
+ "filters": {},
820
+ "preferences": "",
821
+ "store": "nike"
822
+ }
823
+ ```
824
+
825
+ The response contains:
826
+
827
+ - `query_plan`
828
+ - `search_urls`
829
+ - `product_urls`
830
+ - `products`
831
+ - `intermediate_steps`
832
+ - `plan_source`
833
+ - `plan_error`
834
+ - `scrape_error`
835
+ - `saved_json_path`
836
+
837
+ The UI renders products directly from `products`.
838
+
839
+ ## 19. Scraper Recommendation Route
840
+
841
+ The main route is `POST /scraper/recommend`.
842
+
843
+ It performs these steps:
844
+
845
+ 1. Read `user_prompt`.
846
+ 2. Infer structured intent from the prompt with `_infer_structured_request_from_prompt()`.
847
+ 3. Merge explicit payload fields with inferred fields.
848
+ 4. Merge inferred colors, include keywords, and exclude keywords into filters.
849
+ 5. Build a scraper query cache key.
850
+ 6. Return cached result when available.
851
+ 7. Otherwise call the scraper planner function in `app.py`.
852
+ 8. Store the result in in-memory scraper cache.
853
+
854
+ The scraper cache key is:
855
+
856
+ ```text
857
+ md5(user_prompt.lower().strip()|store|gender|target_category)
858
+ ```
859
+
860
+ TTL is `SCRAPER_QUERY_CACHE_TTL_SECONDS`, default 15 days.
861
+
862
+ ## 20. Prompt Intent Inference
863
+
864
+ `_infer_structured_request_from_prompt()` reads the user prompt and extracts:
865
+
866
+ - `target_category`
867
+ - `occasion`
868
+ - `gender`
869
+ - `preferred_colors`
870
+ - `include_keywords`
871
+ - `exclude_keywords`
872
+
873
+ Target category hints:
874
+
875
+ - topwear tokens: top, shirt, blazer, jacket, polo, tee, t-shirt, kurta, upper
876
+ - bottomwear tokens: bottom, trouser, trousers, pants, jeans, shorts, joggers, lower
877
+
878
+ Occasion buckets:
879
+
880
+ - formal: formal, interview, office, work, business, meeting, wedding
881
+ - party: party, festive, diwali, celebration, date, ethnic
882
+ - sports: sports, gym, workout, training, running, active
883
+ - casual: casual, daily, everyday, weekend, outing
884
+
885
+ Color terms include:
886
+
887
+ - black
888
+ - white
889
+ - navy
890
+ - blue
891
+ - grey
892
+ - beige
893
+ - olive
894
+ - green
895
+ - brown
896
+ - khaki
897
+ - cream
898
+ - maroon
899
+ - charcoal
900
+ - tan
901
+
902
+ Include keywords currently recognized:
903
+
904
+ - formal
905
+ - structured
906
+ - minimal
907
+ - smart
908
+ - elegant
909
+ - tailored
910
+
911
+ Exclude keywords are recognized only when phrased like `avoid hoodie`, `no hoodie`, or `without hoodie`.
912
+
913
+ ## 21. NVIDIA Model Shopping Planner
914
+
915
+ 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.
916
+
917
+ It builds:
918
+
919
+ - wardrobe snapshot from SQLite via `_wardrobe_metadata_snapshot()`
920
+ - requested target category
921
+ - safe filters
922
+ - planning context from `_build_scraper_planning_context()`
923
+ - prompt from `_build_scraper_plan_prompt()`
924
+
925
+ ### Wardrobe Snapshot
926
+
927
+ `_wardrobe_metadata_snapshot()` reads backend SQLite items and returns:
928
+
929
+ - `total_items`
930
+ - item metadata list
931
+ - counts by `{type}|{occasion}`
932
+
933
+ 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.
934
+
935
+ ### Planning Context
936
+
937
+ `_build_scraper_planning_context()` computes:
938
+
939
+ - requested target category
940
+ - resolved target category
941
+ - occasion bucket
942
+ - gender preference
943
+ - allowed categories
944
+ - color shortlist
945
+ - color resonance scores
946
+ - style direction
947
+ - reference slot
948
+ - reference item IDs
949
+ - dominant categories/colors by slot
950
+
951
+ If user asks for `both`, `_resolve_target_category()` chooses the category that is underrepresented:
952
+
953
+ - If top count is less than or equal to bottom count, recommend topwear.
954
+ - Otherwise recommend bottomwear.
955
+
956
+ Allowed categories come from `SCRAPER_CATEGORY_POLICY`.
957
+
958
+ For topwear:
959
+
960
+ - formal: shirt, polo, jacket
961
+ - party: shirt, jacket, polo
962
+ - sports: jersey, t-shirt, hoodie
963
+ - casual: shirt, t-shirt, polo, jacket, hoodie
964
+
965
+ For bottomwear:
966
+
967
+ - formal: trousers, pants
968
+ - party: trousers, pants, jeans
969
+ - sports: joggers, shorts, tights, leggings
970
+ - casual: jeans, pants, shorts, joggers, trousers
971
+
972
+ Formal disallowed terms are blocked:
973
+
974
+ - hoodie
975
+ - sweatshirt
976
+ - joggers
977
+ - shorts
978
+ - tank top
979
+ - tights
980
+ - leggings
981
+
982
+ ### Color Resonance
983
+
984
+ `_rank_color_resonance()` scores candidate colors using:
985
+
986
+ - colors from the reference slot
987
+ - colors across topwear and bottomwear
988
+ - user preferred colors
989
+ - occasion boost
990
+
991
+ The reference slot is the opposite of the target:
992
+
993
+ - If recommending topwear, reference bottomwear colors.
994
+ - If recommending bottomwear, reference topwear colors.
995
+
996
+ Score formula:
997
+
998
+ ```text
999
+ (reference_count * 3) + global_count + preferred_bonus + occasion_bonus
1000
+ ```
1001
+
1002
+ Preferred bonus is `2`. Occasion bonus is `1` for formal-friendly colors or sports-friendly colors.
1003
+
1004
+ The planner uses the top ranked colors as `color_shortlist`.
1005
+
1006
+ ### Prompt Contract
1007
+
1008
+ The NVIDIA model planner prompt requires strict JSON:
1009
+
1010
+ ```json
1011
+ {
1012
+ "target_category": "topwear|bottomwear",
1013
+ "color": "string from color_shortlist",
1014
+ "category": "string from allowed_categories post-vetting",
1015
+ "gender": "men|women|unisex",
1016
+ "style_direction": "formal-smart|business-casual|casual-polished|etc",
1017
+ "reference_item_ids": [],
1018
+ "query": "commerce-ready search string",
1019
+ "wardrobe_grounding": "specific evidence from wardrobe_snapshot",
1020
+ "reason": "concise strategic justification"
1021
+ }
1022
+ ```
1023
+
1024
+ `_recover_scraper_plan_from_text()` makes the planner robust:
1025
+
1026
+ - It first tries normal JSON parsing.
1027
+ - If parsing fails, it scans model text for allowed categories, colors, gender, and a quoted `query`.
1028
+ - If enough fields can be recovered, it builds a valid plan.
1029
+
1030
+ If the NVIDIA model planner fails and strict planner mode is disabled, `_fallback_scraper_plan()` builds a deterministic plan using:
1031
+
1032
+ - first allowed category
1033
+ - first color shortlist entry, or black
1034
+ - normalized gender
1035
+ - style direction from planning context
1036
+
1037
+ The plan source becomes `fallback`.
1038
+
1039
+ ## 22. Search URL Formation
1040
+
1041
+ After a query plan is normalized, the scraper planner creates a `ScraperRecommendation`:
1042
+
1043
+ ```python
1044
+ ScraperRecommendation(
1045
+ color=color,
1046
+ category=category,
1047
+ gender=plan_gender,
1048
+ )
1049
+ ```
1050
+
1051
+ Then it calls `_build_store_search_urls_from_query()`.
1052
+
1053
+ ### Nike URL Formation
1054
+
1055
+ Nike functions come from `scraper.py`.
1056
+
1057
+ `build_search_urls_from_query(query, store='nike', gender=None)`:
1058
+
1059
+ - If gender is provided, prefixes the query with gender if it is not already present.
1060
+ - Returns one Nike URL:
1061
+
1062
+ ```text
1063
+ https://www.nike.com/w?q={encoded_query}&vst={encoded_query}
1064
+ ```
1065
+
1066
+ - If gender is not provided, returns three URLs:
1067
+ - men + query
1068
+ - women + query
1069
+ - unmodified query
1070
+
1071
+ `build_nike_search_url(color, category, gender)` uses:
1072
+
1073
+ - `CATEGORY_ALIASES` to normalize category words.
1074
+ - query parts: gender plural, color, category.
1075
+ - `urlencode({"q": query, "vst": query})`.
1076
+
1077
+ Example:
1078
+
1079
+ ```text
1080
+ https://www.nike.com/w?q=mens+navy+shirt&vst=mens+navy+shirt
1081
+ ```
1082
+
1083
+ ### Zalando URL Formation
1084
+
1085
+ Zalando functions come from `zalando_scraper.py`.
1086
+
1087
+ `build_zalando_search_url(query, gender)`:
1088
+
1089
+ 1. Normalizes gender to `men`, `women`, or `unisex`.
1090
+ 2. Picks a path with `_pick_category_path()`.
1091
+ 3. URL-encodes `q`.
1092
+ 4. Returns:
1093
+
1094
+ ```text
1095
+ https://www.zalando.co.uk/{path}?q={encoded_query}
1096
+ ```
1097
+
1098
+ Path examples:
1099
+
1100
+ - men clothing: `mens-clothing`
1101
+ - women clothing: `womens-clothing`
1102
+ - unisex clothing: `clothing`
1103
+ - women dresses: `womens-clothing-dresses`
1104
+ - men shoes: `mens-shoes`
1105
+
1106
+ `build_zalando_search_urls_from_request()` can enrich underspecified queries, compose a final search query, and return URLs plus enrichment metadata.
1107
+
1108
+ 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.
1109
+
1110
+ ## 23. Product Scraping
1111
+
1112
+ After URLs are generated, the scraper planner loops through each URL and calls `_extract_store_product_summaries()`.
1113
+
1114
+ ### Nike Scraping
1115
+
1116
+ `extract_product_summaries()` in `scraper.py`:
1117
+
1118
+ 1. Downloads the page using `requests` with a desktop browser user agent.
1119
+ 2. Parses with BeautifulSoup and `lxml`.
1120
+ 3. Looks for `div.product-card__body`.
1121
+ 4. Inside each card, finds `a.product-card__link-overlay`.
1122
+ 5. Extracts:
1123
+ - product link
1124
+ - title
1125
+ - price
1126
+ - image URL
1127
+ 6. Deduplicates by link.
1128
+
1129
+ If Nike markup changes and no summaries are found:
1130
+
1131
+ - It falls back to `extract_product_urls()`.
1132
+ - That scans anchors with product URL patterns like `/t/`.
1133
+ - Products get `N/A` for title/price and empty image.
1134
+
1135
+ ### Zalando Scraping
1136
+
1137
+ `extract_product_summaries()` in `zalando_scraper.py`:
1138
+
1139
+ 1. Validates search URL.
1140
+ 2. Uses Apify if `use_apify=True` and `APIFY_API_TOKEN` is configured.
1141
+ 3. If Apify returns no products or errors, falls back to HTML scraping.
1142
+ 4. Optionally runs postprocess if products are missing important fields.
1143
+ 5. If both Apify and HTML fail, raises `requests.RequestException`.
1144
+
1145
+ Apify path:
1146
+
1147
+ - `_scrape_with_apify()` calls the actor endpoint.
1148
+ - It tries two payload variants:
1149
+ - `startUrls` as string array
1150
+ - `startUrls` as object array
1151
+ - It caps result count to `APIFY_MAX_RESULTS`, default `20`.
1152
+ - If sync dataset call fails or returns empty, it tries `_scrape_with_apify_run_dataset_fallback()`.
1153
+
1154
+ Apify run-dataset fallback:
1155
+
1156
+ - Starts actor run through `/acts/{actor_id}/runs`.
1157
+ - Waits for finish using `waitForFinish`.
1158
+ - Reads the actor default dataset.
1159
+ - Normalizes the dataset items.
1160
+
1161
+ HTML fallback:
1162
+
1163
+ - Requests the Zalando search page.
1164
+ - Selects product-like cards using `article`, `div[data-testid*="product"]`, and `li[data-testid*="product"]`.
1165
+ - Finds product links with `/p/`.
1166
+ - Extracts name, price, image, and link.
1167
+
1168
+ Zalando normalization handles different Apify payload shapes:
1169
+
1170
+ - `name`, `title`, `productName`, `product_name`
1171
+ - `price`, `currentPrice`, `displayPrice`, `priceLabel`
1172
+ - `promotionalPrice`, `originalPrice`, `discountPercent`
1173
+ - `brand`, `brandName`
1174
+ - `image`, `imageUrl`, `image_url`, `thumbnail`
1175
+ - `url`, `productUrl`, `item_link`, `link`
1176
+
1177
+ ## 24. Product Relevance Filtering
1178
+
1179
+ After scraping, the scraper planner filters products with `_is_relevant_scraped_product()`.
1180
+
1181
+ It rejects products when:
1182
+
1183
+ - Product text is empty.
1184
+ - Product text contains excluded categories such as socks, underwear, swimwear, belts, hats, wallets, bags, watches, shoes, sneakers, boots, sandals, or slippers.
1185
+ - Product text does not contain planned category keywords.
1186
+ - Target is topwear but no topwear terms appear.
1187
+ - Target is bottomwear but no bottomwear terms appear.
1188
+ - Occasion is formal and product text contains blocked casual/sport terms.
1189
+
1190
+ If strict filtering removes everything but there are unfiltered products available, backend uses `scrape_fallback`:
1191
+
1192
+ - It returns the first unfiltered products up to the scrape limit.
1193
+ - It records an intermediate step explaining that strict relevance filtering returned no products.
1194
+
1195
+ ## 25. Product to Wardrobe Matching
1196
+
1197
+ Each accepted product is enriched by `_enrich_scraper_products_with_matches()`.
1198
+
1199
+ For each product, `_build_product_match_context()`:
1200
+
1201
+ 1. Determines product slot:
1202
+ - target topwear -> product is topwear.
1203
+ - target bottomwear -> product is bottomwear.
1204
+ 2. Determines the complementary wardrobe slot:
1205
+ - topwear product matches existing bottomwear.
1206
+ - bottomwear product matches existing topwear.
1207
+ 3. Builds a product stub using:
1208
+ - planned category
1209
+ - planned color
1210
+ - planned style direction
1211
+ - occasion bucket
1212
+ 4. Scores that stub against each complementary wardrobe item using `score_pair_full()`.
1213
+ 5. Sorts matches by score.
1214
+ 6. Adds top 3 matched garments.
1215
+ 7. Creates a reason that explicitly says it only evaluates against the complementary slot.
1216
+
1217
+ Returned product enrichment:
1218
+
1219
+ - `reason`
1220
+ - `match_score`
1221
+ - `matched_with_slot`
1222
+ - `matched_garments`
1223
+
1224
+ This prevents bad explanations like matching a recommended shirt against existing shirts.
1225
+
1226
+ ## 26. Suggestions Endpoint
1227
+
1228
+ `POST /suggestions` and `POST /api/suggestions` wrap the scraper recommendation flow for the older suggestions UI/API shape.
1229
+
1230
+ Input:
1231
+
1232
+ - `occasion`
1233
+ - `target_category`
1234
+ - `gender_preference`
1235
+ - `filters`
1236
+ - `max_results`
1237
+ - `store`
1238
+
1239
+ It calls `_build_shopping_suggestions_from_scraper()`:
1240
+
1241
+ 1. Converts filters into a `preferences` string.
1242
+ 2. Calls the scraper planner function.
1243
+ 3. Maps products into `suggestions`.
1244
+ 4. Assigns a basic match score: `max(65, 95 - index * 4)`.
1245
+ 5. Includes product URL, image, reason, category, color, query, scrape status, gender, and matched garments.
1246
+
1247
+ The frontend `getSuggestions()` has additional fallback behavior:
1248
+
1249
+ - It times out after `VITE_SCRAPER_TIMEOUT_MS`, default `20000`.
1250
+ - It throttles requests by `VITE_SCRAPER_MIN_INTERVAL_MS`, default `1200`.
1251
+ - It logs scrape events.
1252
+ - It builds fallback suggestions if live scraping times out, fails, returns malformed data, or returns too few results.
1253
+
1254
+ ## 27. Frontend Shopping Fallbacks
1255
+
1256
+ Fallback suggestion logic lives in `src/api/client.ts`.
1257
+
1258
+ If live scraper data fails, frontend creates suggestions from templates and provider search URLs.
1259
+
1260
+ Provider fallback behavior:
1261
+
1262
+ - `buildFallbackSuggestions()` produces ranked fallback cards using wardrobe profile and request filters.
1263
+ - `createProviderFallbackSuggestions()` appends external provider search links when live results are below `SCRAPER_MIN_RESULTS`, default `4`.
1264
+ - Fallback URLs often point to Google searches, for example:
1265
+
1266
+ ```text
1267
+ https://www.google.com/search?q={encoded_search_query}
1268
+ ```
1269
+
1270
+ The frontend labels these with:
1271
+
1272
+ - `scrape_status: "fallback"`
1273
+ - `scrape_error` explaining why fallback was used.
1274
+
1275
+ Live suggestions get a small ranking boost over fallback suggestions.
1276
+
1277
+ Filtering and ranking:
1278
+
1279
+ - `computeFilterBoost()` gives boosts for preferred colors, patterns, fabrics, fits, styles, seasons, and include keywords.
1280
+ - Exclude keywords reject a candidate.
1281
+ - Gender filtering removes mismatched products unless gender is `any` or `unisex`.
1282
+ - Final ranking uses match score + filter boost + live boost.
1283
+
1284
+ ## 28. Image Proxy
1285
+
1286
+ `GET /image-proxy?url=...` fetches a remote image through the backend.
1287
+
1288
+ It:
1289
+
1290
+ - Accepts only HTTP(S).
1291
+ - Uses browser-like headers.
1292
+ - Returns the original content type or `image/jpeg`.
1293
+ - Adds `Cache-Control: public, max-age=3600`.
1294
+
1295
+ This is useful when browser CORS or hotlinking blocks direct product image rendering.
1296
+
1297
+ ## 29. Health Endpoint
1298
+
1299
+ `GET /health` returns:
1300
+
1301
+ - `status`
1302
+ - `classification_provider`
1303
+ - `model`
1304
+ - `nvidia_api_configured`
1305
+ - `nvidia_invoke_url`
1306
+ - `engine_version`
1307
+ - `outfit_matching_provider`
1308
+
1309
+ 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.
1310
+
1311
+ ## 30. Supabase Edge Functions
1312
+
1313
+ The repo still contains Supabase Edge Functions:
1314
+
1315
+ - `supabase/functions/wardrobe/index.ts`
1316
+ - `supabase/functions/matching/index.ts`
1317
+ - `supabase/functions/recommendations/index.ts`
1318
+ - shared scoring in `supabase/functions/_shared/scoring.ts`
1319
+
1320
+ These functions implement an earlier/alternate backend path:
1321
+
1322
+ - Authenticated wardrobe CRUD.
1323
+ - Mock image classification and embedding generation.
1324
+ - Rule-based matching with an AI placeholder.
1325
+ - Occasion recommendation with outfit history writes.
1326
+
1327
+ 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.
1328
+
1329
+ ## 31. Environment Variables
1330
+
1331
+ Important FastAPI variables:
1332
+
1333
+ - `DB_PATH`: override SQLite file path.
1334
+ - `NVIDIA_API_KEY`: required for image classification, NVIDIA model planning, and AI grid scoring.
1335
+ - `NVIDIA_INVOKE_URL`: NVIDIA-compatible chat completions endpoint.
1336
+ - `NVIDIA_MODEL_ID`: primary vision/text model.
1337
+ - `NVIDIA_FALLBACK_MODEL_IDS`: comma-separated fallback model IDs.
1338
+ - `NVIDIA_MAX_TOKENS`
1339
+ - `NVIDIA_REASONING_MAX_TOKENS`
1340
+ - `NVIDIA_TEMPERATURE`
1341
+ - `NVIDIA_TOP_P`
1342
+ - `NVIDIA_TIMEOUT_SECONDS`
1343
+ - `NVIDIA_MAX_RETRIES`
1344
+ - `NVIDIA_RETRY_BACKOFF_SECONDS`
1345
+ - `NVIDIA_ENABLE_THINKING`
1346
+ - `NVIDIA_IMAGE_MAX_DIM`
1347
+ - NVIDIA planner model ID: shopping planner model setting used by the backend.
1348
+ - NVIDIA planner max tokens: planner token budget used by the backend.
1349
+ - `SCRAPER_DEFAULT_STORE`: default `nike`.
1350
+ - `SCRAPER_QUERY_CACHE_TTL_SECONDS`: default 15 days.
1351
+ - `MATCHING_RESULT_CACHE_MAX`: default `500`.
1352
+ - `MATCHING_RESULT_CACHE_TTL_SECONDS`: default 1 day.
1353
+ - `APIFY_API_TOKEN`: enables Zalando Apify scraping.
1354
+ - `APIFY_ACTOR_ENDPOINT`
1355
+ - `APIFY_MIN_TIMEOUT_SECONDS`
1356
+ - `APIFY_WAIT_FOR_FINISH_SECONDS`
1357
+ - `ZALANDO_HTML_TIMEOUT_SECONDS`
1358
+ - `FASHION_ENCODER_MODEL_ID`
1359
+ - `FASHION_EMBEDDING_DIM`
1360
+ - `FASHION_IMAGE_TIMEOUT_SECONDS`
1361
+ - `FASHION_EMBEDDING_CACHE_SIZE`
1362
+ - `FASHION_RANKER_CHECKPOINT`
1363
+ - `FASHION_CLASSIFIER_NVIDIA_MODEL`
1364
+ - `FASHION_CLASSIFIER_HF_MODEL`
1365
+ - `FASHION_CLASSIFIER_CACHE_SIZE`
1366
+ - `OUTFIT_GRID_CELL_SIZE`
1367
+ - `OUTFIT_GRID_LABEL_HEIGHT`
1368
+ - `OUTFIT_GRID_PADDING`
1369
+ - `OUTFIT_GRID_FETCH_TIMEOUT_SECONDS`
1370
+ - `OUTFIT_GRID_SESSION_TTL_SECONDS`
1371
+ - `OUTFIT_GRID_SESSION_DIR`
1372
+ - `OUTFIT_GRID_MAX_TOP_ITEMS`
1373
+ - `OUTFIT_GRID_MAX_BOTTOM_ITEMS`
1374
+ - `OUTFIT_ANCHOR_MIN_SCORE`
1375
+ - `OUTFIT_TEXT_PRESELECT_ENABLED`
1376
+ - `OUTFIT_TEXT_SELECTOR_MAX_TOKENS`
1377
+ - `OUTFIT_AI_MAX_TOKENS`
1378
+
1379
+ Important frontend variables:
1380
+
1381
+ - `VITE_API_MODE`
1382
+ - `VITE_API_BASE_URL`
1383
+ - `VITE_FLASK_API_URL`
1384
+ - `VITE_SUPABASE_URL`
1385
+ - `VITE_SUPABASE_ANON_KEY`
1386
+ - `VITE_SUPABASE_STORAGE_BUCKET`
1387
+ - `VITE_SCRAPER_TIMEOUT_MS`
1388
+ - `VITE_SCRAPER_MIN_INTERVAL_MS`
1389
+ - `VITE_SCRAPER_MIN_RESULTS`
1390
+
1391
+ ## 32. End-to-End Feature Flow Summary
1392
+
1393
+ ### Add Wardrobe Item
1394
+
1395
+ ```text
1396
+ UploadModal
1397
+ -> classifyImage()
1398
+ -> FastAPI POST /classify
1399
+ -> NVIDIA vision classification
1400
+ -> frontend editable classification form
1401
+ -> uploadClothingItem()
1402
+ -> Supabase Storage + garment_items
1403
+ -> wardrobe store updates
1404
+ -> matching cache prefetch may run
1405
+ ```
1406
+
1407
+ ### Find Outfits
1408
+
1409
+ ```text
1410
+ Matching page
1411
+ -> outfitStore.findOutfits()
1412
+ -> frontend cache check
1413
+ -> fetchWardrobeOutfits()
1414
+ -> FastAPI POST /ai/recommend-outfits
1415
+ -> backend cache check
1416
+ -> AI grid scoring
1417
+ -> fashion_ai fallback
1418
+ -> scoring.py fallback
1419
+ -> frontend response normalizer
1420
+ -> OutfitCard rendering
1421
+ ```
1422
+
1423
+ ### Score One Outfit
1424
+
1425
+ ```text
1426
+ POST /ai/score-outfit
1427
+ -> normalize top/bottom/other
1428
+ -> AI grid scoring
1429
+ -> fashion_ai score_outfit fallback
1430
+ -> scoring.py fallback
1431
+ -> score/breakdown/reason/tip response
1432
+ ```
1433
+
1434
+ ### Generate Shopping Suggestions
1435
+
1436
+ ```text
1437
+ Suggestions page
1438
+ -> getGemmaScraperRecommendations()
1439
+ -> backend health check
1440
+ -> FastAPI POST /scraper/recommend
1441
+ -> prompt intent inference
1442
+ -> wardrobe snapshot and planning context
1443
+ -> NVIDIA model query planner
1444
+ -> deterministic planner fallback if needed
1445
+ -> Nike/Zalando URL generation
1446
+ -> product scraping
1447
+ -> strict relevance filtering
1448
+ -> scrape fallback if strict filter removed everything
1449
+ -> product-to-wardrobe match enrichment
1450
+ -> saved JSON payload
1451
+ -> product cards in frontend
1452
+ ```
1453
+
1454
+ ## 33. Known Implementation Nuances
1455
+
1456
+ - `getGemmaScraperRecommendations()` is named after Gemma, but the current backend route uses the configured NVIDIA model through the NVIDIA-compatible inference path.
1457
+ - The existing `StyleWellBackend/README.md` mentions Hugging Face primary and NVIDIA fallback, but the current `app.py` path relies heavily on NVIDIA-compatible chat completions for classification, NVIDIA model planning, and AI grid scoring.
1458
+ - `/scraper/recommend` builds 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.
1459
+ - `/upload` in FastAPI stores `memory://` image URLs, so it is useful for backend-local tests but not equivalent to Supabase image persistence.
1460
+ - The Supabase Edge Functions contain useful earlier scoring and CRUD logic, but the main frontend path now uses Supabase client APIs plus FastAPI.
1461
+ - 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.
1462
+
1463
+ ## 34. Local Run
1464
+
1465
+ From `StyleWellBackend/`:
1466
+
1467
+ ```bash
1468
+ pip install -r requirements.txt
1469
+ python app.py
1470
+ ```
1471
+
1472
+ The backend starts on:
1473
+
1474
+ ```text
1475
+ http://0.0.0.0:7860
1476
+ ```
1477
+
1478
+ Health check:
1479
+
1480
+ ```bash
1481
+ curl http://127.0.0.1:7860/health
1482
+ ```
1483
+
1484
+ Classification:
1485
+
1486
+ ```bash
1487
+ curl -X POST http://127.0.0.1:7860/classify -F "image=@/path/to/garment.jpg"
1488
+ ```
1489
+
1490
+ Outfit recommendation:
1491
+
1492
+ ```bash
1493
+ curl -X POST http://127.0.0.1:7860/ai/recommend-outfits \
1494
+ -H "Content-Type: application/json" \
1495
+ -d '{"occasion":"casual","wardrobe_items":[]}'
1496
+ ```
1497
+
1498
+ Scraper recommendation:
1499
+
1500
+ ```bash
1501
+ curl -X POST http://127.0.0.1:7860/scraper/recommend \
1502
+ -H "Content-Type: application/json" \
1503
+ -d '{"user_prompt":"Need a formal navy shirt","gender":"men","store":"nike","max_products":6}'
1504
+ ```