| import pandas as pd
|
| from config import Config
|
|
|
| class StatsManager:
|
| @staticmethod
|
| def get_stats():
|
| """Get usage statistics from Google Sheets"""
|
| try:
|
| if not hasattr(Config, "GOOGLE_SHEET_CSV_URL") or not Config.GOOGLE_SHEET_CSV_URL:
|
| return "<div class='error-stats'>β Google Sheet URL is not configured.</div>"
|
|
|
| df = pd.read_csv(Config.GOOGLE_SHEET_CSV_URL)
|
| return StatsManager._generate_stats_html(df)
|
|
|
| except Exception as e:
|
| return f"<div class='error-stats'>β Error loading stats: {str(e)}</div>"
|
|
|
| @staticmethod
|
| def _generate_stats_html(df):
|
| """Generate HTML for statistics display"""
|
| if df.empty:
|
| return "<div class='no-stats'>π No usage data available yet.</div>"
|
|
|
| total_predictions = len(df)
|
| sentiment_counts = df['sentiment'].value_counts()
|
| model_counts = df['model_used'].value_counts()
|
|
|
| stats_html = f"""
|
| <div class="stats-container">
|
| <h3 class="stats-title">π Usage Statistics</h3>
|
| <p class="stats-total"><strong>Total Predictions:</strong> {total_predictions}</p>
|
| <div class="stats-section">
|
| <p><strong>Sentiment Distribution:</strong></p>
|
| <ul class="stats-list">
|
| """
|
|
|
| for sentiment, count in sentiment_counts.items():
|
| percentage = (count / total_predictions) * 100
|
| stats_html += f"<li class='stats-item stats-{str(sentiment).lower()}'>π {str(sentiment).title()}: {count} ({percentage:.1f}%)</li>"
|
|
|
| stats_html += """
|
| </ul>
|
| </div>
|
| <div class="stats-section">
|
| <p><strong>Model Usage:</strong></p>
|
| <ul class="stats-list">
|
| """
|
|
|
| for model, count in model_counts.items():
|
| percentage = (count / total_predictions) * 100
|
| stats_html += f"<li class='stats-item'>π€ {model}: {count} ({percentage:.1f}%)</li>"
|
|
|
| stats_html += "</ul></div></div>"
|
| return stats_html
|
|
|