Spaces:
Sleeping
Sleeping
Delete faiss_vector.py
Browse files- faiss_vector.py +0 -25
faiss_vector.py
DELETED
|
@@ -1,25 +0,0 @@
|
|
| 1 |
-
try:
|
| 2 |
-
import faiss
|
| 3 |
-
from sentence_transformers import SentenceTransformer
|
| 4 |
-
except ImportError:
|
| 5 |
-
faiss = None
|
| 6 |
-
|
| 7 |
-
class FaissIndex:
|
| 8 |
-
def __init__(self, model_name="all-MiniLM-L6-v2"):
|
| 9 |
-
if faiss is None:
|
| 10 |
-
raise RuntimeError("FAISS not installed")
|
| 11 |
-
self.model = SentenceTransformer(model_name)
|
| 12 |
-
self.index = None
|
| 13 |
-
self.docs = []
|
| 14 |
-
|
| 15 |
-
def add(self, texts):
|
| 16 |
-
emb = self.model.encode(texts)
|
| 17 |
-
if self.index is None:
|
| 18 |
-
self.index = faiss.IndexFlatL2(emb.shape[1])
|
| 19 |
-
self.index.add(emb)
|
| 20 |
-
self.docs.extend(texts)
|
| 21 |
-
|
| 22 |
-
def search(self, query, k=5):
|
| 23 |
-
emb = self.model.encode([query])
|
| 24 |
-
D, I = self.index.search(emb, k)
|
| 25 |
-
return [self.docs[i] for i in I[0] if i < len(self.docs)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|