Spaces:
Running
on
Zero
Running
on
Zero
Update main.py
Browse files
main.py
CHANGED
@@ -24,12 +24,16 @@ data = full_data[~pandas.Series(filter)]
|
|
24 |
data.reset_index(inplace=True)
|
25 |
|
26 |
# Create a FAISS index for fast similarity search
|
27 |
-
|
28 |
-
|
29 |
vectors = numpy.stack(data["embedding"].tolist(), axis=0)
|
30 |
-
|
31 |
-
index.
|
32 |
-
index.
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# Load the model for later use in embeddings
|
35 |
model = sentence_transformers.SentenceTransformer("allenai-specter")
|
@@ -38,7 +42,7 @@ model = sentence_transformers.SentenceTransformer("allenai-specter")
|
|
38 |
def search(query: str, k: int):
|
39 |
query = numpy.expand_dims(model.encode(query), axis=0)
|
40 |
faiss.normalize_L2(query)
|
41 |
-
D, I =
|
42 |
top_five = data.loc[I[0]]
|
43 |
search_results = ""
|
44 |
|
|
|
24 |
data.reset_index(inplace=True)
|
25 |
|
26 |
# Create a FAISS index for fast similarity search
|
27 |
+
let indices = []
|
28 |
+
let metrics = [faiss.METRIC_INNER_PRODUCT]
|
29 |
vectors = numpy.stack(data["embedding"].tolist(), axis=0)
|
30 |
+
for metric in metrics:
|
31 |
+
index = faiss.IndexFlatL2(len(data["embedding"][0]))
|
32 |
+
index.metric_type = metric
|
33 |
+
faiss.normalize_L2(vectors)
|
34 |
+
index.train(vectors)
|
35 |
+
index.add(vectors)
|
36 |
+
indices.append(index)
|
37 |
|
38 |
# Load the model for later use in embeddings
|
39 |
model = sentence_transformers.SentenceTransformer("allenai-specter")
|
|
|
42 |
def search(query: str, k: int):
|
43 |
query = numpy.expand_dims(model.encode(query), axis=0)
|
44 |
faiss.normalize_L2(query)
|
45 |
+
D, I = indices[0].search(query, k)
|
46 |
top_five = data.loc[I[0]]
|
47 |
search_results = ""
|
48 |
|