philipp-zettl commited on
Commit
1375e49
1 Parent(s): 46773aa

implement interactive ui

Browse files
Files changed (2) hide show
  1. app.py +109 -29
  2. model.py +2 -2
app.py CHANGED
@@ -34,28 +34,58 @@ tokenizer = Tokenizer.from_pretrained(tokenizer_path)
34
 
35
  invalid_move_plot = Image.open('./invalid_move.png')
36
 
37
- def generate(prompt):
38
- model_input = torch.tensor(tokenizer.encode(prompt), dtype=torch.long, device=device).view((1, len(prompt)))
39
- pgn = tokenizer.decode(model.generate(model_input, max_new_tokens=4, context_size=context_size)[0].tolist())
 
 
 
 
 
 
 
 
 
 
40
  pgn_str = StringIO(pgn)
41
  try:
42
  game = chess.pgn.read_game(pgn_str)
43
  board = game.board()
44
  for move in game.mainline_moves():
45
  board.push(move)
46
- img = chess.svg.board(board)
47
  except Exception as e:
48
  if 'illegal san' in str(e):
49
- return pgn, invalid_move_plot
50
- filename = f'./moves-{str(uuid4())}'
51
- with open(filename + '.svg', 'w') as f:
52
- f.write(img)
53
- drawing = svg2rlg(filename + '.svg')
54
- renderPM.drawToFile(drawing, f"{filename}.png", fmt="PNG")
55
- plot = Image.open(f'{filename}.png')
56
 
57
- os.remove(f'{filename}.png')
58
- os.remove(f'{filename}.svg')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  return pgn, plot
60
 
61
 
@@ -66,21 +96,71 @@ with gr.Blocks() as demo:
66
 
67
  The **C**hess-**P**re-trained-**T**ransformer.
68
 
69
- The rules are simple: provide a PGN string of your current game, the engine will predict the next token!
 
 
70
  """)
71
- prompt = gr.Text(label="PGN")
72
- output = gr.Text(label="Next turn", interactive=False)
73
- img = gr.Image()
74
- submit = gr.Button("Submit")
75
- submit.click(generate, [prompt], [output, img])
76
-
77
- gr.Examples(
78
- [
79
- ["1. e4", ],
80
- ["1. e4 g6 2."],
81
- ],
82
- inputs=[prompt],
83
- outputs=[output, img],
84
- fn=generate
85
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  demo.launch()
 
34
 
35
  invalid_move_plot = Image.open('./invalid_move.png')
36
 
37
+ def gen_image_from_svg(img, filename):
38
+ with open(filename + '.svg', 'w') as f:
39
+ f.write(img)
40
+ drawing = svg2rlg(filename + '.svg')
41
+ renderPM.drawToFile(drawing, f"{filename}.png", fmt="PNG")
42
+ plot = Image.open(f'{filename}.png')
43
+
44
+ os.remove(f'{filename}.png')
45
+ os.remove(f'{filename}.svg')
46
+ return plot
47
+
48
+
49
+ def get_board(pgn):
50
  pgn_str = StringIO(pgn)
51
  try:
52
  game = chess.pgn.read_game(pgn_str)
53
  board = game.board()
54
  for move in game.mainline_moves():
55
  board.push(move)
 
56
  except Exception as e:
57
  if 'illegal san' in str(e):
58
+ return None
59
+ return board
 
 
 
 
 
60
 
61
+ def gen_board_image(pgn):
62
+ board = get_board(pgn)
63
+ return chess.svg.board(board)
64
+
65
+ def gen_move(pgn):
66
+ model_input = torch.tensor(tokenizer.encode(pgn), dtype=torch.long, device=device).view((1, len(pgn)))
67
+ is_invalid = True
68
+ board = get_board(pgn)
69
+ while is_invalid:
70
+ new_pgn = tokenizer.decode(model.generate(model_input, max_new_tokens=4, context_size=context_size, temperature=0.2)[0].tolist())
71
+ try:
72
+ print(f'checking {new_pgn}')
73
+ mv = new_pgn[len(pgn):].split(' ')[0]
74
+ new_pgn = pgn.rstrip() + f' {mv}'
75
+ is_invalid = get_board(new_pgn) is None
76
+ except Exception:
77
+ is_invalid = True
78
+ print(f'For {pgn} invalid "{new_pgn[len(pgn):].split(" ")[0]}" {new_pgn}')
79
+ #print(mov in board.legal_moves)
80
+ return new_pgn
81
+
82
+
83
+ def generate(prompt):
84
+ model_input = torch.tensor(tokenizer.encode(prompt), dtype=torch.long, device=device).view((1, len(prompt)))
85
+ pgn = tokenizer.decode(model.generate(model_input, max_new_tokens=4, context_size=context_size, temperature=0.2)[0].tolist())
86
+ img = gen_board_image(pgn)
87
+ filename = f'./moves-{str(uuid4())}'
88
+ plot = gen_image_from_svg(img, filename)
89
  return pgn, plot
90
 
91
 
 
96
 
97
  The **C**hess-**P**re-trained-**T**ransformer.
98
 
99
+ The rules are simple:
100
+ - "Interactive": Play a game against the model
101
+ - "Next turn prediction": provide a PGN string of your current game, the model will predict the next token
102
  """)
103
+ def manual():
104
+ with gr.Tab("Next turn prediction"):
105
+ prompt = gr.Text(label="PGN")
106
+ output = gr.Text(label="Next turn", interactive=False)
107
+ img = gr.Image()
108
+ submit = gr.Button("Submit")
109
+ submit.click(generate, [prompt], [output, img])
110
+
111
+ gr.Examples(
112
+ [
113
+ ["1. e4", ],
114
+ ["1. e4 g6 2."],
115
+ ],
116
+ inputs=[prompt],
117
+ outputs=[output, img],
118
+ fn=generate
119
+ )
120
+ def interactive():
121
+ with gr.Tab("Interactive"):
122
+ color = gr.Dropdown(["white", "black"], value='white', label="Chose a color")
123
+ start_button = gr.Button("Start Game")
124
+
125
+ def start_game(c):
126
+ pgn = '1. '
127
+ if c == 'black':
128
+ pgn += gen_move(pgn)
129
+ img = gen_board_image(pgn)
130
+ fn = 'foo'
131
+ return gen_image_from_svg(img, fn), pgn, 1
132
+
133
+ state = gr.Text(label='PGN', value='', interactive=False)
134
+ game = gr.Image()
135
+ move_counter = gr.State(value=1)
136
+ start_button.click(
137
+ start_game,
138
+ inputs=[color],
139
+ outputs=[game, state, move_counter]
140
+ )
141
+
142
+ next_move = gr.Text(label='Next move')
143
+ gen_next_move_button = gr.Button("Submit")
144
+
145
+ def gen_next_move(pgn, new_move, move_ctr, c):
146
+ pgn += new_move.strip() + ' '
147
+ if c == 'black':
148
+ move_ctr += 1
149
+ pgn = f'{pgn.rstrip()} {move_ctr}. '
150
+ print(f'gen for {pgn}')
151
+ pgn = gen_move(pgn)
152
+ print(f'got {pgn}')
153
+ img = gen_board_image(pgn)
154
+ if c == 'white':
155
+ move_ctr += 1
156
+ pgn = f'{pgn.rstrip()} {move_ctr}. '
157
+ return gen_image_from_svg(img, 'foo-bar'), pgn, move_ctr
158
+
159
+ gen_next_move_button.click(
160
+ gen_next_move,
161
+ inputs=[state, next_move, move_counter, color],
162
+ outputs=[game, state, move_counter]
163
+ )
164
+ interactive()
165
+ manual()
166
  demo.launch()
model.py CHANGED
@@ -120,7 +120,7 @@ class DecoderTransformer(nn.Module):
120
  loss = F.cross_entropy(logits, targets)
121
  return logits, loss
122
 
123
- def generate(self, idx, max_new_tokens=50, context_size=None):
124
  if context_size is None:
125
  context_size = int(self.position_embedding_table.weight.shape[0])
126
  print(context_size)
@@ -128,7 +128,7 @@ class DecoderTransformer(nn.Module):
128
  for _ in range(max_new_tokens):
129
  idx_cond = idx[:, -context_size:]
130
  logits, loss = self(idx_cond)
131
- logits = logits[:,-1,:]
132
  probs = F.softmax(logits, dim=-1)
133
  idx_next = torch.multinomial(probs, num_samples=1)
134
  idx = torch.cat([idx, idx_next], dim=1)
 
120
  loss = F.cross_entropy(logits, targets)
121
  return logits, loss
122
 
123
+ def generate(self, idx, max_new_tokens=50, context_size=None, temperature=1.0):
124
  if context_size is None:
125
  context_size = int(self.position_embedding_table.weight.shape[0])
126
  print(context_size)
 
128
  for _ in range(max_new_tokens):
129
  idx_cond = idx[:, -context_size:]
130
  logits, loss = self(idx_cond)
131
+ logits = logits[:,-1,:] / temperature
132
  probs = F.softmax(logits, dim=-1)
133
  idx_next = torch.multinomial(probs, num_samples=1)
134
  idx = torch.cat([idx, idx_next], dim=1)