File size: 7,543 Bytes
922d194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""

Script to store FAISS and pickle files in Azure Cosmos DB MongoDB API

This script should be run only once to upload the vector database files.



run this at root of the project:

python -m app.services.chatbot.vectorDB_upload_script

"""

import os
import asyncio
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
from datetime import datetime
import logging
from dotenv import load_dotenv

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Load environment variables from .env
load_dotenv()

# Database configuration
MONGO_URI = os.getenv("MONGODB_KEY")  
DB_NAME = "SysmodelerDB"
BUCKET_NAME = "vector_db"

# File paths
FAISS_PATH = r"c:\Users\User\Downloads\faiss_index_sysml\index.faiss"
PICKLE_PATH = r"c:\Users\User\Downloads\faiss_index_sysml\index.pkl"

class VectorDBUploader:
    def __init__(self):
        self.client = None
        self.db = None
        self.fs = None
    
    async def connect(self):
        """Connect to MongoDB"""
        try:
            self.client = AsyncIOMotorClient(MONGO_URI)
            self.db = self.client[DB_NAME]
            self.fs = AsyncIOMotorGridFSBucket(self.db, bucket_name=BUCKET_NAME)
            
            # Test connection
            await self.client.admin.command('ping')
            logger.info("Successfully connected to MongoDB")
            return True
        except Exception as e:
            logger.error(f"Failed to connect to MongoDB: {e}")
            return False
    
    async def file_exists(self, filename: str) -> bool:
        """Check if file already exists in GridFS"""
        try:
            async for file_doc in self.fs.find({"filename": filename}):
                return True
            return False
        except Exception as e:
            logger.error(f"Error checking file existence: {e}")
            return False
    
    # async def upload_file(self, file_path: str, filename: str) -> bool:
    #     """Upload a file to GridFS"""
    #     try:
    #         # Check if file exists locally
    #         if not os.path.exists(file_path):
    #             logger.error(f"Local file not found: {file_path}")
    #             return False
            
    #         # Check if file already exists in database
    #         if await self.file_exists(filename):
    #             logger.warning(f"File {filename} already exists in database. Skipping...")
    #             return True
            
    #         # Get file size for logging
    #         file_size = os.path.getsize(file_path)
    #         logger.info(f"Uploading {filename} ({file_size} bytes)...")
            
    #         # Upload file
    #         with open(file_path, 'rb') as f:
    #             file_id = await self.fs.upload_from_stream(
    #                 filename,
    #                 f,
    #                 metadata={
    #                     "uploaded_at": datetime.utcnow(),
    #                     "original_path": file_path,
    #                     "file_size": file_size,
    #                     "description": f"Vector database file: {filename}"
    #                 }
    #             )
            
    #         logger.info(f"Successfully uploaded {filename} with ID: {file_id}")
    #         return True
            
    #     except Exception as e:
    #         logger.error(f"Failed to upload {filename}: {e}")
    #         return False
    

    async def upload_file(self, file_path: str, filename: str) -> bool:
        """Upload a file to GridFS, overwriting any existing files with the same name"""
        try:
            # Check if file exists locally
            if not os.path.exists(file_path):
                logger.error(f"Local file not found: {file_path}")
                return False

            # Remove any existing file with the same name
            async for file_doc in self.fs.find({"filename": filename}):
                await self.fs.delete(file_doc._id)
                logger.info(f"Deleted existing file: {filename} (ID: {file_doc._id})")
            
            # Get file size for logging
            file_size = os.path.getsize(file_path)
            logger.info(f"Uploading {filename} ({file_size} bytes)...")
            
            # Upload file
            with open(file_path, 'rb') as f:
                file_id = await self.fs.upload_from_stream(
                    filename,
                    f,
                    metadata={
                        "uploaded_at": datetime.utcnow(),
                        "original_path": file_path,
                        "file_size": file_size,
                        "description": f"Vector database file: {filename}"
                    }
                )
            
            logger.info(f"Successfully uploaded {filename} with ID: {file_id}")
            return True
            
        except Exception as e:
            logger.error(f"Failed to upload {filename}: {e}")
            return False


    async def list_files(self):
        """List all files in the GridFS bucket"""
        try:
            logger.info(f"Files in {BUCKET_NAME} bucket:")
            async for file_doc in self.fs.find():
                logger.info(f"- {file_doc.filename} (ID: {file_doc._id}, Size: {file_doc.length} bytes)")
        except Exception as e:
            logger.error(f"Failed to list files: {e}")
    
    async def upload_vector_files(self):
        """Upload both FAISS and pickle files"""
        files_to_upload = [
            (FAISS_PATH, "index.faiss"),
            (PICKLE_PATH, "index.pkl")
        ]
        
        success_count = 0
        for file_path, filename in files_to_upload:
            if await self.upload_file(file_path, filename):
                success_count += 1
            else:
                logger.error(f"Failed to upload {filename}")
        
        logger.info(f"Upload completed: {success_count}/{len(files_to_upload)} files uploaded successfully")
        return success_count == len(files_to_upload)
    
    async def close(self):
        """Close database connection"""
        if self.client:
            self.client.close()
            logger.info("Database connection closed")

async def main():
    """Main function to upload vector database files"""
    uploader = VectorDBUploader()
    
    try:
        # Connect to database
        if not await uploader.connect():
            logger.error("Failed to connect to database. Exiting...")
            return
        
        # Upload files
        logger.info("Starting vector database files upload...")
        success = await uploader.upload_vector_files()
        
        if success:
            logger.info("All files uploaded successfully!")
        else:
            logger.error("Some files failed to upload")
        
        # List all files in the bucket
        await uploader.list_files()
        
    except Exception as e:
        logger.error(f"Unexpected error: {e}")
    finally:
        await uploader.close()

if __name__ == "__main__":
    print("Vector Database File Uploader")
    print("=" * 50)
    print(f"Database: {DB_NAME}")
    print(f"Bucket: {BUCKET_NAME}")
    print(f"FAISS file: {FAISS_PATH}")
    print(f"Pickle file: {PICKLE_PATH}")
    print("=" * 50)
    
    # Run the upload process
    asyncio.run(main())