Spaces:
Sleeping
Sleeping
Added an 'overall' tab
Browse files
app.py
CHANGED
@@ -14,34 +14,11 @@ from typing import Dict
|
|
14 |
llm_models = list(LLM_REGISTRY.keys())
|
15 |
|
16 |
# Define game list manually (for now)
|
17 |
-
|
18 |
-
games_list = [
|
19 |
-
"rock_paper_scissors",
|
20 |
-
"prisoners_dilemma",
|
21 |
-
"tic_tac_toe",
|
22 |
-
"connect_four",
|
23 |
-
"matching_pennies",
|
24 |
-
"kuhn_poker",
|
25 |
-
]
|
26 |
|
27 |
# File to persist results
|
28 |
RESULTS_TRACKER_FILE = "results_tracker.json"
|
29 |
|
30 |
-
def generate_stats_file(model_name: str):
|
31 |
-
"""Generate a JSON file with detailed statistics for the selected LLM model."""
|
32 |
-
file_path = f"{model_name}_stats.json"
|
33 |
-
with open(file_path, "w") as f:
|
34 |
-
json.dump(results_tracker.get(model_name, {}), f, indent=4)
|
35 |
-
return file_path
|
36 |
-
|
37 |
-
def provide_download_file(model_name):
|
38 |
-
"""Creates a downloadable JSON file with stats for the selected model."""
|
39 |
-
return generate_stats_file(model_name)
|
40 |
-
|
41 |
-
def refresh_leaderboard():
|
42 |
-
"""Manually refresh the leaderboard."""
|
43 |
-
return calculate_leaderboard(game_dropdown.value)
|
44 |
-
|
45 |
# Load or initialize the results tracker
|
46 |
if os.path.exists(RESULTS_TRACKER_FILE):
|
47 |
with open(RESULTS_TRACKER_FILE, "r") as f:
|
@@ -59,52 +36,48 @@ def save_results_tracker():
|
|
59 |
json.dump(results_tracker, f, indent=4)
|
60 |
|
61 |
def calculate_leaderboard(selected_game: str) -> pd.DataFrame:
|
62 |
-
"""Generate a structured leaderboard table for the selected game."""
|
63 |
-
leaderboard_df = pd.DataFrame(index=llm_models,
|
64 |
-
columns=["# games", "moves/game",
|
65 |
"illegal-moves", "win-rate", "vs Random"])
|
66 |
-
|
67 |
for llm in llm_models:
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
leaderboard_df = leaderboard_df.reset_index()
|
78 |
leaderboard_df.rename(columns={"index": "LLM Model"}, inplace=True)
|
79 |
return leaderboard_df
|
80 |
|
81 |
-
def play_game(game_name, player1_type, player2_type, player1_model, player2_model, rounds):
|
82 |
-
"""Play the selected game with specified players."""
|
83 |
-
llms = {}
|
84 |
-
if player1_type == "llm":
|
85 |
-
llms["Player 1"] = player1_model
|
86 |
-
if player2_type == "llm":
|
87 |
-
llms["Player 2"] = player2_model
|
88 |
-
|
89 |
-
simulator_class = GAMES_REGISTRY[game_name]
|
90 |
-
simulator = simulator_class(game_name, llms=llms)
|
91 |
-
game_states = []
|
92 |
-
|
93 |
-
def log_fn(state):
|
94 |
-
"""Log current state and legal moves."""
|
95 |
-
current_player = state.current_player()
|
96 |
-
legal_moves = state.legal_actions(current_player)
|
97 |
-
board = str(state)
|
98 |
-
game_states.append(f"Current Player: {current_player}\nBoard:\n{board}\nLegal Moves: {legal_moves}")
|
99 |
-
|
100 |
-
results = simulator.simulate(rounds=int(rounds), log_fn=log_fn)
|
101 |
-
return "\n".join(game_states) + f"\nGame Result: {results}"
|
102 |
-
|
103 |
# Gradio Interface
|
104 |
with gr.Blocks() as interface:
|
105 |
with gr.Tab("Game Arena"):
|
106 |
gr.Markdown("# LLM Game Arena\nSelect a game and players to play against LLMs.")
|
107 |
-
|
108 |
game_dropdown = gr.Dropdown(choices=games_list, label="Select a Game", value=games_list[0])
|
109 |
player1_dropdown = gr.Dropdown(choices=["human", "random_bot", "llm"], label="Player 1 Type", value="llm")
|
110 |
player2_dropdown = gr.Dropdown(choices=["human", "random_bot", "llm"], label="Player 2 Type", value="random_bot")
|
@@ -122,18 +95,17 @@ with gr.Blocks() as interface:
|
|
122 |
|
123 |
with gr.Tab("Leaderboard"):
|
124 |
gr.Markdown("# LLM Model Leaderboard\nTrack performance across different games!")
|
125 |
-
|
126 |
game_dropdown = gr.Dropdown(choices=games_list, label="Select Game", value=games_list[0])
|
127 |
leaderboard_table = gr.Dataframe(value=calculate_leaderboard(games_list[0]), label="Leaderboard")
|
128 |
model_dropdown = gr.Dropdown(choices=llm_models, label="Select LLM Model")
|
129 |
download_button = gr.File(label="Download Statistics File")
|
130 |
refresh_button = gr.Button("Refresh Leaderboard")
|
131 |
-
|
132 |
def update_leaderboard(selected_game):
|
133 |
-
"""Updates the leaderboard table based on the selected game."""
|
134 |
return calculate_leaderboard(selected_game)
|
135 |
-
|
136 |
-
model_dropdown.change(fn=provide_download_file, inputs=[model_dropdown], outputs=[download_button])
|
137 |
game_dropdown.change(fn=update_leaderboard, inputs=[game_dropdown], outputs=[leaderboard_table])
|
138 |
refresh_button.click(fn=update_leaderboard, inputs=[game_dropdown], outputs=[leaderboard_table])
|
139 |
|
|
|
14 |
llm_models = list(LLM_REGISTRY.keys())
|
15 |
|
16 |
# Define game list manually (for now)
|
17 |
+
games_list = list(GAMES_REGISTRY.keys()) + ["Overall Leaderboard"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# File to persist results
|
20 |
RESULTS_TRACKER_FILE = "results_tracker.json"
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Load or initialize the results tracker
|
23 |
if os.path.exists(RESULTS_TRACKER_FILE):
|
24 |
with open(RESULTS_TRACKER_FILE, "r") as f:
|
|
|
36 |
json.dump(results_tracker, f, indent=4)
|
37 |
|
38 |
def calculate_leaderboard(selected_game: str) -> pd.DataFrame:
|
39 |
+
"""Generate a structured leaderboard table for the selected game or overall."""
|
40 |
+
leaderboard_df = pd.DataFrame(index=llm_models,
|
41 |
+
columns=["# games", "moves/game",
|
42 |
"illegal-moves", "win-rate", "vs Random"])
|
43 |
+
|
44 |
for llm in llm_models:
|
45 |
+
if selected_game == "Overall Leaderboard":
|
46 |
+
total_games = 0
|
47 |
+
total_moves = 0
|
48 |
+
total_illegal_moves = 0
|
49 |
+
total_wins = 0
|
50 |
+
total_vs_random = 0
|
51 |
+
for game in GAMES_REGISTRY.keys():
|
52 |
+
game_stats = results_tracker[llm].get(game, {})
|
53 |
+
total_games += game_stats.get("games", 0)
|
54 |
+
total_moves += game_stats.get("moves/game", 0) * game_stats.get("games", 0)
|
55 |
+
total_illegal_moves += game_stats.get("illegal-moves", 0)
|
56 |
+
total_wins += (game_stats.get("win-rate", 0) * game_stats.get("games", 0)) / 100
|
57 |
+
total_vs_random += (game_stats.get("vs Random", 0) * game_stats.get("games", 0)) / 100
|
58 |
+
avg_moves = total_moves / total_games if total_games > 0 else 0
|
59 |
+
avg_win_rate = (total_wins / total_games) * 100 if total_games > 0 else 0
|
60 |
+
avg_vs_random = (total_vs_random / total_games) * 100 if total_games > 0 else 0
|
61 |
+
leaderboard_df.loc[llm] = [total_games, avg_moves, total_illegal_moves, f"{avg_win_rate:.1f}%", f"{avg_vs_random:.1f}%"]
|
62 |
+
else:
|
63 |
+
game_stats = results_tracker[llm].get(selected_game, {})
|
64 |
+
leaderboard_df.loc[llm] = [
|
65 |
+
game_stats.get("games", 0),
|
66 |
+
game_stats.get("moves/game", 0),
|
67 |
+
game_stats.get("illegal-moves", 0),
|
68 |
+
f"{game_stats.get('win-rate', 0):.1f}%",
|
69 |
+
f"{game_stats.get('vs Random', 0):.1f}%"
|
70 |
+
]
|
71 |
+
|
72 |
leaderboard_df = leaderboard_df.reset_index()
|
73 |
leaderboard_df.rename(columns={"index": "LLM Model"}, inplace=True)
|
74 |
return leaderboard_df
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
# Gradio Interface
|
77 |
with gr.Blocks() as interface:
|
78 |
with gr.Tab("Game Arena"):
|
79 |
gr.Markdown("# LLM Game Arena\nSelect a game and players to play against LLMs.")
|
80 |
+
|
81 |
game_dropdown = gr.Dropdown(choices=games_list, label="Select a Game", value=games_list[0])
|
82 |
player1_dropdown = gr.Dropdown(choices=["human", "random_bot", "llm"], label="Player 1 Type", value="llm")
|
83 |
player2_dropdown = gr.Dropdown(choices=["human", "random_bot", "llm"], label="Player 2 Type", value="random_bot")
|
|
|
95 |
|
96 |
with gr.Tab("Leaderboard"):
|
97 |
gr.Markdown("# LLM Model Leaderboard\nTrack performance across different games!")
|
98 |
+
|
99 |
game_dropdown = gr.Dropdown(choices=games_list, label="Select Game", value=games_list[0])
|
100 |
leaderboard_table = gr.Dataframe(value=calculate_leaderboard(games_list[0]), label="Leaderboard")
|
101 |
model_dropdown = gr.Dropdown(choices=llm_models, label="Select LLM Model")
|
102 |
download_button = gr.File(label="Download Statistics File")
|
103 |
refresh_button = gr.Button("Refresh Leaderboard")
|
104 |
+
|
105 |
def update_leaderboard(selected_game):
|
106 |
+
"""Updates the leaderboard table based on the selected game or overall."""
|
107 |
return calculate_leaderboard(selected_game)
|
108 |
+
|
|
|
109 |
game_dropdown.change(fn=update_leaderboard, inputs=[game_dropdown], outputs=[leaderboard_table])
|
110 |
refresh_button.click(fn=update_leaderboard, inputs=[game_dropdown], outputs=[leaderboard_table])
|
111 |
|