Spaces:
Runtime error
Runtime error
Download database.py from pvanand/specialized-agents-api: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/pvanand/specialized-agents-api/resolve/main/database.py
- Command line
-
hf download hf://spaces/pvanand/specialized-agents-api/database.py
-
curl -L -o database.py https://huggingface.co/spaces/pvanand/specialized-agents-api/resolve/main/database.py
1.14 kB
| import sqlite3 | |
| import os | |
| from config import DB_PATH | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| def init_db(): | |
| logger.info("Initializing database") | |
| os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) | |
| conn = sqlite3.connect(DB_PATH) | |
| c = conn.cursor() | |
| c.execute('''CREATE TABLE IF NOT EXISTS conversations | |
| (id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| user_id TEXT, | |
| conversation_id TEXT, | |
| message TEXT, | |
| response TEXT, | |
| timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''') | |
| conn.commit() | |
| conn.close() | |
| logger.info("Database initialized successfully") | |
| def update_db(user_id, conversation_id, message, response): | |
| logger.info(f"Updating database for conversation: {conversation_id}") | |
| conn = sqlite3.connect(DB_PATH) | |
| c = conn.cursor() | |
| c.execute('''INSERT INTO conversations (user_id, conversation_id, message, response) | |
| VALUES (?, ?, ?, ?)''', (user_id, conversation_id, message, response)) | |
| conn.commit() | |
| conn.close() | |
| logger.info("Database updated successfully") |