File size: 38,638 Bytes
cfa4dea
 
c50d4d7
d089c8b
ccab657
6e2ef3c
d089c8b
6e2ef3c
d089c8b
cfa4dea
a85c8ec
c50d4d7
6e2ef3c
6858ea9
65e8c7e
104d38c
 
 
 
fdb39ae
 
955e441
fdb39ae
c50d4d7
 
d089c8b
 
fdb39ae
 
 
 
 
 
 
 
 
 
104d38c
 
 
 
 
 
 
 
 
 
 
fdb39ae
104d38c
 
 
 
 
 
 
 
 
fdb39ae
 
 
104d38c
 
 
c50d4d7
104d38c
65e8c7e
104d38c
fdb39ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104d38c
d089c8b
104d38c
d089c8b
fdb39ae
d089c8b
fdb39ae
 
 
c50d4d7
6e2ef3c
 
 
 
fdb39ae
ccab657
 
 
 
 
6e2ef3c
65e8c7e
 
6e2ef3c
65e8c7e
fdb39ae
 
 
 
 
 
 
104d38c
ccab657
65e8c7e
d089c8b
104d38c
d089c8b
104d38c
d089c8b
6e2ef3c
 
d089c8b
559b20a
 
 
 
 
 
6e2ef3c
 
d089c8b
559b20a
 
6e2ef3c
 
 
104d38c
d089c8b
104d38c
d089c8b
ccab657
d089c8b
6e2ef3c
d089c8b
6e2ef3c
d089c8b
ccab657
d089c8b
 
ccab657
cfa4dea
fdb39ae
 
d089c8b
ccab657
 
d089c8b
fdb39ae
d089c8b
c50d4d7
fdb39ae
 
 
cfa4dea
104d38c
d089c8b
104d38c
d089c8b
fdb39ae
 
 
 
 
 
 
 
 
 
 
 
 
 
a85c8ec
ccab657
d089c8b
 
559b20a
 
 
fdb39ae
559b20a
 
d089c8b
104d38c
 
 
 
d089c8b
 
fdb39ae
 
 
 
 
d089c8b
 
 
 
fdb39ae
 
 
 
d089c8b
 
104d38c
 
 
d089c8b
 
 
 
104d38c
d089c8b
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
 
 
 
d089c8b
 
 
 
 
 
 
fdb39ae
 
 
 
d089c8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104d38c
 
d089c8b
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
 
104d38c
 
fdb39ae
104d38c
fdb39ae
104d38c
 
 
 
 
 
 
fdb39ae
 
 
 
 
 
 
 
 
 
 
 
 
 
104d38c
d089c8b
 
104d38c
559b20a
d089c8b
104d38c
fdb39ae
559b20a
fdb39ae
 
104d38c
 
d089c8b
 
104d38c
 
 
d089c8b
 
559b20a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
 
 
559b20a
 
104d38c
 
 
 
d089c8b
 
104d38c
 
d089c8b
104d38c
fdb39ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d089c8b
fdb39ae
 
 
 
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559b20a
104d38c
 
 
 
 
 
559b20a
 
104d38c
559b20a
 
 
 
d089c8b
559b20a
 
 
 
 
 
 
 
 
fdb39ae
 
559b20a
 
fdb39ae
 
 
 
559b20a
d089c8b
fdb39ae
104d38c
 
 
fdb39ae
559b20a
104d38c
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
 
 
 
 
 
 
 
 
 
559b20a
 
 
104d38c
 
559b20a
104d38c
 
 
 
 
fdb39ae
 
 
 
 
104d38c
fdb39ae
 
 
104d38c
fdb39ae
104d38c
 
 
 
 
 
 
fdb39ae
104d38c
 
 
 
 
 
559b20a
 
104d38c
559b20a
104d38c
fdb39ae
 
 
104d38c
 
fdb39ae
 
 
d089c8b
104d38c
 
d089c8b
559b20a
 
 
 
104d38c
 
 
 
d089c8b
104d38c
d089c8b
104d38c
 
 
 
fdb39ae
 
 
104d38c
 
 
 
 
 
 
 
559b20a
104d38c
 
 
 
 
 
d089c8b
 
 
104d38c
 
d089c8b
 
 
104d38c
559b20a
 
 
 
 
 
104d38c
fdb39ae
559b20a
 
fdb39ae
559b20a
fdb39ae
104d38c
d089c8b
104d38c
 
 
 
 
d089c8b
 
104d38c
fdb39ae
 
 
104d38c
 
fdb39ae
 
 
 
104d38c
 
559b20a
 
 
 
 
 
 
 
 
 
 
104d38c
d089c8b
 
 
 
 
104d38c
 
d089c8b
104d38c
 
 
559b20a
d089c8b
 
559b20a
 
 
 
104d38c
 
d089c8b
 
fdb39ae
 
 
559b20a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
559b20a
 
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
 
559b20a
fdb39ae
 
 
 
 
 
559b20a
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
 
 
 
 
 
 
 
559b20a
 
fdb39ae
559b20a
 
fdb39ae
559b20a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
 
 
d089c8b
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
 
d089c8b
 
 
 
 
 
 
 
 
559b20a
 
 
d089c8b
fdb39ae
d089c8b
 
 
 
 
104d38c
 
 
 
 
 
 
 
 
559b20a
 
 
 
 
d089c8b
 
104d38c
 
 
 
 
 
559b20a
 
 
104d38c
 
559b20a
104d38c
d089c8b
 
 
104d38c
 
 
 
 
 
fdb39ae
104d38c
 
 
 
559b20a
d089c8b
 
 
fdb39ae
 
 
 
 
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d089c8b
fdb39ae
104d38c
 
d089c8b
104d38c
 
 
 
f27a5bb
104d38c
 
 
 
 
fdb39ae
 
104d38c
 
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
104d38c
 
 
 
 
 
fdb39ae
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
104d38c
 
 
 
 
 
 
 
fdb39ae
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
104d38c
 
 
 
 
fdb39ae
104d38c
 
 
 
 
fdb39ae
104d38c
 
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
104d38c
 
 
 
559b20a
 
 
 
 
 
 
 
 
104d38c
 
 
 
 
 
 
 
 
 
559b20a
 
104d38c
559b20a
104d38c
d089c8b
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559b20a
 
 
 
 
 
 
 
 
 
 
 
 
 
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
104d38c
 
 
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
 
 
 
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559b20a
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
d089c8b
 
104d38c
d089c8b
104d38c
 
 
 
 
 
 
 
 
fdb39ae
 
 
 
 
104d38c
 
 
 
 
 
 
d089c8b
104d38c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d089c8b
 
 
 
 
 
 
 
 
 
 
 
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
from __future__ import annotations

import os
import random
import re
import sys
from contextlib import nullcontext
from pathlib import Path
from typing import Any, Generator

import gradio as gr
import torch
from huggingface_hub import snapshot_download


# =============================================================================
# Model configuration
# =============================================================================

MODEL_ID = os.getenv(
    "MODEL_ID",
    "MarkChenX/lfm2-quantum-128m-sft-v2-reasoning",
)
HF_TOKEN = os.getenv("HF_TOKEN")

MAX_CONTEXT_TOKENS = 1024

# Set FORCE_HF_DOWNLOAD=1 temporarily after replacing bad files on the Hub.
# Leave it unset/0 normally so a Space restart does not redownload ~445 MB.
FORCE_HF_DOWNLOAD = os.getenv("FORCE_HF_DOWNLOAD", "0").strip().lower() in {
    "1",
    "true",
    "yes",
    "on",
}

BUILT_IN_CONTEXT = """You are Supernova 1 Beta, an experimental custom
LFM2-style hybrid quantum language model running through NanoChat.

Behavior:
- Answer the user's request directly.
- Be concise, clear, and technically precise.
- For research or coding questions, explain the key reasoning in short steps.
- If you are uncertain, say what is uncertain instead of inventing facts.
- Prefer useful answers over long introductions.
- Use Markdown when it improves readability.
"""

# These examples are injected into the actual model prompt.
# Keep them short because the checkpoint has a 1,024-token context window.
FEW_SHOT_MESSAGES: list[dict[str, str]] = [
    {
        "role": "user",
        "content": "What are you?",
    },
    {
        "role": "assistant",
        "content": (
            "I am Supernova 1 Beta, an experimental transformer model "
            "with quantum neural networks and an LFM2-style hybrid "
            "quantum language model running through NanoChat."
        ),
    },
]

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


# =============================================================================
# Helpers used during startup
# =============================================================================

GIT_LFS_POINTER_PREFIX = b"version https://git-lfs.github.com/spec/v1"


def is_git_lfs_pointer(path: Path) -> bool:
    """Return True when a downloaded file is text for a Git LFS pointer."""
    if not path.is_file():
        return False

    try:
        with path.open("rb") as f:
            header = f.read(256)
    except OSError:
        return False

    return header.startswith(GIT_LFS_POINTER_PREFIX)


def find_git_lfs_pointers(directory: Path) -> list[Path]:
    """Find unresolved Git LFS pointer files anywhere under a directory."""
    if not directory.exists():
        return []

    bad_files: list[Path] = []

    for path in directory.rglob("*"):
        if path.is_file() and is_git_lfs_pointer(path):
            bad_files.append(path)

    return bad_files


def validate_tokenizer_directory(tokenizer_dir: Path) -> None:
    """
    Validate the tokenizer payload before NanoChat calls pickle.load().

    This specifically catches the failure:
        _pickle.UnpicklingError: invalid load key, 'v'

    That error commonly occurs when the file being unpickled starts with:
        version https://git-lfs.github.com/spec/v1
    """
    if not tokenizer_dir.exists():
        raise FileNotFoundError(
            "Bundled tokenizer directory is missing.\n"
            f"Expected: {tokenizer_dir}\n\n"
            "Upload the exact tokenizer used to train this checkpoint "
            "under the checkpoint's tokenizer/ directory."
        )

    tokenizer_files = [
        path
        for path in tokenizer_dir.rglob("*")
        if path.is_file()
    ]

    if not tokenizer_files:
        raise RuntimeError(
            "Bundled tokenizer directory exists but contains no files.\n"
            f"Directory: {tokenizer_dir}"
        )

    print("Tokenizer directory:", tokenizer_dir)
    print("Tokenizer files:")

    for path in sorted(tokenizer_files):
        relative = path.relative_to(tokenizer_dir)
        try:
            size = path.stat().st_size
        except OSError:
            size = -1
        print(f"  - {relative} ({size:,} bytes)")

    lfs_pointers = find_git_lfs_pointers(tokenizer_dir)

    if lfs_pointers:
        formatted = "\n".join(
            f"  - {path.relative_to(tokenizer_dir)}"
            for path in lfs_pointers
        )

        raise RuntimeError(
            "\n"
            "The bundled tokenizer contains unresolved Git LFS pointer "
            "file(s), not the real tokenizer binary:\n"
            f"{formatted}\n\n"
            "This is why pickle.load() reports:\n"
            "    _pickle.UnpicklingError: invalid load key, 'v'\n\n"
            "Fix the MODEL repository, not pickle.load():\n"
            "1. Recover the exact tokenizer files used during training.\n"
            "2. Confirm the local files are real binaries and do not begin "
            "with 'version https://git-lfs.github.com/spec/v1'.\n"
            "3. Re-upload the tokenizer/ directory to Hugging Face.\n"
            "4. Restart the Space. You may set FORCE_HF_DOWNLOAD=1 for the "
            "first restart after replacing the files.\n\n"
            "Do NOT substitute an arbitrary AutoTokenizer unless it has "
            "the exact same token-to-id mapping as the training tokenizer."
        )


def find_latest_checkpoint(directory: Path) -> tuple[Path, int]:
    candidates: list[tuple[int, Path]] = []

    for path in directory.rglob("model_*.pt"):
        match = re.fullmatch(r"model_(\d+)\.pt", path.name)
        if match:
            candidates.append((int(match.group(1)), path))

    if not candidates:
        raise FileNotFoundError(
            f"No model_XXXXXX.pt checkpoint found under {directory}"
        )

    candidates.sort(key=lambda item: item[0], reverse=True)
    step, checkpoint_path = candidates[0]
    return checkpoint_path, step


# =============================================================================
# Download repository
# =============================================================================

print("=" * 80)
print(f"Downloading model repository: {MODEL_ID}")
print(f"Force Hub download: {FORCE_HF_DOWNLOAD}")
print(f"Requested runtime device: {device}")
print("=" * 80)

model_dir = Path(
    snapshot_download(
        repo_id=MODEL_ID,
        token=HF_TOKEN,
        force_download=FORCE_HF_DOWNLOAD,
        ignore_patterns=[
            "optim_*.pt",
            "optimizer*.pt",
            "*.bin",
        ],
    )
)

print(f"Model repository: {model_dir}")

# Catch unresolved LFS pointers anywhere in the repository early.
repo_lfs_pointers = find_git_lfs_pointers(model_dir)
if repo_lfs_pointers:
    print("WARNING: unresolved Git LFS pointer files found in snapshot:")
    for path in repo_lfs_pointers:
        print(f"  - {path.relative_to(model_dir)}")

# Make the bundled NanoChat package importable.
sys.path.insert(0, str(model_dir))


# =============================================================================
# Import custom architecture
# =============================================================================

try:
    from nanochat.checkpoint_manager import build_model
    from nanochat.engine import Engine
    from nanochat.reasoning import (
        DEFAULT_BANDS,
        ON_HIGH,
        generate_with_budget,
        with_condition,
    )
except ImportError as error:
    raise RuntimeError(
        "Could not import the bundled NanoChat code. The model repository "
        "must contain nanochat/checkpoint_manager.py, nanochat/engine.py, "
        "and nanochat/reasoning.py."
    ) from error


# =============================================================================
# Locate latest model checkpoint
# =============================================================================

checkpoint_path, checkpoint_step = find_latest_checkpoint(model_dir)
checkpoint_dir = checkpoint_path.parent

metadata_path = checkpoint_dir / f"meta_{checkpoint_step:06d}.json"

if not metadata_path.exists():
    raise FileNotFoundError(
        "Checkpoint metadata is missing. Expected: "
        f"{metadata_path}"
    )

tokenizer_dir = checkpoint_dir / "tokenizer"

print(f"Checkpoint directory: {checkpoint_dir}")
print(f"Checkpoint file: {checkpoint_path}")
print(f"Checkpoint step: {checkpoint_step}")
print(f"Metadata file: {metadata_path}")
print(f"Tokenizer directory: {tokenizer_dir}")
print(f"Loading on device: {device}")

# Validate BEFORE NanoChat reaches pickle.load().
validate_tokenizer_directory(tokenizer_dir)


# =============================================================================
# Load custom NanoChat model
# =============================================================================

try:
    model, tokenizer, metadata = build_model(
        str(checkpoint_dir),
        checkpoint_step,
        device,
        "eval",
    )
except Exception as error:
    raise RuntimeError(
        "\nFailed to load the NanoChat checkpoint.\n"
        f"Checkpoint: {checkpoint_path}\n"
        f"Tokenizer: {tokenizer_dir}\n"
        f"Underlying error: {type(error).__name__}: {error}"
    ) from error

model.eval()
engine = Engine(model, tokenizer)

MODEL_CONTEXT_TOKENS = int(
    getattr(model.config, "sequence_len", MAX_CONTEXT_TOKENS)
)

HIGH_REASONING_SLACK = 1.25

bos_token = tokenizer.get_bos_token_id()
user_start_token = tokenizer.encode_special("<|user_start|>")
user_end_token = tokenizer.encode_special("<|user_end|>")
assistant_start_token = tokenizer.encode_special("<|assistant_start|>")
assistant_end_token = tokenizer.encode_special("<|assistant_end|>")

if device.type == "cuda":
    def autocast_context():
        return torch.amp.autocast(
            device_type="cuda",
            dtype=torch.bfloat16,
        )
else:
    autocast_context = nullcontext

print("Model and NanoChat engine loaded successfully.")
print(
    f"Model context length: {MODEL_CONTEXT_TOKENS} tokens · "
    f"Device: {device.type.upper()}"
)


# =============================================================================
# Conversation helpers
# =============================================================================

def normalize_history(
    history: list[Any] | None,
) -> list[dict[str, str]]:
    """Normalize Gradio chat history into role/content dictionaries."""
    messages: list[dict[str, str]] = []

    for item in history or []:
        if isinstance(item, dict):
            role = item.get("role")
            content = item.get("content")

            if (
                role in {"user", "assistant"}
                and isinstance(content, str)
                and content.strip()
            ):
                messages.append(
                    {
                        "role": role,
                        "content": content.strip(),
                    }
                )

        elif isinstance(item, (tuple, list)) and len(item) == 2:
            user_content, assistant_content = item

            if isinstance(user_content, str) and user_content.strip():
                messages.append(
                    {
                        "role": "user",
                        "content": user_content.strip(),
                    }
                )

            if (
                isinstance(assistant_content, str)
                and assistant_content.strip()
            ):
                messages.append(
                    {
                        "role": "assistant",
                        "content": assistant_content.strip(),
                    }
                )

    return messages


def encode_chat_message(role: str, content: str) -> list[int]:
    content_tokens = tokenizer.encode(content)

    if role == "user":
        return [
            user_start_token,
            *content_tokens,
            user_end_token,
        ]

    if role == "assistant":
        return [
            assistant_start_token,
            *content_tokens,
            assistant_end_token,
        ]

    raise ValueError(f"Unsupported role: {role}")


def group_history_into_turns(
    messages: list[dict[str, str]],
) -> list[list[dict[str, str]]]:
    """
    Group history into user-led turns so old context can be removed cleanly
    without slicing through a message boundary.
    """
    turns: list[list[dict[str, str]]] = []
    current: list[dict[str, str]] = []

    for chat_message in messages:
        if chat_message["role"] == "user":
            if current:
                turns.append(current)
            current = [chat_message]
        elif current:
            current.append(chat_message)

    if current:
        turns.append(current)

    return turns


def trim_tokens_from_left(
    tokens: list[int],
    max_length: int,
) -> list[int]:
    """Keep the newest token suffix, safely handling max_length <= 0."""
    if max_length <= 0:
        return []

    if len(tokens) <= max_length:
        return tokens

    return tokens[-max_length:]


def build_current_user_tokens(
    message: str,
    system_message: str,
    max_tokens: int,
    high_reasoning: bool,
) -> list[int]:
    """
    Build the newest user turn while preserving the behavior instruction.

    The user request is prioritized over optional instructions when the
    1,024-token context window becomes tight.
    """
    instruction = BUILT_IN_CONTEXT.strip()

    if system_message.strip():
        instruction += (
            "\n\nAdditional instruction from the user:\n"
            + system_message.strip()
        )

    if high_reasoning:
        conditioned = with_condition(
            {
                "messages": [
                    {
                        "role": "system",
                        "content": instruction,
                    },
                    {
                        "role": "user",
                        "content": message.strip(),
                    },
                ]
            },
            ON_HIGH,
        )

        # with_condition() adds the exact reasoning directive expected by
        # this reasoning-trained runtime.
        instruction = conditioned["messages"][0]["content"]

    prefix = (
        "[INSTRUCTIONS]\n"
        + instruction
        + "\n\n[CURRENT USER REQUEST]\n"
    )

    prefix_tokens = tokenizer.encode(prefix)
    message_tokens = tokenizer.encode(message.strip())

    wrapper_cost = 2  # user_start + user_end
    payload_budget = max(0, max_tokens - wrapper_cost)

    # Prioritize preserving the newest user request.
    if len(prefix_tokens) + len(message_tokens) > payload_budget:
        minimum_message_budget = min(
            len(message_tokens),
            max(32, payload_budget // 2),
        )

        prefix_budget = max(
            0,
            payload_budget - minimum_message_budget,
        )

        # Keep the beginning of the behavioral instruction.
        prefix_tokens = prefix_tokens[:prefix_budget]

        remaining_for_message = max(
            0,
            payload_budget - len(prefix_tokens),
        )

        message_tokens = trim_tokens_from_left(
            message_tokens,
            remaining_for_message,
        )

    return [
        user_start_token,
        *prefix_tokens,
        *message_tokens,
        user_end_token,
    ]


def build_conversation_tokens(
    message: str,
    history: list[Any] | None,
    system_message: str,
    max_new_tokens: int,
    use_few_shot: bool,
    high_reasoning: bool,
) -> tuple[list[int], dict[str, int]]:
    """
    Build a NanoChat-native prompt with:
      1. optional few-shot demonstrations,
      2. as much recent conversation history as fits,
      3. built-in behavior + newest user request,
      4. optional ON_HIGH reasoning directive,
      5. assistant-start token for generation.
    """
    if high_reasoning:
        high_think_cap = int(
            DEFAULT_BANDS[ON_HIGH.effort][1]
            * HIGH_REASONING_SLACK
        )
        generation_reserve = high_think_cap + max_new_tokens
    else:
        high_think_cap = 0
        generation_reserve = max_new_tokens

    maximum_prompt_length = (
        MODEL_CONTEXT_TOKENS - generation_reserve
    )

    # Keep a minimally useful prompt even if the configured reasoning
    # reserve is large relative to this small model's context.
    maximum_prompt_length = max(
        64,
        min(
            maximum_prompt_length,
            MODEL_CONTEXT_TOKENS - 16,
        ),
    )

    # Reserve BOS + assistant_start outside this helper.
    current_user_tokens = build_current_user_tokens(
        message=message,
        system_message=system_message,
        max_tokens=max(8, maximum_prompt_length - 2),
        high_reasoning=high_reasoning,
    )

    mandatory = [
        bos_token,
        *current_user_tokens,
        assistant_start_token,
    ]

    if len(mandatory) > maximum_prompt_length:
        # Preserve BOS, the newest end of the current user payload, and
        # assistant_start instead of blindly clipping the end marker.
        body_budget = max(0, maximum_prompt_length - 2)
        current_body = trim_tokens_from_left(
            current_user_tokens,
            body_budget,
        )

        clipped = [
            bos_token,
            *current_body,
            assistant_start_token,
        ]

        return clipped, {
            "prompt_tokens": len(clipped),
            "history_turns": 0,
            "few_shot_messages": 0,
            "reasoning_reserve": high_think_cap,
        }

    history_messages = normalize_history(history)
    history_turns = group_history_into_turns(history_messages)

    few_shot_messages = (
        list(FEW_SHOT_MESSAGES)
        if use_few_shot
        else []
    )

    def encode_messages(
        messages: list[dict[str, str]],
    ) -> list[int]:
        tokens: list[int] = []

        for chat_message in messages:
            tokens.extend(
                encode_chat_message(
                    chat_message["role"],
                    chat_message["content"],
                )
            )

        return tokens

    few_shot_tokens = encode_messages(few_shot_messages)

    while (
        few_shot_messages
        and len(mandatory) + len(few_shot_tokens)
        > maximum_prompt_length
    ):
        # Drop the oldest user/assistant demonstration pair.
        few_shot_messages = few_shot_messages[2:]
        few_shot_tokens = encode_messages(
            few_shot_messages
        )

    selected_turns: list[list[dict[str, str]]] = []
    running_length = (
        len(mandatory) + len(few_shot_tokens)
    )

    for turn in reversed(history_turns):
        turn_tokens = encode_messages(turn)

        if (
            running_length + len(turn_tokens)
            <= maximum_prompt_length
        ):
            selected_turns.append(turn)
            running_length += len(turn_tokens)
        else:
            break

    selected_turns.reverse()

    context_tokens: list[int] = [bos_token]
    context_tokens.extend(few_shot_tokens)

    for turn in selected_turns:
        context_tokens.extend(
            encode_messages(turn)
        )

    context_tokens.extend(current_user_tokens)
    context_tokens.append(assistant_start_token)

    return context_tokens, {
        "prompt_tokens": len(context_tokens),
        "history_turns": len(selected_turns),
        "few_shot_messages": len(few_shot_messages),
        "reasoning_reserve": high_think_cap,
    }


# =============================================================================
# Generation
# =============================================================================

def respond(
    message: str,
    history: list[Any] | None,
    custom_instruction: str,
    max_new_tokens: int,
    temperature: float,
    top_k: int,
    use_few_shot: bool,
    high_reasoning: bool,
) -> Generator[
    tuple[str, list[dict[str, str]], str],
    None,
    None,
]:
    """
    Standard:
        Streams directly from engine.generate().

    High reasoning:
        Conditions the prompt with ON_HIGH and calls
        generate_with_budget(). Only the parsed final answer is shown.
    """
    if not message or not message.strip():
        yield (
            "",
            normalize_history(history),
            "Enter a message to begin.",
        )
        return

    clean_message = message.strip()

    # `history` supplied by Gradio is the history BEFORE the current Textbox
    # message is added, so append it to the display history only.
    display_history = normalize_history(history)
    display_history.append(
        {
            "role": "user",
            "content": clean_message,
        }
    )

    mode_label = (
        "High reasoning"
        if high_reasoning
        else "Standard"
    )

    yield (
        "",
        display_history,
        f"**{mode_label}** · Building context…",
    )

    max_new_tokens = int(max_new_tokens)
    temperature = float(temperature)
    top_k = int(top_k)

    try:
        prompt_tokens, stats = build_conversation_tokens(
            message=clean_message,
            history=history,
            system_message=custom_instruction,
            max_new_tokens=max_new_tokens,
            use_few_shot=bool(use_few_shot),
            high_reasoning=bool(high_reasoning),
        )

        base_telemetry = (
            f"Mode **{mode_label}** · "
            f"Context **{stats['prompt_tokens']} / "
            f"{MODEL_CONTEXT_TOKENS}** · "
            f"History **{stats['history_turns']} turns** · "
            f"Few-shot **{stats['few_shot_messages']} messages**"
        )

        # ---------------------------------------------------------------------
        # High reasoning path
        # ---------------------------------------------------------------------
        if high_reasoning:
            yield (
                "",
                display_history,
                base_telemetry + " · **Reasoning…**",
            )

            with autocast_context():
                rollouts = generate_with_budget(
                    engine,
                    tokenizer,
                    prompt_tokens,
                    ON_HIGH,
                    num_samples=1,
                    temperature=temperature,
                    top_k=top_k,
                    seed=random.randint(0, 2**31 - 1),
                    max_answer_tokens=max_new_tokens,
                    slack=HIGH_REASONING_SLACK,
                    max_total_tokens=MODEL_CONTEXT_TOKENS,
                )

            if not rollouts:
                yield (
                    "",
                    [
                        *display_history,
                        {
                            "role": "assistant",
                            "content": (
                                "The reasoning engine generated no rollout."
                            ),
                        },
                    ],
                    base_telemetry,
                )
                return

            rollout = rollouts[0]

            parsed_answer = getattr(
                getattr(rollout, "parsed", None),
                "answer",
                "",
            )

            final_text = (
                parsed_answer.strip()
                if isinstance(parsed_answer, str)
                and parsed_answer.strip()
                else str(
                    getattr(rollout, "text", "")
                ).strip()
            )

            if not final_text:
                final_text = (
                    "The model completed its reasoning pass but "
                    "did not produce a final answer."
                )

            num_think_tokens = int(
                getattr(
                    rollout,
                    "num_think_tokens",
                    0,
                )
            )

            forced = bool(
                getattr(rollout, "forced", False)
            )

            reasoning_telemetry = (
                base_telemetry
                + f" · Think **{num_think_tokens} tokens**"
                + (
                    " · Budget forced **yes**"
                    if forced
                    else " · Budget forced **no**"
                )
            )

            yield (
                "",
                [
                    *display_history,
                    {
                        "role": "assistant",
                        "content": final_text,
                    },
                ],
                reasoning_telemetry,
            )
            return

        # ---------------------------------------------------------------------
        # Standard path
        # ---------------------------------------------------------------------
        generated_tokens: list[int] = []
        last_text = ""

        with autocast_context():
            stream = engine.generate(
                prompt_tokens,
                num_samples=1,
                max_tokens=max_new_tokens,
                temperature=temperature,
                top_k=top_k,
                seed=random.randint(0, 2**31 - 1),
            )

            for token_column, _token_masks in stream:
                raw_token = token_column[0]

                if isinstance(raw_token, torch.Tensor):
                    token = int(raw_token.item())
                else:
                    token = int(raw_token)

                if token in {
                    assistant_end_token,
                    bos_token,
                }:
                    break

                generated_tokens.append(token)

                current_text = tokenizer.decode(
                    generated_tokens
                )

                # Do not stream an incomplete UTF-8 replacement character.
                if current_text.endswith("�"):
                    continue

                if current_text != last_text:
                    last_text = current_text

                    streamed_history = [
                        *display_history,
                        {
                            "role": "assistant",
                            "content": current_text,
                        },
                    ]

                    yield (
                        "",
                        streamed_history,
                        base_telemetry,
                    )

        if not generated_tokens:
            yield (
                "",
                [
                    *display_history,
                    {
                        "role": "assistant",
                        "content": (
                            "The model generated an empty response."
                        ),
                    },
                ],
                base_telemetry,
            )

    except Exception as error:
        yield (
            "",
            [
                *display_history,
                {
                    "role": "assistant",
                    "content": (
                        "**Generation failed**\n\n"
                        f"`{type(error).__name__}: {error}`"
                    ),
                },
            ],
            f"**{mode_label}** · Generation error.",
        )


def clear_chat() -> tuple[
    list[dict[str, str]],
    str,
    str,
]:
    return [], "", "New conversation ready."


# =============================================================================
# Professional Gradio UI
# =============================================================================

CSS = """
:root {
    --app-radius: 18px;
}

.gradio-container {
    max-width: 1440px !important;
    margin: 0 auto !important;
}

#app-shell {
    padding-top: 8px;
}

#hero {
    border: 1px solid var(--border-color-primary);
    border-radius: var(--app-radius);
    padding: 20px 22px;
    margin-bottom: 14px;
    background: linear-gradient(
        135deg,
        color-mix(
            in srgb,
            var(--background-fill-primary) 94%,
            #6366f1 6%
        ),
        var(--background-fill-primary)
    );
}

#hero h1 {
    margin: 0 0 5px 0;
    font-size: 1.5rem;
    line-height: 1.2;
}

#hero p {
    margin: 0;
    opacity: 0.72;
}

.model-badge {
    display: inline-block;
    margin-top: 10px;
    padding: 5px 9px;
    border-radius: 999px;
    border: 1px solid var(--border-color-primary);
    font-size: 0.78rem;
    opacity: 0.85;
}

#chat-card,
#settings-card {
    border: 1px solid var(--border-color-primary);
    border-radius: var(--app-radius);
    background: var(--background-fill-primary);
    overflow: hidden;
}

#chatbot {
    border: none !important;
}

#composer-row {
    padding: 4px 2px 2px 2px;
}

#composer textarea {
    border-radius: 14px !important;
}

.prompt-chip button {
    border-radius: 999px !important;
    font-size: 0.82rem !important;
}

#telemetry {
    min-height: 26px;
    opacity: 0.67;
    font-size: 0.82rem;
}

.section-kicker {
    font-size: 0.76rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    opacity: 0.58;
    margin-bottom: 4px;
}

@media (max-width: 900px) {
    .gradio-container {
        padding-left: 10px !important;
        padding-right: 10px !important;
    }
}
"""


with gr.Blocks(
    title="Supernova 1 Beta · Reasoning Chat",
    theme=gr.themes.Soft(),
    css=CSS,
) as demo:
    with gr.Column(elem_id="app-shell"):
        gr.HTML(
            f"""
            <div id="hero">
                <h1>Supernova 1 Beta</h1>
                <p>
                    Few-shot reasoning chat powered by the bundled NanoChat
                    checkpoint runtime.
                </p>
                <span class="model-badge">
                    {MODEL_ID} · checkpoint {checkpoint_step:,} ·
                    {device.type.upper()}
                </span>
            </div>
            """
        )

        with gr.Row(equal_height=False):
            # -----------------------------------------------------------------
            # Main chat
            # -----------------------------------------------------------------
            with gr.Column(
                scale=8,
                min_width=520,
                elem_id="chat-card",
            ):
                chatbot = gr.Chatbot(
                    value=[],
                    height=610,
                    elem_id="chatbot",
                    layout="bubble",
                    placeholder=(
                        "Start a conversation with Supernova 1 Beta."
                    ),
                    render_markdown=True,
                    buttons=["copy", "copy_all"],
                    feedback_options=["Like", "Dislike"],
                )

                telemetry = gr.Markdown(
                    "New conversation ready.",
                    elem_id="telemetry",
                )

                with gr.Row(elem_id="composer-row"):
                    message = gr.Textbox(
                        placeholder="Message Supernova 1 Beta…",
                        show_label=False,
                        lines=1,
                        max_lines=6,
                        autofocus=True,
                        container=False,
                        scale=8,
                        elem_id="composer",
                    )

                    send_button = gr.Button(
                        "Send",
                        variant="primary",
                        scale=1,
                        min_width=86,
                    )

                with gr.Row():
                    prompt_qml = gr.Button(
                        "Explain QML simply",
                        size="sm",
                        elem_classes="prompt-chip",
                    )

                    prompt_compare = gr.Button(
                        "Compare PPO and GRPO",
                        size="sm",
                        elem_classes="prompt-chip",
                    )

                    prompt_code = gr.Button(
                        "Debug a Python function",
                        size="sm",
                        elem_classes="prompt-chip",
                    )

                    prompt_reason = gr.Button(
                        "Give me a reasoning challenge",
                        size="sm",
                        elem_classes="prompt-chip",
                    )

            # -----------------------------------------------------------------
            # Settings / context
            # -----------------------------------------------------------------
            with gr.Column(
                scale=3,
                min_width=300,
                elem_id="settings-card",
            ):
                gr.Markdown(
                    "<div class='section-kicker'>Model controls</div>"
                )

                high_reasoning = gr.Checkbox(
                    value=False,
                    label="High reasoning",
                    info=(
                        "Uses ON_HIGH with budget-forced reasoning. "
                        "Standard mode streams faster."
                    ),
                )

                use_few_shot = gr.Checkbox(
                    value=True,
                    label="Use few-shot prompting",
                    info=(
                        "Injects compact examples into the actual prompt."
                    ),
                )

                max_new_tokens = gr.Slider(
                    minimum=32,
                    maximum=256,
                    value=128,
                    step=16,
                    label="Max answer tokens",
                )

                temperature = gr.Slider(
                    minimum=0.0,
                    maximum=2.0,
                    value=0.7,
                    step=0.1,
                    label="Temperature",
                )

                top_k = gr.Slider(
                    minimum=1,
                    maximum=200,
                    value=50,
                    step=1,
                    label="Top-k",
                )

                custom_instruction = gr.Textbox(
                    value="",
                    label="Additional instruction",
                    placeholder=(
                        "Example: Answer like a concise research assistant."
                    ),
                    lines=4,
                    max_lines=8,
                )

                with gr.Accordion(
                    "Reasoning behavior",
                    open=False,
                ):
                    gr.Markdown(
                        """
                        **High reasoning** adds the checkpoint's
                        `reasoning: high` condition and uses
                        budget-forced generation. The UI displays the
                        final answer while reporting thinking-token
                        usage in the telemetry line.
                        """
                    )

                with gr.Accordion(
                    "Built-in context",
                    open=False,
                ):
                    gr.Textbox(
                        value=BUILT_IN_CONTEXT.strip(),
                        interactive=False,
                        show_label=False,
                        lines=11,
                    )

                with gr.Accordion(
                    "Few-shot examples",
                    open=False,
                ):
                    gr.Markdown(
                        "\n\n".join(
                            (
                                f"**{item['role'].title()}**\n\n"
                                f"{item['content']}"
                            )
                            for item in FEW_SHOT_MESSAGES
                        )
                    )

                new_chat = gr.Button(
                    "New chat",
                    variant="secondary",
                )

        gr.Markdown(
            """
            <div style="
                text-align:center;
                opacity:.55;
                font-size:.78rem;
                padding:8px 0 2px 0;
            ">
                Experimental model demo · Responses may be incomplete
                or incorrect.
            </div>
            """
        )

    # -------------------------------------------------------------------------
    # Events
    # -------------------------------------------------------------------------

    generation_inputs = [
        message,
        chatbot,
        custom_instruction,
        max_new_tokens,
        temperature,
        top_k,
        use_few_shot,
        high_reasoning,
    ]

    generation_outputs = [
        message,
        chatbot,
        telemetry,
    ]

    message.submit(
        fn=respond,
        inputs=generation_inputs,
        outputs=generation_outputs,
        show_progress="hidden",
        concurrency_limit=1,
    )

    send_button.click(
        fn=respond,
        inputs=generation_inputs,
        outputs=generation_outputs,
        show_progress="hidden",
        concurrency_limit=1,
    )

    new_chat.click(
        fn=clear_chat,
        inputs=None,
        outputs=[
            chatbot,
            message,
            telemetry,
        ],
        show_progress="hidden",
    )

    prompt_qml.click(
        fn=lambda: (
            "Explain quantum machine learning to a software engineer "
            "in under 120 words."
        ),
        outputs=message,
        show_progress="hidden",
    )

    prompt_compare.click(
        fn=lambda: (
            "Compare PPO and GRPO: objective, data flow, advantages, "
            "and when you would use each."
        ),
        outputs=message,
        show_progress="hidden",
    )

    prompt_code.click(
        fn=lambda: (
            "Give me a short Python function containing one subtle bug, "
            "then ask me to find and fix it."
        ),
        outputs=message,
        show_progress="hidden",
    )

    prompt_reason.click(
        fn=lambda: (
            "Give me a difficult but self-contained reasoning problem. "
            "Do not reveal the answer until I respond."
        ),
        outputs=message,
        show_progress="hidden",
    )


if __name__ == "__main__":
    demo.queue(
        default_concurrency_limit=1,
    ).launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        ssr_mode=False,
    )