File size: 5,450 Bytes
aa2d45f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
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 = """
<table class="provider-table">
<tr>
<th>Provider</th>
<th>Games</th>
<th>Wins</th>
<th>Losses</th>
<th>Win Rate</th>
</tr>
"""
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"""
<tr>
<td class="logo_cell"><img src="{logo_url}" class="provider-logo"> {provider.title()}</td>
<td>{stats['games']}</td>
<td>{stats['wins']}</td>
<td>{stats['losses']}</td>
<td>{win_rate:.1f}%</td>
</tr>
"""
html += "</table>"
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"""
<div class="played-games-container">
<div class="played-games-label">๐ฎ PLAYED GAMES</div>
<div class="played-games-count">{total_games}</div>
</div>
"""
html_played_games = f"""
<div class="stats-card">
<div class="stats-icon">๐ฎ</div>
<div class="stats-content">
<div class="stats-label">Total Games Played</div>
<div class="stats-value">{total_games}</div>
</div>
</div>
"""
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("""
<div style="display: flex; align-items: center; gap: 20px; margin-left: 1%; margin-right: 1%;">
<h1 style="margin: 0;">๐ Statistics Dashboard</h1>
<button onclick='refreshStats()' class='custom-refresh-btn'>
๐ Refresh Stats
</button>
</div>
""")
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()
|