Spaces:
Sleeping
Sleeping
Download database.py from brightmind-ai/test: direct link, hf CLI and curl.
- Browser
- Download file 4.52 kB
-
https://huggingface.co/spaces/brightmind-ai/test/resolve/main/database.py
- Command line
-
hf download hf://spaces/brightmind-ai/test/database.py
-
curl -L -o database.py https://huggingface.co/spaces/brightmind-ai/test/resolve/main/database.py
4.52 kB
| """ | |
| Database module for BrightMind AI | |
| Handles all database operations including feedback storage and retrieval | |
| """ | |
| import sqlite3 | |
| from datetime import datetime | |
| from config import DATABASE_FILE | |
| def init_database(): | |
| """Initialize the SQLite database""" | |
| try: | |
| conn = sqlite3.connect(DATABASE_FILE) | |
| cursor = conn.cursor() | |
| # Create feedback table | |
| cursor.execute(''' | |
| CREATE TABLE IF NOT EXISTS feedback ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| timestamp TEXT NOT NULL, | |
| feedback_type TEXT NOT NULL, | |
| rating INTEGER NOT NULL, | |
| comments TEXT NOT NULL, | |
| user_email TEXT, | |
| created_at DATETIME DEFAULT CURRENT_TIMESTAMP | |
| ) | |
| ''') | |
| conn.commit() | |
| conn.close() | |
| print("✅ Database initialized successfully") | |
| return True | |
| except Exception as e: | |
| print(f"❌ Database initialization error: {e}") | |
| return False | |
| def save_feedback_to_db(feedback_type, rating, comments, user_email=""): | |
| """Save feedback to SQLite database""" | |
| try: | |
| conn = sqlite3.connect(DATABASE_FILE) | |
| cursor = conn.cursor() | |
| # Convert feedback_type list to string | |
| feedback_type_str = ', '.join(feedback_type) if isinstance(feedback_type, list) else str(feedback_type) | |
| cursor.execute(''' | |
| INSERT INTO feedback (timestamp, feedback_type, rating, comments, user_email) | |
| VALUES (?, ?, ?, ?, ?) | |
| ''', ( | |
| datetime.now().isoformat(), | |
| feedback_type_str, | |
| rating, | |
| comments, | |
| user_email | |
| )) | |
| conn.commit() | |
| conn.close() | |
| print("✅ Feedback saved to database successfully") | |
| return True | |
| except Exception as e: | |
| print(f"❌ Database save error: {e}") | |
| return False | |
| def get_feedback_stats(): | |
| """Get feedback statistics for admin""" | |
| try: | |
| conn = sqlite3.connect(DATABASE_FILE) | |
| cursor = conn.cursor() | |
| # Get total feedback count | |
| cursor.execute('SELECT COUNT(*) FROM feedback') | |
| total_feedback = cursor.fetchone()[0] | |
| # Get average rating | |
| cursor.execute('SELECT AVG(rating) FROM feedback') | |
| avg_rating = cursor.fetchone()[0] | |
| avg_rating = round(avg_rating, 2) if avg_rating else 0 | |
| # Get recent feedback (last 5) | |
| cursor.execute(''' | |
| SELECT timestamp, feedback_type, rating, comments, user_email | |
| FROM feedback | |
| ORDER BY created_at DESC | |
| LIMIT 5 | |
| ''') | |
| recent_feedback = cursor.fetchall() | |
| conn.close() | |
| return { | |
| 'total_feedback': total_feedback, | |
| 'avg_rating': avg_rating, | |
| 'recent_feedback': recent_feedback | |
| } | |
| except Exception as e: | |
| print(f"❌ Database stats error: {e}") | |
| return { | |
| 'total_feedback': 0, | |
| 'avg_rating': 0, | |
| 'recent_feedback': [] | |
| } | |
| def view_feedback_admin(): | |
| """Admin function to view all feedback""" | |
| try: | |
| conn = sqlite3.connect(DATABASE_FILE) | |
| cursor = conn.cursor() | |
| cursor.execute(''' | |
| SELECT id, timestamp, feedback_type, rating, comments, user_email, created_at | |
| FROM feedback | |
| ORDER BY created_at DESC | |
| ''') | |
| feedback_data = cursor.fetchall() | |
| conn.close() | |
| if not feedback_data: | |
| return "No feedback found in database." | |
| # Format feedback data | |
| result = "# 📊 Feedback Database\n\n" | |
| result += f"**Total Feedback Entries:** {len(feedback_data)}\n\n" | |
| for entry in feedback_data: | |
| id_val, timestamp, feedback_type, rating, comments, user_email, created_at = entry | |
| result += f"## Entry #{id_val}\n" | |
| result += f"**Date:** {timestamp}\n" | |
| result += f"**Type:** {feedback_type}\n" | |
| result += f"**Rating:** {rating}/5 ⭐\n" | |
| result += f"**Comments:** {comments}\n" | |
| result += f"**Email:** {user_email if user_email else 'Not provided'}\n" | |
| result += f"**Created:** {created_at}\n" | |
| result += "---\n\n" | |
| return result | |
| except Exception as e: | |
| return f"❌ Error accessing database: {str(e)}" | |