File size: 28,017 Bytes
fc27239
86caef8
fc27239
 
b2b432e
c0b0144
fc27239
7464f98
 
 
 
 
 
 
731d58f
b357ce0
fae1b43
731d58f
fbaf850
f5159cd
9befcf3
f5159cd
d43be46
7464f98
 
1459309
7464f98
478efec
1459309
523e980
b357ce0
ed98dc0
43a0204
 
1459309
 
 
b357ce0
c822f74
ed98dc0
7e5247f
ed98dc0
c822f74
ed98dc0
 
 
43a0204
1459309
 
ed98dc0
46417a7
1459309
4e9429d
f672d95
7464f98
731d58f
 
 
1459309
ed98dc0
02e4007
f672d95
02e4007
f672d95
 
81783d4
f672d95
b357ce0
81783d4
b357ce0
 
 
 
 
 
15be159
b357ce0
 
15be159
 
 
f672d95
 
7464f98
 
 
 
ed98dc0
81783d4
 
 
7464f98
81783d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed98dc0
 
 
 
 
 
 
 
 
 
 
 
02e4007
ed98dc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43a0204
 
 
 
 
 
 
478efec
7464f98
 
478efec
7464f98
 
478efec
7464f98
 
 
 
 
478efec
ed98dc0
7464f98
 
 
 
478efec
 
 
c822f74
15be159
1aeae08
 
15be159
 
 
 
 
4d2c32e
a9d68e0
 
 
 
15be159
 
 
 
 
 
 
 
 
 
f672d95
15be159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4792b99
7464f98
 
 
 
 
1459309
 
478efec
 
 
 
7464f98
1459309
 
478efec
7464f98
 
 
 
 
 
478efec
65379b3
7464f98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478efec
7464f98
 
 
 
 
 
 
 
1459309
7464f98
 
 
 
 
 
 
 
20bc3be
7464f98
 
 
 
478efec
20bc3be
7464f98
 
 
 
 
1459309
7464f98
 
 
 
 
 
a9d68e0
 
7464f98
 
 
 
20bc3be
1459309
7464f98
 
 
 
 
 
 
 
 
 
1459309
7464f98
 
 
 
 
 
1459309
 
 
 
 
 
 
 
 
 
 
 
 
 
fc27239
1459309
7464f98
 
 
 
 
fc27239
7464f98
1459309
7464f98
 
fc27239
1459309
7464f98
1459309
 
 
 
 
7464f98
 
1459309
 
 
7464f98
1459309
 
7464f98
 
 
 
 
 
 
 
 
 
 
 
 
fc27239
7464f98
fc27239
1459309
 
fc27239
7464f98
fc27239
1459309
 
fc27239
7464f98
 
 
 
 
 
1459309
7464f98
 
 
65379b3
7464f98
 
fc27239
1459309
 
fc27239
7464f98
 
 
 
 
 
 
 
 
c822f74
7464f98
 
 
 
1459309
fc27239
7464f98
 
 
 
 
 
 
 
 
1459309
7464f98
 
 
 
 
fc27239
 
 
7464f98
 
 
 
 
 
 
 
 
 
 
478efec
7464f98
478efec
 
 
 
 
7464f98
478efec
7464f98
 
 
 
 
478efec
 
 
1459309
7464f98
 
478efec
7464f98
 
 
478efec
 
1459309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc27239
 
478efec
 
 
7464f98
478efec
9633f26
7464f98
 
 
478efec
65379b3
 
9633f26
7464f98
 
478efec
 
7464f98
 
478efec
7464f98
478efec
 
7464f98
 
478efec
7464f98
20bc3be
7464f98
 
478efec
 
7464f98
478efec
 
 
7464f98
 
 
478efec
 
7464f98
478efec
 
7464f98
 
 
65379b3
 
7464f98
 
 
20bc3be
7464f98
 
 
 
 
 
 
 
 
 
65379b3
 
fc27239
1459309
fc27239
 
 
 
 
 
 
 
 
 
 
 
65379b3
478efec
7464f98
 
 
478efec
1459309
 
 
86caef8
478efec
7464f98
478efec
fc27239
65379b3
7464f98
fc27239
1459309
 
 
86caef8
fc27239
1459309
 
86caef8
7464f98
 
 
fc27239
65379b3
fc27239
 
7464f98
fc27239
 
1459309
fc27239
 
 
 
478efec
1459309
7464f98
fc27239
1459309
 
 
 
 
 
86caef8
1459309
 
 
 
 
86caef8
1459309
 
 
 
 
 
 
 
7464f98
 
 
 
 
 
 
fc27239
7464f98
1459309
7464f98
1459309
 
fc27239
 
 
 
 
 
 
 
7464f98
9633f26
1459309
fc27239
1459309
 
fc27239
 
 
 
7464f98
1459309
7464f98
1459309
 
9e5f96d
fc27239
 
 
 
 
 
 
 
 
 
 
86caef8
 
1459309
86caef8
fc27239
 
 
 
 
1459309
 
 
 
 
 
 
 
7464f98
fc27239
1459309
 
 
7464f98
 
 
 
 
1459309
 
7464f98
 
 
 
 
1459309
 
7464f98
478efec
fc27239
478efec
92bbd6b
 
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
"""
ChessQween โ€” Play against zhoudoe23/CheesQween
Hugging Face Space  |  Gradio app
"""
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""

import re
import random
import chess
import chess.svg
import chess.pgn
import gradio as gr
import torch
import spaces

torch.cuda.is_available = lambda: False

from transformers import GPT2LMHeadModel, GPT2Tokenizer
import outlines
from typing import Literal
from transformers import AutoTokenizer, AutoModelForCausalLM


# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Available models
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

AVAILABLE_MODELS = {
    "ChessQween": "zhoudoe23/ChessQween-124m",
    "ChessQween1.5-nano": "zhoudoe23/ChessQween1.5-nano",
    "ChessQween2-tiny": "zhoudoe23/ChessQween2-tiny",
    "ChessQween3-base": "zhoudoe23/ChessQween3-base",

}

MODEL_DESCRIPTIONS = {
    "ChessQween": "Trained on 200,000 best games on Lichess, at least 1900 elo",
    "ChessQween1.5-nano": "A smaller version, but trained on 400,000 master games, at least 2300 elo",
    "ChessQween2-tiny": "New model type with new tokenizer",
    "ChessQween3-base": "Trained with Stockfish's best moves, using FEN board",
}
MODEL_TYPE_MAP = {
    "ChessQween": 1,
    "ChessQween1.5-nano": 1,
    "ChessQween2-tiny": 2,
    "ChessQween3-base": 3,
}


device = torch.device("cpu")

# Lazy cache: {model_id: (tokenizer, model)}
_model_cache: dict = {}

@spaces.GPU(duration=30)
def foo(bar):
    return bar

current_model_type = 1
def load_model(model_key: str):
    """Load (or retrieve from cache) tokenizer + model for the given key."""
    model_id = AVAILABLE_MODELS[model_key]
    if model_id not in _model_cache:
        print(f"Loading {model_id} โ€ฆ")
        tokenizer = AutoTokenizer.from_pretrained(model_id)
        tokenizer.pad_token = tokenizer.eos_token
        
        raw_model = AutoModelForCausalLM.from_pretrained(model_id)
        raw_model.to(device)
        raw_model.eval()
        raw_model.config.use_cache = True
        
        # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ ๆ–ฐๅขž๏ผš็”จ Outlines ๅŒ…่ฃ…ๆจกๅž‹ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        # outlines ไผšๅฐ่ฃ…ๅŽŸๅง‹ๆจกๅž‹ๅ’Œ tokenizer๏ผŒๆŽฅ็ฎก token ็บงๅˆซ็š„ๆŽฉ็ ็”Ÿๆˆ
        # outlines_model = outlines.from_transformers(raw_model, tokenizer)
        
        # ๅฐ† outlines_model ๅญ˜ๅ…ฅ็ผ“ๅญ˜๏ผˆtokenizer ไพ็„ถไฟ็•™๏ผŒๆ–นไพฟๅŽ็ปญๆ‹ผๆŽฅ prompt ้€‰็”จ๏ผ‰
        # _model_cache[model_id] = (tokenizer, raw_model, outlines_model)
        _model_cache[model_id] = (tokenizer, raw_model)

        print(f"โœ“ {model_id} ready on {device}")
    return _model_cache[model_id]

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Chess / model logic
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def get_history_uci(board: chess.Board) -> str:
    rounds = []
    current_round = []

    for move in board.move_stack:
        current_round.append(move.uci())
        if len(current_round) == 2:
            rounds.append(" ".join(current_round))
            current_round = []

    if current_round:  # ๅฝ“ๅ‰่ฝฎๅชๆœ‰็™ฝๆ–น่ตฐไบ†ไธ€ๆญฅ
        rounds.append(" ".join(current_round))

    if not rounds:
        return ""

    history_str = " | ".join(rounds)

    # ๅ…ณ้”ฎ้€ป่พ‘๏ผšๅฆ‚ๆžœๆ˜ฏๅถๆ•ฐๆญฅ๏ผˆ่ฝฎๅˆฐ็™ฝๆ–น่ตฐๆฃ‹๏ผ‰๏ผŒไธ”ไธๆ˜ฏๅผ€ๅฑ€๏ผŒๆœซๅฐพๅฟ…้กป่กฅไธŠ " |"
    if len(board.move_stack) % 2 == 0 and len(board.move_stack) > 0:
        history_str += " |"

    return " " + history_str

def board_to_prompt(board: chess.Board, model_type) -> str:
    if model_type == 1:
        game = chess.pgn.Game()
        node = game
        for move in board.move_stack:
            node = node.add_variation(move)
        exporter = chess.pgn.StringExporter(headers=False, variations=False, comments=False)
        pgn = game.accept(exporter).strip()
        pgn = re.sub(r"\s*[\*\d][-\d/]*\s*$", "", pgn).strip().replace("\n"," ")
        
        full_move = board.fullmove_number
        
        # ไฟฎๅคๅŽ็š„้€ป่พ‘๏ผš
        if board.turn == chess.WHITE:
            # ็™ฝๆ–นๅ›žๅˆ๏ผšๆ‹ผๆŽฅๅ›žๅˆๆ•ฐๅ’Œ็‚น๏ผŒไพ‹ๅฆ‚ " 2."
            prompt = f"Result: 1-0 | {pgn} {full_move}."
        else:
            # ้ป‘ๆ–นๅ›žๅˆ๏ผšๅฆ‚ๆžœไฝ ็š„่ฎญ็ปƒ้›†้ป‘ๆ–นๅ‰้ขๅชๆœ‰็ฉบๆ ผ๏ผŒ็›ดๆŽฅๅœจ็™ฝๆ–น่ตฐๆณ•ๅŽๅŠ ็ฉบๆ ผ๏ผ
            # ็ปไธ่ƒฝๅŠ ๅญค้›ถ้›ถ็š„ๆ•ฐๅญ— {full_move}
            prompt = f"Result: 0-1 | {pgn}"
            
        return prompt
    elif model_type == 2:
        if board.turn == chess.WHITE:
            return f"<|WHITE|>{get_history_uci(board)}"
        else:
            return f"<|BLACK|>{get_history_uci(board)}"
    elif model_type == 3:
        fen_list = board.fen().split()
        board_content = " ".join(list(fen_list[0]))
        side = "<|WHITE|>" if fen_list[1] == 'w' else "<|BLACK|>"
        castling = fen_list[2]
        en_passant = fen_list[3]
        return f"{board_content} {side} {castling} {en_passant} 0 1 >"


def extract_move(text: str, board: chess.Board):
    text = re.sub(r"^\s*\d+\.+\s*", "", text).strip()
    for token in text.split()[:5]:
        clean = re.sub(r"[!?+#,;]+$", "", token)
        try:
            move = board.parse_san(clean)
            if move in board.legal_moves:
                return move
        except Exception:
            pass
        try:
            move = chess.Move.from_uci(clean.lower())
            if move in board.legal_moves:
                return move
        except Exception:
            pass
    return None

@torch.no_grad()
def get_model_move(board: chess.Board, model_key: str):
    tokenizer, model = load_model(model_key)
    model_type = MODEL_TYPE_MAP[model_key]
    prompt  = board_to_prompt(board, model_type)
    print("Current prompt: "+prompt)
    inputs  = tokenizer(prompt, return_tensors="pt").to(device)
    outputs = model.generate(
        inputs.input_ids,
        max_new_tokens=12,
        do_sample=False,
        # do_sample=True,
        # temperature=0.3,
        # top_k=40,
        # top_p=0.9,
        repetition_penalty=1.1,
        pad_token_id=tokenizer.eos_token_id,
        eos_token_id=tokenizer.eos_token_id,
    )
    new_tokens = outputs[0][inputs.input_ids.shape[1]:]
    generated  = tokenizer.decode(new_tokens, skip_special_tokens=True)
    move       = extract_move(generated, board)
    if move:
        return move, True
    print(f"Wrong move: {generated}")
    return random.choice(list(board.legal_moves)), False
# @torch.no_grad()
# def get_model_move(board: chess.Board, model_key: str):
#     # ๐Ÿ‘ˆ ๅ–ๅ‡บ raw_model ๅ’Œ outlines_model
#     tokenizer, raw_model, outlines_model = load_model(model_key)
#     model_type = MODEL_TYPE_MAP[model_key]
#     prompt = board_to_prompt(board, model_type)
#     print("Current prompt:", prompt)

#     # ------------------ ๆ—งๆจกๅž‹ (Model Type 1) ------------------
#     if model_type == 1:
#         legal_sans = [board.san(m) for m in board.legal_moves]
#         choices = [" " + san for san in legal_sans]
#         MoveType = Literal.__getitem__(tuple(choices))

#         # ไฝฟ็”จ outlines_model
#         generator = outlines.Generator(outlines_model, MoveType)
#         generated = generator(
#             prompt, temperature=0.3, top_k=40, top_p=0.9
#         ).strip()

#         move = extract_move(generated, board)
#         if move:
#             return move, True

#     # ------------------ ๆ–ฐๆจกๅž‹ (Model Type 2) ------------------
#     elif model_type == 2:
#         legal_moves = list(board.legal_moves)
#         legal_ucis = [m.uci() for m in legal_moves]

#         # 1. ็ผ–็  Prompt
#         input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)

#         # ๐Ÿ‘ˆ ๆ ธๅฟƒไฟฎๆ”น๏ผšไฝฟ็”จๅŽŸ็”Ÿ raw_model ่ฎก็ฎ— Tensor ่พ“ๅ…ฅ
#         outputs = raw_model(input_ids)
#         next_token_logits = outputs.logits[0, -1, :].clone()

#         # 2. ๐Ÿš€ ้‡ๅค่ตฐๆณ•ๆƒฉ็ฝš (้˜ฒๆญปๅพช็Žฏๆ‘†็ƒ‚)
#         for move in legal_moves:
#             uci_str = move.uci()
#             if uci_str in tokenizer.vocab:
#                 token_id = tokenizer.vocab[uci_str]
#                 board.push(move)
#     return random.choice(list(board.legal_moves)), False

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Board rendering
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

PIECE_COLORS = {
    "square light":          "#f0d9b5",
    "square dark":           "#b58863",
    "square light lastmove": "#cdd16e",
    "square dark lastmove":  "#aaa23a",
}


def render_board_html(board: chess.Board, last_move=None, flipped=False, size=480):
    check_square = board.king(board.turn) if board.is_check() else None
    svg = chess.svg.board(
        board,
        lastmove=last_move,
        check=check_square,
        flipped=flipped,
        size=size,
        colors=PIECE_COLORS,
    )
    return f"""
    <div style="
        display:flex; justify-content:center; align-items:center;
        padding: 16px;
        background: radial-gradient(ellipse at center, #1a1208 0%, #0d0d0d 100%);
        border-radius: 12px;
        box-shadow: 0 0 60px rgba(0,0,0,0.8), inset 0 0 30px rgba(0,0,0,0.4);
    ">
        <div style="
            border-radius: 4px;
            overflow: hidden;
            box-shadow: 0 8px 32px rgba(0,0,0,0.6), 0 0 0 3px #3d2b0e, 0 0 0 5px #6b4c1e;
        ">
            {svg}
        </div>
    </div>
    """


def get_legal_moves_san(board: chess.Board):
    moves = []
    for move in board.legal_moves:
        try:
            moves.append(board.san(move))
        except Exception:
            pass
    return sorted(moves)


def format_move_history(board: chess.Board):
    if not board.move_stack:
        return "<em style='color:#666'>No moves yet.</em>"

    temp  = chess.Board()
    lines = []
    moves = list(board.move_stack)
    i     = 0

    while i < len(moves):
        move_num  = temp.fullmove_number
        white_san = temp.san(moves[i])
        temp.push(moves[i])
        i += 1

        if i < len(moves):
            black_san = temp.san(moves[i])
            temp.push(moves[i])
            i += 1
            lines.append(
                f"<span style='color:#8a7a5a;font-size:0.8em'>{move_num}.</span> "
                f"<span style='color:#e8d5a3'>{white_san}</span> "
                f"<span style='color:#c4b48a'>{black_san}</span>"
            )
        else:
            lines.append(
                f"<span style='color:#8a7a5a;font-size:0.8em'>{move_num}.</span> "
                f"<span style='color:#e8d5a3'>{white_san}</span>"
            )

    visible = lines[-10:]
    html  = "<div style='font-family:\"Courier New\",monospace; line-height:2; font-size:0.92em;'>"
    html += "<br>".join(visible)
    html += "</div>"
    return html


def game_status(board: chess.Board, player_color: str):
    empty_board = chess.Board()
    print(f"Current PGN: \n{empty_board.variation_san(board.move_stack)}")
    if board.is_checkmate():
        winner = "Black" if board.turn == chess.WHITE else "White"
        if (winner == "White") == (player_color == "white"):
            return "โ™Ÿ Checkmate โ€” You win! ๐ŸŽ‰", "win"
        else:
            return "โ™Ÿ Checkmate โ€” AI wins!", "loss"
    if board.is_stalemate():
        return "ยฝ Stalemate โ€” Draw", "draw"
    if board.is_insufficient_material():
        return "ยฝ Insufficient material โ€” Draw", "draw"
    if board.is_seventyfive_moves():
        return "ยฝ 75-move rule โ€” Draw", "draw"
    if board.is_fivefold_repetition():
        return "ยฝ Fivefold repetition โ€” Draw", "draw"
    if board.is_check():
        return "โš  Check!", "check"
    whose = "Your turn" if (board.turn == chess.WHITE) == (player_color == "white") else "AI is thinkingโ€ฆ"
    return whose, "playing"

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Gradio callbacks
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

def update_model_description(model_key: str):
    desc = MODEL_DESCRIPTIONS.get(model_key, "")
    hf_id = AVAILABLE_MODELS.get(model_key, "")
    return (
        f"<div id='model-desc'>"
        f"<span style='color:#8a7a5a;font-style:italic;'>{desc}</span><br>"
        f"<a href='https://huggingface.co/{hf_id}' target='_blank' "
        f"style='color:#8a6a30;font-size:0.85em;text-decoration:none;'>"
        f"๐Ÿค— {hf_id}</a>"
        f"</div>"
    )


def new_game(player_color_choice: str, model_key: str):
    """Reset the board and, if player chose Black, let the model move first."""
    board        = chess.Board()
    player_color = "white" if player_color_choice == "โฌœ White (move first)" else "black"
    flipped      = (player_color == "black")
    last_move    = None
    log_lines    = []

    # If player chose Black, model plays White first
    if player_color == "black":
        move, legal = get_model_move(board, model_key)
        san         = board.san(move)
        board.push(move)
        last_move   = move
        log_lines.append(f"{model_key} opens with **{san}**")

    legal_moves  = get_legal_moves_san(board)
    status_text, _ = game_status(board, player_color)
    board_html     = render_board_html(board, last_move=last_move, flipped=flipped)
    history_html   = format_move_history(board)
    log_html       = "<br>".join(log_lines) if log_lines else "<em style='color:#666'>Game started.</em>"

    state = {
        "fen":           board.fen(),
        "move_stack":    [m.uci() for m in board.move_stack],
        "player_color":  player_color,
        "last_move_uci": last_move.uci() if last_move else None,
        "game_over":     False,
        "model_key":     model_key,
    }

    return (
        board_html,
        gr.Dropdown(choices=legal_moves, value=None, interactive=True, label="Your move"),
        status_text,
        history_html,
        log_html,
        state,
    )


def make_player_move(move_san: str, state: dict):
    """Apply the player's chosen move, then let the model respond."""
    if not state or state.get("game_over"):
        return (
            gr.update(), gr.update(), "Game is over. Start a new game.",
            gr.update(), gr.update(), state,
        )
    if not move_san:
        return (
            gr.update(), gr.update(), "Please select a move first.",
            gr.update(), gr.update(), state,
        )

    board = chess.Board()
    for uci in state["move_stack"]:
        board.push(chess.Move.from_uci(uci))

    player_color = state["player_color"]
    model_key    = state.get("model_key", "ChessSLM")
    flipped      = (player_color == "black")
    log_lines    = []

    try:
        player_move = board.parse_san(move_san)
    except Exception:
        return (
            gr.update(), gr.update(), f"Invalid move: {move_san}",
            gr.update(), gr.update(), state,
        )

    board.push(player_move)
    log_lines.append(f"You played **{move_san}**")
    last_move = player_move

    status_text, status_key = game_status(board, player_color)
    game_over = status_key in ("win", "loss", "draw")

    if not game_over:
        model_move_obj, legal = get_model_move(board, model_key)
        model_san = board.san(model_move_obj)
        board.push(model_move_obj)
        last_move = model_move_obj
        flag = "" if legal else " *(random fallback)*"
        log_lines.append(f"{model_key} plays **{model_san}**{flag}")

        status_text, status_key = game_status(board, player_color)
        game_over = status_key in ("win", "loss", "draw")

    state = {
        "fen":           board.fen(),
        "move_stack":    [m.uci() for m in board.move_stack],
        "player_color":  player_color,
        "last_move_uci": last_move.uci() if last_move else None,
        "game_over":     game_over,
        "model_key":     model_key,
    }

    legal_moves  = [] if game_over else get_legal_moves_san(board)
    board_html   = render_board_html(board, last_move=last_move, flipped=flipped)
    history_html = format_move_history(board)
    log_html     = "<br>".join(
        [f"<span style='color:#c8a96e'>{l}</span>" for l in log_lines]
    )

    return (
        board_html,
        gr.Dropdown(choices=legal_moves, value=None, interactive=not game_over, label="Your move"),
        status_text,
        history_html,
        log_html,
        state,
    )

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# CSS
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

CSS = """
@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;600;700&family=Crimson+Text:ital,wght@0,400;0,600;1,400&display=swap');

body, .gradio-container {
    background: #0d0d0d !important;
    color: #e8d5a3 !important;
}

.gradio-container {
    max-width: 1100px !important;
    margin: 0 auto !important;
    font-family: 'Crimson Text', Georgia, serif !important;
}

h1, h2, h3 { font-family: 'Cinzel', serif !important; letter-spacing: 0.08em; }

#title-block {
    text-align: center;
    padding: 2rem 0 1rem;
    border-bottom: 1px solid #3d2b0e;
    margin-bottom: 1.5rem;
}

/* โ”€โ”€ Model selector card โ”€โ”€ */
#model-selector-card {
    background: linear-gradient(135deg, #161005 0%, #1e1810 100%);
    border: 1px solid #5a4020;
    border-radius: 8px;
    padding: 1rem 1.2rem;
    margin-bottom: 0.6rem;
    box-shadow: inset 0 1px 0 rgba(212,168,67,0.08);
}

#model-desc {
    margin-top: 0.4rem;
    font-family: 'Crimson Text', serif;
    font-size: 0.92em;
    line-height: 1.5;
    color: #8a7a5a;
}

#status-bar {
    text-align: center;
    font-family: 'Cinzel', serif;
    font-size: 1.1em;
    letter-spacing: 0.05em;
    padding: 0.6rem 1rem;
    border-radius: 6px;
    background: #1a1208;
    border: 1px solid #4a3520;
    color: #f0c060;
}

button.primary {
    background: linear-gradient(135deg, #8b6914 0%, #c4922a 50%, #8b6914 100%) !important;
    border: 1px solid #d4a843 !important;
    color: #fff8e8 !important;
    font-family: 'Cinzel', serif !important;
    letter-spacing: 0.06em !important;
    font-size: 0.9em !important;
    border-radius: 4px !important;
    transition: all 0.2s ease !important;
}
button.primary:hover {
    background: linear-gradient(135deg, #a07820 0%, #d4a843 50%, #a07820 100%) !important;
    box-shadow: 0 0 16px rgba(212,168,67,0.4) !important;
}

button.secondary {
    background: #1e1810 !important;
    border: 1px solid #5a4020 !important;
    color: #c8a96e !important;
    font-family: 'Cinzel', serif !important;
    letter-spacing: 0.04em !important;
    border-radius: 4px !important;
}

select, .gr-dropdown select {
    background: #1a1208 !important;
    border: 1px solid #5a4020 !important;
    color: #e8d5a3 !important;
    font-family: 'Crimson Text', serif !important;
    font-size: 1em !important;
}

#move-log {
    background: #0f0c06 !important;
    border: 1px solid #3d2b0e !important;
    border-radius: 6px;
    padding: 0.8rem 1rem;
    font-family: 'Crimson Text', serif;
    font-size: 0.95em;
    line-height: 1.8;
    min-height: 80px;
    color: #c8a96e;
}

#history-panel {
    background: #0f0c06 !important;
    border: 1px solid #3d2b0e !important;
    border-radius: 6px;
    padding: 0.8rem 1rem;
    min-height: 200px;
    max-height: 320px;
    overflow-y: auto;
}

.gr-radio label { color: #e8d5a3 !important; font-family: 'Crimson Text', serif !important; }

label span {
    color: #a08050 !important;
    font-family: 'Cinzel', serif !important;
    font-size: 0.8em !important;
    letter-spacing: 0.06em !important;
    text-transform: uppercase !important;
}

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: #0d0d0d; }
::-webkit-scrollbar-thumb { background: #5a4020; border-radius: 3px; }
"""

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Layout
# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

SECTION = "<div style='height:1px;background:#3d2b0e;margin:1rem 0;'></div>"
LABEL   = lambda t: f"<h3 style='font-family:Cinzel,serif;color:#c8a96e;font-size:1em;letter-spacing:0.1em;margin:0 0 0.5rem;'>{t}</h3>"

with gr.Blocks(css=CSS, title="ChessQween โ€” Play vs AI") as demo:

    state = gr.State({})

    # โ”€โ”€ Header โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    gr.HTML("""
    <div id="title-block">
        <h1 style="
            font-family:'Cinzel',serif; font-size:2.4em; font-weight:700;
            color:#e8c96e; text-shadow:0 0 30px rgba(232,180,80,0.4);
            margin:0 0 0.3rem; letter-spacing:0.12em;
        ">โ™› ChessQween</h1>
        <p style="
            font-family:'Crimson Text',serif; color:#8a7a5a;
            font-size:1.1em; font-style:italic; margin:0;
        ">Play against a GPT-2 model trained on 200,000 chess games</p>
    </div>
    """)

    # โ”€โ”€ Main layout โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    with gr.Row():

        # Left: board
        with gr.Column(scale=3):
            board_display = gr.HTML(
                value=render_board_html(chess.Board()),
                label="Board",
            )
            status_display = gr.HTML(
                value="<div id='status-bar'>Choose your colour and press New Game</div>"
            )

        # Right: controls
        with gr.Column(scale=2):

            # โ”€โ”€ Opponent selector โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            gr.HTML(LABEL("CHOOSE OPPONENT"))
            gr.HTML("<div id='model-selector-card'>")

            model_dropdown = gr.Dropdown(
                choices=list(AVAILABLE_MODELS.keys()),
                value="ChessQween",
                label="Opponent model",
                interactive=True,
            )

            model_desc_display = gr.HTML(
                value=update_model_description("ChessQween"),
            )

            gr.HTML("</div>")  # close card

            gr.HTML(SECTION)

            # โ”€โ”€ New game โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            gr.HTML(LABEL("NEW GAME"))

            color_choice = gr.Radio(
                choices=["โฌœ White (move first)", "โฌ› Black (move second)"],
                value="โฌœ White (move first)",
                label="Play as",
            )

            new_game_btn = gr.Button("โ™Ÿ  New Game", variant="primary", size="lg")

            gr.HTML(SECTION)

            # โ”€โ”€ Your move โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            gr.HTML(LABEL("YOUR MOVE"))

            move_dropdown = gr.Dropdown(
                choices=[],
                value=None,
                label="Select move (SAN notation)",
                interactive=False,
            )

            move_btn = gr.Button("โ–ถ  Make Move", variant="secondary")

            gr.HTML(SECTION)

            # โ”€โ”€ Move log โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            gr.HTML(LABEL("MOVE LOG"))

            log_display = gr.HTML(
                value="<div id='move-log'><em style='color:#555'>Start a new game to begin.</em></div>",
            )

            gr.HTML(SECTION)

            # โ”€โ”€ Game history โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            gr.HTML(LABEL("GAME HISTORY"))

            history_display = gr.HTML(
                value="<div id='history-panel'><em style='color:#555'>No moves yet.</em></div>",
            )

    # โ”€โ”€ Footer โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    gr.HTML("""
    <div style="
        text-align:center; margin-top:2rem; padding-top:1rem;
        border-top:1px solid #2a1e0a;
        font-family:'Crimson Text',serif; font-size:0.85em; color:#5a4a30;
    ">
        Model by
        <a href="https://huggingface.co/zhoudoe23" target="_blank"
            style="color:#8a6a30;text-decoration:none;">FlameF0X</a>
        &nbsp;ยท&nbsp; GPT-2 pre-trained on PGN games
        &nbsp;ยท&nbsp; Move selection uses top-k sampling (temp=0.3)
    </div>
    """)

    # โ”€โ”€ Wiring โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    # Live-update description when model changes
    model_dropdown.change(
        fn=update_model_description,
        inputs=[model_dropdown],
        outputs=[model_desc_display],
    )

    new_game_btn.click(
        fn=new_game,
        inputs=[color_choice, model_dropdown],
        outputs=[board_display, move_dropdown, status_display,
                 history_display, log_display, state],
    )

    move_btn.click(
        fn=make_player_move,
        inputs=[move_dropdown, state],
        outputs=[board_display, move_dropdown, status_display,
                 history_display, log_display, state],
    )

    move_dropdown.select(
        fn=make_player_move,
        inputs=[move_dropdown, state],
        outputs=[board_display, move_dropdown, status_display,
                 history_display, log_display, state],
    )


if __name__ == "__main__":
    demo.launch()
    demo.launch(mcp_server=True)