Spaces:
Sleeping
Sleeping
Create semantic/faiss_index.py
Browse files- semantic/faiss_index.py +26 -0
semantic/faiss_index.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import faiss
|
| 2 |
+
import numpy as np
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
|
| 5 |
+
MODEL = SentenceTransformer("all-MiniLM-L6-v2")
|
| 6 |
+
|
| 7 |
+
class SemanticIndex:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.index = None
|
| 10 |
+
self.texts = []
|
| 11 |
+
|
| 12 |
+
def build(self, texts):
|
| 13 |
+
self.texts = texts
|
| 14 |
+
embeddings = MODEL.encode(texts, convert_to_numpy=True)
|
| 15 |
+
dim = embeddings.shape[1]
|
| 16 |
+
self.index = faiss.IndexFlatL2(dim)
|
| 17 |
+
self.index.add(embeddings)
|
| 18 |
+
|
| 19 |
+
def search(self, query, k=10):
|
| 20 |
+
if not self.index:
|
| 21 |
+
return []
|
| 22 |
+
|
| 23 |
+
q_emb = MODEL.encode([query], convert_to_numpy=True)
|
| 24 |
+
distances, indices = self.index.search(q_emb, k)
|
| 25 |
+
|
| 26 |
+
return [self.texts[i] for i in indices[0] if i < len(self.texts)]
|