Spaces:
Sleeping
Sleeping
|
Download LLD.md from jashp2323/visual-search: direct link, hf CLI and curl.
- Browser
- Download file 5.85 kB
-
https://huggingface.co/spaces/jashp2323/visual-search/resolve/main/LLD.md
- Command line
-
hf download hf://spaces/jashp2323/visual-search/LLD.md
-
curl -L -o LLD.md https://huggingface.co/spaces/jashp2323/visual-search/resolve/main/LLD.md
5.85 kB
LLD: AURA // Neural Visual Search System
This Low-Level Design (LLD) document outlines the architecture, data models, exact match detection algorithms, API specifications, and frontend component layout of the AURA Neural Visual Search System.
1. Architectural Overview
The system is split into a FastAPI backend running a local CLIP-based embedding engine and a Vanilla HTML/CSS/JS frontend featuring a glassmorphic user interface.
graph TD
UI[Frontend Client] -->|HTTP POST Image/Text| API[FastAPI App]
API -->|Raw Text/Image| CLIP[CLIP Embedder]
CLIP -->|512-dim Normalized Embedding| API
API -->|Vector + Query Params| DB[In-Memory Vector Store]
DB -->|Matches & Scores| API
API -->|Classified Results exact_match + related| UI
2. Component Specifications
2.1 Backend Modules
models.py // CLIPEmbedder
- Model:
openai/clip-vit-base-patch32 - Execution Target: Auto-detects
cuda(if available), falls back tocpu. - Image Embeddings: Normalizes and runs image pixel values through the CLIP Vision Transformer.
- Text Embeddings: Standardizes input strings, applies sub-word tokenization, and computes CLIP Text Transformer embeddings.
- Normalizing Vector Output: Normalizes outputs to unit length ($L_2$ norm = 1.0) so that dot-product searches compute exact Cosine Similarity: $$\text{Cosine Similarity} = \vec{A} \cdot \vec{B}$$
vector_store.py // InMemoryVectorEngine & VectorStore
- Data Catalog: Serialized as JSON list (
catalog.json) containing metadata:id(e.g.prod-15970)titledescpricecategoryimage_url
- Embeddings Store: Serialized as a
.npyNumPy array (embeddings.npy) of dimensions $(N, 512)$. - Search Execution:
- Computes the dot product of the search vector with all stored product vectors.
- Sorts indices in descending order.
- Returns top $K$ results containing metadata and corresponding cosine similarity score.
3. Exact Product Match Classifier Logic
The search classifier differentiates between an Exact Match and Related Recommendations.
flowchart TD
Start([Receive Query]) --> QueryType{Query Type?}
QueryType -->|Text Query: q| TextExact{Exact Title/ID Match?}
TextExact -->|Yes| SetExactText[exact_match = Product <br> score = Vector Score / 1.0]
TextExact -->|No| SetExactNull[exact_match = null]
QueryType -->|Image Upload| VectorSearch[Perform Vector Search]
VectorSearch --> ImageScore{Top Similarity >= 0.92?}
ImageScore -->|Yes| SetExactImage[exact_match = Top Product <br> score = Similarity Score]
ImageScore -->|No| SetExactNull
SetExactText --> FilterRelated[Filter exact_match ID from related_products]
SetExactImage --> FilterRelated
SetExactNull --> FilterRelated
FilterRelated --> ReturnJSON([Return exact_match & related_products])
3.1 Text Search Exact Matching
- The search query
qis stripped of leading/trailing spaces and lowercased. - The database is checked for a product where:
q_clean == product["title"].strip().lower()orq_clean == product["id"].strip().lower(). - If matched, it is returned as
exact_match. If it is present in the vector search results, its calculated score is returned; otherwise, it defaults to a score of1.0.
3.2 Visual Search Exact Matching
- The upload image is processed, and its unit-normalized embedding is calculated.
- The vector index retrieves the top results.
- If the highest cosine similarity score is $\ge 0.92$ (calibrated for matching identical or slightly transformed images), the top match is classified as the
exact_match. - Otherwise, no exact match is returned.
3.3 Related Products Resolution
- Vector search is executed with $K = 7$ to find similar items.
- If an
exact_matchwas resolved (via text or image check), its corresponding ID is filtered out of the results array. - The remaining array is sliced to return the top 6
related_products.
4. API Endpoints
4.1 Search Endpoint: /api/search
- Method:
POST - Content-Type:
multipart/form-data - Parameters:
q(string, optional)file(binary stream, optional)
- Response Format (
application/json):{ "exact_match": { "product": { "id": "prod-15970", "title": "Turtle Check Men Navy Blue Shirt", "desc": "Navy Blue Shirts for Men. Designed for casual wear.", "price": 64.0, "category": "Apparel", "image_url": "/static/images/prod-15970.png" }, "score": 0.9998 }, "related_products": [ { "product": { ... }, "score": 0.8431 } ] }
4.2 Similarity Endpoint: /api/search/similar
- Method:
POST - Content-Type:
application/json - Payload:
{ "product_id": "prod-15970" } - Response: Array of similar products, excluding the queried product itself.
5. Frontend Layout & CSS Styling
The page layout consists of a responsive container structured with CSS grids and glassmorphism panels.
5.1 Results Section Layout
- Exact Match Area (
#exact-match-section):- Border: Dashed accent border with soft glow (
hsl(263, 70%, 50%)). - Active State: Shows up to 1 card representing the exact match (enhanced with green
Exact Matchbadge). - Empty State: Renders a premium banner (🕵️♂️ No matching products found) informing the user that no exact match is available in the database.
- Border: Dashed accent border with soft glow (
- Related Recommendations Grid (
#related-products-grid):- CSS Grid structure:
repeat(auto-fill, minmax(260px, 1fr)). - Header: Related Products You Might Like.
- Renders the remaining 6 visual matches.
- CSS Grid structure: