lcipolina commited on
Commit
84f0932
·
verified ·
1 Parent(s): 2fbda8c

Added a totaler in the drop down

Browse files
Files changed (1) hide show
  1. app.py +40 -37
app.py CHANGED
@@ -14,7 +14,15 @@ 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()) + ["Overall Leaderboard"]
 
 
 
 
 
 
 
 
18
 
19
  # File to persist results
20
  RESULTS_TRACKER_FILE = "results_tracker.json"
@@ -35,6 +43,17 @@ def save_results_tracker():
35
  with open(RESULTS_TRACKER_FILE, "w") 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,
@@ -48,7 +67,7 @@ def calculate_leaderboard(selected_game: str) -> pd.DataFrame:
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)
@@ -73,40 +92,24 @@ def calculate_leaderboard(selected_game: str) -> pd.DataFrame:
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")
84
- player1_model_dropdown = gr.Dropdown(choices=llm_models, label="Player 1 Model", visible=False)
85
- player2_model_dropdown = gr.Dropdown(choices=llm_models, label="Player 2 Model", visible=False)
86
- rounds_slider = gr.Slider(1, 10, step=1, label="Rounds")
87
- result_output = gr.Textbox(label="Game Result")
88
 
89
- play_button = gr.Button("Play Game")
90
- play_button.click(
91
- play_game,
92
- inputs=[game_dropdown, player1_dropdown, player2_dropdown, player1_model_dropdown, player2_model_dropdown, rounds_slider],
93
- outputs=result_output,
94
- )
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
-
112
- interface.launch()
 
14
  llm_models = list(LLM_REGISTRY.keys())
15
 
16
  # Define game list manually (for now)
17
+ games_list = [
18
+ "rock_paper_scissors",
19
+ "prisoners_dilemma",
20
+ "tic_tac_toe",
21
+ "connect_four",
22
+ "matching_pennies",
23
+ "kuhn_poker",
24
+ "Overall Leaderboard"
25
+ ]
26
 
27
  # File to persist results
28
  RESULTS_TRACKER_FILE = "results_tracker.json"
 
43
  with open(RESULTS_TRACKER_FILE, "w") as f:
44
  json.dump(results_tracker, f, indent=4)
45
 
46
+ def generate_stats_file(model_name: str):
47
+ """Generate a JSON file with detailed statistics for the selected LLM model."""
48
+ file_path = f"{model_name}_stats.json"
49
+ with open(file_path, "w") as f:
50
+ json.dump(results_tracker.get(model_name, {}), f, indent=4)
51
+ return file_path
52
+
53
+ def provide_download_file(model_name):
54
+ """Creates a downloadable JSON file with stats for the selected model."""
55
+ return generate_stats_file(model_name)
56
+
57
  def calculate_leaderboard(selected_game: str) -> pd.DataFrame:
58
  """Generate a structured leaderboard table for the selected game or overall."""
59
  leaderboard_df = pd.DataFrame(index=llm_models,
 
67
  total_illegal_moves = 0
68
  total_wins = 0
69
  total_vs_random = 0
70
+ for game in games_list[:-1]:
71
  game_stats = results_tracker[llm].get(game, {})
72
  total_games += game_stats.get("games", 0)
73
  total_moves += game_stats.get("moves/game", 0) * game_stats.get("games", 0)
 
92
  leaderboard_df.rename(columns={"index": "LLM Model"}, inplace=True)
93
  return leaderboard_df
94
 
95
+ def play_game(game_name, player1_type, player2_type, player1_model, player2_model, rounds):
96
+ """Play the selected game with specified players."""
97
+ llms = {}
98
+ if player1_type == "llm":
99
+ llms["Player 1"] = player1_model
100
+ if player2_type == "llm":
101
+ llms["Player 2"] = player2_model
 
 
 
 
 
102
 
103
+ simulator_class = GAMES_REGISTRY[game_name]
104
+ simulator = simulator_class(game_name, llms=llms)
105
+ game_states = []
 
 
 
106
 
107
+ def log_fn(state):
108
+ """Log current state and legal moves."""
109
+ current_player = state.current_player()
110
+ legal_moves = state.legal_actions(current_player)
111
+ board = str(state)
112
+ game_states.append(f"Current Player: {current_player}\nBoard:\n{board}\nLegal Moves: {legal_moves}")
113
+
114
+ results = simulator.simulate(rounds=int(rounds), log_fn=log_fn)
115
+ return "\n".join(game_states) + f"\nGame Result: {results}"