GodsDevProject commited on
Commit
352cc38
·
verified ·
1 Parent(s): 385bf90

Create semantic.py

Browse files
Files changed (1) hide show
  1. semantic.py +29 -15
semantic.py CHANGED
@@ -1,18 +1,32 @@
1
- import faiss
2
- import numpy as np
3
- from sentence_transformers import SentenceTransformer
4
- from typing import List, Dict
5
 
6
- model = SentenceTransformer("all-MiniLM-L6-v2")
 
 
 
 
 
7
 
8
- def build_faiss_index(docs: List[Dict]):
9
- texts = [d["content"] for d in docs]
10
- embeddings = model.encode(texts)
11
- index = faiss.IndexFlatL2(embeddings.shape[1])
12
- index.add(np.array(embeddings))
13
- return index, embeddings
14
 
15
- def semantic_search(query: str, docs: List[Dict], index):
16
- q_emb = model.encode([query])
17
- D, I = index.search(np.array(q_emb), k=5)
18
- return [docs[i] for i in I[0]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FAISS_AVAILABLE = False
 
 
 
2
 
3
+ try:
4
+ import faiss
5
+ from sentence_transformers import SentenceTransformer
6
+ FAISS_AVAILABLE = True
7
+ except Exception:
8
+ FAISS_AVAILABLE = False
9
 
10
+ class SemanticIndex:
11
+ def __init__(self):
12
+ if not FAISS_AVAILABLE:
13
+ raise RuntimeError("FAISS unavailable")
 
 
14
 
15
+ self.model = SentenceTransformer("all-MiniLM-L6-v2")
16
+ self.index = None
17
+ self.texts = []
18
+
19
+ def build(self, texts):
20
+ embeddings = self.model.encode(texts)
21
+ dim = embeddings.shape[1]
22
+ self.index = faiss.IndexFlatL2(dim)
23
+ self.index.add(embeddings)
24
+ self.texts = texts
25
+
26
+ def search(self, query, k=5):
27
+ if not self.index:
28
+ return []
29
+
30
+ q = self.model.encode([query])
31
+ _, idxs = self.index.search(q, k)
32
+ return [self.texts[i] for i in idxs[0]]