Abhishek7356 commited on
Commit ·
4c2a15d
1
Parent(s): b6041af
updated code
Browse files- app.py +33 -18
- templates/index.html +40 -5
app.py
CHANGED
|
@@ -122,36 +122,51 @@ def predict():
|
|
| 122 |
return jsonify({"error": "title or description required"}), 400
|
| 123 |
|
| 124 |
tags_str = ", ".join(tags) if isinstance(tags, list) else str(tags)
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
f"{title} {desc} {product_type} {tags_str}"
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
# -------------------------
|
| 131 |
-
# FAISS first-stage search (top 20)
|
| 132 |
-
# -------------------------
|
| 133 |
q = embed(full_text)
|
| 134 |
D, I = index.search(q, 20)
|
| 135 |
|
| 136 |
-
# Compute confidence scores
|
| 137 |
scores = (D[0] - D[0].min()) / (D[0].max() - D[0].min() + 1e-9)
|
| 138 |
-
candidates = [categories[i]["category_path"] for i in I[0]]
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
| 143 |
-
# GPT Rerank
|
| 144 |
-
# -------------------------
|
| 145 |
-
final = gpt_rerank(full_text, top10)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
return jsonify({
|
| 148 |
-
"predicted_category":
|
|
|
|
|
|
|
| 149 |
"top_candidates": [
|
| 150 |
-
{
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
]
|
| 153 |
})
|
| 154 |
-
|
| 155 |
@app.route("/")
|
| 156 |
def home():
|
| 157 |
return render_template("index.html")
|
|
|
|
| 122 |
return jsonify({"error": "title or description required"}), 400
|
| 123 |
|
| 124 |
tags_str = ", ".join(tags) if isinstance(tags, list) else str(tags)
|
| 125 |
+
full_text = preprocess(f"{title} {desc} {product_type} {tags_str}")
|
| 126 |
|
| 127 |
+
# --------------- FAISS SEARCH ---------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
q = embed(full_text)
|
| 129 |
D, I = index.search(q, 20)
|
| 130 |
|
|
|
|
| 131 |
scores = (D[0] - D[0].min()) / (D[0].max() - D[0].min() + 1e-9)
|
|
|
|
| 132 |
|
| 133 |
+
candidate_objects = [
|
| 134 |
+
{
|
| 135 |
+
"category_path": categories[i]["category_path"],
|
| 136 |
+
"category_id": categories[i]["category_id"],
|
| 137 |
+
"distance": float(D[0][idx])
|
| 138 |
+
}
|
| 139 |
+
for idx, i in enumerate(I[0])
|
| 140 |
+
]
|
| 141 |
|
| 142 |
+
top10 = candidate_objects[:10]
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
# --------------- GPT RERANK ---------------
|
| 145 |
+
paths_only = [c["category_path"] for c in top10]
|
| 146 |
+
final_path = gpt_rerank(full_text, paths_only)
|
| 147 |
+
|
| 148 |
+
final_category = next(
|
| 149 |
+
(c for c in top10 if c["category_path"].lower() == final_path.lower()),
|
| 150 |
+
None
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
# -----------------------------
|
| 154 |
+
# 🟦 FIXED RESPONSE FOR UI
|
| 155 |
+
# -----------------------------
|
| 156 |
return jsonify({
|
| 157 |
+
"predicted_category": final_category["category_path"] if final_category else None,
|
| 158 |
+
"predicted_category_id": final_category["category_id"] if final_category else None,
|
| 159 |
+
|
| 160 |
"top_candidates": [
|
| 161 |
+
{
|
| 162 |
+
"category": c["category_path"],
|
| 163 |
+
"category_id": c["category_id"],
|
| 164 |
+
"confidence": float(scores[idx])
|
| 165 |
+
}
|
| 166 |
+
for idx, c in enumerate(top10)
|
| 167 |
]
|
| 168 |
})
|
| 169 |
+
|
| 170 |
@app.route("/")
|
| 171 |
def home():
|
| 172 |
return render_template("index.html")
|
templates/index.html
CHANGED
|
@@ -287,6 +287,18 @@
|
|
| 287 |
margin-bottom: 12px;
|
| 288 |
}
|
| 289 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
.confidence-badge {
|
| 291 |
display: inline-flex;
|
| 292 |
align-items: center;
|
|
@@ -322,8 +334,8 @@
|
|
| 322 |
|
| 323 |
.candidate-header {
|
| 324 |
display: flex;
|
| 325 |
-
justify-content: space-between;
|
| 326 |
align-items: center;
|
|
|
|
| 327 |
margin-bottom: 8px;
|
| 328 |
}
|
| 329 |
|
|
@@ -338,14 +350,26 @@
|
|
| 338 |
justify-content: center;
|
| 339 |
font-size: 13px;
|
| 340 |
font-weight: 700;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
}
|
| 342 |
|
| 343 |
.candidate-name {
|
| 344 |
color: #2d3748;
|
| 345 |
font-weight: 600;
|
| 346 |
font-size: 15px;
|
| 347 |
-
|
| 348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
}
|
| 350 |
|
| 351 |
.confidence-pill {
|
|
@@ -355,6 +379,7 @@
|
|
| 355 |
border-radius: 20px;
|
| 356 |
font-size: 13px;
|
| 357 |
font-weight: 700;
|
|
|
|
| 358 |
}
|
| 359 |
|
| 360 |
.error {
|
|
@@ -463,7 +488,10 @@
|
|
| 463 |
<div class="result-header">
|
| 464 |
<h2>Predicted Category</h2>
|
| 465 |
<div class="result-category" id="predictedCategory"></div>
|
| 466 |
-
<
|
|
|
|
|
|
|
|
|
|
| 467 |
</div>
|
| 468 |
|
| 469 |
<div class="candidates-list">
|
|
@@ -574,6 +602,10 @@
|
|
| 574 |
|
| 575 |
// Update main predicted category
|
| 576 |
document.getElementById('predictedCategory').textContent = data.predicted_category;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
// Use confidence of candidate #1 for display
|
| 579 |
const topConf = data.top_candidates?.[0]?.confidence || 0;
|
|
@@ -589,7 +621,10 @@
|
|
| 589 |
<div class="candidate-item">
|
| 590 |
<div class="candidate-header">
|
| 591 |
<div class="candidate-rank">${index + 1}</div>
|
| 592 |
-
<div class="candidate-
|
|
|
|
|
|
|
|
|
|
| 593 |
<div class="confidence-pill">${(candidate.confidence * 100).toFixed(1)}%</div>
|
| 594 |
</div>
|
| 595 |
</div>
|
|
|
|
| 287 |
margin-bottom: 12px;
|
| 288 |
}
|
| 289 |
|
| 290 |
+
.category-id {
|
| 291 |
+
display: inline-block;
|
| 292 |
+
background: rgba(255, 255, 255, 0.2);
|
| 293 |
+
padding: 6px 14px;
|
| 294 |
+
border-radius: 8px;
|
| 295 |
+
font-size: 14px;
|
| 296 |
+
font-weight: 600;
|
| 297 |
+
font-family: 'Courier New', monospace;
|
| 298 |
+
margin-bottom: 12px;
|
| 299 |
+
backdrop-filter: blur(10px);
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
.confidence-badge {
|
| 303 |
display: inline-flex;
|
| 304 |
align-items: center;
|
|
|
|
| 334 |
|
| 335 |
.candidate-header {
|
| 336 |
display: flex;
|
|
|
|
| 337 |
align-items: center;
|
| 338 |
+
gap: 12px;
|
| 339 |
margin-bottom: 8px;
|
| 340 |
}
|
| 341 |
|
|
|
|
| 350 |
justify-content: center;
|
| 351 |
font-size: 13px;
|
| 352 |
font-weight: 700;
|
| 353 |
+
flex-shrink: 0;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
.candidate-details {
|
| 357 |
+
flex: 1;
|
| 358 |
+
min-width: 0;
|
| 359 |
}
|
| 360 |
|
| 361 |
.candidate-name {
|
| 362 |
color: #2d3748;
|
| 363 |
font-weight: 600;
|
| 364 |
font-size: 15px;
|
| 365 |
+
margin-bottom: 4px;
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
.candidate-id {
|
| 369 |
+
color: #718096;
|
| 370 |
+
font-size: 12px;
|
| 371 |
+
font-family: 'Courier New', monospace;
|
| 372 |
+
font-weight: 500;
|
| 373 |
}
|
| 374 |
|
| 375 |
.confidence-pill {
|
|
|
|
| 379 |
border-radius: 20px;
|
| 380 |
font-size: 13px;
|
| 381 |
font-weight: 700;
|
| 382 |
+
flex-shrink: 0;
|
| 383 |
}
|
| 384 |
|
| 385 |
.error {
|
|
|
|
| 488 |
<div class="result-header">
|
| 489 |
<h2>Predicted Category</h2>
|
| 490 |
<div class="result-category" id="predictedCategory"></div>
|
| 491 |
+
<div class="category-id" id="predictedCategoryId"></div>
|
| 492 |
+
<div>
|
| 493 |
+
<span class="confidence-badge" id="confidenceBadge"></span>
|
| 494 |
+
</div>
|
| 495 |
</div>
|
| 496 |
|
| 497 |
<div class="candidates-list">
|
|
|
|
| 602 |
|
| 603 |
// Update main predicted category
|
| 604 |
document.getElementById('predictedCategory').textContent = data.predicted_category;
|
| 605 |
+
|
| 606 |
+
// Update predicted category ID
|
| 607 |
+
document.getElementById('predictedCategoryId').textContent =
|
| 608 |
+
`ID: ${data.predicted_category_id || 'N/A'}`;
|
| 609 |
|
| 610 |
// Use confidence of candidate #1 for display
|
| 611 |
const topConf = data.top_candidates?.[0]?.confidence || 0;
|
|
|
|
| 621 |
<div class="candidate-item">
|
| 622 |
<div class="candidate-header">
|
| 623 |
<div class="candidate-rank">${index + 1}</div>
|
| 624 |
+
<div class="candidate-details">
|
| 625 |
+
<div class="candidate-name">${candidate.category}</div>
|
| 626 |
+
<div class="candidate-id">ID: ${candidate.category_id || 'N/A'}</div>
|
| 627 |
+
</div>
|
| 628 |
<div class="confidence-pill">${(candidate.confidence * 100).toFixed(1)}%</div>
|
| 629 |
</div>
|
| 630 |
</div>
|