Spaces:
Sleeping
Sleeping
Create app1.py
Browse files
app1.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import sqlite3
|
4 |
+
import glob
|
5 |
+
import pandas as pd
|
6 |
+
import gradio as gr
|
7 |
+
from datetime import datetime
|
8 |
+
from typing import Dict, List
|
9 |
+
|
10 |
+
# Directory to store SQLite results
|
11 |
+
db_dir = "results/"
|
12 |
+
|
13 |
+
def find_or_download_db():
|
14 |
+
"""Check if SQLite .db files exist; if not, attempt to download from cloud storage."""
|
15 |
+
if not os.path.exists(db_dir):
|
16 |
+
os.makedirs(db_dir)
|
17 |
+
db_files = glob.glob(os.path.join(db_dir, "*.db"))
|
18 |
+
|
19 |
+
# Ensure the random bot database exists
|
20 |
+
if "results/random_None.db" not in db_files:
|
21 |
+
raise FileNotFoundError("Please upload results for the random agent in a file named 'random_None.db'.")
|
22 |
+
|
23 |
+
return db_files
|
24 |
+
|
25 |
+
def extract_agent_info(filename: str):
|
26 |
+
"""Extract agent type and model name from the filename."""
|
27 |
+
base_name = os.path.basename(filename).replace(".db", "")
|
28 |
+
parts = base_name.split("_", 1)
|
29 |
+
if len(parts) == 2:
|
30 |
+
agent_type, model_name = parts
|
31 |
+
else:
|
32 |
+
agent_type, model_name = parts[0], "Unknown"
|
33 |
+
return agent_type, model_name
|
34 |
+
|
35 |
+
def extract_leaderboard_stats(game_name: str) -> pd.DataFrame:
|
36 |
+
"""Extract and aggregate leaderboard stats from all SQLite databases."""
|
37 |
+
db_files = find_or_download_db()
|
38 |
+
all_stats = []
|
39 |
+
|
40 |
+
for db_file in db_files:
|
41 |
+
conn = sqlite3.connect(db_file)
|
42 |
+
agent_type, model_name = extract_agent_info(db_file)
|
43 |
+
|
44 |
+
if game_name == "Total Performance":
|
45 |
+
query = "SELECT game_name, COUNT(DISTINCT episode) AS games_played, " \
|
46 |
+
"AVG(generation_time) AS avg_gen_time, SUM(reward) AS total_rewards " \
|
47 |
+
"FROM game_results GROUP BY game_name"
|
48 |
+
else:
|
49 |
+
query = "SELECT COUNT(DISTINCT episode) AS games_played, " \
|
50 |
+
"AVG(generation_time) AS avg_gen_time, SUM(reward) AS total_rewards " \
|
51 |
+
"FROM game_results WHERE game_name = ?"
|
52 |
+
|
53 |
+
df = pd.read_sql_query(query, conn, params=(game_name,) if game_name != "Total Performance" else None)
|
54 |
+
df["agent_name"] = model_name
|
55 |
+
df["agent_type"] = agent_type
|
56 |
+
all_stats.append(df)
|
57 |
+
conn.close()
|
58 |
+
|
59 |
+
leaderboard_df = pd.concat(all_stats, ignore_index=True) if all_stats else pd.DataFrame()
|
60 |
+
return leaderboard_df
|
61 |
+
|
62 |
+
def generate_leaderboard_json():
|
63 |
+
"""Generate a JSON file containing leaderboard stats."""
|
64 |
+
leaderboard = extract_leaderboard_stats("Total Performance").to_dict(orient="records")
|
65 |
+
json_file = "results/leaderboard_stats.json"
|
66 |
+
with open(json_file, "w", encoding="utf-8") as f:
|
67 |
+
json.dump({"timestamp": datetime.utcnow().isoformat(), "leaderboard": leaderboard}, f, indent=4)
|
68 |
+
return json_file
|
69 |
+
|
70 |
+
with gr.Blocks() as interface:
|
71 |
+
with gr.Tab("Leaderboard"):
|
72 |
+
gr.Markdown("# Leaderboard")
|
73 |
+
leaderboard_game_dropdown = gr.Dropdown(["Total Performance", "tic_tac_toe"], label="Select Game")
|
74 |
+
leaderboard_table = gr.Dataframe()
|
75 |
+
download_button = gr.File(label="Download Leaderboard")
|
76 |
+
refresh_button = gr.Button("Refresh Leaderboard")
|
77 |
+
leaderboard_game_dropdown.change(extract_leaderboard_stats, inputs=[leaderboard_game_dropdown], outputs=[leaderboard_table])
|
78 |
+
refresh_button.click(extract_leaderboard_stats, inputs=[leaderboard_game_dropdown], outputs=[leaderboard_table])
|
79 |
+
download_button.click(generate_leaderboard_json, outputs=[download_button])
|
80 |
+
|
81 |
+
interface.launch()
|