Spaces:
Runtime error
Runtime error
Download config/database.py from dev2004v/content-detection-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/dev2004v/content-detection-backend/resolve/refs%2Fpr%2F1/config/database.py
- Command line
-
hf download hf://spaces/dev2004v/content-detection-backend@refs/pr/1/config/database.py
-
curl -L -o database.py https://huggingface.co/spaces/dev2004v/content-detection-backend/resolve/refs%2Fpr%2F1/config/database.py
1.06 kB
| from pymongo import MongoClient | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| client = None | |
| db = None | |
| def init_db(): | |
| global client, db | |
| MONGO_URI = os.getenv('MONGO_URI') | |
| DATABASE_NAME = os.getenv('DATABASE_NAME', 'recommendation_system') | |
| if not MONGO_URI: | |
| raise ValueError("MONGO_URI environment variable is required") | |
| try: | |
| client = MongoClient(MONGO_URI) | |
| db = client[DATABASE_NAME] | |
| # Test connection | |
| client.admin.command('ping') | |
| print(f"Connected to MongoDB database: {DATABASE_NAME}") | |
| # Create indexes for performance | |
| db.users.create_index("email", unique=True) | |
| db.users.create_index("username", unique=True) | |
| db.history.create_index([("user_id", 1), ("timestamp", -1)]) | |
| return db | |
| except Exception as e: | |
| print(f"Failed to connect to MongoDB: {e}") | |
| raise e | |
| def get_db(): | |
| global db | |
| if db is None: | |
| init_db() | |
| return db |