Spaces:
Sleeping
Sleeping
| import os | |
| from motor.motor_asyncio import AsyncIOMotorClient | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| # MongoDB connection settings | |
| MONGODB_URL = os.getenv("MONGODB_URL", "mongodb://localhost:27017") | |
| DATABASE_NAME = os.getenv("DATABASE_NAME", "fake_news_detector") | |
| # Global database client | |
| client: AsyncIOMotorClient = None | |
| db = None | |
| async def connect_to_mongodb(): | |
| """Connect to MongoDB database""" | |
| global client, db | |
| try: | |
| client = AsyncIOMotorClient(MONGODB_URL) | |
| db = client[DATABASE_NAME] | |
| # Verify connection | |
| await client.admin.command('ping') | |
| print(f"✅ Connected to MongoDB: {DATABASE_NAME}") | |
| except Exception as e: | |
| print(f"❌ Failed to connect to MongoDB: {e}") | |
| raise e | |
| async def close_mongodb_connection(): | |
| """Close MongoDB connection""" | |
| global client | |
| if client: | |
| client.close() | |
| print("MongoDB connection closed") | |
| def get_database(): | |
| """Get database instance""" | |
| return db | |
| def get_users_collection(): | |
| """Get users collection""" | |
| return db["users"] | |
| def get_predictions_collection(): | |
| """Get predictions history collection""" | |
| return db["predictions"] | |