muhammadshaheryar commited on
Commit
a0d821f
·
verified ·
1 Parent(s): b49a872

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -0
app.py CHANGED
@@ -1,4 +1,25 @@
1
  import faiss
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
 
4
 
 
1
  import faiss
2
+ conda install -c conda-forge faiss-cpu
3
+ python3.8 -m venv myenv
4
+ source myenv/bin/activate
5
+ pip install faiss-cpu streamlit numpy
6
+ pip install annoy
7
+ pip install annoy
8
+ from annoy import AnnoyIndex
9
+
10
+ # Build Annoy index
11
+ def create_annoy_index(embeddings, num_trees=10):
12
+ index = AnnoyIndex(embeddings.shape[1], 'angular')
13
+ for i, emb in enumerate(embeddings):
14
+ index.add_item(i, emb)
15
+ index.build(num_trees)
16
+ return index
17
+
18
+ # Query Annoy index
19
+ def retrieve_relevant_text(query, annoy_index, texts, top_k=3):
20
+ query_embedding = embedder.encode([query])[0]
21
+ indices = annoy_index.get_nns_by_vector(query_embedding, top_k)
22
+ return [texts[i] for i in indices]
23
 
24
 
25