Spaces:
Runtime error
Runtime error
import os | |
from pymongo.mongo_client import MongoClient | |
from pymongo.server_api import ServerApi | |
import certifi | |
from datetime import datetime, timezone | |
from app.core.logging_setup import logger | |
# MongoDB setup | |
MONGO_DB = os.getenv("MONGO_URI", "") | |
if not MONGO_DB: | |
raise ValueError("MONGO_URI environment variable not set. Please set it to your MongoDB connection string.") | |
# Create a new client and connect to the server | |
client = MongoClient(MONGO_DB, server_api=ServerApi('1'), tls=True, tlsCAFile=certifi.where()) | |
# Send a ping to confirm a successful connection | |
try: | |
client.admin.command('ping') | |
print("Pinged your deployment. You successfully connected to MongoDB!") | |
except Exception as e: | |
print(e) | |
db = client["arina_database"] | |
# Define collections | |
conversations_collection = db["conversations"] | |
user_memory_collection = db["user_memory"] | |
feedback_collection = db["feedback"] | |
interaction_patterns_collection = db["interaction_patterns"] | |
web_search_keywords_collection = db["web_search_keywords"] | |
# Initialize collections with default data | |
def init_db(): | |
try: | |
# Insert default user memory if not exists | |
if not user_memory_collection.find_one({"key": "last_interaction"}): | |
user_memory_collection.insert_one({"key": "last_interaction", "value": datetime.now(timezone.utc).isoformat()}) | |
# Insert default web search keywords if not exists | |
if web_search_keywords_collection.count_documents({}) == 0: | |
default_keywords = [ | |
"news", "update", "latest", "recent", "event", "schedule", "release", | |
"stock price", "weather", "sports score", "match result", "forecast", | |
"celebrities", "trending", "movie times", "game release", "scientific discovery", | |
"politics", "economic news", "current time", "today's date", "happening now" | |
] | |
web_search_keywords_collection.insert_many([{"keyword": kw} for kw in default_keywords]) | |
logger.info("Database initialized successfully.") | |
except Exception as e: | |
logger.error(f"Unexpected error during database initialization: {e}") | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser(description="Database Initialization") | |
parser.add_argument("--init", action="store_true", help="Initialize the database") | |
args = parser.parse_args() | |
if args.init: | |
init_db() |