lcipolina commited on
Commit
fa1d454
·
verified ·
1 Parent(s): 106f4f6

Trying to bring back the games tab and the files download

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -14,11 +14,34 @@ from typing import Dict
14
  llm_models = list(LLM_REGISTRY.keys())
15
 
16
  # Define game list manually (for now)
17
- games_list = list(GAMES_REGISTRY.keys())
 
 
 
 
 
 
 
 
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:
@@ -37,10 +60,10 @@ def save_results_tracker():
37
 
38
  def calculate_leaderboard(selected_game: str) -> pd.DataFrame:
39
  """Generate a structured leaderboard table for the selected game."""
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
  game_stats = results_tracker[llm].get(selected_game, {})
46
  leaderboard_df.loc[llm] = [
@@ -50,7 +73,7 @@ def calculate_leaderboard(selected_game: str) -> pd.DataFrame:
50
  f"{game_stats.get('win-rate', 0):.1f}%",
51
  f"{game_stats.get('vs Random', 0):.1f}%"
52
  ]
53
-
54
  leaderboard_df = leaderboard_df.reset_index()
55
  leaderboard_df.rename(columns={"index": "LLM Model"}, inplace=True)
56
  return leaderboard_df
@@ -73,7 +96,7 @@ def play_game(game_name, player1_type, player2_type, player1_model, player2_mode
73
  legal_moves = state.legal_actions(current_player)
74
  board = str(state)
75
  game_states.append(f"Current Player: {current_player}\nBoard:\n{board}\nLegal Moves: {legal_moves}")
76
-
77
  results = simulator.simulate(rounds=int(rounds), log_fn=log_fn)
78
  return "\n".join(game_states) + f"\nGame Result: {results}"
79
 
@@ -81,7 +104,7 @@ def play_game(game_name, player1_type, player2_type, player1_model, player2_mode
81
  with gr.Blocks() as interface:
82
  with gr.Tab("Game Arena"):
83
  gr.Markdown("# LLM Game Arena\nSelect a game and players to play against LLMs.")
84
-
85
  game_dropdown = gr.Dropdown(choices=games_list, label="Select a Game", value=games_list[0])
86
  player1_dropdown = gr.Dropdown(choices=["human", "random_bot", "llm"], label="Player 1 Type", value="llm")
87
  player2_dropdown = gr.Dropdown(choices=["human", "random_bot", "llm"], label="Player 2 Type", value="random_bot")
@@ -99,17 +122,18 @@ with gr.Blocks() as interface:
99
 
100
  with gr.Tab("Leaderboard"):
101
  gr.Markdown("# LLM Model Leaderboard\nTrack performance across different games!")
102
-
103
  game_dropdown = gr.Dropdown(choices=games_list, label="Select Game", value=games_list[0])
104
  leaderboard_table = gr.Dataframe(value=calculate_leaderboard(games_list[0]), label="Leaderboard")
105
  model_dropdown = gr.Dropdown(choices=llm_models, label="Select LLM Model")
106
  download_button = gr.File(label="Download Statistics File")
107
  refresh_button = gr.Button("Refresh Leaderboard")
108
-
109
  def update_leaderboard(selected_game):
110
  """Updates the leaderboard table based on the selected game."""
111
  return calculate_leaderboard(selected_game)
112
-
 
113
  game_dropdown.change(fn=update_leaderboard, inputs=[game_dropdown], outputs=[leaderboard_table])
114
  refresh_button.click(fn=update_leaderboard, inputs=[game_dropdown], outputs=[leaderboard_table])
115
 
 
14
  llm_models = list(LLM_REGISTRY.keys())
15
 
16
  # Define game list manually (for now)
17
+ #games_list = list(GAMES_REGISTRY.keys())
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:
 
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
  game_stats = results_tracker[llm].get(selected_game, {})
69
  leaderboard_df.loc[llm] = [
 
73
  f"{game_stats.get('win-rate', 0):.1f}%",
74
  f"{game_stats.get('vs Random', 0):.1f}%"
75
  ]
76
+
77
  leaderboard_df = leaderboard_df.reset_index()
78
  leaderboard_df.rename(columns={"index": "LLM Model"}, inplace=True)
79
  return leaderboard_df
 
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
 
 
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
 
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