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 = """
Provider
Games
Wins
Losses
Win Rate
"""
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"""
{provider.title()}
{stats['games']}
{stats['wins']}
{stats['losses']}
{win_rate:.1f}%
"""
html += "
"
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("""