Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,23 @@
|
|
1 |
# Install required libraries
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
|
4 |
import os
|
|
|
1 |
# Install required libraries
|
2 |
+
pip uninstall faiss-cpu
|
3 |
+
pip install faiss-cpu
|
4 |
+
pip install faiss-cpu==1.7.3
|
5 |
+
pip install annoy
|
6 |
+
from annoy import AnnoyIndex # Importing annoy for vector search
|
7 |
+
|
8 |
+
# Function to create an Annoy index from the embeddings
|
9 |
+
def create_annoy_index(embeddings, num_trees=10):
|
10 |
+
index = AnnoyIndex(embeddings.shape[1], 'angular') # Using angular distance metric
|
11 |
+
for i, emb in enumerate(embeddings):
|
12 |
+
index.add_item(i, emb)
|
13 |
+
index.build(num_trees)
|
14 |
+
return index
|
15 |
+
|
16 |
+
# Function to retrieve the most relevant text using Annoy
|
17 |
+
def retrieve_relevant_text(query, annoy_index, texts, top_k=3):
|
18 |
+
query_embedding = embedder.encode([query], convert_to_tensor=True)
|
19 |
+
indices = annoy_index.get_nns_by_vector(query_embedding[0], top_k)
|
20 |
+
return [texts[i] for i in indices]
|
21 |
|
22 |
|
23 |
import os
|