File size: 9,967 Bytes
70830d6 d10df37 70830d6 683d749 70830d6 683d749 70830d6 683d749 d10df37 683d749 d10df37 683d749 d10df37 683d749 70830d6 683d749 70830d6 683d749 70830d6 683d749 70830d6 683d749 70830d6 683d749 70830d6 683d749 70830d6 683d749 70830d6 683d749 70830d6 683d749 70830d6 683d749 bae16e4 70830d6 c908a5c 70830d6 c908a5c 70830d6 c908a5c 70830d6 683d749 70830d6 683d749 70830d6 683d749 70830d6 683d749 3bcdbc2 eefcbe7 70830d6 683d749 eefcbe7 683d749 70830d6 683d749 3bcdbc2 6147079 3bcdbc2 70830d6 683d749 70830d6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
from arena.game import Game
from arena.board import RED, YELLOW
from arena.llm import LLM
import gradio as gr
import pandas as pd
css = """
.dataframe-fix .table-wrap {
min-height: 800px;
max-height: 800px;
}
footer{display:none !important}
"""
js = """
function refresh() {
const url = new URL(window.location);
if (url.searchParams.get('__theme') !== 'dark') {
url.searchParams.set('__theme', 'dark');
window.location.href = url.href;
}
}
"""
def message_html(game) -> str:
"""
Return the message for the top of the UI
"""
return (
f'<div style="text-align: center;font-size:18px">{game.board.message()}</div>'
)
def format_records_for_table(games):
"""
Turn the results objects into a pandas DataFrame for the Gradio Dataframe
"""
df = pd.DataFrame(
[
[
game.when,
game.red_player,
game.yellow_player,
"Red" if game.red_won else "Yellow" if game.yellow_won else "Draw",
]
for game in reversed(games)
],
columns=["When", "Red Player", "Yellow Player", "Winner"],
)
# Remove microseconds while preserving datetime format
df["When"] = pd.to_datetime(df["When"]).dt.floor("s")
return df
def format_ratings_for_table(ratings):
"""
Turn the ratings into a List of Lists for the Gradio Dataframe
"""
items = sorted(ratings.items(), key=lambda x: x[1], reverse=True)
return [[item[0], int(round(item[1]))] for item in items]
def load_callback(red_llm, yellow_llm):
"""
Callback called when the game is started. Create a new Game object for the state.
"""
game = Game(red_llm, yellow_llm)
enabled = gr.Button(interactive=True)
message = message_html(game)
return (
game,
game.board.svg(),
message,
"",
"",
enabled,
enabled,
enabled,
)
def leaderboard_callback(game):
"""
Callback called when the user switches to the Leaderboard tab. Load in the results.
"""
records_df = format_records_for_table(Game.get_games())
ratings_df = format_ratings_for_table(Game.get_ratings())
return records_df, ratings_df
def move_callback(game):
"""
Callback called when the user clicks to do a single move.
"""
game.move()
message = message_html(game)
if_active = gr.Button(interactive=game.board.is_active())
return (
game,
game.board.svg(),
message,
game.thoughts(RED),
game.thoughts(YELLOW),
if_active,
if_active,
)
def run_callback(game):
"""
Callback called when the user runs an entire game. Reset the board, run the game, store results.
Yield interim results so the UI updates.
"""
enabled = gr.Button(interactive=True)
disabled = gr.Button(interactive=False)
game.reset()
message = message_html(game)
yield game, game.board.svg(), message, game.thoughts(RED), game.thoughts(
YELLOW
), disabled, disabled, disabled
while game.board.is_active():
game.move()
message = message_html(game)
yield game, game.board.svg(), message, game.thoughts(RED), game.thoughts(
YELLOW
), disabled, disabled, disabled
game.record()
yield game, game.board.svg(), message, game.thoughts(RED), game.thoughts(
YELLOW
), disabled, disabled, enabled
def model_callback(player_name, game, new_model_name):
"""
Callback when the user changes the model
"""
player = game.players[player_name]
player.switch_model(new_model_name)
return game
def red_model_callback(game, new_model_name):
"""
Callback when red model is changed
"""
return model_callback(RED, game, new_model_name)
def yellow_model_callback(game, new_model_name):
"""
Callback when yellow model is changed
"""
return model_callback(YELLOW, game, new_model_name)
def player_section(name, default):
"""
Create the left and right sections of the UI
"""
all_model_names = LLM.all_model_names()
with gr.Row():
gr.HTML(f'<div style="text-align: center;font-size:18px">{name} Player</div>')
with gr.Row():
dropdown = gr.Dropdown(
all_model_names, value=default, label="LLM", interactive=True
)
with gr.Row():
gr.HTML('<div style="text-align: center;font-size:16px">Inner thoughts</div>')
with gr.Row():
thoughts = gr.HTML(label="Thoughts")
return thoughts, dropdown
def make_display():
"""
The Gradio UI to show the Game, with event handlers
"""
with gr.Blocks(
title="C4 Battle",
css=css,
js=js,
theme=gr.themes.Default(primary_hue="sky"),
) as blocks:
game = gr.State()
with gr.Tabs():
with gr.TabItem("Game"):
with gr.Row():
gr.HTML(
'<div style="text-align: center;font-size:24px">Four-in-a-row LLM Showdown</div>'
)
with gr.Row():
with gr.Column(scale=1):
red_thoughts, red_dropdown = player_section(
"Red", "gpt-4o-mini"
)
with gr.Column(scale=2):
with gr.Row():
message = gr.HTML(
'<div style="text-align: center;font-size:18px">The Board</div>'
)
with gr.Row():
board_display = gr.HTML()
with gr.Row():
with gr.Column(scale=1):
move_button = gr.Button("Next move")
with gr.Column(scale=1):
run_button = gr.Button("Run game", variant="primary")
with gr.Column(scale=1):
reset_button = gr.Button("Start Over", variant="stop")
with gr.Row():
gr.HTML(
'<div style="text-align: center;font-size:16px">See the <a href="https://youtu.be/0OF-ChlKOQY">video walkthrough</a> of the code and <a href="https://github.com/ed-donner/connect">clone</a> the repo</div>'
)
with gr.Column(scale=1):
yellow_thoughts, yellow_dropdown = player_section(
"Yellow", "claude-3-7-sonnet-latest"
)
with gr.TabItem("Leaderboard") as leaderboard_tab:
with gr.Row():
with gr.Column(scale=1):
ratings_df = gr.Dataframe(
headers=["Player", "ELO"],
label="Ratings",
column_widths=[2, 1],
wrap=True,
col_count=2,
row_count=10,
max_height=800,
elem_classes=["dataframe-fix"],
)
with gr.Column(scale=2):
results_df = gr.Dataframe(
headers=["When", "Red Player", "Yellow Player", "Winner"],
label="Game History",
column_widths=[2, 2, 2, 1],
wrap=True,
col_count=4,
row_count=10,
max_height=800,
elem_classes=["dataframe-fix"],
)
with gr.Row():
gr.HTML(
'<div style="text-align: center;font-size:16px">See the <a href="https://youtu.be/0OF-ChlKOQY">video walkthrough</a> of the code and <a href="https://github.com/ed-donner/connect">clone</a> the repo</div>'
)
blocks.load(
load_callback,
inputs=[red_dropdown, yellow_dropdown],
outputs=[
game,
board_display,
message,
red_thoughts,
yellow_thoughts,
move_button,
run_button,
reset_button,
],
)
move_button.click(
move_callback,
inputs=[game],
outputs=[
game,
board_display,
message,
red_thoughts,
yellow_thoughts,
move_button,
run_button,
],
)
red_dropdown.change(
red_model_callback, inputs=[game, red_dropdown], outputs=[game]
)
yellow_dropdown.change(
yellow_model_callback, inputs=[game, yellow_dropdown], outputs=[game]
)
run_button.click(
run_callback,
inputs=[game],
outputs=[
game,
board_display,
message,
red_thoughts,
yellow_thoughts,
move_button,
run_button,
reset_button,
],
)
reset_button.click(
load_callback,
inputs=[red_dropdown, yellow_dropdown],
outputs=[
game,
board_display,
message,
red_thoughts,
yellow_thoughts,
move_button,
run_button,
reset_button,
],
)
leaderboard_tab.select(
leaderboard_callback, inputs=[game], outputs=[results_df, ratings_df]
)
return blocks
|