File size: 89,181 Bytes
b57ed91
3e64737
 
 
 
2c68bd8
3e64737
ce1eb3c
 
 
efc52d4
cd3e466
 
 
 
 
 
e881a6a
 
 
0459869
 
9dc6d98
 
 
66cb301
ce1eb3c
 
9b7ad24
ce1eb3c
 
 
cd3e466
 
9dc6d98
cd3e466
0459869
b198b5a
55e2010
e21d148
 
b198b5a
0459869
5b7059f
444c474
db322cc
cd3e466
 
b198b5a
 
 
 
 
ce1eb3c
9dc6d98
 
 
 
 
 
 
 
efc52d4
db322cc
 
 
 
 
 
 
5b7059f
db322cc
5b7059f
db322cc
 
b198b5a
55e2010
db322cc
55e2010
 
0459869
b198b5a
55e2010
 
 
5b7059f
db322cc
 
 
b198b5a
55e2010
 
 
 
 
 
 
 
 
 
db322cc
55e2010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0459869
55e2010
 
db322cc
 
55e2010
 
db322cc
55e2010
5b7059f
db322cc
e881a6a
db322cc
 
e881a6a
ce1eb3c
cd3e466
 
 
 
 
ce1eb3c
 
5a3b92c
 
d62b229
 
 
 
 
 
 
ce1eb3c
e581856
 
 
 
41f6b04
e581856
41f6b04
e581856
41f6b04
e581856
 
ce1eb3c
 
 
 
 
41f6b04
ce1eb3c
 
41f6b04
ce1eb3c
 
 
 
ce9371b
ce1eb3c
e581856
 
41f6b04
ce1eb3c
 
 
41f6b04
66cb301
e881a6a
fcf1816
 
 
 
 
 
058e198
fcf1816
 
 
 
 
058e198
 
 
 
fcf1816
 
 
 
 
 
 
 
41f6b04
fcf1816
 
 
 
0459869
ed548e3
fcf1816
 
 
 
 
 
 
 
 
0459869
 
 
 
fcf1816
ed548e3
fcf1816
 
cd3e466
fcf1816
cd3e466
 
fcf1816
0459869
fcf1816
 
 
 
 
 
cd3e466
 
ed548e3
fcf1816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
929de97
fcf1816
 
55e2010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e881a6a
f17f847
 
 
 
 
 
0d7fd90
f17f847
 
4ed126e
41f6b04
d3a1938
55e2010
d3a1938
 
 
 
 
 
 
 
4ed126e
e21d148
ce9371b
 
 
55e2010
ce9371b
 
 
 
 
e21d148
 
ce9371b
 
e21d148
ce9371b
e21d148
 
ce9371b
e21d148
 
ce9371b
 
 
e21d148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce9371b
 
 
 
 
e21d148
ce9371b
 
 
 
 
 
 
 
e21d148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce9371b
 
e21d148
 
 
 
 
 
 
 
 
 
 
 
d3a1938
ce9371b
 
 
e21d148
ce9371b
 
 
 
 
 
 
e21d148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce9371b
 
e21d148
 
 
 
 
 
 
 
 
 
 
 
 
 
ce9371b
 
f17f847
0d7fd90
9dc6d98
0d7fd90
 
 
 
 
 
 
 
 
 
9dc6d98
 
 
 
 
 
 
 
 
 
0d7fd90
 
 
9dc6d98
 
 
 
 
 
 
 
 
 
 
 
ce9371b
9dc6d98
ce9371b
9dc6d98
 
 
 
 
0d7fd90
ce9371b
0d7fd90
ce9371b
 
 
 
 
 
 
 
0d7fd90
 
 
 
 
d3a1938
 
97d65ae
0d7fd90
 
 
 
 
 
ce9371b
 
 
9dc6d98
0d7fd90
 
 
 
f17f847
55e2010
9b7ad24
55e2010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b198b5a
55e2010
db322cc
9dc6d98
55e2010
 
 
 
e881a6a
efc52d4
e881a6a
55e2010
 
d3a1938
 
55e2010
e881a6a
9b7ad24
efc52d4
9b7ad24
55e2010
0459869
55e2010
 
d3a1938
55e2010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a1938
55e2010
 
fcf1816
55e2010
 
 
 
b198b5a
55e2010
 
efc52d4
55e2010
9b7ad24
55e2010
 
 
 
e881a6a
9dc6d98
 
 
 
 
db322cc
0d7fd90
ce1eb3c
e581856
fcf1816
e581856
ce1eb3c
cd3e466
 
0d7fd90
 
 
 
fcf1816
ce1eb3c
0d7fd90
ed548e3
0d7fd90
55e2010
0d7fd90
55e2010
 
 
 
 
 
 
 
 
 
 
6f8fb84
55e2010
0d7fd90
55e2010
 
 
 
fcf1816
ce1eb3c
55e2010
 
ed548e3
55e2010
ed548e3
55e2010
 
 
 
ed548e3
0d7fd90
0e95f56
6f8fb84
ce1eb3c
 
 
97d65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1eb3c
 
 
97d65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1eb3c
 
 
97d65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1eb3c
0e95f56
97d65ae
 
 
ce1eb3c
ed548e3
ce1eb3c
97d65ae
ce1eb3c
97d65ae
 
 
 
 
 
 
 
ce1eb3c
97d65ae
 
 
ce1eb3c
97d65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1eb3c
 
 
0e95f56
6f8fb84
ce1eb3c
 
 
97d65ae
 
 
 
 
 
 
 
 
 
9dc6d98
 
 
97d65ae
0e95f56
97d65ae
 
 
 
48e62d8
41f6b04
ce1eb3c
41f6b04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1eb3c
97d65ae
 
 
 
 
 
 
 
 
 
 
ce1eb3c
97d65ae
 
ce1eb3c
41f6b04
 
97d65ae
 
 
 
9dc6d98
 
ce1eb3c
 
97d65ae
 
ce1eb3c
97d65ae
 
ce1eb3c
97d65ae
f640af2
97d65ae
 
 
 
 
 
 
 
0459869
ce1eb3c
97d65ae
 
ce1eb3c
41f6b04
 
ce1eb3c
97d65ae
 
a703d91
97d65ae
 
 
 
a703d91
97d65ae
 
 
a703d91
97d65ae
9dc6d98
 
97d65ae
9dc6d98
97d65ae
9dc6d98
97d65ae
 
 
f640af2
97d65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
a703d91
97d65ae
9dc6d98
 
 
 
 
97d65ae
a703d91
0459869
97d65ae
3e64737
97d65ae
 
 
 
 
 
 
 
 
 
9dc6d98
 
 
 
 
 
97d65ae
 
 
 
 
 
 
 
48e62d8
 
 
 
 
 
12d1d7f
97d65ae
 
 
0459869
48e62d8
97d65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a1938
97d65ae
 
48e62d8
d3a1938
 
 
 
 
 
 
 
 
 
97d65ae
 
 
 
d3a1938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48e62d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a1938
 
48e62d8
d3a1938
48e62d8
 
 
 
 
 
 
 
 
 
ce1eb3c
48e62d8
97d65ae
48e62d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a1938
 
48e62d8
 
d3a1938
 
48e62d8
 
 
d3a1938
 
 
48e62d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a1938
 
 
 
 
 
 
 
 
 
 
 
 
48e62d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e95f56
 
ce1eb3c
97d65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
9dc6d98
97d65ae
efc52d4
 
 
 
 
 
 
 
ed548e3
 
9dc6d98
 
 
 
 
 
 
 
 
97d65ae
 
9dc6d98
 
 
 
 
 
 
 
 
 
 
efc52d4
97d65ae
efc52d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1eb3c
 
97d65ae
efc52d4
 
 
97d65ae
 
 
efc52d4
ed548e3
97d65ae
 
 
efc52d4
97d65ae
 
 
 
efc52d4
 
0459869
ed548e3
41f6b04
ed548e3
efc52d4
41f6b04
 
ed548e3
 
 
 
 
 
 
0459869
e7a939b
41f6b04
 
 
 
 
 
 
 
 
 
 
 
 
0459869
41f6b04
ed548e3
55e2010
e7a939b
ed548e3
 
e7a939b
 
 
ed548e3
 
e7a939b
97d65ae
 
efc52d4
 
ed548e3
97d65ae
efc52d4
 
 
ed548e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97d65ae
ed548e3
97d65ae
ed548e3
 
 
 
 
 
efc52d4
97d65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0459869
 
 
 
 
 
 
 
 
 
efc52d4
909d163
e7a939b
0459869
 
 
 
 
 
 
ed548e3
e7a939b
 
97d65ae
 
 
 
 
 
ed548e3
 
 
 
 
 
 
 
efc52d4
 
 
ce1eb3c
 
97d65ae
 
 
 
 
 
 
 
 
 
 
efc52d4
 
97d65ae
 
 
 
 
 
 
 
 
 
48e62d8
 
 
 
 
 
 
 
97d65ae
48e62d8
0459869
 
 
 
 
97d65ae
 
0459869
 
 
 
 
e7a939b
 
0459869
 
 
 
 
 
ed548e3
e7a939b
a703d91
97d65ae
 
48e62d8
97d65ae
 
efc52d4
 
 
a703d91
97d65ae
 
 
 
 
 
efc52d4
97d65ae
 
efc52d4
 
97d65ae
efc52d4
97d65ae
a703d91
97d65ae
 
 
 
a703d91
efc52d4
8dff1ad
 
 
 
0459869
8dff1ad
 
 
 
 
0459869
 
 
 
 
 
 
e7a939b
 
0459869
 
 
 
 
 
ed548e3
e7a939b
97d65ae
 
 
 
 
 
8dff1ad
97d65ae
 
 
 
 
 
 
0459869
97d65ae
 
0459869
97d65ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efc52d4
97d65ae
 
 
 
 
 
 
 
 
 
48e62d8
97d65ae
 
 
 
48e62d8
97d65ae
 
efc52d4
 
 
97d65ae
 
 
3af8029
 
 
 
 
 
 
 
 
97d65ae
3af8029
97d65ae
 
 
 
 
 
 
 
 
efc52d4
 
97d65ae
 
 
0459869
 
 
 
 
 
41f6b04
 
 
 
 
 
0459869
 
41f6b04
 
 
 
efc52d4
0459869
 
a703d91
efc52d4
97d65ae
efc52d4
97d65ae
0459869
0e95f56
97d65ae
efc52d4
97d65ae
0459869
0e95f56
97d65ae
efc52d4
97d65ae
0459869
0e95f56
97d65ae
efc52d4
97d65ae
0459869
ce1eb3c
97d65ae
efc52d4
97d65ae
0459869
a703d91
e881a6a
9dc6d98
 
 
 
 
 
 
 
 
 
efc52d4
 
97d65ae
 
ce1eb3c
efc52d4
ce1eb3c
6e6aad7
ce1eb3c
 
0d7fd90
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
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
import gradio as gr
import pandas as pd
import json
import os
import re
from PyPDF2 import PdfReader
from collections import defaultdict
from typing import Dict, List, Optional, Tuple, Union
import html
from pathlib import Path
import fitz  # PyMuPDF
import pytesseract
from PIL import Image
import io
import secrets
import string
from huggingface_hub import HfApi, HfFolder
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import time
import logging
import asyncio
from functools import lru_cache
import hashlib
from concurrent.futures import ThreadPoolExecutor

# ========== CONFIGURATION ==========
PROFILES_DIR = "student_profiles"
ALLOWED_FILE_TYPES = [".pdf", ".png", ".jpg", ".jpeg"]
MAX_FILE_SIZE_MB = 5
MIN_AGE = 5
MAX_AGE = 120
SESSION_TOKEN_LENGTH = 32
HF_TOKEN = os.getenv("HF_TOKEN")
SESSION_TIMEOUT = 3600  # 1 hour session timeout

# Initialize logging
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    filename='transcript_parser.log'
)

# Model configuration - Only DeepSeek
MODEL_NAME = "deepseek-ai/deepseek-llm-7b"

# Initialize Hugging Face API
if HF_TOKEN:
    try:
        hf_api = HfApi(token=HF_TOKEN)
        HfFolder.save_token(HF_TOKEN)
    except Exception as e:
        logging.error(f"Failed to initialize Hugging Face API: {str(e)}")

# ========== CACHING AND PERFORMANCE OPTIMIZATIONS ==========
executor = ThreadPoolExecutor(max_workers=4)

# Cache model loading
@lru_cache(maxsize=1)
def get_model_and_tokenizer():
    return model_loader.load_model()

# ========== MODEL LOADER ==========
class ModelLoader:
    def __init__(self):
        self.model = None
        self.tokenizer = None
        self.loaded = False
        self.loading = False
        self.error = None
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
    
    def load_model(self, progress: gr.Progress = None) -> Tuple[Optional[AutoModelForCausalLM], Optional[AutoTokenizer]]:
        """Lazy load the model with progress feedback"""
        try:
            if progress:
                progress(0.1, desc="Checking GPU availability...")
            
            # Clear CUDA cache first
            torch.cuda.empty_cache()
            
            if progress:
                progress(0.2, desc="Loading tokenizer...")
                
            tokenizer = AutoTokenizer.from_pretrained(
                MODEL_NAME,
                trust_remote_code=True
            )
            
            if progress:
                progress(0.5, desc="Loading model (this may take a few minutes)...")
                
            # More robust model loading
            model_kwargs = {
                "trust_remote_code": True,
                "torch_dtype": torch.float16 if self.device == "cuda" else torch.float32,
                "device_map": "auto" if self.device == "cuda" else None,
                "low_cpu_mem_usage": True,
                "offload_folder": "offload"  # For handling large models
            }
            
            try:
                model = AutoModelForCausalLM.from_pretrained(
                    MODEL_NAME,
                    **model_kwargs
                )
            except torch.cuda.OutOfMemoryError:
                # Fallback to CPU if GPU OOM
                model_kwargs["device_map"] = None
                model = AutoModelForCausalLM.from_pretrained(
                    MODEL_NAME,
                    **model_kwargs
                ).to('cpu')
                self.device = 'cpu'
                
            # Verify model is responsive
            test_input = tokenizer("Test", return_tensors="pt").to(self.device)
            _ = model.generate(**test_input, max_new_tokens=1)
            
            self.model = model.eval()
            self.tokenizer = tokenizer
            self.loaded = True
            
            return model, tokenizer
            
        except Exception as e:
            self.error = f"Model loading failed: {str(e)}"
            logging.error(self.error)
            return None, None

# Initialize model loader
model_loader = ModelLoader()

# ========== UTILITY FUNCTIONS ==========
def generate_session_token() -> str:
    """Generate a random session token for user identification."""
    alphabet = string.ascii_letters + string.digits
    return ''.join(secrets.choice(alphabet) for _ in range(SESSION_TOKEN_LENGTH))

def sanitize_input(text: str) -> str:
    """Sanitize user input to prevent XSS and injection attacks."""
    if not text:
        return ""
    # Basic HTML escaping and removal of potentially dangerous characters
    text = html.escape(text.strip())
    # Remove any remaining HTML tags
    text = re.sub(r'<[^>]*>', '', text)
    # Remove potentially dangerous characters
    text = re.sub(r'[^\w\s\-.,!?@#\$%^&*()+=]', '', text)
    return text

def validate_name(name: str) -> str:
    """Validate name input."""
    name = name.strip()
    if not name:
        raise ValueError("Name cannot be empty. Please enter your full name.")
    if len(name) > 100:
        raise ValueError("Name is too long (maximum 100 characters).")
    if any(c.isdigit() for c in name):
        raise ValueError("Name cannot contain numbers.")
    return name

def validate_age(age: Union[int, float, str]) -> int:
    """Validate and convert age input."""
    try:
        age_int = int(age)
        if not MIN_AGE <= age_int <= MAX_AGE:
            raise ValueError(f"Age must be between {MIN_AGE} and {MAX_AGE}.")
        return age_int
    except (ValueError, TypeError):
        raise ValueError("Please enter a valid age number.")

def validate_file(file_obj) -> None:
    """Validate uploaded file."""
    if not file_obj:
        raise ValueError("Please upload a file first")
    
    file_ext = os.path.splitext(file_obj.name)[1].lower()
    if file_ext not in ALLOWED_FILE_TYPES:
        raise ValueError(f"Invalid file type. Allowed types: {', '.join(ALLOWED_FILE_TYPES)}")
    
    file_size = os.path.getsize(file_obj.name) / (1024 * 1024)  # MB
    if file_size > MAX_FILE_SIZE_MB:
        raise ValueError(f"File too large. Maximum size is {MAX_FILE_SIZE_MB}MB.")

# ========== TEXT EXTRACTION FUNCTIONS ==========
def extract_text_from_file(file_path: str, file_ext: str) -> str:
    """Enhanced text extraction with better error handling and fallbacks."""
    text = ""
    
    try:
        if file_ext == '.pdf':
            # First try PyMuPDF for text extraction
            try:
                doc = fitz.open(file_path)
                for page in doc:
                    text += page.get_text("text") + '\n'
                if not text.strip():
                    raise ValueError("PyMuPDF returned empty text - the PDF may be image-based")
            except Exception as e:
                logging.warning(f"PyMuPDF failed: {str(e)}. Trying OCR fallback...")
                text = extract_text_from_pdf_with_ocr(file_path)
        
        elif file_ext in ['.png', '.jpg', '.jpeg']:
            text = extract_text_with_ocr(file_path)
            
        # Clean up the extracted text
        text = clean_extracted_text(text)
        
        if not text.strip():
            raise ValueError("No text could be extracted. Please ensure the file is clear and readable.")
            
        return text
    
    except Exception as e:
        logging.error(f"Text extraction error: {str(e)}")
        raise gr.Error(f"Failed to extract text: {str(e)}\n\nTIPS:\n1. For PDFs, try saving as a different PDF format\n2. For images, ensure they are high-quality and well-lit\n3. Try cropping to just the transcript area")

def extract_text_from_pdf_with_ocr(file_path: str) -> str:
    """Fallback PDF text extraction using OCR."""
    text = ""
    try:
        doc = fitz.open(file_path)
        for page in doc:
            pix = page.get_pixmap()
            img = Image.open(io.BytesIO(pix.tobytes()))
            # Preprocess image for better OCR
            img = img.convert('L')  # Grayscale
            img = img.point(lambda x: 0 if x < 128 else 255)  # Binarize
            text += pytesseract.image_to_string(img, config='--psm 6 --oem 3') + '\n'
    except Exception as e:
        raise ValueError(f"PDF OCR failed: {str(e)}. The PDF may be password protected or corrupted.")
    return text

def extract_text_with_ocr(file_path: str) -> str:
    """Extract text from image files using OCR with preprocessing."""
    try:
        image = Image.open(file_path)
        
        # Enhanced preprocessing
        image = image.convert('L')  # Convert to grayscale
        image = image.point(lambda x: 0 if x < 128 else 255, '1')  # Thresholding
        
        # Custom Tesseract configuration
        custom_config = r'--oem 3 --psm 6'
        text = pytesseract.image_to_string(image, config=custom_config)
        return text
    except Exception as e:
        raise ValueError(f"OCR processing failed: {str(e)}. Please ensure the image is clear and not blurry.")

def clean_extracted_text(text: str) -> str:
    """Clean and normalize the extracted text."""
    # Remove multiple spaces and newlines
    text = re.sub(r'\s+', ' ', text).strip()
    
    # Fix common OCR errors
    replacements = {
        '|': 'I',
        '‘': "'",
        '’': "'",
        '“': '"',
        '”': '"',
        'fi': 'fi',
        'fl': 'fl'
    }
    
    for wrong, right in replacements.items():
        text = text.replace(wrong, right)
    
    return text

def remove_sensitive_info(text: str) -> str:
    """Remove potentially sensitive information from transcript text."""
    # Remove social security numbers
    text = re.sub(r'\b\d{3}-\d{2}-\d{4}\b', '[REDACTED]', text)
    # Remove student IDs (assuming 6-9 digit numbers)
    text = re.sub(r'\b\d{6,9}\b', '[ID]', text)
    # Remove email addresses
    text = re.sub(r'\b[A-Za-z0-9._%+-]+@[A-Za-z9.-]+\.[A-Z|a-z]{2,}\b', '[EMAIL]', text)
    return text

def validate_parsed_data(data: Dict) -> bool:
    """Validate the structure of parsed transcript data"""
    required_student_fields = ['name', 'current_grade']
    required_course_fields = ['description', 'credits']
    
    if 'student_info' not in data:
        return False
    if not all(field in data['student_info'] for field in required_student_fields):
        return False
    if 'course_history' not in data or not isinstance(data['course_history'], list):
        return False
    if len(data['course_history']) > 0:
        if not all(field in data['course_history'][0] for field in required_course_fields):
            return False
    return True

# ========== TRANSCRIPT PARSING ==========
class TranscriptParser:
    def __init__(self):
        self.student_data = {}
        self.requirements = {}
        self.current_courses = []
        self.course_history = []
        self.graduation_status = {}
        
    def parse_transcript(self, text: str) -> Dict:
        """Parse Miami-Dade formatted transcripts with updated regex patterns."""
        try:
            # First try structured parsing for Miami-Dade format
            if "Graduation Progress Summary" in text or "Miami-Dade" in text:
                return self._parse_miami_dade_format(text)
            else:
                # Fall back to AI parsing if not Miami-Dade format
                return parse_transcript_with_ai_fallback(text)
                
        except Exception as e:
            logging.error(f"Error parsing transcript: {str(e)}")
            raise ValueError(f"Couldn't parse transcript: {str(e)}")

    def _parse_miami_dade_format(self, text: str, strict_mode: bool = False) -> Dict:
        """Parse Miami-Dade County Public Schools transcripts."""
        # Initialize PDF reader from text (simulating the PDF structure)
        lines = [line.strip() for line in text.split('\n') if line.strip()]
        
        # Initialize data structure
        data = {
            'student_info': {},
            'graduation_requirements': [],
            'course_history': [],
            'summary': {},
            'format': 'miami_dade'  # Add format identifier
        }
        
        # Parse student information with more robust checks
        student_info_lines = []
        for i, line in enumerate(lines):
            logging.debug(f"Processing line: {line}")  # Added debug logging
            if "DORAL ACADEMY HIGH SCHOOL" in line:
                # Get the next 4 lines (or fewer if we're at the end)
                student_info_lines = lines[i:i+5]
                break
        
        if student_info_lines:
            try:
                # Parse school and cohort info - more defensive
                school_info_parts = student_info_lines[0].split('|')
                if len(school_info_parts) > 2:
                    data['student_info']['school'] = school_info_parts[1].strip() if len(school_info_parts) > 1 else ''
                    data['student_info']['district'] = school_info_parts[2].strip() if len(school_info_parts) > 2 else ''
                
                # Parse student name and ID - more defensive
                if len(student_info_lines) > 1:
                    name_id_line = student_info_lines[1].split('-')
                    if len(name_id_line) > 1:
                        name_parts = name_id_line[1].split(',')
                        if len(name_parts) > 1:
                            data['student_info']['student_id'] = name_id_line[0].strip()
                            data['student_info']['student_name'] = name_parts[1].strip() + " " + name_parts[0].strip()
                
                # Parse academic info - more defensive
                if len(student_info_lines) > 2:
                    academic_info = student_info_lines[2].split('|')
                    if len(academic_info) > 5:
                        data['student_info']['current_grade'] = academic_info[1].split(':')[1].strip() if ':' in academic_info[1] else ''
                        data['student_info']['graduation_year'] = academic_info[2].strip()
                        data['student_info']['weighted_gpa'] = academic_info[3].split(':')[1].strip() if ':' in academic_info[3] else ''
                        data['student_info']['community_service_date'] = academic_info[4].split(':')[1].strip() if ':' in academic_info[4] else ''
                        data['student_info']['total_credits_earned'] = academic_info[5].split(':')[1].strip() if ':' in academic_info[5] else ''
                        
                # Validate we got the essential student info
                if not data['student_info'].get('student_name'):
                    logging.warning("Failed to parse student name")
                    if strict_mode:
                        raise ValueError("Could not parse student name from transcript")
                        
            except Exception as e:
                logging.warning(f"Error parsing student info: {str(e)}")
                if strict_mode:
                    raise
        
        # Parse graduation requirements
        requirements_start = None
        requirements_end = None
        for i, line in enumerate(lines):
            logging.debug(f"Processing line: {line}")  # Added debug logging
            if "Code" in line and "Description" in line and "Required" in line:
                requirements_start = i + 1
            if requirements_start and "Total" in line:
                requirements_end = i
                break
        
        if requirements_start and requirements_end:
            for line in lines[requirements_start:requirements_end]:
                try:
                    if '|' in line:
                        parts = [p.strip() for p in line.split('|') if p.strip()]
                        if len(parts) >= 6:
                            req = {
                                'code': parts[0],
                                'description': parts[1],
                                'required': parts[2],
                                'waived': parts[3],
                                'completed': parts[4],
                                'status': parts[5]
                            }
                            data['graduation_requirements'].append(req)
                except Exception as e:
                    logging.warning(f"Error parsing requirement line: {line} - {str(e)}")
                    if strict_mode:
                        raise
            
            # Parse total line
            try:
                total_line = lines[requirements_end]
                total_parts = [p.strip() for p in total_line.split('|') if p.strip()]
                if len(total_parts) >= 5:
                    data['summary']['total_required'] = total_parts[1]
                    data['summary']['total_waived'] = total_parts[2]
                    data['summary']['total_completed'] = total_parts[3]
                    data['summary']['completion_percentage'] = total_parts[4]
            except Exception as e:
                logging.warning(f"Error parsing requirements summary: {str(e)}")
                if strict_mode:
                    raise
        
        # Parse course history
        course_history_start = None
        for i, line in enumerate(lines):
            logging.debug(f"Processing line: {line}")  # Added debug logging
            if "Requirement" in line and "School Year" in line and "GradeLv1" in line:
                course_history_start = i + 1
                break
        
        if course_history_start:
            current_requirement = None
            for line in lines[course_history_start:]:
                try:
                    if '|' in line:
                        parts = [p.strip() for p in line.split('|') if p.strip()]
                        
                        # Check if this is a new requirement line
                        if len(parts) >= 2 and parts[0] and parts[0] in [req['code'] for req in data['graduation_requirements']]:
                            current_requirement = parts[0]
                            parts = parts[1:]  # Remove the requirement code
                        
                        if len(parts) >= 9:
                            course = {
                                'requirement': current_requirement,
                                'school_year': parts[0],
                                'grade_level': parts[1],
                                'course_number': parts[2],
                                'description': parts[3],
                                'term': parts[4],
                                'district_number': parts[5],
                                'fg': parts[6],
                                'included': parts[7],
                                'credits': parts[8]
                            }
                            data['course_history'].append(course)
                except Exception as e:
                    logging.warning(f"Error parsing course line: {line} - {str(e)}")
                    if strict_mode:
                        raise
        
        # Calculate graduation status
        try:
            if data['summary'].get('total_required') and data['summary'].get('total_completed'):
                graduation_status = {
                    'total_required_credits': float(data['summary']['total_required']),
                    'total_completed_credits': float(data['summary']['total_completed']),
                    'percent_complete': float(data['summary']['completion_percentage'].replace('%', '')),
                    'remaining_credits': float(data['summary']['total_required']) - float(data['summary']['total_completed']),
                    'on_track': float(data['summary']['completion_percentage'].replace('%', '')) >= 75.0
                }
                data['graduation_status'] = graduation_status
        except Exception as e:
            logging.warning(f"Error calculating graduation status: {str(e)}")
            if strict_mode:
                raise
        
        return data

def format_transcript_output(data: Dict) -> str:
    """Enhanced formatting for transcript output with format awareness"""
    output = []
    
    # Student Info Section
    student = data.get("student_info", {})
    output.append(f"## Student Transcript Summary\n{'='*50}")
    output.append(f"**Name:** {student.get('name', 'Unknown')}")
    output.append(f"**Student ID:** {student.get('id', 'Unknown')}")
    output.append(f"**Current Grade:** {student.get('current_grade', 'Unknown')}")
    output.append(f"**Graduation Year:** {student.get('graduation_year', 'Unknown')}")
    
    if 'unweighted_gpa' in student and 'weighted_gpa' in student:
        output.append(f"**Unweighted GPA:** {student['unweighted_gpa']}")
        output.append(f"**Weighted GPA:** {student['weighted_gpa']}")
    elif 'gpa' in student:
        output.append(f"**GPA:** {student['gpa']}")
    
    if 'total_credits' in student:
        output.append(f"**Total Credits Earned:** {student['total_credits']}")
    if 'community_service_hours' in student:
        output.append(f"**Community Service Hours:** {student['community_service_hours']}")
    
    output.append("")
    
    # Graduation Requirements Section (for Miami-Dade format)
    if data.get('format') == 'miami_dade':
        grad_status = data.get("graduation_status", {})
        output.append(f"## Graduation Progress\n{'='*50}")
        output.append(f"**Overall Completion:** {grad_status.get('percent_complete', 0)}%")
        output.append(f"**Credits Required:** {grad_status.get('total_required_credits', 0)}")
        output.append(f"**Credits Completed:** {grad_status.get('total_completed_credits', 0)}")
        output.append(f"**Credits Remaining:** {grad_status.get('remaining_credits', 0)}")
        output.append(f"**On Track to Graduate:** {'Yes' if grad_status.get('on_track', False) else 'No'}\n")
        
        # Detailed Requirements
        output.append("### Detailed Requirements:")
        for req in data.get("graduation_requirements", []):
            output.append(
                f"- **{req['code']}**: {req['description']}\n"
                f"  Required: {req['required']} | Completed: {req['completed']} | "
                f"Status: {req['status']}"
            )
        output.append("")
    
    # Current Courses
    if any(c.get('credits', '') == 'inProgress' for c in data.get("course_history", [])):
        output.append("## Current Courses (In Progress)\n" + '='*50)
        for course in data["course_history"]:
            if course.get('credits', '') == 'inProgress':
                output.append(
                    f"- **{course['course_number']} {course['description']}**\n"
                    f"  Category: {course['requirement']} | "
                    f"Grade Level: {course['grade_level']} | "
                    f"Term: {course['term']} | Credits: {course['credits']}"
                )
        output.append("")
    
    # Course History by Year
    courses_by_year = defaultdict(list)
    for course in data.get("course_history", []):
        if course.get("school_year"):
            courses_by_year[course["school_year"]].append(course)
    
    if courses_by_year:
        output.append("## Course History\n" + '='*50)
        for year in sorted(courses_by_year.keys()):
            output.append(f"\n### {year}")
            for course in courses_by_year[year]:
                output.append(
                    f"- **{course.get('course_number', '')} {course.get('description', 'Unnamed course')}**\n"
                    f"  Subject: {course.get('requirement', 'N/A')} | "
                    f"Grade: {course.get('fg', 'N/A')} | "
                    f"Credits: {course.get('credits', 'N/A')}"
                )
    
    return '\n'.join(output)

def parse_transcript_with_ai_fallback(text: str, progress=gr.Progress()) -> Dict:
    """More robust AI parsing with better error handling"""
    try:
        text = remove_sensitive_info(text[:20000])  # Increased limit
        
        # Improved prompt with examples
        prompt = f"""Extract academic transcript data as JSON. Follow this structure:
        
        Example Input:
        Student ID: 1234567 Name: DOE, JOHN Current Grade: 12 YOG: 2024
        Unweighted GPA: 3.5 Weighted GPA: 4.2 Total Credits: 24.5
        
        Example Output:
        {{
            "student_info": {{
                "name": "John Doe",
                "id": "1234567",
                "current_grade": "12",
                "graduation_year": "2024",
                "unweighted_gpa": 3.5,
                "weighted_gpa": 4.2,
                "total_credits": 24.5
            }},
            "course_history": [
                {{
                    "course_code": "MATH101",
                    "description": "Algebra I",
                    "grade": "A",
                    "credits": 1.0,
                    "school_year": "2022-2023"
                }}
            ]
        }}
        
        Actual Transcript:
        {text}
        """
        
        if progress:
            progress(0.3, desc="Processing with AI...")
        
        model, tokenizer = get_model_and_tokenizer()
        if model is None:
            raise ValueError("Model not loaded")
            
        inputs = tokenizer(prompt, return_tensors="pt", truncation=True).to(model_loader.device)
        
        outputs = model.generate(
            **inputs,
            max_new_tokens=2500,
            temperature=0.3,  # Lower for more consistent results
            do_sample=True,
            top_p=0.9,
            repetition_penalty=1.2
        )
        
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        # More robust JSON extraction
        try:
            if '```json' in response:
                json_str = response.split('```json')[1].split('```')[0].strip()
            else:
                json_str = response.split('{', 1)[1].rsplit('}', 1)[0]
                json_str = '{' + json_str + '}'
                
            parsed_data = json.loads(json_str)
            
            # Validate required fields
            if not all(k in parsed_data for k in ["student_info", "course_history"]):
                raise ValueError("Missing required fields in AI response")
                
            return parsed_data
            
        except Exception as e:
            logging.error(f"JSON parsing failed: {str(e)}")
            raise ValueError(f"AI returned invalid format. Please try again.")
            
    except Exception as e:
        logging.error(f"AI parsing error: {str(e)}")
        raise gr.Error(f"Failed to parse transcript: {str(e)}")

def parse_transcript_with_ai(text: str, progress=gr.Progress()) -> Dict:
    """Enhanced AI parsing with fallback to structured parsing"""
    try:
        # First try structured parsing
        if progress:
            progress(0.1, desc="Attempting structured parsing...")
        
        parser = TranscriptParser()
        parsed_data = parser.parse_transcript(text)
        
        # Validate the parsed data
        if not validate_parsed_data(parsed_data):
            raise ValueError("Structured parsing returned incomplete data")
            
        if progress:
            progress(0.8, desc="Formatting results...")
        
        return parsed_data
        
    except Exception as e:
        logging.warning(f"Structured parsing failed, falling back to AI: {str(e)}")
        
        # Fall back to AI parsing if structured parsing fails
        return parse_transcript_with_ai_fallback(text, progress)

async def parse_transcript_async(file_obj, progress=gr.Progress()) -> Tuple[str, Optional[Dict]]:
    """Async wrapper for transcript parsing"""
    loop = asyncio.get_event_loop()
    return await loop.run_in_executor(executor, parse_transcript, file_obj, progress)

def parse_transcript(file_obj, progress=gr.Progress()) -> Tuple[str, Optional[Dict]]:
    """Main function to parse transcript files with better error handling"""
    try:
        if not file_obj:
            raise ValueError("Please upload a file first")
            
        validate_file(file_obj)
        file_ext = os.path.splitext(file_obj.name)[1].lower()
        
        # Extract text from file with better error reporting
        if progress:
            progress(0.2, desc="Extracting text from file...")
        
        text = extract_text_from_file(file_obj.name, file_ext)
        
        if not text.strip():
            raise ValueError("No text could be extracted from the file. The file may be corrupted or in an unsupported format.")
        
        # Try structured parsing first
        if progress:
            progress(0.4, desc="Attempting structured parsing...")
            
        parser = TranscriptParser()
        try:
            parsed_data = parser.parse_transcript(text)
            if validate_parsed_data(parsed_data):
                if progress:
                    progress(0.9, desc="Formatting results...")
                return format_transcript_output(parsed_data), parsed_data
        except Exception as e:
            logging.warning(f"Structured parsing failed: {str(e)}")
        
        # Fall back to AI if structured fails
        if progress:
            progress(0.5, desc="Using AI analysis...")
            
        parsed_data = parse_transcript_with_ai_fallback(text, progress)
        return format_transcript_output(parsed_data), parsed_data
        
    except Exception as e:
        error_msg = f"Error processing transcript: {str(e)}"
        # Add specific troubleshooting tips
        if "PDF" in str(e):
            error_msg += "\n\nTIPS:\n1. Try converting to image (screenshot)\n2. Ensure text is selectable in PDF\n3. Try a different PDF reader"
        elif "image" in str(e).lower():
            error_msg += "\n\nTIPS:\n1. Use high contrast images\n2. Crop to just the transcript\n3. Ensure good lighting"
        elif "AI" in str(e):
            error_msg += "\n\nTIPS:\n1. Try a smaller section of the transcript\n2. Check for sensitive info that may be redacted\n3. Try again later"
            
        logging.error(error_msg)
        return error_msg, None

# ========== LEARNING STYLE QUIZ ==========
class LearningStyleQuiz:
    def __init__(self):
        self.questions = [
            "When you study for a test, you prefer to:",
            "When you need directions to a new place, you prefer:",
            "When you learn a new skill, you prefer to:",
            "When you're trying to concentrate, you:",
            "When you meet new people, you remember them by:",
            "When you're assembling furniture or a gadget, you:",
            "When choosing a restaurant, you rely most on:",
            "When you're in a waiting room, you typically:",
            "When giving someone instructions, you tend to:",
            "When you're trying to recall information, you:",
            "When you're at a museum or exhibit, you:",
            "When you're learning a new language, you prefer:",
            "When you're taking notes in class, you:",
            "When you're explaining something complex, you:",
            "When you're at a party, you enjoy:",
            "When you're trying to remember a phone number, you:",
            "When you're relaxing, you prefer to:",
            "When you're learning to use new software, you:",
            "When you're giving a presentation, you rely on:",
            "When you're solving a difficult problem, you:"
        ]
        
        self.options = [
            ["Read the textbook (Reading/Writing)", "Listen to lectures (Auditory)", "Use diagrams/charts (Visual)", "Practice problems (Kinesthetic)"],
            ["Look at a map (Visual)", "Have someone tell you (Auditory)", "Write down directions (Reading/Writing)", "Try walking/driving there (Kinesthetic)"],
            ["Read instructions (Reading/Writing)", "Have someone show you (Visual)", "Listen to explanations (Auditory)", "Try it yourself (Kinesthetic)"],
            ["Need quiet (Reading/Writing)", "Need background noise (Auditory)", "Need to move around (Kinesthetic)", "Need visual stimulation (Visual)"],
            ["Their face (Visual)", "Their name (Auditory)", "What you talked about (Reading/Writing)", "What you did together (Kinesthetic)"],
            ["Read the instructions carefully (Reading/Writing)", "Look at the diagrams (Visual)", "Ask someone to explain (Auditory)", "Start putting pieces together (Kinesthetic)"],
            ["Online photos of the food (Visual)", "Recommendations from friends (Auditory)", "Reading the menu online (Reading/Writing)", "Remembering how it felt to eat there (Kinesthetic)"],
            ["Read magazines (Reading/Writing)", "Listen to music (Auditory)", "Watch TV (Visual)", "Fidget or move around (Kinesthetic)"],
            ["Write them down (Reading/Writing)", "Explain verbally (Auditory)", "Demonstrate (Visual)", "Guide them physically (Kinesthetic)"],
            ["See written words in your mind (Visual)", "Hear the information in your head (Auditory)", "Write it down to remember (Reading/Writing)", "Associate it with physical actions (Kinesthetic)"],
            ["Read all the descriptions (Reading/Writing)", "Listen to audio guides (Auditory)", "Look at the displays (Visual)", "Touch interactive exhibits (Kinesthetic)"],
            ["Study grammar rules (Reading/Writing)", "Listen to native speakers (Auditory)", "Use flashcards with images (Visual)", "Practice conversations (Kinesthetic)"],
            ["Write detailed paragraphs (Reading/Writing)", "Record the lecture (Auditory)", "Draw diagrams and charts (Visual)", "Doodle while listening (Kinesthetic)"],
            ["Write detailed steps (Reading/Writing)", "Explain verbally with examples (Auditory)", "Draw diagrams (Visual)", "Use physical objects to demonstrate (Kinesthetic)"],
            ["Conversations with people (Auditory)", "Watching others or the environment (Visual)", "Writing notes or texting (Reading/Writing)", "Dancing or physical activities (Kinesthetic)"],
            ["See the numbers in your head (Visual)", "Say them aloud (Auditory)", "Write them down (Reading/Writing)", "Dial them on a keypad (Kinesthetic)"],
            ["Read a book (Reading/Writing)", "Listen to music (Auditory)", "Watch TV/movies (Visual)", "Do something physical (Kinesthetic)"],
            ["Read the manual (Reading/Writing)", "Ask someone to show you (Visual)", "Call tech support (Auditory)", "Experiment with the software (Kinesthetic)"],
            ["Detailed notes (Reading/Writing)", "Verbal explanations (Auditory)", "Visual slides (Visual)", "Physical demonstrations (Kinesthetic)"],
            ["Write out possible solutions (Reading/Writing)", "Talk through it with someone (Auditory)", "Draw diagrams (Visual)", "Build a model or prototype (Kinesthetic)"]
        ]
        
        self.learning_styles = {
            "Visual": {
                "description": "Visual learners prefer using images, diagrams, and spatial understanding.",
                "tips": [
                    "Use color coding in your notes",
                    "Create mind maps and diagrams",
                    "Watch educational videos",
                    "Use flashcards with images",
                    "Highlight important information in different colors"
                ],
                "careers": [
                    "Graphic Designer", "Architect", "Photographer", 
                    "Engineer", "Surgeon", "Pilot"
                ]
            },
            "Auditory": {
                "description": "Auditory learners learn best through listening and speaking.",
                "tips": [
                    "Record lectures and listen to them",
                    "Participate in study groups",
                    "Explain concepts out loud to yourself",
                    "Use rhymes or songs to remember information",
                    "Listen to educational podcasts"
                ],
                "careers": [
                    "Musician", "Journalist", "Lawyer",
                    "Psychologist", "Teacher", "Customer Service"
                ]
            },
            "Reading/Writing": {
                "description": "These learners prefer information displayed as words.",
                "tips": [
                    "Write detailed notes",
                    "Create summaries in your own words",
                    "Read textbooks and articles",
                    "Make lists to organize information",
                    "Rewrite your notes to reinforce learning"
                ],
                "careers": [
                    "Writer", "Researcher", "Editor",
                    "Accountant", "Programmer", "Historian"
                ]
            },
            "Kinesthetic": {
                "description": "Kinesthetic learners learn through movement and hands-on activities.",
                "tips": [
                    "Use hands-on activities",
                    "Take frequent movement breaks",
                    "Create physical models",
                    "Associate information with physical actions",
                    "Study while walking or pacing"
                ],
                "careers": [
                    "Athlete", "Chef", "Mechanic",
                    "Dancer", "Physical Therapist", "Carpenter"
                ]
            }
        }
    
    def evaluate_quiz(self, *answers) -> str:
        """Evaluate quiz answers and generate enhanced results."""
        answers = list(answers)  # Convert tuple to list
        if len(answers) != len(self.questions):
            raise gr.Error("Please answer all questions before submitting")
        
        scores = {style: 0 for style in self.learning_styles}
        
        for i, answer in enumerate(answers):
            if not answer:
                continue  # Skip unanswered questions
                
            for j, style in enumerate(self.learning_styles):
                if answer == self.options[i][j]:
                    scores[style] += 1
                    break
        
        total_answered = sum(1 for ans in answers if ans)
        if total_answered == 0:
            raise gr.Error("No answers provided")
        
        percentages = {style: (score/total_answered)*100 for style, score in scores.items()}
        sorted_styles = sorted(scores.items(), key=lambda x: x[1], reverse=True)
        
        # Generate enhanced results report
        result = "## Your Learning Style Results\n\n"
        result += "### Scores:\n"
        for style, score in sorted_styles:
            result += f"- **{style}**: {score}/{total_answered} ({percentages[style]:.1f}%)\n"
        
        max_score = max(scores.values())
        primary_styles = [style for style, score in scores.items() if score == max_score]
        
        result += "\n### Analysis:\n"
        if len(primary_styles) == 1:
            primary_style = primary_styles[0]
            style_info = self.learning_styles[primary_style]
            
            result += f"Your primary learning style is **{primary_style}**\n\n"
            result += f"**{primary_style} Characteristics**:\n"
            result += f"{style_info['description']}\n\n"
            
            result += "**Recommended Study Strategies**:\n"
            for tip in style_info['tips']:
                result += f"- {tip}\n"
            
            result += "\n**Potential Career Paths**:\n"
            for career in style_info['careers'][:6]:
                result += f"- {career}\n"
            
            # Add complementary strategies
            complementary = [s for s in sorted_styles if s[0] != primary_style][0][0]
            result += f"\nYou might also benefit from some **{complementary}** strategies:\n"
            for tip in self.learning_styles[complementary]['tips'][:3]:
                result += f"- {tip}\n"
        else:
            result += "You have multiple strong learning styles:\n"
            for style in primary_styles:
                result += f"- **{style}**\n"
            
            result += "\n**Combined Learning Strategies**:\n"
            result += "You may benefit from combining different learning approaches:\n"
            for style in primary_styles:
                result += f"\n**{style}** techniques:\n"
                for tip in self.learning_styles[style]['tips'][:2]:
                    result += f"- {tip}\n"
                
                result += f"\n**{style}** career suggestions:\n"
                for career in self.learning_styles[style]['careers'][:3]:
                    result += f"- {career}\n"
        
        return result

# Initialize quiz instance
learning_style_quiz = LearningStyleQuiz()

# ========== PROFILE MANAGEMENT ==========
class ProfileManager:
    def __init__(self):
        self.profiles_dir = Path(PROFILES_DIR)
        self.profiles_dir.mkdir(exist_ok=True, parents=True)
        self.current_session = None
    
    def set_session(self, session_token: str) -> None:
        """Set the current session token."""
        self.current_session = session_token
    
    def get_profile_path(self, name: str) -> Path:
        """Get profile path with session token if available."""
        if self.current_session:
            # Hash the name for security
            name_hash = hashlib.sha256(name.encode()).hexdigest()[:16]
            return self.profiles_dir / f"{name_hash}_{self.current_session}_profile.json"
        return self.profiles_dir / f"{name.replace(' ', '_')}_profile.json"
    
    def save_profile(self, name: str, age: Union[int, str], interests: str, 
                    transcript: Dict, learning_style: str, 
                    movie: str, movie_reason: str, show: str, show_reason: str, 
                    book: str, book_reason: str, character: str, character_reason: str, 
                    blog: str) -> str:
        """Save student profile with better validation messages"""
        try:
            # Validate required fields with specific messages
            if not name.strip():
                raise ValueError("Name cannot be empty. Please enter your full name.")
            if len(name) > 100:
                raise ValueError("Name is too long (maximum 100 characters).")
            if any(c.isdigit() for c in name):
                raise ValueError("Name cannot contain numbers.")
            
            try:
                age_int = int(age)
                if not MIN_AGE <= age_int <= MAX_AGE:
                    raise ValueError(f"Age must be between {MIN_AGE} and {MAX_AGE}.")
            except (ValueError, TypeError):
                raise ValueError("Please enter a valid age number.")
            
            if not interests.strip():
                raise ValueError("Please describe at least one interest or hobby.")
            
            if not transcript:
                raise ValueError("Please complete the transcript analysis first.")
            
            # Validate learning style quiz completion
            if not learning_style or "Your primary learning style is:" not in learning_style:
                raise ValueError("Please complete the learning style quiz first.")
            
            # Prepare favorites data
            favorites = {
                "movie": sanitize_input(movie),
                "movie_reason": sanitize_input(movie_reason),
                "show": sanitize_input(show),
                "show_reason": sanitize_input(show_reason),
                "book": sanitize_input(book),
                "book_reason": sanitize_input(book_reason),
                "character": sanitize_input(character),
                "character_reason": sanitize_input(character_reason)
            }
            
            # Prepare full profile data
            data = {
                "name": name,
                "age": age_int,
                "interests": sanitize_input(interests),
                "transcript": transcript if transcript else {},
                "learning_style": learning_style if learning_style else "Not assessed",
                "favorites": favorites,
                "blog": sanitize_input(blog) if blog else "",
                "session_token": self.current_session,
                "last_updated": time.time()
            }
            
            # Save to JSON file
            filepath = self.get_profile_path(name)
            
            with open(filepath, "w", encoding='utf-8') as f:
                json.dump(data, f, indent=2, ensure_ascii=False)
            
            # Upload to HF Hub if token is available
            if HF_TOKEN and 'hf_api' in globals():
                try:
                    hf_api.upload_file(
                        path_or_fileobj=filepath,
                        path_in_repo=f"profiles/{filepath.name}",
                        repo_id="your-username/student-learning-assistant",
                        repo_type="dataset"
                    )
                except Exception as e:
                    logging.error(f"Failed to upload to HF Hub: {str(e)}")
            
            return self._generate_profile_summary(data)
        
        except Exception as e:
            logging.error(f"Profile validation error: {str(e)}")
            raise gr.Error(f"Couldn't save profile: {str(e)}")
    
    def load_profile(self, name: str = None, session_token: str = None) -> Dict:
        """Load profile by name or return the first one found."""
        try:
            if session_token:
                profile_pattern = f"*{session_token}_profile.json"
            else:
                profile_pattern = "*.json"
            
            profiles = list(self.profiles_dir.glob(profile_pattern))
            if not profiles:
                return {}
            
            if name:
                # Find profile by name (hashed)
                name_hash = hashlib.sha256(name.encode()).hexdigest()[:16]
                if session_token:
                    profile_file = self.profiles_dir / f"{name_hash}_{session_token}_profile.json"
                else:
                    profile_file = self.profiles_dir / f"{name_hash}_profile.json"
                
                if not profile_file.exists():
                    # Try loading from HF Hub
                    if HF_TOKEN and 'hf_api' in globals():
                        try:
                            hf_api.download_file(
                                path_in_repo=f"profiles/{profile_file.name}",
                                repo_id="your-username/student-learning-assistant",
                                repo_type="dataset",
                                local_dir=self.profiles_dir
                            )
                        except:
                            raise gr.Error(f"No profile found for {name}")
                    else:
                        raise gr.Error(f"No profile found for {name}")
            else:
                # Load the first profile found
                profile_file = profiles[0]
            
            with open(profile_file, "r", encoding='utf-8') as f:
                profile_data = json.load(f)
                # Check session timeout
                if time.time() - profile_data.get('last_updated', 0) > SESSION_TIMEOUT:
                    raise gr.Error("Session expired. Please start a new session.")
                return profile_data
        
        except Exception as e:
            logging.error(f"Error loading profile: {str(e)}")
            return {}
    
    def list_profiles(self, session_token: str = None) -> List[str]:
        """List all available profile names for the current session."""
        if session_token:
            profiles = list(self.profiles_dir.glob(f"*{session_token}_profile.json"))
        else:
            profiles = list(self.profiles_dir.glob("*.json"))
        
        # Extract just the name part (without session token)
        profile_names = []
        for p in profiles:
            with open(p, "r", encoding='utf-8') as f:
                try:
                    data = json.load(f)
                    profile_names.append(data.get('name', p.stem))
                except json.JSONDecodeError:
                    continue
        
        return profile_names
    
    def _generate_profile_summary(self, data: Dict) -> str:
        """Generate markdown summary of the profile."""
        transcript = data.get("transcript", {})
        favorites = data.get("favorites", {})
        
        # Extract just the learning style name
        learning_style = data.get("learning_style", "")
        if "Your primary learning style is:" in learning_style:
            style_match = re.search(r"Your primary learning style is: \*\*(.*?)\*\*", learning_style)
            if style_match:
                learning_style = style_match.group(1)
        
        markdown = f"""## Student Profile: {data['name']}
### Basic Information
- **Age:** {data['age']}
- **Interests:** {data.get('interests', 'Not specified')}
- **Learning Style:** {learning_style}
### Academic Information
{self._format_transcript(transcript)}
### Favorites
- **Movie:** {favorites.get('movie', 'Not specified')}  
  *Reason:* {favorites.get('movie_reason', 'Not specified')}
- **TV Show:** {favorites.get('show', 'Not specified')}  
  *Reason:* {favorites.get('show_reason', 'Not specified')}
- **Book:** {favorites.get('book', 'Not specified')}  
  *Reason:* {favorites.get('book_reason', 'Not specified')}
- **Character:** {favorites.get('character', 'Not specified')}  
  *Reason:* {favorites.get('character_reason', 'Not specified')}
### Personal Blog
{data.get('blog', '_No blog provided_')}
"""
        return markdown
    
    def _format_transcript(self, transcript: Dict) -> str:
        """Format transcript data for display."""
        if not transcript or "course_history" not in transcript:
            return "_No transcript information available_"
        
        display = "#### Course History\n"
        courses_by_year = defaultdict(list)
        for course in transcript.get("course_history", []):
            if course.get("school_year"):
                courses_by_year[course["school_year"]].append(course)
        
        if courses_by_year:
            for year in sorted(courses_by_year.keys()):
                display += f"\n**{year}**\n"
                for course in courses_by_year[year]:
                    display += f"- {course.get('course_code', '')} {course.get('description', 'Unnamed course')}"
                    if 'grade' in course and course['grade']:
                        display += f" (Grade: {course['grade']})"
                    if 'credits' in course:
                        display += f" | Credits: {course['credits']}"
                    display += f" | Category: {course.get('requirement_category', 'N/A')}\n"
        
        if 'student_info' in transcript:
            student = transcript['student_info']
            display += "\n**Academic Summary**\n"
            display += f"- Unweighted GPA: {student.get('unweighted_gpa', 'N/A')}\n"
            display += f"- Weighted GPA: {student.get('weighted_gpa', 'N/A')}\n"
            display += f"- Total Credits: {student.get('total_credits', 'N/A')}\n"
        
        if 'graduation_status' in transcript:
            status = transcript['graduation_status']
            display += "\n**Graduation Progress**\n"
            display += f"- Completion: {status.get('percent_complete', 0)}%\n"
            display += f"- Credits Required: {status.get('total_required_credits', 0)}\n"
            display += f"- Credits Completed: {status.get('total_completed_credits', 0)}\n"
            display += f"- On Track: {'Yes' if status.get('on_track', False) else 'No'}\n"
        
        return display

# Initialize profile manager
profile_manager = ProfileManager()

# ========== AI TEACHING ASSISTANT ==========
class TeachingAssistant:
    def __init__(self):
        self.context_history = []
        self.max_context_length = 5  # Keep last 5 exchanges for context
    
    async def generate_response(self, message: str, history: List[List[Union[str, None]]], session_token: str) -> str:
        """Generate personalized response based on student profile and context."""
        try:
            # Load profile with session token
            profile = profile_manager.load_profile(session_token=session_token)
            if not profile:
                return "Please complete and save your profile first using the previous tabs."
            
            # Update context history
            self._update_context(message, history)
            
            # Extract profile information
            name = profile.get("name", "there")
            learning_style = profile.get("learning_style", "")
            grade_level = profile.get("transcript", {}).get("student_info", {}).get("current_grade", "unknown")
            gpa = profile.get("transcript", {}).get("student_info", {})
            interests = profile.get("interests", "")
            courses = profile.get("transcript", {}).get("course_history", [])
            favorites = profile.get("favorites", {})
            
            # Process message with context
            response = await self._process_message(message, profile)
            
            # Add follow-up suggestions
            if "study" in message.lower() or "learn" in message.lower():
                response += "\n\nWould you like me to suggest a study schedule based on your courses?"
            elif "course" in message.lower() or "class" in message.lower():
                response += "\n\nWould you like help finding resources for any of these courses?"
            
            return response
        
        except Exception as e:
            logging.error(f"Error generating response: {str(e)}")
            return "I encountered an error processing your request. Please try again."
    
    def _update_context(self, message: str, history: List[List[Union[str, None]]]) -> None:
        """Maintain conversation context."""
        self.context_history.append({"role": "user", "content": message})
        if history:
            for h in history[-self.max_context_length:]:
                if h[0]:  # User message
                    self.context_history.append({"role": "user", "content": h[0]})
                if h[1]:  # Assistant message
                    self.context_history.append({"role": "assistant", "content": h[1]})
        
        # Trim to maintain max context length
        self.context_history = self.context_history[-(self.max_context_length*2):]
    
    async def _process_message(self, message: str, profile: Dict) -> str:
        """Process user message with profile context."""
        message_lower = message.lower()
        
        # Greetings
        if any(greet in message_lower for greet in ["hi", "hello", "hey", "greetings"]):
            return f"Hello {profile.get('name', 'there')}! How can I help you with your learning today?"
        
        # Study help
        study_words = ["study", "learn", "prepare", "exam", "test", "homework"]
        if any(word in message_lower for word in study_words):
            return self._generate_study_advice(profile)
        
        # Grade help
        grade_words = ["grade", "gpa", "score", "marks", "results"]
        if any(word in message_lower for word in grade_words):
            return self._generate_grade_advice(profile)
        
        # Interest help
        interest_words = ["interest", "hobby", "passion", "extracurricular"]
        if any(word in message_lower for word in interest_words):
            return self._generate_interest_advice(profile)
        
        # Course help
        course_words = ["courses", "classes", "transcript", "schedule", "subject"]
        if any(word in message_lower for word in course_words):
            return self._generate_course_advice(profile)
        
        # Favorites
        favorite_words = ["movie", "show", "book", "character", "favorite"]
        if any(word in message_lower for word in favorite_words):
            return self._generate_favorites_response(profile)
        
        # General help
        if "help" in message_lower:
            return self._generate_help_response()
        
        # Default response
        return ("I'm your personalized teaching assistant. I can help with study tips, "
                "grade information, course advice, and more. Try asking about how to "
                "study effectively or about your course history.")
    
    def _generate_study_advice(self, profile: Dict) -> str:
        """Generate study advice based on learning style."""
        learning_style = profile.get("learning_style", "")
        response = ""
        
        if "Visual" in learning_style:
            response = ("Based on your visual learning style, I recommend:\n"
                       "- Creating colorful mind maps or diagrams\n"
                       "- Using highlighters to color-code your notes\n"
                       "- Watching educational videos on the topics\n"
                       "- Creating flashcards with images\n\n")
        elif "Auditory" in learning_style:
            response = ("Based on your auditory learning style, I recommend:\n"
                       "- Recording your notes and listening to them\n"
                       "- Participating in study groups to discuss concepts\n"
                       "- Explaining the material out loud to yourself\n"
                       "- Finding podcasts or audio lectures on the topics\n\n")
        elif "Reading/Writing" in learning_style:
            response = ("Based on your reading/writing learning style, I recommend:\n"
                       "- Writing detailed summaries in your own words\n"
                       "- Creating organized outlines of the material\n"
                       "- Reading additional textbooks or articles\n"
                       "- Rewriting your notes to reinforce learning\n\n")
        elif "Kinesthetic" in learning_style:
            response = ("Based on your kinesthetic learning style, I recommend:\n"
                       "- Creating physical models or demonstrations\n"
                       "- Using hands-on activities to learn concepts\n"
                       "- Taking frequent movement breaks while studying\n"
                       "- Associating information with physical actions\n\n")
        else:
            response = ("Here are some general study tips:\n"
                       "- Use the Pomodoro technique (25 min study, 5 min break)\n"
                       "- Space out your study sessions over time\n"
                       "- Test yourself with practice questions\n"
                       "- Teach the material to someone else\n\n")
        
        # Add time management advice
        response += ("**Time Management Tips**:\n"
                    "- Create a study schedule and stick to it\n"
                    "- Prioritize difficult subjects when you're most alert\n"
                    "- Break large tasks into smaller, manageable chunks\n"
                    "- Set specific goals for each study session")
        
        return response
    
    def _generate_grade_advice(self, profile: Dict) -> str:
        """Generate response about grades and GPA."""
        gpa = profile.get("transcript", {}).get("student_info", {})
        courses = profile.get("transcript", {}).get("course_history", [])
        
        response = (f"Your GPA information:\n"
                   f"- Unweighted: {gpa.get('unweighted_gpa', 'N/A')}\n"
                   f"- Weighted: {gpa.get('weighted_gpa', 'N/A')}\n\n")
        
        # Identify any failing grades
        weak_subjects = []
        for course in courses:
            if course.get('grade', '').upper() in ['D', 'F']:
                weak_subjects.append(f"{course.get('course_code', '')} {course.get('description', 'Unknown course')}")
        
        if weak_subjects:
            response += ("**Areas for Improvement**:\n"
                        f"You might want to focus on these subjects: {', '.join(weak_subjects)}\n\n")
        
        response += ("**Grade Improvement Strategies**:\n"
                    "- Meet with your teachers to discuss your performance\n"
                    "- Identify specific areas where you lost points\n"
                    "- Create a targeted study plan for weak areas\n"
                    "- Practice with past exams or sample questions")
        
        return response
    
    def _generate_interest_advice(self, profile: Dict) -> str:
        """Generate response based on student interests."""
        interests = profile.get("interests", "")
        response = f"I see you're interested in: {interests}\n\n"
        
        response += ("**Suggestions**:\n"
                    "- Look for clubs or extracurricular activities related to these interests\n"
                    "- Explore career paths that align with these interests\n"
                    "- Find online communities or forums about these topics\n"
                    "- Consider projects or independent study in these areas")
        
        return response
    
    def _generate_course_advice(self, profile: Dict) -> str:
        """Generate response about courses."""
        courses = profile.get("transcript", {}).get("course_history", [])
        grade_level = profile.get("transcript", {}).get("student_info", {}).get("current_grade", "unknown")
        
        response = "Here's a summary of your courses by year:\n"
        courses_by_year = defaultdict(list)
        for course in courses:
            if course.get("school_year"):
                courses_by_year[course["school_year"]].append(course)
        
        for year in sorted(courses_by_year.keys()):
            response += f"\n**{year}**:\n"
            for course in courses_by_year[year]:
                response += f"- {course.get('course_code', '')} {course.get('description', 'Unnamed course')}"
                if 'grade' in course:
                    response += f" (Grade: {course['grade']})"
                response += "\n"
        
        response += f"\nAs a grade {grade_level} student, you might want to:\n"
        if grade_level in ["9", "10"]:
            response += ("- Focus on building strong foundational skills\n"
                        "- Explore different subjects to find your interests\n"
                        "- Start thinking about college/career requirements")
        elif grade_level in ["11", "12"]:
            response += ("- Focus on courses relevant to your college/career goals\n"
                        "- Consider taking AP or advanced courses if available\n"
                        "- Ensure you're meeting graduation requirements")
        
        return response
    
    def _generate_favorites_response(self, profile: Dict) -> str:
        """Generate response about favorite items."""
        favorites = profile.get("favorites", {})
        response = "I see you enjoy:\n"
        
        if favorites.get('movie'):
            response += f"- Movie: {favorites['movie']} ({favorites.get('movie_reason', 'no reason provided')})\n"
        if favorites.get('show'):
            response += f"- TV Show: {favorites['show']} ({favorites.get('show_reason', 'no reason provided')})\n"
        if favorites.get('book'):
            response += f"- Book: {favorites['book']} ({favorites.get('book_reason', 'no reason provided')})\n"
        if favorites.get('character'):
            response += f"- Character: {favorites['character']} ({favorites.get('character_reason', 'no reason provided')})\n"
        
        response += "\nThese preferences suggest you might enjoy:\n"
        response += "- Similar books/movies in the same genre\n"
        response += "- Creative projects related to these stories\n"
        response += "- Analyzing themes or characters in your schoolwork"
        
        return response
    
    def _generate_help_response(self) -> str:
        """Generate help response with available commands."""
        return ("""I can help with:
- **Study tips**: "How should I study for math?"
- **Grade information**: "What's my GPA?"
- **Course advice**: "Show me my course history"
- **Interest suggestions**: "What clubs match my interests?"
- **General advice**: "How can I improve my grades?"
Try asking about any of these topics!""")

# Initialize teaching assistant
teaching_assistant = TeachingAssistant()

# ========== GRADIO INTERFACE ==========
def create_interface():
    with gr.Blocks(theme=gr.themes.Soft(), title="Student Learning Assistant") as app:
        # Session state
        session_token = gr.State(value=generate_session_token())
        profile_manager.set_session(session_token.value)
        
        # Track completion status for each tab
        tab_completed = gr.State({
            0: False,  # Transcript Upload
            1: False,  # Learning Style Quiz
            2: False,  # Personal Questions
            3: False,  # Save & Review
            4: False   # AI Assistant
        })
        
        # Custom CSS with dark mode support
        app.css = """
        .gradio-container { max-width: 1200px !important; margin: 0 auto !important; }
        .tab-content { padding: 20px !important; border: 1px solid #e0e0e0 !important; border-radius: 8px !important; margin-top: 10px !important; }
        .completed-tab { background: #4CAF50 !important; color: white !important; }
        .incomplete-tab { background: #E0E0E0 !important; }
        .nav-message { padding: 10px; margin: 10px 0; border-radius: 4px; background-color: #ffebee; color: #c62828; }
        .file-upload { border: 2px dashed #4CAF50 !important; padding: 20px !important; border-radius: 8px !important; }
        .progress-bar { height: 5px; background: linear-gradient(to right, #4CAF50, #8BC34A); margin-bottom: 15px; border-radius: 3px; }
        .quiz-question { margin-bottom: 15px; padding: 15px; background: #f5f5f5; border-radius: 5px; }
        .quiz-results { margin-top: 20px; padding: 20px; background: #e8f5e9; border-radius: 8px; }
        .error-message { color: #d32f2f; background-color: #ffebee; padding: 10px; border-radius: 4px; margin: 10px 0; }
        
        /* Dark mode support */
        .dark .tab-content { background-color: #2d2d2d !important; border-color: #444 !important; }
        .dark .quiz-question { background-color: #3d3d3d !important; }
        .dark .quiz-results { background-color: #2e3d2e !important; }
        .dark textarea, .dark input { background-color: #333 !important; color: #eee !important; }
        .dark .output-markdown { color: #eee !important; }
        .dark .chatbot { background-color: #333 !important; }
        .dark .chatbot .user, .dark .chatbot .assistant { color: #eee !important; }
        """
        
        # Header with dark mode toggle
        with gr.Row():
            with gr.Column(scale=4):
                gr.Markdown("""
                # Student Learning Assistant
                **Your personalized education companion**  
                Complete each step to get customized learning recommendations.
                """)
            with gr.Column(scale=1):
                dark_mode = gr.Checkbox(label="Dark Mode", value=False)
        
        # Navigation buttons
        with gr.Row():
            with gr.Column(scale=1, min_width=100):
                step1 = gr.Button("1. Transcript", elem_classes="incomplete-tab")
            with gr.Column(scale=1, min_width=100):
                step2 = gr.Button("2. Quiz", elem_classes="incomplete-tab", interactive=False)
            with gr.Column(scale=1, min_width=100):
                step3 = gr.Button("3. Profile", elem_classes="incomplete-tab", interactive=False)
            with gr.Column(scale=1, min_width=100):
                step4 = gr.Button("4. Review", elem_classes="incomplete-tab", interactive=False)
            with gr.Column(scale=1, min_width=100):
                step5 = gr.Button("5. Assistant", elem_classes="incomplete-tab", interactive=False)

        nav_message = gr.HTML(visible=False)

        # Main tabs container - Now VISIBLE
        with gr.Tabs(visible=True) as tabs:
            # ===== TAB 1: TRANSCRIPT UPLOAD =====
            with gr.Tab("Transcript", id=0):
                with gr.Row():
                    with gr.Column(scale=1):
                        gr.Markdown("### Step 1: Upload Your Transcript")
                        with gr.Group(elem_classes="file-upload"):
                            file_input = gr.File(
                                label="Drag and drop your transcript here (PDF or Image)",
                                file_types=ALLOWED_FILE_TYPES,
                                type="filepath"
                            )
                            upload_btn = gr.Button("Analyze Transcript", variant="primary")
                            file_error = gr.HTML(visible=False)
                    
                    with gr.Column(scale=2):
                        transcript_output = gr.Textbox(
                            label="Analysis Results", 
                            lines=20,
                            interactive=False
                        )
                        transcript_data = gr.State()

                def process_transcript(file_obj, current_tab_status):
                    try:
                        if not file_obj:
                            raise ValueError("Please upload a transcript file first.")
                            
                        output_text, data = parse_transcript(file_obj)
                        
                        if "Error" in output_text:
                            return (
                                output_text,
                                None,
                                current_tab_status,
                                gr.update(),
                                gr.update(),
                                gr.update(visible=True, value=f"<div class='error-message'>{output_text}</div>"),
                                gr.update(visible=False)
                            )
                        
                        new_status = current_tab_status.copy()
                        new_status[0] = True
                        return (
                            output_text, 
                            data, 
                            new_status,
                            gr.update(elem_classes="completed-tab"),
                            gr.update(interactive=True),
                            gr.update(visible=False),
                            gr.update(visible=False)
                        )
                        
                    except Exception as e:
                        error_msg = f"Error processing transcript: {str(e)}"
                        if "PDF" in str(e):
                            error_msg += "\n\nTIPS:\n- Try converting to image (screenshot)\n- Ensure text is selectable in PDF\n- Try a different PDF reader"
                        return (
                            error_msg,
                            None,
                            current_tab_status,
                            gr.update(),
                            gr.update(),
                            gr.update(visible=True, value=f"<div class='error-message'>{error_msg}</div>"),
                            gr.update(visible=False)
                        )
                
                upload_btn.click(
                    process_transcript,
                    inputs=[file_input, tab_completed],
                    outputs=[transcript_output, transcript_data, tab_completed, step1, step2, file_error, nav_message]
                )

            # ===== TAB 2: LEARNING STYLE QUIZ =====
            with gr.Tab("Learning Style Quiz", id=1):
                with gr.Column():
                    gr.Markdown("### Step 2: Discover Your Learning Style")
                    progress = gr.HTML("<div class='progress-bar' style='width: 0%'></div>")
                    
                    quiz_components = []
                    with gr.Accordion("Quiz Questions", open=True):
                        for i, (question, options) in enumerate(zip(learning_style_quiz.questions, learning_style_quiz.options)):
                            with gr.Group(elem_classes="quiz-question"):
                                q = gr.Radio(
                                    options,
                                    label=f"{i+1}. {question}",
                                    show_label=True
                                )
                                quiz_components.append(q)
                    
                    with gr.Row():
                        quiz_submit = gr.Button("Submit Quiz", variant="primary")
                        quiz_clear = gr.Button("Clear Answers")
                    
                    quiz_alert = gr.HTML(visible=False)
                    learning_output = gr.Markdown(
                        label="Your Learning Style Results",
                        visible=False,
                        elem_classes="quiz-results"
                    )

                # Update progress bar as questions are answered
                for component in quiz_components:
                    component.change(
                        fn=lambda *answers: {
                            progress: gr.HTML(
                                f"<div class='progress-bar' style='width: {sum(1 for a in answers if a)/len(answers)*100}%'></div>"
                            )
                        },
                        inputs=quiz_components,
                        outputs=progress
                    )
                
                def submit_quiz_and_update(*args):
                    current_tab_status = args[0]
                    answers = args[1:]
                    
                    try:
                        result = learning_style_quiz.evaluate_quiz(*answers)
                        new_status = current_tab_status.copy()
                        new_status[1] = True
                        return (
                            result,
                            gr.update(visible=True),
                            new_status,
                            gr.update(elem_classes="completed-tab"),
                            gr.update(interactive=True),
                            gr.update(value="<div class='alert-box'>Quiz submitted successfully!</div>", visible=True),
                            gr.update(visible=False)
                        )
                    except Exception as e:
                        return (
                            f"Error evaluating quiz: {str(e)}",
                            gr.update(visible=True),
                            current_tab_status,
                            gr.update(),
                            gr.update(),
                            gr.update(value=f"<div class='error-message'>Error: {str(e)}</div>", visible=True),
                            gr.update(visible=False)
                        )
                
                quiz_submit.click(
                    fn=submit_quiz_and_update,
                    inputs=[tab_completed] + quiz_components,
                    outputs=[learning_output, learning_output, tab_completed, step2, step3, quiz_alert, nav_message]
                )
                
                quiz_clear.click(
                    fn=lambda: [None] * len(quiz_components),
                    outputs=quiz_components
                ).then(
                    fn=lambda: gr.HTML("<div class='progress-bar' style='width: 0%'></div>"),
                    outputs=progress
                )

            # ===== TAB 3: PERSONAL QUESTIONS =====
            with gr.Tab("Personal Profile", id=2):
                with gr.Row():
                    with gr.Column(scale=1):
                        gr.Markdown("### Step 3: Tell Us About Yourself")
                        with gr.Group():
                            name = gr.Textbox(label="Full Name", placeholder="Your name")
                            age = gr.Number(label="Age", minimum=MIN_AGE, maximum=MAX_AGE, precision=0)
                            interests = gr.Textbox(
                                label="Your Interests/Hobbies",
                                placeholder="e.g., Science, Music, Sports, Art..."
                            )
                        
                        save_personal_btn = gr.Button("Save Information", variant="primary")
                        save_confirmation = gr.HTML(visible=False)
                    
                    with gr.Column(scale=1):
                        gr.Markdown("### Favorites")
                        with gr.Group():
                            movie = gr.Textbox(label="Favorite Movie")
                            movie_reason = gr.Textbox(label="Why do you like it?", lines=2)
                            show = gr.Textbox(label="Favorite TV Show")
                            show_reason = gr.Textbox(label="Why do you like it?", lines=2)
                            book = gr.Textbox(label="Favorite Book")
                            book_reason = gr.Textbox(label="Why do you like it?", lines=2)
                            character = gr.Textbox(label="Favorite Character (from any story)")
                            character_reason = gr.Textbox(label="Why do you like them?", lines=2)
                        
                        # Added blog section
                        with gr.Accordion("Personal Blog (Optional)", open=False):
                            blog = gr.Textbox(
                                label="Share your thoughts",
                                placeholder="Write something about yourself, your goals, or anything you'd like to share...",
                                lines=5
                            )
                
                def save_personal_info(name, age, interests, current_tab_status):
                    try:
                        name = validate_name(name)
                        age = validate_age(age)
                        interests = sanitize_input(interests)
                        
                        new_status = current_tab_status.copy()
                        new_status[2] = True
                        return (
                            new_status,
                            gr.update(elem_classes="completed-tab"),
                            gr.update(interactive=True),
                            gr.update(value="<div class='alert-box'>Information saved!</div>", visible=True),
                            gr.update(visible=False)
                        )
                    except Exception as e:
                        return (
                            current_tab_status,
                            gr.update(),
                            gr.update(),
                            gr.update(visible=False),
                            gr.update(visible=True, value=f"<div class='error-message'>Error: {str(e)}</div>")
                        )
                
                save_personal_btn.click(
                    fn=save_personal_info,
                    inputs=[name, age, interests, tab_completed],
                    outputs=[tab_completed, step3, step4, save_confirmation, nav_message]
                )

            # ===== TAB 4: SAVE & REVIEW =====
            with gr.Tab("Save Profile", id=3):
                with gr.Row():
                    with gr.Column(scale=1):
                        gr.Markdown("### Step 4: Review & Save Your Profile")
                        with gr.Group():
                            load_profile_dropdown = gr.Dropdown(
                                label="Load Existing Profile",
                                choices=profile_manager.list_profiles(session_token.value),
                                visible=False
                            )
                            with gr.Row():
                                load_btn = gr.Button("Load", visible=False)
                                delete_btn = gr.Button("Delete", variant="stop", visible=False)
                        
                        save_btn = gr.Button("Save Profile", variant="primary")
                        clear_btn = gr.Button("Clear Form")
                    
                    with gr.Column(scale=2):
                        output_summary = gr.Markdown(
                            "Your profile summary will appear here after saving.",
                            label="Profile Summary"
                        )

                def save_profile_and_update(name, age, interests, transcript_data, learning_style, 
                                         movie, movie_reason, show, show_reason,
                                         book, book_reason, character, character_reason, blog,
                                         current_tab_status):
                    try:
                        summary = profile_manager.save_profile(
                            name, age, interests, transcript_data, learning_style,
                            movie, movie_reason, show, show_reason,
                            book, book_reason, character, character_reason, blog
                        )
                        new_status = current_tab_status.copy()
                        new_status[3] = True
                        return (
                            summary,
                            new_status,
                            gr.update(elem_classes="completed-tab"),
                            gr.update(interactive=True),
                            gr.update(visible=False)
                        )
                    except Exception as e:
                        return (
                            f"Error saving profile: {str(e)}",
                            current_tab_status,
                            gr.update(),
                            gr.update(),
                            gr.update(visible=True, value=f"<div class='error-message'>Error: {str(e)}</div>")
                        )
                
                save_btn.click(
                    fn=save_profile_and_update,
                    inputs=[
                        name, age, interests, transcript_data, learning_output,
                        movie, movie_reason, show, show_reason,
                        book, book_reason, character, character_reason, blog,
                        tab_completed
                    ],
                    outputs=[output_summary, tab_completed, step4, step5, nav_message]
                ).then(
                    fn=lambda: profile_manager.list_profiles(session_token.value),
                    outputs=load_profile_dropdown
                ).then(
                    fn=lambda: gr.update(visible=bool(profile_manager.list_profiles(session_token.value))),
                    outputs=load_btn
                ).then(
                    fn=lambda: gr.update(visible=bool(profile_manager.list_profiles(session_token.value))),
                    outputs=delete_btn
                )
                
                def delete_profile(name, session_token):
                    if not name:
                        raise gr.Error("Please select a profile to delete")
                    try:
                        profile_path = profile_manager.get_profile_path(name)
                        if profile_path.exists():
                            profile_path.unlink()
                        return "Profile deleted successfully", ""
                    except Exception as e:
                        raise gr.Error(f"Error deleting profile: {str(e)}")
                
                delete_btn.click(
                    fn=delete_profile,
                    inputs=[load_profile_dropdown, session_token],
                    outputs=[output_summary, load_profile_dropdown]
                ).then(
                    fn=lambda: profile_manager.list_profiles(session_token.value),
                    outputs=load_profile_dropdown
                ).then(
                    fn=lambda: gr.update(visible=bool(profile_manager.list_profiles(session_token.value))),
                    outputs=load_btn
                ).then(
                    fn=lambda: gr.update(visible=bool(profile_manager.list_profiles(session_token.value))),
                    outputs=delete_btn
                )
                
                clear_btn.click(
                    fn=lambda: [gr.update(value="") for _ in range(12)],
                    outputs=[
                        name, age, interests, 
                        movie, movie_reason, show, show_reason,
                        book, book_reason, character, character_reason,
                        output_summary
                    ]
                )

            # ===== TAB 5: AI ASSISTANT =====
            with gr.Tab("AI Assistant", id=4):
                gr.Markdown("## Your Personalized Learning Assistant")
                gr.Markdown("Ask me anything about studying, your courses, grades, or learning strategies.")
                
                # Create a wrapper function that properly awaits the async function
                async def chat_wrapper(message: str, history: List[List[str]]):
                    response = await teaching_assistant.generate_response(
                        message, 
                        history, 
                        session_token.value
                    )
                    return response
                
                chatbot = gr.ChatInterface(
                    fn=chat_wrapper,
                    examples=[
                        "How should I study for my next math test?",
                        "What's my current GPA?",
                        "Show me my course history",
                        "How can I improve my grades in science?",
                        "What study methods match my learning style?"
                    ],
                    title=""
                )

        # Navigation logic
        def navigate_to_tab(tab_index: int, tab_completed_status):
            current_tab = tabs.selected
            
            # Allow backward navigation
            if tab_index <= current_tab:
                return gr.Tabs(selected=tab_index), gr.update(visible=False)
            
            # Check if current tab is completed
            if not tab_completed_status.get(current_tab, False):
                messages = {
                    0: "Please complete the transcript analysis first.",
                    1: "Please complete the learning style quiz first.",
                    2: "Please fill out your personal information first.",
                    3: "Please save your profile first."
                }
                return (
                    gr.Tabs(selected=current_tab),
                    gr.update(
                        value=f"<div class='error-message'>⚠️ {messages.get(current_tab, 'Please complete this step first')}</div>", 
                        visible=True
                    )
                )
            
            return gr.Tabs(selected=tab_index), gr.update(visible=False)
        
        # Connect navigation buttons
        step1.click(
            lambda idx, status: navigate_to_tab(idx, status),
            inputs=[gr.State(0), tab_completed],
            outputs=[tabs, nav_message]
        )
        step2.click(
            lambda idx, status: navigate_to_tab(idx, status),
            inputs=[gr.State(1), tab_completed],
            outputs=[tabs, nav_message]
        )
        step3.click(
            lambda idx, status: navigate_to_tab(idx, status),
            inputs=[gr.State(2), tab_completed],
            outputs=[tabs, nav_message]
        )
        step4.click(
            lambda idx, status: navigate_to_tab(idx, status),
            inputs=[gr.State(3), tab_completed],
            outputs=[tabs, nav_message]
        )
        step5.click(
            lambda idx, status: navigate_to_tab(idx, status),
            inputs=[gr.State(4), tab_completed],
            outputs=[tabs, nav_message]
        )
        
        # Dark mode toggle
        def toggle_dark_mode(dark):
            return gr.themes.Soft(primary_hue="blue", secondary_hue="gray") if not dark else gr.themes.Soft(primary_hue="blue", secondary_hue="gray", neutral_hue="slate")
        
        dark_mode.change(
            fn=toggle_dark_mode,
            inputs=dark_mode,
            outputs=None
        )
        
        # Load model on startup
        app.load(fn=lambda: model_loader.load_model(), outputs=[])
    
    return app

# Create and launch the interface
app = create_interface()

if __name__ == "__main__":
    app.launch()