Spaces:
Sleeping
Sleeping
Abhishek7356 commited on
Commit ·
7aee28b
1
Parent(s): 9274923
updated
Browse files- app/faiss_utils.py +9 -6
- app/routes.py +28 -5
app/faiss_utils.py
CHANGED
|
@@ -2,14 +2,17 @@ import numpy as np
|
|
| 2 |
import faiss
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
model = SentenceTransformer(
|
| 7 |
|
| 8 |
-
def build_index(
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
embeddings = embeddings / np.linalg.norm(embeddings, axis=1, keepdims=True)
|
| 12 |
dim = embeddings.shape[1]
|
|
|
|
| 13 |
index = faiss.IndexFlatIP(dim)
|
| 14 |
index.add(embeddings)
|
| 15 |
-
|
|
|
|
|
|
| 2 |
import faiss
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
|
| 5 |
+
# Load global embedding model once
|
| 6 |
+
model = SentenceTransformer("multi-qa-mpnet-base-dot-v1")
|
| 7 |
|
| 8 |
+
def build_index(keywords):
|
| 9 |
+
"""Builds FAISS index dynamically from keywords."""
|
| 10 |
+
enriched = [f"{k} category" for k in keywords]
|
| 11 |
+
embeddings = model.encode(enriched).astype("float32")
|
| 12 |
embeddings = embeddings / np.linalg.norm(embeddings, axis=1, keepdims=True)
|
| 13 |
dim = embeddings.shape[1]
|
| 14 |
+
|
| 15 |
index = faiss.IndexFlatIP(dim)
|
| 16 |
index.add(embeddings)
|
| 17 |
+
|
| 18 |
+
return index, embeddings
|
app/routes.py
CHANGED
|
@@ -4,16 +4,39 @@ from .faiss_utils import build_index, model
|
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
import pickle
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
SIMILARITY_THRESHOLD = 0.00
|
| 9 |
|
| 10 |
main = Blueprint('main', __name__)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
# Render UI at root
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
import pickle
|
| 7 |
+
import os
|
| 8 |
+
import yake
|
| 9 |
+
import faiss
|
| 10 |
+
|
| 11 |
|
| 12 |
SIMILARITY_THRESHOLD = 0.00
|
| 13 |
|
| 14 |
main = Blueprint('main', __name__)
|
| 15 |
|
| 16 |
+
# Default fallback keywords (you can load from DB or JSON too)
|
| 17 |
+
DEFAULT_KEYWORDS = ["shoes", "t-shirt", "bag", "watch", "jewelry", "electronics"]
|
| 18 |
+
|
| 19 |
+
# Try loading precomputed embeddings (not FAISS object)
|
| 20 |
+
if os.path.exists("keyword_faiss_index.pkl"):
|
| 21 |
+
try:
|
| 22 |
+
with open("keyword_faiss_index.pkl", "rb") as f:
|
| 23 |
+
saved_data = pickle.load(f)
|
| 24 |
+
category_keywords = saved_data.get("keywords", DEFAULT_KEYWORDS)
|
| 25 |
+
embeddings = saved_data.get("embeddings")
|
| 26 |
+
|
| 27 |
+
if embeddings is not None:
|
| 28 |
+
dim = embeddings.shape[1]
|
| 29 |
+
keyword_index = faiss.IndexFlatIP(dim)
|
| 30 |
+
keyword_index.add(embeddings)
|
| 31 |
+
else:
|
| 32 |
+
keyword_index, embeddings = build_index(category_keywords)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"⚠️ Failed to load FAISS index: {e}. Rebuilding...")
|
| 35 |
+
category_keywords = DEFAULT_KEYWORDS
|
| 36 |
+
keyword_index, embeddings = build_index(category_keywords)
|
| 37 |
+
else:
|
| 38 |
+
category_keywords = DEFAULT_KEYWORDS
|
| 39 |
+
keyword_index, embeddings = build_index(category_keywords)
|
| 40 |
|
| 41 |
|
| 42 |
# Render UI at root
|