Yashashvibhardwaj commited on
Commit
a93aa09
·
verified ·
1 Parent(s): 8bda539

Update build_index.py

Browse files
Files changed (1) hide show
  1. 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(product_names, convert_to_numpy=True, show_progress_bar=True)
26
-
27
- # Build FAISS index
 
 
 
 
 
28
  dimension = embeddings.shape[1]
29
- index = faiss.IndexFlatL2(dimension)
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