abdurrehmansaeed commited on
Commit
4031985
·
verified ·
1 Parent(s): f2deca3

Upload 11 files

Browse files
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ FROM python:3.10-slim
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+ EXPOSE 7860
10
+ WORKDIR /app/api
11
+
12
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,10 +1,40 @@
1
- ---
2
- title: ThreadNode
3
- emoji: 📊
4
- colorFrom: green
5
- colorTo: gray
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ThreadNode: Visual Inference Engine
2
+
3
+ **Live Deployment:** [Hugging Face Space](https://huggingface.co/spaces/abdurrehmansaeed/ThreadNode)
4
+ **Source Code:** [GitHub Repository](https://github.com/abdur-rehmansaeed/ThreadNode)
5
+
6
+ ThreadNode is a production-ready, hierarchical multi-label computer vision pipeline designed to automate product categorization for e-commerce platforms.
7
+
8
+ ## The Vision: E-Commerce Automation
9
+ The primary motivation behind ThreadNode is not simply to build a standalone classifier, but to solve a massive operational bottleneck in retail technology. Currently, many e-commerce platforms rely on manual data entry to categorize thousands of new product listings.
10
+
11
+ ThreadNode demonstrates an automated pipeline where raw product imagery is instantly parsed into a highly accurate semantic taxonomy (e.g., `Apparel > Topwear > Tshirts`). By integrating this visual inference engine, platforms can automatically route inventory into the correct catalog architecture, entirely bypassing manual human classification.
12
+
13
+ ## Dataset & Taxonomy
14
+ This engine was trained using the [Fashion Product Images Dataset](https://www.kaggle.com/datasets/paramaggarwal/fashion-product-images-dataset), which provides a robust corpus of high-resolution e-commerce imagery paired with rich product metadata.
15
+
16
+ To create the taxonomy engine, the raw tabular metadata was parsed to extract a hierarchical relationship (e.g., `MasterCategory > SubCategory > ArticleType`). This text data was then vectorized into a 132-node MultiLabelBinarizer matrix to serve as the ground truth for the neural network's classification head.
17
+ ## System Architecture
18
+ This project utilizes a decoupled microservice architecture:
19
+ * **The Brain (PyTorch):** An EfficientNet-B0 CNN, optimized with transfer learning, featuring a custom 132-node classification head.
20
+ * **The Engine (FastAPI):** An asynchronous Python backend that holds the model state in memory and executes inference via a RESTful `/predict` endpoint.
21
+ * **The Client (Vanilla JS/Tailwind):** A lightweight, zero-dependency diagnostic dashboard for real-time visual inference.
22
+
23
+ ## Engineering Solutions
24
+
25
+ ### 1. Solving the "Long-Tail" Class Imbalance
26
+ The original dataset exhibited a severe 1:1000 class imbalance (e.g., thousands of "Shirts" but only a few "Shrugs"). A standard binary cross-entropy (BCE) loss function resulted in a model that simply ignored minority classes (Macro F1: ~0.05).
27
+
28
+ **The Fix:** I engineered a dynamically calculated `pos_weight` tensor and injected it directly into the `BCEWithLogitsLoss` function. This mathematically penalized the network heavily for missing rare items, forcing it to learn minority visual features and drastically improving the Macro F1 score across all 132 classes.
29
+
30
+ ### 2. The API Confidence Threshold
31
+ To prevent the model from hallucinating "safe" guesses, business logic was separated from the neural network. The FastAPI endpoint enforces a strict 85% confidence threshold.
32
+ * **Micro Precision:** Increased from 35.5% to 61.5%.
33
+ * **Micro Recall:** Maintained at an excellent 93.5%.
34
+ * **Micro F1-Score:** Stabilized at a highly functional 0.7422.
35
+
36
+ ## Usage & Deployment
37
+
38
+ ThreadNode is fully containerized via Docker and is deployed live on Hugging Face Spaces for public access and testing.
39
+
40
+ **Try the live application here:** [ThreadNode on Hugging Face](https://huggingface.co/spaces/abdurrehmansaeed/ThreadNode)
api/main.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.responses import JSONResponse
4
+ from fastapi.staticfiles import StaticFiles
5
+ import torch
6
+ import torch.nn as nn
7
+ from torchvision import models, transforms
8
+ from PIL import Image
9
+ import pickle
10
+ import io
11
+
12
+ # --- 1. CONFIGURATION ---
13
+ MODEL_PATH = "../models/fashion_hierarchy_model_v3.pth"
14
+ VOCAB_PATH = "../data/mlb_vocab.pkl"
15
+ THRESHOLD = 0.85
16
+
17
+ # --- 2. INITIALIZE FASTAPI ---
18
+ app = FastAPI(title="Apparel Taxonomy API", version="1.0")
19
+
20
+ app.add_middleware(
21
+ CORSMiddleware,
22
+ allow_origins=["*"],
23
+ allow_credentials=True,
24
+ allow_methods=["*"],
25
+ allow_headers=["*"],
26
+ )
27
+
28
+ # --- 3. HARDWARE & TRANSFORMS ---
29
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
30
+ print(f"Server spinning up. Computation routed to: {device.type.upper()}")
31
+
32
+ transform = transforms.Compose([
33
+ transforms.Resize((224, 224)),
34
+ transforms.ToTensor(),
35
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
36
+ ])
37
+
38
+ # --- 4. LOAD STATE INTO MEMORY ---
39
+ try:
40
+ with open(VOCAB_PATH, 'rb') as f:
41
+ mlb = pickle.load(f)
42
+ num_classes = len(mlb.classes_)
43
+
44
+ model = models.efficientnet_b0(weights=None)
45
+ in_features = model.classifier[1].in_features
46
+ model.classifier[1] = nn.Linear(in_features, num_classes)
47
+
48
+ model.load_state_dict(torch.load(MODEL_PATH, map_location=device))
49
+ model.to(device)
50
+ model.eval()
51
+ print("V3 Engine successfully loaded and locked into memory.")
52
+
53
+ except Exception as e:
54
+ print(f"CRITICAL BOOT ERROR: {e}")
55
+
56
+ # --- 5. THE INFERENCE ROUTE ---
57
+ @app.post("/predict")
58
+ async def predict_apparel(file: UploadFile = File(...)):
59
+ if not file.content_type.startswith("image/"):
60
+ raise HTTPException(status_code=400, detail="Invalid file type. Please upload an image.")
61
+
62
+ try:
63
+ image_bytes = await file.read()
64
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
65
+
66
+ input_tensor = transform(image).unsqueeze(0).to(device)
67
+
68
+ with torch.no_grad():
69
+ outputs = model(input_tensor)
70
+ probs = torch.sigmoid(outputs)
71
+ preds = (probs > THRESHOLD).float().cpu().numpy()
72
+
73
+ predicted_tags = mlb.inverse_transform(preds)[0]
74
+
75
+ return JSONResponse(content={
76
+ "status": "success",
77
+ "threshold_applied": THRESHOLD,
78
+ "tags": list(predicted_tags)
79
+ })
80
+
81
+ except Exception as e:
82
+ raise HTTPException(status_code=500, detail=f"Inference Engine Error: {str(e)}")
83
+
84
+ # --- 6. SERVE THE UI ---
85
+ app.mount("/", StaticFiles(directory="../frontend", html=True), name="frontend")
data/mlb_vocab.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d8d95d7346f3455a4405eac6e41b9e407095d264621bcdec267e094ba1532e74
3
+ size 1905
frontend/index.html ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>ThreadNode</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <style>
9
+ /* Playful Floating Animations */
10
+ @keyframes float {
11
+ 0% { transform: translateY(0px); }
12
+ 50% { transform: translateY(-10px); }
13
+ 100% { transform: translateY(0px); }
14
+ }
15
+ .animate-float {
16
+ animation: float 6s ease-in-out infinite;
17
+ }
18
+ .animate-in { animation: animate-in 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275); }
19
+ .fade-in { opacity: 0; animation-fill-mode: forwards; }
20
+ @keyframes animate-in {
21
+ from { opacity: 0; transform: scale(0.95) translateY(10px); }
22
+ to { opacity: 1; transform: scale(1) translateY(0); }
23
+ }
24
+
25
+ /* Custom scrollbar for dark mode */
26
+ ::-webkit-scrollbar { width: 8px; }
27
+ ::-webkit-scrollbar-track { background: transparent; }
28
+ ::-webkit-scrollbar-thumb { background: #4c1d95; border-radius: 10px; }
29
+ </style>
30
+ </head>
31
+ <body class="min-h-screen bg-slate-950 text-slate-200 font-sans flex flex-col items-center justify-center p-6 relative overflow-hidden selection:bg-fuchsia-500/30">
32
+
33
+ <div class="absolute top-[-10%] left-[-10%] w-[500px] h-[500px] bg-violet-600/20 rounded-full mix-blend-screen filter blur-[120px] animate-float" style="animation-delay: 0s;"></div>
34
+ <div class="absolute bottom-[-10%] right-[-10%] w-[500px] h-[500px] bg-fuchsia-600/20 rounded-full mix-blend-screen filter blur-[120px] animate-float" style="animation-delay: 3s;"></div>
35
+
36
+ <div class="max-w-4xl w-full mb-8 relative z-10 text-center md:text-left">
37
+ <h1 class="text-4xl font-extrabold tracking-tight text-white mb-1">
38
+ Thread<span class="text-transparent bg-clip-text bg-gradient-to-r from-violet-400 to-fuchsia-400">Node</span>
39
+ </h1>
40
+ <p class="text-slate-400 font-medium text-sm">Visual Inference Engine</p>
41
+ </div>
42
+
43
+ <div class="max-w-4xl w-full grid grid-cols-1 md:grid-cols-2 gap-8 relative z-10">
44
+
45
+ <div id="drop-zone" class="bg-slate-900 rounded-[2rem] shadow-2xl shadow-violet-900/10 border border-slate-800 p-8 transition-all duration-300 min-h-[420px] flex flex-col items-center justify-center relative hover:shadow-violet-500/10 hover:border-violet-500/30 hover:-translate-y-1">
46
+
47
+ <div id="upload-prompt" class="text-center w-full cursor-pointer group" onclick="document.getElementById('file-input').click()">
48
+ <input type="file" id="file-input" accept="image/*" class="hidden" />
49
+ <div class="w-16 h-16 rounded-full bg-slate-800 text-violet-400 flex items-center justify-center mx-auto mb-4 group-hover:bg-slate-700 group-hover:scale-110 transition-all duration-300 shadow-sm border border-slate-700 group-hover:border-violet-500/50">
50
+ <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"></path></svg>
51
+ </div>
52
+ <p class="text-slate-300 font-semibold mb-1">Upload Target Image</p>
53
+ <p class="text-slate-500 text-sm">Drag & drop or browse</p>
54
+ </div>
55
+
56
+ <div id="preview-container" class="hidden relative w-full h-full flex flex-col items-center justify-between">
57
+ <div class="flex-1 flex items-center justify-center mb-6 w-full relative">
58
+ <img id="image-preview" class="max-h-[280px] object-contain rounded-2xl shadow-xl border border-slate-700 bg-slate-800/50 p-1" />
59
+ </div>
60
+ <button id="execute-btn" class="w-full py-4 px-6 bg-gradient-to-r from-violet-600 to-fuchsia-600 text-white font-bold rounded-full hover:shadow-[0_0_20px_rgba(217,70,239,0.4)] hover:-translate-y-0.5 active:translate-y-0 transition-all duration-200 text-lg">
61
+ Classify Image
62
+ </button>
63
+ </div>
64
+ </div>
65
+
66
+ <div class="flex flex-col bg-slate-900 rounded-[2rem] shadow-2xl shadow-violet-900/10 border border-slate-800 p-8 min-h-[420px]">
67
+
68
+ <div class="flex items-center gap-3 mb-6 pb-4 border-b border-slate-800">
69
+ <div class="p-2 bg-fuchsia-500/10 rounded-xl text-fuchsia-400 border border-fuchsia-500/20">
70
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path></svg>
71
+ </div>
72
+ <h2 class="text-sm font-bold tracking-widest text-slate-300 uppercase">
73
+ Telemetry Stream
74
+ </h2>
75
+ </div>
76
+
77
+ <div id="error-message" class="hidden bg-rose-950/50 text-rose-400 border border-rose-900/50 p-4 rounded-2xl text-sm mb-4 font-medium"></div>
78
+
79
+ <div id="idle-state" class="flex-1 flex flex-col items-center justify-center text-slate-600 text-sm font-medium">
80
+ <svg class="w-12 h-12 text-slate-700 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"></path></svg>
81
+ Awaiting Payload...
82
+ </div>
83
+
84
+ <div id="loading-state" class="hidden flex-1 flex flex-col items-center justify-center space-y-4">
85
+ <div class="flex gap-2">
86
+ <div class="w-3 h-3 bg-violet-500 rounded-full animate-bounce shadow-[0_0_10px_rgba(139,92,246,0.8)]" style="animation-delay: 0s"></div>
87
+ <div class="w-3 h-3 bg-fuchsia-500 rounded-full animate-bounce shadow-[0_0_10px_rgba(217,70,239,0.8)]" style="animation-delay: 0.1s"></div>
88
+ <div class="w-3 h-3 bg-pink-500 rounded-full animate-bounce shadow-[0_0_10px_rgba(236,72,153,0.8)]" style="animation-delay: 0.2s"></div>
89
+ </div>
90
+ <p class="text-slate-400 font-medium text-sm animate-pulse">Analyzing visual features...</p>
91
+ </div>
92
+
93
+ <div id="results-state" class="hidden flex-1 flex flex-col space-y-8 animate-in fade-in">
94
+
95
+ <div class="flex-1 flex flex-col justify-center">
96
+ <p class="text-xs text-slate-500 font-bold uppercase tracking-widest mb-4">Extracted Taxonomy</p>
97
+ <div id="tags-container" class="flex flex-wrap items-center gap-2.5">
98
+ </div>
99
+ </div>
100
+
101
+ <div class="pt-6 border-t border-slate-800 bg-slate-950/50 -mx-8 -mb-8 p-8 rounded-b-[2rem] flex justify-end items-center">
102
+ <div class="text-right">
103
+ <p class="text-[10px] text-slate-500 font-bold uppercase tracking-widest mb-1">Status</p>
104
+ <p class="font-bold text-sm uppercase flex items-center gap-2 justify-end text-slate-200">
105
+ <span class="w-2.5 h-2.5 rounded-full bg-emerald-400 shadow-[0_0_12px_rgba(52,211,153,0.6)]"></span>
106
+ <span id="status-text"></span>
107
+ </p>
108
+ </div>
109
+ </div>
110
+ </div>
111
+ </div>
112
+ </div>
113
+
114
+ <script>
115
+ const dropZone = document.getElementById('drop-zone');
116
+ const fileInput = document.getElementById('file-input');
117
+ const uploadPrompt = document.getElementById('upload-prompt');
118
+ const previewContainer = document.getElementById('preview-container');
119
+ const imagePreview = document.getElementById('image-preview');
120
+ const executeBtn = document.getElementById('execute-btn');
121
+
122
+ const idleState = document.getElementById('idle-state');
123
+ const loadingState = document.getElementById('loading-state');
124
+ const resultsState = document.getElementById('results-state');
125
+ const errorMessage = document.getElementById('error-message');
126
+ const tagsContainer = document.getElementById('tags-container');
127
+
128
+ let selectedFile = null;
129
+
130
+ function resetUI() {
131
+ idleState.classList.add('hidden');
132
+ loadingState.classList.add('hidden');
133
+ resultsState.classList.add('hidden');
134
+ errorMessage.classList.add('hidden');
135
+ }
136
+
137
+ function handleFile(file) {
138
+ if (!file || !file.type.startsWith('image/')) {
139
+ resetUI();
140
+ errorMessage.textContent = 'Oops! Please upload an image file.';
141
+ errorMessage.classList.remove('hidden');
142
+ return;
143
+ }
144
+ selectedFile = file;
145
+ const reader = new FileReader();
146
+ reader.onload = (e) => {
147
+ imagePreview.src = e.target.result;
148
+ uploadPrompt.classList.add('hidden');
149
+ previewContainer.classList.remove('hidden');
150
+ resetUI();
151
+ idleState.classList.remove('hidden');
152
+ };
153
+ reader.readAsDataURL(file);
154
+ }
155
+
156
+ fileInput.addEventListener('change', (e) => handleFile(e.target.files[0]));
157
+
158
+ dropZone.addEventListener('dragover', (e) => {
159
+ e.preventDefault();
160
+ dropZone.classList.add('border-violet-500/50', 'bg-slate-800');
161
+ });
162
+
163
+ dropZone.addEventListener('dragleave', () => {
164
+ dropZone.classList.remove('border-violet-500/50', 'bg-slate-800');
165
+ });
166
+
167
+ dropZone.addEventListener('drop', (e) => {
168
+ e.preventDefault();
169
+ dropZone.classList.remove('border-violet-500/50', 'bg-slate-800');
170
+ handleFile(e.dataTransfer.files[0]);
171
+ });
172
+
173
+ executeBtn.addEventListener('click', async () => {
174
+ if (!selectedFile) return;
175
+
176
+ resetUI();
177
+ loadingState.classList.remove('hidden');
178
+ executeBtn.disabled = true;
179
+ executeBtn.textContent = 'Analyzing...';
180
+ executeBtn.classList.add('opacity-70', 'cursor-not-allowed', 'scale-[0.98]');
181
+
182
+ const formData = new FormData();
183
+ formData.append('file', selectedFile);
184
+
185
+ try {
186
+ const response = await fetch('/predict', {
187
+ method: 'POST',
188
+ body: formData
189
+ });
190
+
191
+ if (!response.ok) throw new Error('Could not connect to the inference engine.');
192
+ const data = await response.json();
193
+
194
+ tagsContainer.innerHTML = '';
195
+ if (data.tags.length > 0) {
196
+ data.tags.forEach((tag, index) => {
197
+ // Updated Tags for dark mode: glowing borders, deep violet backgrounds
198
+ const tagHTML = `<span class="px-5 py-2.5 bg-violet-500/10 text-violet-300 rounded-full text-sm font-extrabold tracking-wide border border-violet-500/20 hover:bg-violet-500/20 hover:border-violet-400/50 hover:shadow-[0_0_15px_rgba(139,92,246,0.2)] hover:scale-105 transition-all cursor-default backdrop-blur-sm">${tag}</span>`;
199
+ tagsContainer.innerHTML += tagHTML;
200
+
201
+ if (index < data.tags.length - 1) {
202
+ tagsContainer.innerHTML += `<svg class="w-5 h-5 text-fuchsia-400/60 stroke-[3px]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M9 5l7 7-7 7"></path></svg>`;
203
+ }
204
+ });
205
+ } else {
206
+ tagsContainer.innerHTML = '<span class="text-slate-500 font-medium bg-slate-800 px-4 py-2 rounded-full text-sm border border-slate-700">No classes detected.</span>';
207
+ }
208
+
209
+ document.getElementById('status-text').textContent = data.status;
210
+
211
+ loadingState.classList.add('hidden');
212
+ resultsState.classList.remove('hidden');
213
+
214
+ } catch (err) {
215
+ loadingState.classList.add('hidden');
216
+ errorMessage.textContent = err.message;
217
+ errorMessage.classList.remove('hidden');
218
+ } finally {
219
+ executeBtn.disabled = false;
220
+ executeBtn.textContent = 'Classify Image';
221
+ executeBtn.classList.remove('opacity-70', 'cursor-not-allowed', 'scale-[0.98]');
222
+ }
223
+ });
224
+ </script>
225
+ </body>
226
+ </html>
frontend/src/app.jsx ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState, useRef } from 'react';
2
+
3
+ export default function App() {
4
+ const [image, setImage] = useState(null);
5
+ const [preview, setPreview] = useState(null);
6
+ const [isDragging, setIsDragging] = useState(false);
7
+ const [loading, setLoading] = useState(false);
8
+ const [results, setResults] = useState(null);
9
+ const [error, setError] = useState(null);
10
+
11
+ const fileInputRef = useRef(null);
12
+
13
+ const API_URL = import.meta.env.DEV ? 'http://localhost:8000/predict' : '/predict';
14
+
15
+ const handleDragOver = (e) => {
16
+ e.preventDefault();
17
+ setIsDragging(true);
18
+ };
19
+
20
+ const handleDragLeave = (e) => {
21
+ e.preventDefault();
22
+ setIsDragging(false);
23
+ };
24
+
25
+ const processFile = (file) => {
26
+ if (!file || !file.type.startsWith('image/')) {
27
+ setError('Please upload a valid image file.');
28
+ return;
29
+ }
30
+ setError(null);
31
+ setImage(file);
32
+ setPreview(URL.createObjectURL(file));
33
+ setResults(null);
34
+ };
35
+
36
+ const handleDrop = (e) => {
37
+ e.preventDefault();
38
+ setIsDragging(false);
39
+ processFile(e.dataTransfer.files[0]);
40
+ };
41
+
42
+ const handleRunInference = async () => {
43
+ if (!image) return;
44
+
45
+ setLoading(true);
46
+ setError(null);
47
+
48
+ const formData = new FormData();
49
+ formData.append('file', image);
50
+
51
+ try {
52
+ const response = await fetch(API_URL, {
53
+ method: 'POST',
54
+ body: formData,
55
+ });
56
+
57
+ if (!response.ok) throw new Error('Inference engine failed to process the image.');
58
+
59
+ const data = await response.json();
60
+ setResults(data);
61
+ } catch (err) {
62
+ setError(err.message);
63
+ } finally {
64
+ setLoading(false);
65
+ }
66
+ };
67
+
68
+ return (
69
+ <div className="min-h-screen bg-[#09090b] text-zinc-100 font-sans flex flex-col items-center justify-center p-6">
70
+
71
+ <div className="max-w-4xl w-full mb-8">
72
+ <h1 className="text-3xl font-light tracking-tight text-white mb-1">
73
+ Apparel <span className="font-semibold text-blue-500">Taxonomy Engine</span>
74
+ </h1>
75
+ </div>
76
+
77
+ <div className="max-w-4xl w-full grid grid-cols-1 md:grid-cols-2 gap-6">
78
+
79
+ <div
80
+ className={`relative flex flex-col items-center justify-center border border-zinc-800 rounded-xl p-8 transition-all duration-300 min-h-[400px] ${
81
+ isDragging ? 'border-blue-500 bg-blue-500/5' : 'bg-[#121214] hover:border-zinc-700'
82
+ }`}
83
+ onDragOver={handleDragOver}
84
+ onDragLeave={handleDragLeave}
85
+ onDrop={handleDrop}
86
+ >
87
+ {preview ? (
88
+ <div className="relative w-full h-full flex flex-col items-center justify-between">
89
+ <div className="flex-1 flex items-center justify-center mb-6">
90
+ <img
91
+ src={preview}
92
+ alt="Upload preview"
93
+ className="max-h-[280px] object-contain rounded-md shadow-2xl"
94
+ />
95
+ </div>
96
+ <button
97
+ onClick={handleRunInference}
98
+ disabled={loading}
99
+ className="w-full py-3 px-4 bg-zinc-100 text-zinc-900 font-medium rounded-lg hover:bg-white transition-colors disabled:opacity-50"
100
+ >
101
+ {loading ? 'Running Inference...' : 'Execute Classification'}
102
+ </button>
103
+ </div>
104
+ ) : (
105
+ <div className="text-center w-full cursor-pointer" onClick={() => fileInputRef.current?.click()}>
106
+ <input
107
+ type="file"
108
+ accept="image/*"
109
+ className="hidden"
110
+ ref={fileInputRef}
111
+ onChange={(e) => processFile(e.target.files[0])}
112
+ />
113
+ <p className="text-zinc-400 text-sm">Drag & drop or click to upload</p>
114
+ </div>
115
+ )}
116
+ </div>
117
+
118
+ <div className="flex flex-col bg-[#121214] border border-zinc-800 rounded-xl p-8 min-h-[400px]">
119
+ <h2 className="text-xs uppercase tracking-widest text-zinc-500 mb-6 border-b border-zinc-800 pb-4">
120
+ Telemetry & Output
121
+ </h2>
122
+
123
+ {error && (
124
+ <div className="bg-red-500/10 border border-red-500/20 text-red-400 p-4 rounded-md text-sm mb-4">
125
+ {error}
126
+ </div>
127
+ )}
128
+
129
+ {!results && !loading && !error && (
130
+ <div className="flex-1 flex items-center justify-center text-zinc-600 text-sm">
131
+ Awaiting payload...
132
+ </div>
133
+ )}
134
+
135
+ {loading && (
136
+ <div className="flex-1 flex flex-col items-center justify-center space-y-4">
137
+ <div className="w-5 h-5 border-2 border-zinc-700 border-t-blue-500 rounded-full animate-spin"></div>
138
+ </div>
139
+ )}
140
+
141
+ {results && !loading && (
142
+ <div className="flex-1 flex flex-col space-y-8 animate-in fade-in duration-500">
143
+
144
+ <div>
145
+ <p className="text-[10px] text-zinc-500 uppercase tracking-widest mb-3">Extracted Taxonomy</p>
146
+ <div className="flex flex-wrap items-center gap-3">
147
+ {results.tags.length > 0 ? results.tags.map((tag, index) => (
148
+ <React.Fragment key={tag}>
149
+ <span className="px-4 py-1.5 bg-blue-500/10 border border-blue-500/20 text-blue-400 rounded-md text-sm font-medium tracking-wide">
150
+ {tag}
151
+ </span>
152
+ {index < results.tags.length - 1 && (
153
+ <svg className="w-3 h-3 text-zinc-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
154
+ <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M9 5l7 7-7 7"></path>
155
+ </svg>
156
+ )}
157
+ </React.Fragment>
158
+ )) : (
159
+ <span className="text-zinc-500 italic text-sm">No classes detected above threshold.</span>
160
+ )}
161
+ </div>
162
+ </div>
163
+
164
+ <div className="pt-6 border-t border-zinc-800">
165
+ <p className="text-[10px] text-zinc-500 uppercase tracking-widest mb-2">Status</p>
166
+ <p className="text-emerald-400 font-mono text-xs uppercase flex items-center gap-2">
167
+ <span className="w-1.5 h-1.5 rounded-full bg-emerald-400"></span> {results.status}
168
+ </p>
169
+ </div>
170
+
171
+ </div>
172
+ )}
173
+ </div>
174
+ </div>
175
+ </div>
176
+ );
177
+ }
frontend/src/index.css ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @tailwind base;
2
+ @tailwind components;
3
+ @tailwind utilities;
4
+
5
+ @layer utilities {
6
+ .animate-in {
7
+ animation: animate-in 0.5s ease-out;
8
+ }
9
+ .fade-in {
10
+ opacity: 0;
11
+ animation-fill-mode: forwards;
12
+ }
13
+ @keyframes animate-in {
14
+ from { opacity: 0; transform: translateY(5px); }
15
+ to { opacity: 1; transform: translateY(0); }
16
+ }
17
+ }
frontend/tailwind.config.js ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /** @type {import('tailwindcss').Config} */
2
+ export default {
3
+ content: [
4
+ "./index.html",
5
+ "./src/**/*.{js,ts,jsx,tsx}",
6
+ ],
7
+ theme: {
8
+ extend: {},
9
+ },
10
+ plugins: [],
11
+ }
models/fashion_hierarchy_model_v3.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f16b657974e4d423d40d51a563fe144ec75f4c83217c6523c0063f5f3e25eeea
3
+ size 17012185
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ python-multipart
4
+ torch
5
+ torchvision
6
+ pillow
7
+ scikit-learn
train.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pickle
3
+ import numpy as np
4
+ import pandas as pd
5
+ import torch
6
+ import torch.nn as nn
7
+ import torch.optim as optim
8
+ from torch.utils.data import Dataset, DataLoader
9
+ from torchvision import models, transforms
10
+ from sklearn.preprocessing import MultiLabelBinarizer
11
+ from PIL import Image
12
+
13
+ # --- 1. CONFIGURATION ---
14
+ DATA_DIR = "./dataset"
15
+ CSV_PATH = "./dataset/metadata.csv"
16
+ MODEL_SAVE_PATH = "fashion_hierarchy_model_v3.pth"
17
+ VOCAB_SAVE_PATH = "mlb_vocab.pkl"
18
+ BATCH_SIZE = 32
19
+ EPOCHS = 10
20
+ LEARNING_RATE = 1e-4
21
+
22
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
+ print(f"Initializing training pipeline on: {device}")
24
+
25
+ # --- 2. DATA PIPELINE ---
26
+ class ApparelDataset(Dataset):
27
+ def __init__(self, df, img_dir, mlb, transform=None):
28
+ self.df = df
29
+ self.img_dir = img_dir
30
+ self.transform = transform
31
+ self.mlb = mlb
32
+
33
+ # Assuming your CSV has 'image_name' and 'tags' (comma separated) columns
34
+ self.images = self.df['image_name'].values
35
+ self.labels = self.mlb.transform(self.df['tags'].apply(lambda x: x.split(',')))
36
+
37
+ def __len__(self):
38
+ return len(self.images)
39
+
40
+ def __getitem__(self, idx):
41
+ img_path = os.path.join(self.img_dir, self.images[idx])
42
+ image = Image.open(img_path).convert("RGB")
43
+ label = torch.tensor(self.labels[idx], dtype=torch.float32)
44
+
45
+ if self.transform:
46
+ image = self.transform(image)
47
+
48
+ return image, label
49
+
50
+ def get_transforms():
51
+ return transforms.Compose([
52
+ transforms.Resize((224, 224)),
53
+ transforms.RandomHorizontalFlip(),
54
+ transforms.ColorJitter(brightness=0.2, contrast=0.2),
55
+ transforms.ToTensor(),
56
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
57
+ ])
58
+
59
+ # --- 3. ARCHITECTURE ---
60
+ def build_model(num_classes):
61
+ model = models.efficientnet_b0(weights=models.EfficientNet_B0_Weights.IMAGENET1K_V1)
62
+ in_features = model.classifier[1].in_features
63
+ model.classifier[1] = nn.Linear(in_features, num_classes)
64
+ return model.to(device)
65
+
66
+ # --- 4. LOSS ENGINEERING (Handling Class Imbalance) ---
67
+ def calculate_pos_weights(labels_matrix):
68
+ class_counts = labels_matrix.sum(axis=0)
69
+ total_samples = labels_matrix.shape[0]
70
+ # Add small epsilon to prevent division by zero
71
+ pos_weights = (total_samples - class_counts) / (class_counts + 1e-5)
72
+ return torch.tensor(pos_weights, dtype=torch.float32).to(device)
73
+
74
+ # --- 5. EXECUTION ---
75
+ def main():
76
+ print("Loading metadata...")
77
+ df = pd.read_csv(CSV_PATH)
78
+
79
+ print("Fitting MultiLabelBinarizer...")
80
+ mlb = MultiLabelBinarizer()
81
+ mlb.fit(df['tags'].apply(lambda x: x.split(',')))
82
+ num_classes = len(mlb.classes_)
83
+
84
+ # Save the vocabulary matrix for inference
85
+ with open(VOCAB_SAVE_PATH, 'wb') as f:
86
+ pickle.dump(mlb, f)
87
+ print(f"Vocabulary matrix locked and saved to {VOCAB_SAVE_PATH}")
88
+
89
+ print("Initializing DataLoader...")
90
+ dataset = ApparelDataset(df, DATA_DIR, mlb, transform=get_transforms())
91
+ dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
92
+
93
+ print("Building EfficientNet-B0 Architecture...")
94
+ model = build_model(num_classes)
95
+
96
+ print("Computing dynamic positive weights for minority classes...")
97
+ pos_weight = calculate_pos_weights(dataset.labels)
98
+ criterion = nn.BCEWithLogitsLoss(pos_weight=pos_weight)
99
+ optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)
100
+
101
+ print("Commencing Training Loop...")
102
+ for epoch in range(EPOCHS):
103
+ model.train()
104
+ running_loss = 0.0
105
+
106
+ for batch_idx, (images, labels) in enumerate(dataloader):
107
+ images, labels = images.to(device), labels.to(device)
108
+
109
+ optimizer.zero_grad()
110
+ outputs = model(images)
111
+ loss = criterion(outputs, labels)
112
+ loss.backward()
113
+ optimizer.step()
114
+
115
+ running_loss += loss.item()
116
+
117
+ if batch_idx % 50 == 0:
118
+ print(f"Epoch [{epoch+1}/{EPOCHS}] Batch {batch_idx}/{len(dataloader)} Loss: {loss.item():.4f}")
119
+
120
+ epoch_loss = running_loss / len(dataloader)
121
+ print(f"--- Epoch {epoch+1} Completed | Average Loss: {epoch_loss:.4f} ---")
122
+
123
+ print("Training sequence complete. Saving V3 state dict...")
124
+ torch.save(model.state_dict(), MODEL_SAVE_PATH)
125
+ print("Engine weights successfully exported.")
126
+
127
+ if __name__ == "__main__":
128
+ main()