yuxwu commited on
Commit
911dbbb
·
1 Parent(s): 79fc8b6

Updated project homepage

Browse files
Files changed (1) hide show
  1. app.py +54 -37
app.py CHANGED
@@ -100,7 +100,7 @@ with gr.Blocks(css=css) as demo:
100
  with gr.Column(elem_id="col-container"):
101
  gr.Markdown("""# 🏟 ChatArena️<br>
102
  Prompting multiple AI agents to play games in a language-driven environment.
103
- **[Project Homepage](https://github.com/chatarena/chatarena)**""", elem_id="header")
104
 
105
  with gr.Row():
106
  env_selector = gr.Dropdown(choices=list(ENV_REGISTRY.keys()), value=DEFAULT_ENV, interactive=True,
@@ -117,7 +117,8 @@ Prompting multiple AI agents to play games in a language-driven environment.
117
  with gr.Row():
118
  with gr.Column(elem_id="col-chatbox"):
119
  with gr.Tab("All", visible=True):
120
- chatbot = gr.Chatbot(elem_id="chatbox", visible=True, show_label=False)
 
121
 
122
  player_chatbots = []
123
  for i in range(MAX_NUM_PLAYERS):
@@ -131,7 +132,8 @@ Prompting multiple AI agents to play games in a language-driven environment.
131
 
132
  with gr.Column(elem_id="col-config"): # Player Configuration
133
  # gr.Markdown("Player Configuration")
134
- parallel_checkbox = gr.Checkbox(label="Parallel Actions", value=False, visible=True)
 
135
  with gr.Accordion("Moderator", open=False, visible=True):
136
  moderator_components = get_moderator_components(True)
137
  all_components += [parallel_checkbox, *moderator_components]
@@ -143,14 +145,14 @@ Prompting multiple AI agents to play games in a language-driven environment.
143
  for i in range(MAX_NUM_PLAYERS):
144
  player_name = f"Player {i + 1}"
145
  with gr.Tab(player_name, visible=(i < DEFAULT_NUM_PLAYERS)) as tab:
146
- player_comps = get_player_components(player_name, visible=(i < DEFAULT_NUM_PLAYERS))
 
147
 
148
  players_idx2comp[i] = player_comps + [tab]
149
  all_players_components += player_comps + [tab]
150
 
151
  all_components += [num_player_slider] + all_players_components
152
 
153
-
154
  def variable_players(k):
155
  k = int(k)
156
  update_dict = {}
@@ -158,15 +160,17 @@ Prompting multiple AI agents to play games in a language-driven environment.
158
  if i < k:
159
  for comp in players_idx2comp[i]:
160
  update_dict[comp] = gr.update(visible=True)
161
- update_dict[player_chatbots[i]] = gr.update(visible=True)
 
162
  else:
163
  for comp in players_idx2comp[i]:
164
  update_dict[comp] = gr.update(visible=False)
165
- update_dict[player_chatbots[i]] = gr.update(visible=False)
 
166
  return update_dict
167
 
168
-
169
- num_player_slider.change(variable_players, num_player_slider, all_players_components + player_chatbots)
170
 
171
  human_input_textbox = gr.Textbox(show_label=True, label="Human Input", lines=1, visible=True,
172
  interactive=True, placeholder="Enter your input here")
@@ -176,14 +180,16 @@ Prompting multiple AI agents to play games in a language-driven environment.
176
 
177
  all_components += [human_input_textbox, btn_step, btn_restart]
178
 
179
-
180
  def _convert_to_chatbot_output(all_messages, display_recv=False):
181
  chatbot_output = []
182
  for i, message in enumerate(all_messages):
183
- agent_name, msg, recv = message.agent_name, message.content, str(message.visible_to)
184
- new_msg = re.sub(r'\n+', '<br>', msg.strip()) # Preprocess message for chatbot output
 
 
185
  if display_recv:
186
- new_msg = f"**{agent_name} (-> {recv})**: {new_msg}" # Add role to the message
 
187
  else:
188
  new_msg = f"**{agent_name}**: {new_msg}"
189
 
@@ -193,7 +199,6 @@ Prompting multiple AI agents to play games in a language-driven environment.
193
  chatbot_output.append((None, new_msg))
194
  return chatbot_output
195
 
196
-
197
  def _create_arena_config_from_components(all_comps: dict) -> ArenaConfig:
198
  env_desc = all_comps[env_desc_textbox]
199
 
@@ -240,10 +245,10 @@ Prompting multiple AI agents to play games in a language-driven environment.
240
  }
241
 
242
  # arena_config = {"players": player_configs, "environment": env_config}
243
- arena_config = ArenaConfig(players=player_configs, environment=env_config)
 
244
  return arena_config
245
 
246
-
247
  def step_game(all_comps: dict):
248
  yield {btn_step: gr.update(value="Running...", interactive=False),
249
  btn_restart: gr.update(interactive=False)}
@@ -283,14 +288,16 @@ Prompting multiple AI agents to play games in a language-driven environment.
283
  all_messages = timestep.observation # user sees what the moderator sees
284
  log_messages(arena, all_messages, database=DB)
285
 
286
- chatbot_output = _convert_to_chatbot_output(all_messages, display_recv=True)
 
287
  update_dict = {human_input_textbox: gr.Textbox.update(value=""),
288
  chatbot: chatbot_output,
289
  btn_step: gr.update(value="Next Step", interactive=not timestep.terminal),
290
  btn_restart: gr.update(interactive=True), state: cur_state}
291
  # Get the visible messages for each player
292
  for i, player in enumerate(arena.players):
293
- player_messages = arena.environment.get_observation(player.name)
 
294
  player_output = _convert_to_chatbot_output(player_messages)
295
  # Update the player's chatbot output
296
  update_dict[player_chatbots[i]] = player_output
@@ -300,7 +307,6 @@ Prompting multiple AI agents to play games in a language-driven environment.
300
 
301
  yield update_dict
302
 
303
-
304
  def restart_game(all_comps: dict):
305
  cur_state = all_comps[state]
306
  cur_state["arena"] = None
@@ -315,9 +321,9 @@ Prompting multiple AI agents to play games in a language-driven environment.
315
  yield {btn_step: gr.update(value="Start", interactive=True),
316
  btn_restart: gr.update(interactive=True), state: cur_state}
317
 
318
-
319
  # Remove Accordion and Tab from the list of components
320
- all_components = [comp for comp in all_components if not isinstance(comp, (gr.Accordion, gr.Tab))]
 
321
 
322
  # If any of the Textbox, Slider, Checkbox, Dropdown, RadioButtons is changed, the Step button is disabled
323
  for comp in all_components:
@@ -327,7 +333,6 @@ Prompting multiple AI agents to play games in a language-driven environment.
327
  else:
328
  return gr.update()
329
 
330
-
331
  if isinstance(comp,
332
  (gr.Textbox, gr.Slider, gr.Checkbox, gr.Dropdown, gr.Radio)) and comp is not human_input_textbox:
333
  comp.change(_disable_step_button, state, btn_step)
@@ -337,8 +342,8 @@ Prompting multiple AI agents to play games in a language-driven environment.
337
  btn_restart.click(restart_game, set(all_components + [state]),
338
  [chatbot, *player_chatbots, btn_step, btn_restart, state, human_input_textbox])
339
 
340
-
341
  # If an example is selected, update the components
 
342
  def update_components_from_example(all_comps: dict):
343
  example_name = all_comps[example_selector]
344
  example_config = EXAMPLE_REGISTRY[example_name]
@@ -346,36 +351,48 @@ Prompting multiple AI agents to play games in a language-driven environment.
346
 
347
  # Update the environment components
348
  env_config = example_config['environment']
349
- update_dict[env_desc_textbox] = gr.update(value=example_config['global_prompt'])
 
350
  update_dict[env_selector] = gr.update(value=env_config['env_type'])
351
- update_dict[parallel_checkbox] = gr.update(value=env_config['parallel'])
 
352
 
353
  # Update the moderator components
354
  if "moderator" in env_config:
355
  mod_role_desc, mod_terminal_condition, moderator_backend_type, mod_temp, mod_max_tokens = [
356
  c for c in moderator_components if not isinstance(c, (gr.Accordion, gr.Tab))
357
  ]
358
- update_dict[mod_role_desc] = gr.update(value=env_config['moderator']['role_desc'])
359
- update_dict[mod_terminal_condition] = gr.update(value=env_config['moderator']['terminal_condition'])
360
- update_dict[moderator_backend_type] = gr.update(value=env_config['moderator']['backend']['backend_type'])
361
- update_dict[mod_temp] = gr.update(value=env_config['moderator']['backend']['temperature'])
362
- update_dict[mod_max_tokens] = gr.update(value=env_config['moderator']['backend']['max_tokens'])
 
 
 
 
 
363
 
364
  # Update the player components
365
- update_dict[num_player_slider] = gr.update(value=len(example_config['players']))
 
366
  for i, player_config in enumerate(example_config['players']):
367
  role_desc, backend_type, temperature, max_tokens = [
368
  c for c in players_idx2comp[i] if not isinstance(c, (gr.Accordion, gr.Tab))
369
  ]
370
- update_dict[role_desc] = gr.update(value=player_config['role_desc'])
371
- update_dict[backend_type] = gr.update(value=player_config['backend']['backend_type'])
372
- update_dict[temperature] = gr.update(value=player_config['backend']['temperature'])
373
- update_dict[max_tokens] = gr.update(value=player_config['backend']['max_tokens'])
 
 
 
 
374
 
375
  return update_dict
376
 
377
-
378
- example_selector.change(update_components_from_example, set(all_components + [state]), all_components + [state])
379
 
380
  demo.queue()
381
  demo.launch(debug=DEBUG)
 
100
  with gr.Column(elem_id="col-container"):
101
  gr.Markdown("""# 🏟 ChatArena️<br>
102
  Prompting multiple AI agents to play games in a language-driven environment.
103
+ **[Project Homepage](https://www.chatarena.org)**""", elem_id="header")
104
 
105
  with gr.Row():
106
  env_selector = gr.Dropdown(choices=list(ENV_REGISTRY.keys()), value=DEFAULT_ENV, interactive=True,
 
117
  with gr.Row():
118
  with gr.Column(elem_id="col-chatbox"):
119
  with gr.Tab("All", visible=True):
120
+ chatbot = gr.Chatbot(
121
+ elem_id="chatbox", visible=True, show_label=False)
122
 
123
  player_chatbots = []
124
  for i in range(MAX_NUM_PLAYERS):
 
132
 
133
  with gr.Column(elem_id="col-config"): # Player Configuration
134
  # gr.Markdown("Player Configuration")
135
+ parallel_checkbox = gr.Checkbox(
136
+ label="Parallel Actions", value=False, visible=True)
137
  with gr.Accordion("Moderator", open=False, visible=True):
138
  moderator_components = get_moderator_components(True)
139
  all_components += [parallel_checkbox, *moderator_components]
 
145
  for i in range(MAX_NUM_PLAYERS):
146
  player_name = f"Player {i + 1}"
147
  with gr.Tab(player_name, visible=(i < DEFAULT_NUM_PLAYERS)) as tab:
148
+ player_comps = get_player_components(
149
+ player_name, visible=(i < DEFAULT_NUM_PLAYERS))
150
 
151
  players_idx2comp[i] = player_comps + [tab]
152
  all_players_components += player_comps + [tab]
153
 
154
  all_components += [num_player_slider] + all_players_components
155
 
 
156
  def variable_players(k):
157
  k = int(k)
158
  update_dict = {}
 
160
  if i < k:
161
  for comp in players_idx2comp[i]:
162
  update_dict[comp] = gr.update(visible=True)
163
+ update_dict[player_chatbots[i]
164
+ ] = gr.update(visible=True)
165
  else:
166
  for comp in players_idx2comp[i]:
167
  update_dict[comp] = gr.update(visible=False)
168
+ update_dict[player_chatbots[i]
169
+ ] = gr.update(visible=False)
170
  return update_dict
171
 
172
+ num_player_slider.change(
173
+ variable_players, num_player_slider, all_players_components + player_chatbots)
174
 
175
  human_input_textbox = gr.Textbox(show_label=True, label="Human Input", lines=1, visible=True,
176
  interactive=True, placeholder="Enter your input here")
 
180
 
181
  all_components += [human_input_textbox, btn_step, btn_restart]
182
 
 
183
  def _convert_to_chatbot_output(all_messages, display_recv=False):
184
  chatbot_output = []
185
  for i, message in enumerate(all_messages):
186
+ agent_name, msg, recv = message.agent_name, message.content, str(
187
+ message.visible_to)
188
+ # Preprocess message for chatbot output
189
+ new_msg = re.sub(r'\n+', '<br>', msg.strip())
190
  if display_recv:
191
+ # Add role to the message
192
+ new_msg = f"**{agent_name} (-> {recv})**: {new_msg}"
193
  else:
194
  new_msg = f"**{agent_name}**: {new_msg}"
195
 
 
199
  chatbot_output.append((None, new_msg))
200
  return chatbot_output
201
 
 
202
  def _create_arena_config_from_components(all_comps: dict) -> ArenaConfig:
203
  env_desc = all_comps[env_desc_textbox]
204
 
 
245
  }
246
 
247
  # arena_config = {"players": player_configs, "environment": env_config}
248
+ arena_config = ArenaConfig(
249
+ players=player_configs, environment=env_config)
250
  return arena_config
251
 
 
252
  def step_game(all_comps: dict):
253
  yield {btn_step: gr.update(value="Running...", interactive=False),
254
  btn_restart: gr.update(interactive=False)}
 
288
  all_messages = timestep.observation # user sees what the moderator sees
289
  log_messages(arena, all_messages, database=DB)
290
 
291
+ chatbot_output = _convert_to_chatbot_output(
292
+ all_messages, display_recv=True)
293
  update_dict = {human_input_textbox: gr.Textbox.update(value=""),
294
  chatbot: chatbot_output,
295
  btn_step: gr.update(value="Next Step", interactive=not timestep.terminal),
296
  btn_restart: gr.update(interactive=True), state: cur_state}
297
  # Get the visible messages for each player
298
  for i, player in enumerate(arena.players):
299
+ player_messages = arena.environment.get_observation(
300
+ player.name)
301
  player_output = _convert_to_chatbot_output(player_messages)
302
  # Update the player's chatbot output
303
  update_dict[player_chatbots[i]] = player_output
 
307
 
308
  yield update_dict
309
 
 
310
  def restart_game(all_comps: dict):
311
  cur_state = all_comps[state]
312
  cur_state["arena"] = None
 
321
  yield {btn_step: gr.update(value="Start", interactive=True),
322
  btn_restart: gr.update(interactive=True), state: cur_state}
323
 
 
324
  # Remove Accordion and Tab from the list of components
325
+ all_components = [comp for comp in all_components if not isinstance(
326
+ comp, (gr.Accordion, gr.Tab))]
327
 
328
  # If any of the Textbox, Slider, Checkbox, Dropdown, RadioButtons is changed, the Step button is disabled
329
  for comp in all_components:
 
333
  else:
334
  return gr.update()
335
 
 
336
  if isinstance(comp,
337
  (gr.Textbox, gr.Slider, gr.Checkbox, gr.Dropdown, gr.Radio)) and comp is not human_input_textbox:
338
  comp.change(_disable_step_button, state, btn_step)
 
342
  btn_restart.click(restart_game, set(all_components + [state]),
343
  [chatbot, *player_chatbots, btn_step, btn_restart, state, human_input_textbox])
344
 
 
345
  # If an example is selected, update the components
346
+
347
  def update_components_from_example(all_comps: dict):
348
  example_name = all_comps[example_selector]
349
  example_config = EXAMPLE_REGISTRY[example_name]
 
351
 
352
  # Update the environment components
353
  env_config = example_config['environment']
354
+ update_dict[env_desc_textbox] = gr.update(
355
+ value=example_config['global_prompt'])
356
  update_dict[env_selector] = gr.update(value=env_config['env_type'])
357
+ update_dict[parallel_checkbox] = gr.update(
358
+ value=env_config['parallel'])
359
 
360
  # Update the moderator components
361
  if "moderator" in env_config:
362
  mod_role_desc, mod_terminal_condition, moderator_backend_type, mod_temp, mod_max_tokens = [
363
  c for c in moderator_components if not isinstance(c, (gr.Accordion, gr.Tab))
364
  ]
365
+ update_dict[mod_role_desc] = gr.update(
366
+ value=env_config['moderator']['role_desc'])
367
+ update_dict[mod_terminal_condition] = gr.update(
368
+ value=env_config['moderator']['terminal_condition'])
369
+ update_dict[moderator_backend_type] = gr.update(
370
+ value=env_config['moderator']['backend']['backend_type'])
371
+ update_dict[mod_temp] = gr.update(
372
+ value=env_config['moderator']['backend']['temperature'])
373
+ update_dict[mod_max_tokens] = gr.update(
374
+ value=env_config['moderator']['backend']['max_tokens'])
375
 
376
  # Update the player components
377
+ update_dict[num_player_slider] = gr.update(
378
+ value=len(example_config['players']))
379
  for i, player_config in enumerate(example_config['players']):
380
  role_desc, backend_type, temperature, max_tokens = [
381
  c for c in players_idx2comp[i] if not isinstance(c, (gr.Accordion, gr.Tab))
382
  ]
383
+ update_dict[role_desc] = gr.update(
384
+ value=player_config['role_desc'])
385
+ update_dict[backend_type] = gr.update(
386
+ value=player_config['backend']['backend_type'])
387
+ update_dict[temperature] = gr.update(
388
+ value=player_config['backend']['temperature'])
389
+ update_dict[max_tokens] = gr.update(
390
+ value=player_config['backend']['max_tokens'])
391
 
392
  return update_dict
393
 
394
+ example_selector.change(update_components_from_example, set(
395
+ all_components + [state]), all_components + [state])
396
 
397
  demo.queue()
398
  demo.launch(debug=DEBUG)