Update build_index.py
Browse files- build_index.py +10 -5
build_index.py
CHANGED
|
@@ -19,14 +19,19 @@ print(f"📦 Loaded {len(products)} products from products.json")
|
|
| 19 |
print("🧠 Loading CLIP model...")
|
| 20 |
model = SentenceTransformer("sentence-transformers/clip-ViT-B-32", cache_folder="./cache")
|
| 21 |
|
| 22 |
-
# Encode product names
|
| 23 |
print("🔎 Encoding product features...")
|
| 24 |
product_names = [p["name"] for p in products]
|
| 25 |
-
embeddings = model.encode(
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
dimension = embeddings.shape[1]
|
| 29 |
-
index = faiss.
|
| 30 |
index.add(embeddings)
|
| 31 |
|
| 32 |
# Save index
|
|
|
|
| 19 |
print("🧠 Loading CLIP model...")
|
| 20 |
model = SentenceTransformer("sentence-transformers/clip-ViT-B-32", cache_folder="./cache")
|
| 21 |
|
| 22 |
+
# Encode product names (normalize for cosine sim)
|
| 23 |
print("🔎 Encoding product features...")
|
| 24 |
product_names = [p["name"] for p in products]
|
| 25 |
+
embeddings = model.encode(
|
| 26 |
+
product_names,
|
| 27 |
+
convert_to_numpy=True,
|
| 28 |
+
show_progress_bar=True,
|
| 29 |
+
normalize_embeddings=True, # important for cosine similarity
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Build FAISS index with cosine similarity (inner product on normalized vectors)
|
| 33 |
dimension = embeddings.shape[1]
|
| 34 |
+
index = faiss.IndexFlatIP(dimension)
|
| 35 |
index.add(embeddings)
|
| 36 |
|
| 37 |
# Save index
|