import gradio as gr import pandas as pd from support.database import get_stats from support.utils import display_model_name, format_game_history_html def create_model_stats_table(model_stats): """Create a DataFrame for model statistics""" if not model_stats: return pd.DataFrame() data = [] for model, stats in model_stats.items(): win_rate = (stats['wins'] / stats['games'] * 100) if stats['games'] > 0 else 0 data.append({ 'Model': display_model_name(model), 'Games': stats['games'], 'Wins': stats['wins'], 'Losses': stats['losses'], 'Win Rate': f"{win_rate:.1f}%" }) df = pd.DataFrame(data) df = df.sort_values('Games', ascending=False) return df # def create_provider_stats_table(provider_stats): # """Create a DataFrame for provider statistics""" # data = [] # for provider, stats in provider_stats.items(): # if stats['games'] > 0: # win_rate = (stats['wins'] / stats['games'] * 100) if stats['games'] > 0 else 0 # data.append({ # 'Provider': provider.title(), # 'Games': stats['games'], # 'Wins': stats['wins'], # 'Losses': stats['losses'], # 'Win Rate': f"{win_rate:.1f}%" # }) # df = pd.DataFrame(data) # df = df.sort_values('Games', ascending=False) # return df def create_provider_stats_html(provider_stats): html = """ """ for provider, stats in provider_stats.items(): if stats['games'] == 0: continue win_rate = (stats['wins'] / stats['games']) * 100 logo_url = f"/gradio_api/file=assets/providers/{provider}.png" # <= adjust path html += f""" """ html += "
Provider Games Wins Losses Win Rate
{provider.title()} {stats['games']} {stats['wins']} {stats['losses']} {win_rate:.1f}%
" return html def load_stats(): """Load and format all statistics""" stats = get_stats() total_games = stats['total_games'] game_history_html = format_game_history_html(stats['game_history']) model_df = create_model_stats_table(stats['model_stats']) # provider_df = create_provider_stats_table(stats['provider_stats']) provider_df = create_provider_stats_html(stats['provider_stats']) html_played_games = f"""
🎮 PLAYED GAMES
{total_games}
""" html_played_games = f"""
🎮
Total Games Played
{total_games}
""" return html_played_games, game_history_html, model_df, provider_df # Create the Gradio interface with gr.Blocks(elem_id="stats_container") as demo: with gr.Row(elem_classes="games_played"): gr.HTML("""

📊 Statistics Dashboard

""") refresh_btn = gr.Button("🔄 Refresh Stats", variant="primary", size="sm", elem_classes="refresh_btn", visible=True, elem_id="refresh_btn") with gr.Row(): total_games_display = gr.HTML() with gr.Tabs(elem_id="stats_tabs"): with gr.Tab("🎮 Game History"): game_history_display = gr.HTML(label="Game History") with gr.Tab("🤖 Model Statistics", elem_classes="model_stats_tab"): gr.Markdown("### Individual Model Performance", elem_classes="stats_title") model_stats_table = gr.Dataframe( label="Model Statistics", interactive=False, wrap=True, elem_classes="stats_table" ) with gr.Tab("🏢 Provider Statistics", elem_classes="grouped_stats_tab"): gr.Markdown("### Provider Performance (Grouped)", elem_classes="stats_title") # provider_stats_table = gr.Dataframe( # label="Provider Statistics", # interactive=False, # wrap=True, # elem_classes="stats_table" # ) provider_stats_table = gr.HTML(label="Provider Stats") # Load stats on page load demo.load( fn=load_stats, outputs=[total_games_display, game_history_display, model_stats_table, provider_stats_table] ) # Refresh button refresh_btn.click( fn=load_stats, outputs=[total_games_display, game_history_display, model_stats_table, provider_stats_table] ) if __name__ == "__main__": demo.launch()