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

Trying to bring back the game playing

Browse files
Files changed (1) hide show
  1. app.py +45 -25
app.py CHANGED
@@ -3,19 +3,18 @@ import json
3
  import pandas as pd
4
  import gradio as gr
5
  from agents.llm_registry import LLM_REGISTRY # Dynamically fetch LLM models
 
 
 
 
 
 
6
 
7
  # Extract available LLM models
8
  llm_models = list(LLM_REGISTRY.keys())
9
 
10
  # Define game list manually (for now)
11
- games_list = [
12
- "rock_paper_scissors",
13
- "prisoners_dilemma",
14
- "tic_tac_toe",
15
- "connect_four",
16
- "matching_pennies",
17
- "kuhn_poker",
18
- ]
19
 
20
  # File to persist results
21
  RESULTS_TRACKER_FILE = "results_tracker.json"
@@ -56,17 +55,47 @@ def calculate_leaderboard(selected_game: str) -> pd.DataFrame:
56
  leaderboard_df.rename(columns={"index": "LLM Model"}, inplace=True)
57
  return leaderboard_df
58
 
59
- def generate_stats_file(model_name: str):
60
- """Generate a JSON file with detailed statistics for the selected LLM model."""
61
- file_path = f"{model_name}_stats.json"
62
- with open(file_path, "w") as f:
63
- json.dump(results_tracker.get(model_name, {}), f, indent=4)
64
- return file_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  # Gradio Interface
67
  with gr.Blocks() as interface:
68
  with gr.Tab("Game Arena"):
69
- gr.Markdown("# LLM Game Arena\nPlay against LLMs or other players in classic games!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  with gr.Tab("Leaderboard"):
72
  gr.Markdown("# LLM Model Leaderboard\nTrack performance across different games!")
@@ -81,16 +110,7 @@ with gr.Blocks() as interface:
81
  """Updates the leaderboard table based on the selected game."""
82
  return calculate_leaderboard(selected_game)
83
 
84
- def provide_download_file(model_name):
85
- """Creates a downloadable JSON file with stats for the selected model."""
86
- return generate_stats_file(model_name)
87
-
88
- def refresh_leaderboard():
89
- """Manually refresh the leaderboard."""
90
- return calculate_leaderboard(game_dropdown.value)
91
-
92
  game_dropdown.change(fn=update_leaderboard, inputs=[game_dropdown], outputs=[leaderboard_table])
93
- model_dropdown.change(fn=provide_download_file, inputs=[model_dropdown], outputs=[download_button])
94
- refresh_button.click(fn=refresh_leaderboard, inputs=[], outputs=[leaderboard_table])
95
 
96
  interface.launch()
 
3
  import pandas as pd
4
  import gradio as gr
5
  from agents.llm_registry import LLM_REGISTRY # Dynamically fetch LLM models
6
+ from simulators.tic_tac_toe_simulator import TicTacToeSimulator
7
+ from simulators.prisoners_dilemma_simulator import PrisonersDilemmaSimulator
8
+ from simulators.rock_paper_scissors_simulator import RockPaperScissorsSimulator
9
+ from games_registry import GAMES_REGISTRY
10
+ from simulators.base_simulator import PlayerType
11
+ from typing import Dict
12
 
13
  # Extract available LLM models
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"
 
55
  leaderboard_df.rename(columns={"index": "LLM Model"}, inplace=True)
56
  return leaderboard_df
57
 
58
+ def play_game(game_name, player1_type, player2_type, player1_model, player2_model, rounds):
59
+ """Play the selected game with specified players."""
60
+ llms = {}
61
+ if player1_type == "llm":
62
+ llms["Player 1"] = player1_model
63
+ if player2_type == "llm":
64
+ llms["Player 2"] = player2_model
65
+
66
+ simulator_class = GAMES_REGISTRY[game_name]
67
+ simulator = simulator_class(game_name, llms=llms)
68
+ game_states = []
69
+
70
+ def log_fn(state):
71
+ """Log current state and legal moves."""
72
+ current_player = state.current_player()
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
 
80
  # Gradio Interface
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")
88
+ player1_model_dropdown = gr.Dropdown(choices=llm_models, label="Player 1 Model", visible=False)
89
+ player2_model_dropdown = gr.Dropdown(choices=llm_models, label="Player 2 Model", visible=False)
90
+ rounds_slider = gr.Slider(1, 10, step=1, label="Rounds")
91
+ result_output = gr.Textbox(label="Game Result")
92
+
93
+ play_button = gr.Button("Play Game")
94
+ play_button.click(
95
+ play_game,
96
+ inputs=[game_dropdown, player1_dropdown, player2_dropdown, player1_model_dropdown, player2_model_dropdown, rounds_slider],
97
+ outputs=result_output,
98
+ )
99
 
100
  with gr.Tab("Leaderboard"):
101
  gr.Markdown("# LLM Model Leaderboard\nTrack performance across different games!")
 
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
 
116
  interface.launch()