Spaces:
Runtime error
Runtime error
Update utils/migrate.py
Browse files- utils/migrate.py +49 -43
utils/migrate.py
CHANGED
|
@@ -3,46 +3,52 @@ from pymongo import MongoClient
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
if not
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
def migrate_faiss_index():
|
| 7 |
+
"""Migrate FAISS index from QA cluster to index cluster"""
|
| 8 |
+
# Load environment variables from .env
|
| 9 |
+
load_dotenv()
|
| 10 |
+
# Connection strings (update as needed)
|
| 11 |
+
mongo_uri = os.getenv("MONGO_URI") # QA cluster connection string
|
| 12 |
+
index_uri = os.getenv("INDEX_URI") # FAISS index cluster connection string
|
| 13 |
+
|
| 14 |
+
if not mongo_uri:
|
| 15 |
+
raise ValueError("MONGO_URI is missing!")
|
| 16 |
+
if not index_uri:
|
| 17 |
+
raise ValueError("INDEX_URI is missing!")
|
| 18 |
+
|
| 19 |
+
# Connect to the QA cluster (where FAISS data was accidentally stored)
|
| 20 |
+
qa_client = MongoClient(mongo_uri)
|
| 21 |
+
qa_db = qa_client["MedicalChatbotDB"]
|
| 22 |
+
|
| 23 |
+
# Connect to the FAISS index cluster
|
| 24 |
+
faiss_client = MongoClient(index_uri)
|
| 25 |
+
faiss_db = faiss_client["MedicalChatbotDB"] # Use the same database name if desired
|
| 26 |
+
|
| 27 |
+
# Define the GridFS collections to move.
|
| 28 |
+
# In GridFS, files are stored in two collections: "<bucket>.files" and "<bucket>.chunks".
|
| 29 |
+
source_files = qa_db["faiss_index_files.files"]
|
| 30 |
+
source_chunks = qa_db["faiss_index_files.chunks"]
|
| 31 |
+
|
| 32 |
+
dest_files = faiss_db["faiss_index_files.files"]
|
| 33 |
+
dest_chunks = faiss_db["faiss_index_files.chunks"]
|
| 34 |
+
|
| 35 |
+
print("Moving FAISS index GridFS files...")
|
| 36 |
+
|
| 37 |
+
# Copy documents from the source 'files' collection
|
| 38 |
+
for doc in source_files.find():
|
| 39 |
+
dest_files.insert_one(doc)
|
| 40 |
+
|
| 41 |
+
# Copy documents from the source 'chunks' collection
|
| 42 |
+
for doc in source_chunks.find():
|
| 43 |
+
dest_chunks.insert_one(doc)
|
| 44 |
+
|
| 45 |
+
print("✅ FAISS GridFS collections moved successfully.")
|
| 46 |
+
|
| 47 |
+
# Optionally, drop the old collections from the QA cluster to free up space:
|
| 48 |
+
qa_db.drop_collection("faiss_index_files.files")
|
| 49 |
+
qa_db.drop_collection("faiss_index_files.chunks")
|
| 50 |
+
print("Old FAISS GridFS collections dropped from the QA cluster.")
|
| 51 |
+
|
| 52 |
+
# Only run when called directly
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
migrate_faiss_index()
|