mlabonne commited on
Commit
8cf5324
1 Parent(s): 2fc92d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -18,8 +18,11 @@ from tqdm.auto import tqdm
18
  from PIL import Image as PILImage
19
  import gradio as gr
20
 
21
- # Generate regular expression for legal moves
22
  def generate_regex(board):
 
 
 
23
  legal_moves = list(board.legal_moves)
24
  move_strings = [board.san(move) for move in legal_moves]
25
  move_strings = [re.sub(r"[+#]", "", move) for move in move_strings]
@@ -69,6 +72,7 @@ def determine_termination(board, time_budget_white, time_budget_black):
69
  else:
70
  return "Unknown"
71
 
 
72
  def format_elapsed(seconds):
73
  """Formats elapsed time dynamically to hh:mm:ss, mm:ss, or ss format."""
74
  hours, remainder = divmod(int(seconds), 3600)
@@ -80,6 +84,7 @@ def format_elapsed(seconds):
80
  else:
81
  return f"{seconds:02d}"
82
 
 
83
  def create_gif(image_list, gif_path, duration):
84
  # Convert numpy arrays back to PIL images
85
  pil_images = [PILImage.fromarray(image) for image in image_list]
@@ -89,6 +94,7 @@ def create_gif(image_list, gif_path, duration):
89
  gif_path, save_all=True, append_images=pil_images[1:], duration=duration, loop=0
90
  )
91
 
 
92
  def render_init(board):
93
  svg = chess.svg.board(board=board).encode("utf-8")
94
  png = cairosvg.svg2png(bytestring=svg)
@@ -110,6 +116,7 @@ def render_init(board):
110
 
111
  return new_image
112
 
 
113
  def render_new(board):
114
  last_move = board.peek()
115
  svg = chess.svg.board(board=board, arrows=[(last_move.from_square, last_move.to_square)]).encode("utf-8")
@@ -131,7 +138,8 @@ def render_new(board):
131
  new_image.paste(image, (left, upper))
132
 
133
  return new_image
134
-
 
135
  def save_result_file(
136
  pgn_id, model_id_white, model_id_black, termination, result, auth_token, gist_id
137
  ):
@@ -182,6 +190,12 @@ def download_file(base_url, file_name):
182
  print(f"Failed to download file. HTTP status code: {response.status_code}")
183
 
184
 
 
 
 
 
 
 
185
  def calculate_elo(rank1, rank2, result):
186
  """
187
  Calculate the new ELO rating for a player.
@@ -412,9 +426,8 @@ LEAD_GIST_ID = "696115fe2df47fb2350fcff2663678c9"
412
  # Download results and leaderboard
413
  url1 = (f"https://gist.githubusercontent.com/chessllm/{RESULT_GIST_ID}/raw")
414
  download_file(url1, "chessllm_results.csv")
415
- url2 = f"https://gist.githubusercontent.com/chessllm/{LEAD_GIST_ID}/raw"
416
- download_file(url2, "chessllm_leaderboard.csv")
417
- elo_ratings_df = pd.read_csv("chessllm_leaderboard.csv")
418
 
419
  title = """
420
  <div align="center">
@@ -434,6 +447,6 @@ with gr.Blocks() as demo:
434
  out = gr.Image(width=256)
435
  btn.click(fn=update, inputs=[model_id_white, model_id_black], outputs=out)
436
  gr.Markdown('<div align="center"><p style="font-size: 36px;">🏆 Leaderboard</p></div>')
437
- leaderboard = gr.Dataframe(value=elo_ratings_df)
438
 
439
  demo.queue(api_open=False).launch(show_api=False)
 
18
  from PIL import Image as PILImage
19
  import gradio as gr
20
 
21
+
22
  def generate_regex(board):
23
+ """
24
+ Generate regular expression for legal moves.
25
+ """
26
  legal_moves = list(board.legal_moves)
27
  move_strings = [board.san(move) for move in legal_moves]
28
  move_strings = [re.sub(r"[+#]", "", move) for move in move_strings]
 
72
  else:
73
  return "Unknown"
74
 
75
+
76
  def format_elapsed(seconds):
77
  """Formats elapsed time dynamically to hh:mm:ss, mm:ss, or ss format."""
78
  hours, remainder = divmod(int(seconds), 3600)
 
84
  else:
85
  return f"{seconds:02d}"
86
 
87
+
88
  def create_gif(image_list, gif_path, duration):
89
  # Convert numpy arrays back to PIL images
90
  pil_images = [PILImage.fromarray(image) for image in image_list]
 
94
  gif_path, save_all=True, append_images=pil_images[1:], duration=duration, loop=0
95
  )
96
 
97
+
98
  def render_init(board):
99
  svg = chess.svg.board(board=board).encode("utf-8")
100
  png = cairosvg.svg2png(bytestring=svg)
 
116
 
117
  return new_image
118
 
119
+
120
  def render_new(board):
121
  last_move = board.peek()
122
  svg = chess.svg.board(board=board, arrows=[(last_move.from_square, last_move.to_square)]).encode("utf-8")
 
138
  new_image.paste(image, (left, upper))
139
 
140
  return new_image
141
+
142
+
143
  def save_result_file(
144
  pgn_id, model_id_white, model_id_black, termination, result, auth_token, gist_id
145
  ):
 
190
  print(f"Failed to download file. HTTP status code: {response.status_code}")
191
 
192
 
193
+ def get_leaderboard():
194
+ url = f"https://gist.githubusercontent.com/chessllm/{LEAD_GIST_ID}/raw"
195
+ download_file(url, "chessllm_leaderboard.csv")
196
+ return pd.read_csv("chessllm_leaderboard.csv")
197
+
198
+
199
  def calculate_elo(rank1, rank2, result):
200
  """
201
  Calculate the new ELO rating for a player.
 
426
  # Download results and leaderboard
427
  url1 = (f"https://gist.githubusercontent.com/chessllm/{RESULT_GIST_ID}/raw")
428
  download_file(url1, "chessllm_results.csv")
429
+ elo_ratings_df = get_leaderboard()
430
+
 
431
 
432
  title = """
433
  <div align="center">
 
447
  out = gr.Image(width=256)
448
  btn.click(fn=update, inputs=[model_id_white, model_id_black], outputs=out)
449
  gr.Markdown('<div align="center"><p style="font-size: 36px;">🏆 Leaderboard</p></div>')
450
+ leaderboard = gr.Dataframe(value=get_leaderboard, every=60)
451
 
452
  demo.queue(api_open=False).launch(show_api=False)