Spaces:
Build error
Build error
Create create_faiss_index.py
Browse files- create_faiss_index.py +23 -0
create_faiss_index.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import faiss
|
2 |
+
import pickle
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
import chromadb
|
5 |
+
|
6 |
+
# Initialize SentenceTransformer model
|
7 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
8 |
+
|
9 |
+
# Load documents or data that you want to index
|
10 |
+
documents = ['document 1 text', 'document 2 text', 'document 3 text']
|
11 |
+
|
12 |
+
# Generate embeddings for documents
|
13 |
+
embeddings = model.encode(documents)
|
14 |
+
|
15 |
+
# Create FAISS index
|
16 |
+
faiss_index = faiss.IndexFlatL2(embeddings.shape[1]) # Using L2 distance
|
17 |
+
|
18 |
+
# Add embeddings to FAISS index
|
19 |
+
faiss_index.add(embeddings)
|
20 |
+
|
21 |
+
# Save the FAISS index
|
22 |
+
with open('faiss_index.index', 'wb') as f:
|
23 |
+
pickle.dump(faiss_index, f)
|