Spaces:
Runtime error
Runtime error
Update src/utils.py
Browse files- src/utils.py +9 -8
src/utils.py
CHANGED
@@ -1,19 +1,20 @@
|
|
1 |
-
from sentence_transformers import SentenceTransformer
|
2 |
import faiss
|
3 |
import numpy as np
|
|
|
4 |
|
5 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
6 |
|
7 |
def get_embeddings(texts):
|
8 |
return model.encode(texts, convert_to_numpy=True)
|
9 |
|
10 |
-
def build_index(
|
11 |
-
dim =
|
12 |
index = faiss.IndexFlatIP(dim)
|
13 |
-
faiss.normalize_L2(
|
14 |
-
index.add(
|
15 |
return index
|
16 |
|
17 |
-
def search_similar(index,
|
18 |
-
faiss.normalize_L2(
|
19 |
-
|
|
|
|
|
|
1 |
import faiss
|
2 |
import numpy as np
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
|
5 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
6 |
|
7 |
def get_embeddings(texts):
|
8 |
return model.encode(texts, convert_to_numpy=True)
|
9 |
|
10 |
+
def build_index(embeddings):
|
11 |
+
dim = embeddings.shape[1]
|
12 |
index = faiss.IndexFlatIP(dim)
|
13 |
+
faiss.normalize_L2(embeddings)
|
14 |
+
index.add(embeddings)
|
15 |
return index
|
16 |
|
17 |
+
def search_similar(index, query_embedding, k=5):
|
18 |
+
faiss.normalize_L2(query_embedding)
|
19 |
+
distances, indices = index.search(query_embedding, k)
|
20 |
+
return distances, indices
|