File size: 51,881 Bytes
9cc6120
9109509
9cc6120
a075bb3
 
 
 
9cc6120
 
 
 
9109509
5a9e88a
212d06b
 
9cc6120
590f42f
1457419
9cc6120
7ed0603
20e9445
9cc6120
 
d2680fa
a549649
 
15efe4a
86f7d0b
9cc6120
1ef2970
348c664
 
 
 
 
 
 
 
 
 
 
15efe4a
 
 
 
 
dedc4f1
 
 
549219e
dedc4f1
77e00bd
590f42f
dedc4f1
 
 
549219e
dedc4f1
 
549219e
dedc4f1
86f7d0b
8a563d0
712897d
590f42f
 
 
 
 
 
 
 
 
86f7d0b
 
 
 
 
 
dedc4f1
 
590f42f
 
 
549219e
 
e39f857
549219e
 
e39f857
549219e
 
 
 
 
e39f857
3260867
 
 
 
 
 
 
 
 
 
 
 
e39f857
549219e
 
 
 
 
 
 
e39f857
ec4667c
549219e
 
 
 
 
 
 
 
 
 
 
 
 
e39f857
549219e
3260867
e39f857
549219e
 
 
 
 
 
3260867
d2680fa
3260867
15efe4a
 
 
94ff6a1
15efe4a
 
 
 
 
 
 
 
 
94ff6a1
15efe4a
 
94ff6a1
15efe4a
94ff6a1
15efe4a
 
 
94ff6a1
15efe4a
 
94ff6a1
15efe4a
 
 
 
94ff6a1
15efe4a
 
 
 
 
 
94ff6a1
15efe4a
 
94ff6a1
15efe4a
 
3260867
e39f857
3260867
 
 
590f42f
3260867
 
 
9cc6120
 
 
dedc4f1
 
 
 
 
 
 
 
9cc6120
 
32e1e65
6ea04d8
baee2b2
 
 
 
 
 
 
 
 
15efe4a
baee2b2
6ea04d8
 
5a9e88a
fe81470
 
 
baee2b2
6ea04d8
 
baee2b2
6ea04d8
 
104bc54
9cc6120
 
 
 
590f42f
 
 
 
 
1dfdd1b
 
348c664
1dfdd1b
 
9cc6120
 
 
 
 
 
 
 
 
 
 
 
d39610d
195ed03
d39610d
195ed03
d39610d
9cc6120
195ed03
9cc6120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c39cd57
9cc6120
 
 
 
 
c39cd57
9cc6120
 
 
 
85e41fb
 
 
 
 
 
 
 
 
7ccbe37
dedc4f1
 
 
 
 
7ccbe37
8880b78
9109509
 
7ccbe37
9109509
 
8880b78
9109509
dedc4f1
 
 
 
 
 
104bc54
348c664
1457419
15efe4a
549219e
590f42f
 
90748c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
590f42f
90748c8
e39f857
90748c8
590f42f
e39f857
590f42f
 
 
 
 
afc8109
14b2ad0
590f42f
e39f857
549219e
590f42f
 
 
 
 
 
549219e
104bc54
348c664
dedc4f1
 
 
 
 
549219e
e5ddfb2
 
 
 
 
590f42f
15efe4a
590f42f
2aa1893
 
590f42f
 
 
 
 
15efe4a
590f42f
 
e39f857
9cc6120
 
 
 
 
9109509
 
8880b78
 
 
 
 
 
 
 
 
9109509
 
9cc6120
 
 
104bc54
 
 
 
9cc6120
 
 
8880b78
 
9cc6120
62aa801
 
 
 
 
1ef2970
 
 
8880b78
94ff6a1
9cc6120
 
 
 
 
 
94c1e6b
9cc6120
8880b78
 
 
 
 
 
 
 
 
 
 
 
 
9cc6120
94c1e6b
 
 
9cc6120
 
 
 
 
8880b78
dedc4f1
 
 
 
 
 
8880b78
9109509
 
 
104bc54
 
 
 
 
8880b78
 
 
 
9109509
8880b78
16f921a
dedc4f1
 
 
 
 
16f921a
dedc4f1
 
 
 
 
16f921a
dedc4f1
 
 
2aa1893
9109509
 
8880b78
9109509
dedc4f1
 
 
 
 
 
 
 
 
 
 
 
 
8880b78
 
20e9445
 
8880b78
 
 
 
 
dedc4f1
 
 
 
 
 
8880b78
9109509
 
 
dedc4f1
 
 
 
 
 
9109509
 
dedc4f1
 
 
2aa1893
15efe4a
9109509
8880b78
104bc54
a549649
15efe4a
a549649
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94ff6a1
a549649
 
 
 
 
 
 
 
 
94ff6a1
a549649
 
 
94ff6a1
a549649
 
 
 
 
94ff6a1
a549649
 
 
 
 
 
 
104bc54
dedc4f1
a549649
7ccbe37
 
26d56ee
9cc6120
 
85e41fb
16f921a
9cc6120
16f921a
9cc6120
 
dedc4f1
7ccbe37
9cc6120
 
a549649
 
94ff6a1
26d56ee
9cc6120
 
463e428
 
 
 
 
 
 
3260867
e39f857
 
549219e
3260867
e39f857
3260867
 
 
 
 
e39f857
463e428
 
 
 
e39f857
549219e
3260867
 
 
 
 
e39f857
7ca0870
549219e
463e428
 
98e1a6e
f587c01
ccd40e2
 
f587c01
98e1a6e
ccd40e2
98e1a6e
 
ccd40e2
 
 
 
 
 
 
 
 
 
98e1a6e
de86b59
f587c01
98e1a6e
 
 
f587c01
 
ccd40e2
98e1a6e
 
ccd40e2
 
 
 
 
 
98e1a6e
f587c01
ccd40e2
de86b59
f587c01
ccd40e2
f587c01
 
ccd40e2
 
 
 
 
 
f587c01
 
 
212d06b
 
 
 
 
f587c01
ccd40e2
 
 
212d06b
ccd40e2
212d06b
 
 
 
f587c01
ccd40e2
212d06b
 
ccd40e2
 
 
 
 
 
 
 
 
 
212d06b
 
 
 
 
 
 
 
9221d1e
212d06b
 
 
 
ccd40e2
212d06b
 
 
 
 
 
 
 
 
 
ccd40e2
 
212d06b
ccd40e2
 
212d06b
de86b59
8880b78
ba5ed41
16f921a
 
 
 
8880b78
 
35d154e
 
 
463e428
 
 
 
 
 
 
 
 
114b14a
 
 
ba5ed41
114b14a
 
 
 
 
 
 
 
 
 
ba5ed41
196cc92
 
114b14a
 
 
 
 
 
 
 
 
 
 
196cc92
114b14a
463e428
1bed0da
ba5ed41
1bed0da
ba5ed41
1bed0da
 
 
 
 
 
0179b81
 
ba5ed41
 
0179b81
ba5ed41
 
0179b81
 
 
 
ba5ed41
 
 
0179b81
 
 
ba5ed41
0179b81
 
 
ba5ed41
0179b81
 
 
ba5ed41
0179b81
 
 
ba5ed41
 
0179b81
 
 
 
 
 
ba5ed41
0179b81
becdb13
ba5ed41
becdb13
ba5ed41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
becdb13
a549649
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8880b78
 
f3b9331
1ef2970
 
f3b9331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8911cd4
 
 
f3b9331
 
8911cd4
 
 
26d56ee
15efe4a
26d56ee
 
 
 
 
 
 
 
 
 
 
 
15efe4a
 
 
 
 
 
 
 
94ff6a1
15efe4a
 
 
 
 
 
 
 
94ff6a1
15efe4a
 
 
8911cd4
 
f3b9331
 
a549649
94ff6a1
3f49e61
20e9445
aac80e9
 
 
 
 
 
 
b1155b9
aac80e9
 
b1155b9
aac80e9
 
a431110
aac80e9
a431110
 
 
 
 
 
 
 
 
 
 
 
 
 
1bed0da
aac80e9
 
 
 
 
32e1e65
aac80e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0179b81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aac80e9
94ff6a1
15efe4a
f3b9331
 
 
 
15efe4a
 
 
 
 
 
 
94ff6a1
15efe4a
 
 
 
 
94ff6a1
15efe4a
94ff6a1
15efe4a
 
 
 
 
 
 
 
 
94ff6a1
15efe4a
 
94ff6a1
15efe4a
94ff6a1
 
15efe4a
 
 
 
 
5cafd31
94ff6a1
 
f3b9331
 
 
 
 
94ff6a1
f3b9331
 
27df61b
 
 
94ff6a1
15efe4a
 
 
 
 
f3b9331
15efe4a
27df61b
15efe4a
 
a549649
 
aac80e9
 
 
 
faecac3
 
 
 
 
 
aac80e9
 
 
7ca0870
 
01c3274
463e428
 
 
 
 
9cc6120
463e428
 
 
 
 
dedc4f1
ab8585f
 
 
6ddb3ed
ab8585f
 
 
 
 
 
1ef2970
5f0717e
 
 
 
aecd0a1
5f0717e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab8585f
 
 
 
6ddb3ed
ab8585f
 
 
 
 
5f0717e
 
 
ab8585f
6ddb3ed
ab8585f
8b9f557
 
 
9bee81e
8b9f557
ab8585f
 
5f0717e
1ef2970
 
259b6f1
463e428
259b6f1
 
 
4b0859f
 
259b6f1
4b0859f
463e428
9cc6120
 
 
 
 
aac80e9
6ea04d8
32e1e65
6ea04d8
aac80e9
 
 
 
6ea04d8
 
9109509
 
 
 
26d56ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81490b8
 
26d56ee
a549649
4b0859f
a549649
 
076b4f2
9cc6120
 
 
 
 
 
dedc4f1
 
 
9cc6120
 
104bc54
9109509
dedc4f1
8880b78
104bc54
 
 
 
dedc4f1
104bc54
8880b78
104bc54
dedc4f1
 
9cc6120
dedc4f1
9cc6120
dedc4f1
 
 
 
9cc6120
dedc4f1
15efe4a
7ca0870
 
 
aac80e9
26d56ee
e39f857
15efe4a
94ff6a1
a549649
 
94ff6a1
a549649
 
 
88bed01
94ff6a1
9cc6120
26d56ee
5a9e88a
a549649
94ff6a1
26d56ee
 
a549649
 
26d56ee
a549649
9cc6120
 
463e428
 
 
 
 
 
 
 
 
 
 
 
 
aac80e9
463e428
 
98e1a6e
94ff6a1
 
 
 
 
 
 
 
 
98e1a6e
9753033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a549649
 
 
 
 
94ff6a1
a549649
 
 
 
 
 
9cc6120
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
import os
import random
import uuid
import smtplib
import ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from base64 import b64encode
from datetime import datetime
from mimetypes import guess_type
from pathlib import Path
from typing import Optional
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

import spaces
import spaces
import gradio as gr
from feedback import save_feedback, scheduler
from gradio.components.chatbot import Option
from huggingface_hub import InferenceClient
from pandas import DataFrame
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
import threading
from collections import defaultdict
from datasets import load_dataset


BASE_MODEL = os.getenv("MODEL", "google/gemma-3-12b-pt")
ZERO_GPU = (
    bool(os.getenv("ZERO_GPU", False)) or True
    if str(os.getenv("ZERO_GPU")).lower() == "true"
    else False
)
TEXT_ONLY = (
    bool(os.getenv("TEXT_ONLY", False)) or True
    if str(os.getenv("TEXT_ONLY")).lower() == "true"
    else False
)

# os.environ["HF_DATASETS_CACHE"] = "/data/datasets_cache"

# # caches dataset after first download
# dataset = load_dataset("feel-fl/feel-feedback")


def create_inference_client(
    model: Optional[str] = None, base_url: Optional[str] = None
) -> InferenceClient | dict:
    """Create an InferenceClient instance with the given model or environment settings.
    This function will run the model locally if ZERO_GPU is set to True.
    This function will run the model locally if ZERO_GPU is set to True.

    Args:
        model: Optional model identifier to use. If not provided, will use environment settings.
        base_url: Optional base URL for the inference API.

    Returns:
        Either an InferenceClient instance or a dictionary with pipeline and tokenizer
    """
    if ZERO_GPU:
        tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
        model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, load_in_8bit=False)
        return {
            "pipeline": pipeline(
                "text-generation",
                model=model,
                tokenizer=tokenizer,
                max_new_tokens=2000,
            ),
            "tokenizer": tokenizer
        }
    else:
        return InferenceClient(
            token=os.getenv("HF_TOKEN"),
            model=model if model else (BASE_MODEL if not base_url else None),
            base_url=base_url,
        )


CLIENT = create_inference_client()


def get_persistent_storage_path(filename: str) -> tuple[Path, bool]:
    """Check if persistent storage is available and return the appropriate path.

    Args:
        filename: The name of the file to check/create

    Returns:
        A tuple containing (file_path, is_persistent)
    """
    persistent_path = Path("/data") / filename
    local_path = Path(__file__).parent / filename

    # Check if persistent storage is available and writable
    use_persistent = False
    if Path("/data").exists() and Path("/data").is_dir():
        try:
            # Test if we can write to the directory
            test_file = Path("/data/write_test.tmp")
            test_file.touch()
            test_file.unlink()  # Remove the test file
            use_persistent = True
        except (PermissionError, OSError):
            print("Persistent storage exists but is not writable, falling back to local storage")
            use_persistent = False

    return (persistent_path if use_persistent else local_path, use_persistent)


def load_languages() -> dict[str, str]:
    """Load languages from JSON file or persistent storage"""
    languages_path, use_persistent = get_persistent_storage_path("languages.json")
    local_path = Path(__file__).parent / "languages.json"

    # If persistent storage is available but file doesn't exist yet, copy the local file to persistent storage
    if use_persistent and not languages_path.exists():
        try:
            if local_path.exists():
                import shutil
                shutil.copy(local_path, languages_path)
                print(f"Copied languages to persistent storage at {languages_path}")
            else:
                with open(languages_path, "w", encoding="utf-8") as f:
                    json.dump({"English": "You are a helpful assistant."}, f, ensure_ascii=False, indent=2)
                print(f"Created new languages file in persistent storage at {languages_path}")
        except Exception as e:
            print(f"Error setting up persistent storage: {e}")
            languages_path = local_path  # Fall back to local path if any error occurs

    if not languages_path.exists() and local_path.exists():
        languages_path = local_path

    if languages_path.exists():
        with open(languages_path, "r", encoding="utf-8") as f:
            return json.load(f)
    else:
        default_languages = {"English": "You are a helpful assistant."}
        return default_languages

LANGUAGES = load_languages()

def update_language_counts_from_dataset():
    """update language data points count from the dataset"""
    data_file, use_persistent = get_persistent_storage_path("language_data_points.json")

    if data_file.exists():
        with open(data_file, "r", encoding="utf-8") as f:
            try:
                data = json.load(f)
            except json.JSONDecodeError:
                print("error reading data file. Creating new data.")
                data = {}
    else:
        data = {}

    cache_dir, _ = get_persistent_storage_path("datasets_cache")
    os.environ["HF_DATASETS_CACHE"] = str(cache_dir)

    try:
        # load the dataset (cached after first download - note that this might need to be changed because
        #  we dont want it to only refer to some old cached version if there have been updates since)
        print("loading dataset from HuggingFace...")
        dataset = load_dataset("feel-fl/feel-feedback")

        train_dataset = dataset["train"]
        df = train_dataset.to_pandas()

        if 'language' in df.columns:
            language_counts = df['language'].value_counts().to_dict()
            for lang, count in language_counts.items():
                data[lang] = count

            print(f"Updated counts from dataset for {len(language_counts)} languages")
        else:
            print("Warning: No 'language' column found in the dataset.")
            print("Available columns:", df.columns.tolist())
    except Exception as e:
        print(f"Error updating from dataset: {e}")

    with open(data_file, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=2)

    return data

USER_AGREEMENT = """
You have been asked to participate in a research study conducted by Lingo Lab from the Computer Science and Artificial Intelligence Laboratory at the Massachusetts Institute of Technology (M.I.T.), together with huggingface.

The purpose of this study is the collection of multilingual human feedback to improve language models. As part of this study you will interat with a language model in a langugage of your choice, and provide indication to wether its reponses are helpful or not.

Your name and personal data will never be recorded. You may decline further participation, at any time, without adverse consequences.There are no foreseeable risks or discomforts for participating in this study. Note participating in the study may pose risks that are currently unforeseeable. If you have questions or concerns about the study, you can contact the researchers at [email protected]. If you have any questions about your rights as a participant in this research (E-6610), feel you have been harmed, or wish to discuss other study-related concerns with someone who is not part of the research team, you can contact the M.I.T. Committee on the Use of Humans as Experimental Subjects (COUHES) by phone at (617) 253-8420, or by email at [email protected].

Clicking on the next button at the bottom of this page indicates that you are at least 18 years of age and willingly agree to participate in the research voluntarily.
"""


def add_user_message(history, message):
    if isinstance(message, dict) and "files" in message:
        for x in message["files"]:
            history.append({"role": "user", "content": {"path": x}})
        if message["text"] is not None:
            history.append({"role": "user", "content": message["text"]})
    else:
        history.append({"role": "user", "content": message})
    return history, gr.Textbox(value=None, interactive=False)


def format_system_message(language: str):
    system_message = [
        {
            "role": "system",
            "content": LANGUAGES.get(language, LANGUAGES["English"]),
        },
        {
            "role": "user",
            "content": f"Start by asking me a question in {language}."
        }
    ]
    response = call_pipeline(system_message)
    new_system_message = [
        {
            "role": "system",
            "content": LANGUAGES.get(language, LANGUAGES["English"]),
        },
        {
            "role": "assistant",
            "content": response
        }
    ]
    return new_system_message


def format_history_as_messages(history: list):
    messages = []
    current_role = None
    current_message_content = []

    if TEXT_ONLY:
        for entry in history:
            messages.append({"role": entry["role"], "content": entry["content"]})
        return messages

    if TEXT_ONLY:
        for entry in history:
            messages.append({"role": entry["role"], "content": entry["content"]})
        return messages

    for entry in history:
        content = entry["content"]

        if entry["role"] != current_role:
            if current_role is not None:
                messages.append(
                    {"role": current_role, "content": current_message_content}
                )
            current_role = entry["role"]
            current_message_content = []

        if isinstance(content, tuple):  # Handle file paths
            for temp_path in content:
                if space_host := os.getenv("SPACE_HOST"):
                    url = f"https://{space_host}/gradio_api/file%3D{temp_path}"
                else:
                    url = _convert_path_to_data_uri(temp_path)
                current_message_content.append(
                    {"type": "image_url", "image_url": {"url": url}}
                )
        elif isinstance(content, str):  # Handle text
            current_message_content.append({"type": "text", "text": content})

    if current_role is not None:
        messages.append({"role": current_role, "content": current_message_content})

    return messages


def _convert_path_to_data_uri(path) -> str:
    mime_type, _ = guess_type(path)
    with open(path, "rb") as image_file:
        data = image_file.read()
        data_uri = f"data:{mime_type};base64," + b64encode(data).decode("utf-8")
    return data_uri


def _is_file_safe(path) -> bool:
    try:
        return Path(path).is_file()
    except Exception:
        return ""


def _process_content(content) -> str | list[str]:
    if isinstance(content, str) and _is_file_safe(content):
        return _convert_path_to_data_uri(content)
    elif isinstance(content, list) or isinstance(content, tuple):
        return _convert_path_to_data_uri(content[0])
    return content


def _process_rating(rating) -> int:
    if isinstance(rating, str):
        return 0
    elif isinstance(rating, int):
        return rating
    else:
        raise ValueError(f"Invalid rating: {rating}")


def add_fake_like_data(
    history: list,
    conversation_id: str,
    session_id: str,
    language: str,
    liked: bool = False,
) -> None:
    data = {
        "index": len(history) - 1,
        "value": history[-1],
        "liked": liked,
    }
    _, dataframe = wrangle_like_data(
        gr.LikeData(target=None, data=data), history.copy()
    )
    submit_conversation(
        dataframe=dataframe,
        conversation_id=conversation_id,
        session_id=session_id,
        language=language,
    )


@spaces.GPU
def call_pipeline(messages: list):
    """Call the appropriate model pipeline based on configuration"""
    if ZERO_GPU:
        tokenizer = CLIENT["tokenizer"]
        # Ensure messages follow the proper alternating pattern
        formatted_messages = []
        prev_role = None

        for msg in messages:
            role = msg.get("role", "")
            content = msg.get("content", "")

            # Skip empty messages
            if not content.strip():
                continue

            # Enforce alternating pattern
            if role == prev_role:
                # If same role repeats, combine with previous message or skip
                continue

            # Only allow "user" and "assistant" roles
            if role not in ["user", "assistant"]:
                # Convert to proper role or skip
                continue

            formatted_messages.append(msg)
            prev_role = role

        # Ensure we start with user message
        if formatted_messages and formatted_messages[0]["role"] != "user":
            formatted_messages = formatted_messages[1:]

        # Now use the properly formatted messages
        formatted_prompt = tokenizer.apply_chat_template(
            formatted_messages,  # Use the fixed messages
            tokenize=False,
            add_generation_prompt=True
        )

        response = CLIENT["pipeline"](
            formatted_prompt,
            clean_up_tokenization_spaces=False,
            max_length=2000,
            return_full_text=False,
            temperature=1.0,
            do_sample=True,
        )

        return response[0]["generated_text"]
    else:
        response = CLIENT(
            messages,
            clean_up_tokenization_spaces=False,
            max_length=2000,
        )
        return response[0]["generated_text"][-1]["content"]


def respond(
    history: list,
    language: str,
    temperature: Optional[float] = None,
    seed: Optional[int] = None,
) -> list:
    """Respond to the user message with a system message

    Return the history with the new message"""
    messages = format_history_as_messages(history)

    if ZERO_GPU:
        content = call_pipeline(messages)
    else:
        if temperature is None:
            temperature = 0.7
        response = CLIENT.chat.completions.create(
            messages=messages,
            max_tokens=2000,
            stream=False,
            seed=seed,
            temperature=temperature,
        )
        content = response.choices[0].message.content

    message = gr.ChatMessage(role="assistant", content=content)
    history.append(message)
    return history


def update_dataframe(dataframe: DataFrame, history: list) -> DataFrame:
    """Update the dataframe with the new message"""
    data = {
        "index": 9999,
        "value": None,
        "liked": False,
    }
    _, dataframe = wrangle_like_data(
        gr.LikeData(target=None, data=data), history.copy()
    )
    return dataframe


def wrangle_like_data(x: gr.LikeData, history) -> DataFrame:
    """Wrangle conversations and liked data into a DataFrame"""

    if isinstance(x.index, int):
        liked_index = x.index
    else:
        liked_index = x.index[0]

    output_data = []
    for idx, message in enumerate(history):
        if isinstance(message, gr.ChatMessage):
            message = message.__dict__
        if idx == liked_index:
            if x.liked is True:
                message["metadata"] = {"title": "liked"}
            elif x.liked is False:
                message["metadata"] = {"title": "disliked"}

        if message["metadata"] is None:
            message["metadata"] = {}
        elif not isinstance(message["metadata"], dict):
            message["metadata"] = message["metadata"].__dict__

        rating = message["metadata"].get("title")
        if rating == "liked":
            message["rating"] = 1
        elif rating == "disliked":
            message["rating"] = -1
        else:
            message["rating"] = 0

        message["chosen"] = ""
        message["rejected"] = ""
        if message["options"]:
            for option in message["options"]:
                if not isinstance(option, dict):
                    option = option.__dict__
                message[option["label"]] = option["value"]
        else:
            if message["rating"] == 1:
                message["chosen"] = message["content"]
            elif message["rating"] == -1:
                message["rejected"] = message["content"]

        output_data.append(
            dict(
                [(k, v) for k, v in message.items() if k not in ["metadata", "options"]]
            )
        )

    return history, DataFrame(data=output_data)


def wrangle_edit_data(
    x: gr.EditData,
    history: list,
    dataframe: DataFrame,
    conversation_id: str,
    session_id: str,
    language: str,
) -> list:
    """Edit the conversation and add negative feedback if assistant message is edited, otherwise regenerate the message

    Return the history with the new message"""
    if isinstance(x.index, int):
        index = x.index
    else:
        index = x.index[0]

    original_message = gr.ChatMessage(
        role="assistant", content=dataframe.iloc[index]["content"]
    ).__dict__

    if history[index]["role"] == "user":
        # Add feedback on original and corrected message
        add_fake_like_data(
            history=history[: index + 2],
            conversation_id=conversation_id,
            session_id=session_id,
            language=language,
            liked=True,
        )
        add_fake_like_data(
            history=history[: index + 1] + [original_message],
            conversation_id=conversation_id,
            session_id=session_id,
            language=language,
        )
        history = respond(
            history=history[: index + 1],
            language=language,
            temperature=1.5,
            seed=random.randint(0, 1000000),
        )
        return history
    else:
        add_fake_like_data(
            history=history[: index + 1],
            conversation_id=conversation_id,
            session_id=session_id,
            language=language,
            liked=True,
        )
        add_fake_like_data(
            history=history[:index] + [original_message],
            conversation_id=conversation_id,
            session_id=session_id,
            language=language,
        )
        history = history[: index + 1]
        history[-1]["options"] = [
            Option(label="chosen", value=x.value),
            Option(label="rejected", value=original_message["content"]),
        ]
        return history


def wrangle_retry_data(
    x: gr.RetryData,
    history: list,
    dataframe: DataFrame,
    conversation_id: str,
    session_id: str,
    language: str,
) -> list:
    """Respond to the user message with a system message and add negative feedback on the original message

    Return the history with the new message"""
    add_fake_like_data(
        history=history,
        conversation_id=conversation_id,
        session_id=session_id,
        language=language,
    )

    # Return the history without a new message
    history = respond(
        history=history[:-1],
        language=language,
        temperature=1.5,
        seed=random.randint(0, 1000000),
    )
    return history, update_dataframe(dataframe, history)

# Global variables for tracking language data points
LANGUAGE_DATA_POINTS = update_language_counts_from_dataset()
language_data_lock = threading.Lock()

def get_leaderboard_data():
    """Get sorted leaderboard data for all languages"""
    with language_data_lock:
        leaderboard_data = {lang: LANGUAGE_DATA_POINTS.get(lang, 0) for lang in LANGUAGES.keys()}
        sorted_data = sorted(leaderboard_data.items(), key=lambda x: x[1], reverse=True)
        return sorted_data

def increment_language_data_point(language):
    """Increment the data point count for a specific language"""
    with language_data_lock:
        LANGUAGE_DATA_POINTS[language] += 1
    return get_leaderboard_data()

def set_language_data_points(language, count):
    """Manually set the data point count for a specific language"""
    with language_data_lock:
        LANGUAGE_DATA_POINTS[language] = count
    return get_leaderboard_data()

def load_initial_language_data():
    """Load initial language data points from persistent storage or default values"""
    data_points_path, use_persistent = get_persistent_storage_path("language_data_points.json")

    if data_points_path.exists():
        try:
            with open(data_points_path, "r", encoding="utf-8") as f:
                data = json.load(f)
                with language_data_lock:
                    LANGUAGE_DATA_POINTS.clear()
                    LANGUAGE_DATA_POINTS.update(data)
        except Exception as e:
            print(f"Error loading language data points: {e}")

    for lang in LANGUAGES.keys():
        if lang not in LANGUAGE_DATA_POINTS:
            LANGUAGE_DATA_POINTS[lang] = 0

    return get_leaderboard_data()

def save_language_data_points():
    """Save language data points to persistent storage"""
    data_points_path, use_persistent = get_persistent_storage_path("language_data_points.json")

    try:
        with language_data_lock:
            with open(data_points_path, "w", encoding="utf-8") as f:
                json.dump(dict(LANGUAGE_DATA_POINTS), f, ensure_ascii=False, indent=2)
    except Exception as e:
        print(f"Error saving language data points: {e}")


def submit_conversation(dataframe, conversation_id, session_id, language):
    """ "Submit the conversation to dataset repo & update leaderboard"""
    if dataframe.empty or len(dataframe) < 2:
        gr.Info("No feedback to submit.")
        return (gr.Dataframe(value=None, interactive=False), gr.update(), None)

    dataframe["content"] = dataframe["content"].apply(_process_content)
    dataframe["rating"] = dataframe["rating"].apply(_process_rating)
    conversation = dataframe.to_dict(orient="records")
    conversation_data = {
        "conversation": conversation,
        "timestamp": datetime.now().isoformat(),
        "session_id": session_id,
        "conversation_id": conversation_id,
        "language": language,
    }
    save_feedback(input_object=conversation_data)
    leaderboard_data = increment_language_data_point(language)
    save_language_data_points()

    return (gr.Dataframe(value=None, interactive=False), gr.update(), leaderboard_data)


def open_add_language_modal():
    return gr.Group(visible=True)

def close_add_language_modal():
    return gr.Group(visible=False)

def save_new_language(lang_name, system_prompt):
    """Save the new language and system prompt to persistent storage if available, otherwise to local file."""
    global LANGUAGES

    languages_path, use_persistent = get_persistent_storage_path("languages.json")
    local_path = Path(__file__).parent / "languages.json"

    if languages_path.exists():
        with open(languages_path, "r", encoding="utf-8") as f:
            data = json.load(f)
    else:
        data = {}

    data[lang_name] = system_prompt

    with open(languages_path, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=2)

    if use_persistent and local_path != languages_path:
        try:
            with open(local_path, "w", encoding="utf-8") as f:
                json.dump(data, f, ensure_ascii=False, indent=2)
        except Exception as e:
            print(f"Error updating local backup: {e}")

    LANGUAGES.update({lang_name: system_prompt})
    return gr.Group(visible=False), gr.HTML("<script>window.location.reload();</script>"), gr.Dropdown(choices=list(LANGUAGES.keys()))


def save_contributor_email(email, name=""):
    """Save contributor email to persistent storage and send notification to admins"""
    print(f"[DEBUG] Starting save_contributor_email for: {email}, {name}")

    # Still save to persistent storage for record keeping
    emails_path, use_persistent = get_persistent_storage_path("contributors.json")
    print(f"[DEBUG] Using path: {emails_path}, persistent: {use_persistent}")

    # Read existing emails
    contributors = []
    try:
        if emails_path.exists():
            with open(emails_path, "r", encoding="utf-8") as f:
                contributors = json.load(f)
                print(f"[DEBUG] Loaded {len(contributors)} existing contributors")
        else:
            print(f"[DEBUG] No existing contributors file found at {emails_path}")
    except Exception as e:
        print(f"[DEBUG] Error reading contributors file: {e}")

    # Add new email with timestamp
    contributor_data = {
        "email": email,
        "name": name,
        "timestamp": datetime.now().isoformat()
    }
    contributors.append(contributor_data)
    print(f"[DEBUG] Added new contributor data: {contributor_data}")

    # Save back to file
    try:
        with open(emails_path, "w", encoding="utf-8") as f:
            json.dump(contributors, f, ensure_ascii=False, indent=2)
            print(f"[DEBUG] Successfully saved contributors file with {len(contributors)} entries")
    except Exception as e:
        print(f"[DEBUG] Error saving contributors file: {e}")

    # Send email notification to admins
    print(f"[DEBUG] Attempting to send notification email")
    try:
        send_notification_email(contributor_data)
        print(f"[DEBUG] Successfully sent notification email")
        return True
    except Exception as e:
        print(f"[DEBUG] Failed to send notification email: {e}")
        print(f"[DEBUG] Error type: {type(e).__name__}")
        if hasattr(e, 'args'):
            print(f"[DEBUG] Error args: {e.args}")
        import traceback
        print(f"[DEBUG] Full traceback: {traceback.format_exc()}")
        return False

def send_notification_email(contributor_data):
    """Send email notification to admins about new contributor using SendGrid API"""
    # Get configuration from environment variables
    sender_email = os.getenv("NOTIFICATION_EMAIL", "[email protected]")
    recipient_email = os.getenv("ADMIN_EMAIL", "[email protected]")
    sendgrid_api_key = os.getenv("SENDGRID_API_KEY", "")

    print(f"[DEBUG] Email configuration:")
    print(f"[DEBUG] - Sender Email: {sender_email}")
    print(f"[DEBUG] - Recipient Email: {recipient_email}")
    print(f"[DEBUG] - API Key Set: {'Yes' if sendgrid_api_key else 'No'}")

    # If no API key is set, log instead of sending
    if not sendgrid_api_key:
        print(f"[DEBUG] No SendGrid API key set, would send notification email about contributor: {contributor_data}")
        return False

    try:
        # Create message content
        html_content = f"""
        <html>
        <body>
            <h2>New FeeL Contributor Submission</h2>
            <p><strong>Name:</strong> {contributor_data.get('name', 'Not provided')}</p>
            <p><strong>Email:</strong> {contributor_data.get('email', 'Not provided')}</p>
            <p><strong>Timestamp:</strong> {contributor_data.get('timestamp', datetime.now().isoformat())}</p>
        </body>
        </html>
        """

        # Create mail message
        print(f"[DEBUG] Creating email message")
        message = Mail(
            from_email=sender_email,
            to_emails=recipient_email,
            subject='New FeeL Contributor Submission',
            html_content=html_content
        )

        # Send via API
        print(f"[DEBUG] Sending via SendGrid API")
        sg = SendGridAPIClient(sendgrid_api_key)
        response = sg.send(message)

        print(f"[DEBUG] SendGrid API response code: {response.status_code}")

        # 202 is success for SendGrid
        if response.status_code == 202:
            print(f"[DEBUG] Email sent successfully via SendGrid API")
            return True
        else:
            print(f"[DEBUG] SendGrid API returned non-success status code: {response.status_code}")
            print(f"[DEBUG] Response body: {response.body}")
            return False

    except Exception as e:
        print(f"[DEBUG] Error in send_notification_email: {e}")
        import traceback
        print(f"[DEBUG] Full traceback: {traceback.format_exc()}")
        return False

css = """
/* Style for the options and retry button */
.options.svelte-pcaovb {
    display: none !important;
}
.option.svelte-pcaovb {
    display: none !important;
}
.retry-btn {
    display: none !important;
}
/* Style for the add language button */
button#add-language-btn {
    padding: 0 !important;
    font-size: 30px !important;
    font-weight: bold !important;
}
/* Style for the user agreement container */
.user-agreement-container {
    box-shadow: 0 2px 5px rgba(0,0,0,0.1) !important;
    max-height: 300px;
    overflow-y: auto;
    padding: 10px;
    border: 1px solid var(--border-color-primary) !important;
    border-radius: 5px;
    margin-bottom: 10px;
}
/* Style for the consent modal */
.consent-modal {
    position: fixed !important;
    top: 50% !important;
    left: 50% !important;
    transform: translate(-50%, -50%) !important;
    z-index: 9999 !important;
    background: var(--background-fill-primary) !important;
    padding: 20px !important;
    border-radius: 10px !important;
    box-shadow: 0 4px 10px rgba(0,0,0,0.2) !important;
    max-width: 90% !important;
    width: 600px !important;
}
/* Overlay for the consent modal */
.modal-overlay {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: 100% !important;
    background-color: rgba(0, 0, 0, 0.5) !important;
    z-index: 9998 !important;
}
.footer-banner {
    background-color: var(--background-fill-secondary);
    padding: 10px 20px;
    border-top: 1px solid var(--border-color-primary);
    margin-top: 20px;
    text-align: center;
}
.footer-banner p {
    margin: 0;
}
/* Language settings styling */
.language-settings-header {
    background-color: var(--primary-500);  /* Use Gradio's primary color */
    padding: 5px;
    border-radius: 8px 8px 0 0;
    margin-bottom: 0;
    color: var(--body-text-color);
    font-weight: bold;
}

.language-instruction {
    margin-top: 5px;
    margin-bottom: 5px;
    padding: 0 15px;
}

.language-container {
    border: 1px solid var(--border-color-primary);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    margin-bottom: 20px;
}

.language-dropdown {
    padding: 10px 15px 20px 15px;
}

.add-language-btn {
    background-color: var(--primary-500) !important;
    color: var(--body-text-color) !important;
    border: none !important;
    font-weight: bold !important;
    transition: background-color 0.3s !important;
}

.add-language-btn:hover {
    background-color: var(--primary-600) !important;
}

/* Yellow button styling - now using primary color variable */
button.yellow-btn {
    background-color: var(--primary-500) !important;
}

.footer-section {
    margin-top: 40px;
    border-top: 1px solid var(--border-color-primary);
    padding-top: 20px;
}
.admin-tools-accordion {
    max-width: 800px;
    margin: 0 auto;
}
.edit-instructions {
    padding: 10px 0;
    margin-top: 5px;
}

/* Leaderboard styles */
.leaderboard-container {
    border-left: 1px solid #eaeaea;
    padding-left: 1rem;
    height: 100%;
}
.leaderboard-title {
    font-weight: bold;
    text-align: center;
    margin-bottom: 1rem;
}
.leaderboard-item {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border-bottom: 1px solid #f0f0f0;
}
.leaderboard-rank {
    font-weight: bold;
    margin-right: 0.5rem;
}
.leaderboard-language {
    flex-grow: 1;
}
.leaderboard-count {
    font-weight: bold;
}
.leaderboard-admin-panel {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid #eaeaea;
}
"""


def get_config(request: gr.Request):
    """Get configuration from cookies"""
    config = {
        "feel_consent": "false",
    }

    if request and request.cookies:
        for key in config.keys():
            if key in request.cookies:
                config[key] = request.cookies[key]

    return config["feel_consent"] == "true"

def initialize_consent_status(request: gr.Request):
    """Initialize consent status and language preference from cookies"""
    has_consent = get_config(request)
    return has_consent

js = '''function js(){
    window.set_cookie = function(key, value){
        document.cookie = key+'='+value+'; Path=/; SameSite=Strict';
        return [value];
    }
}'''

def render_leaderboard(leaderboard_data=None):
    """Render the leaderboard HTML"""
    # Use the input parameter if provided, otherwise use global data
    if leaderboard_data:
        sorted_langs = leaderboard_data
    else:
        counts = LANGUAGE_DATA_POINTS  # Use the global variable directly
        languages = LANGUAGES
        sorted_langs = sorted(
            [(lang, counts.get(lang, 0)) for lang in languages.keys()],
            key=lambda x: x[1],
            reverse=True
        )
    
    html = """
    <table class="leaderboard">
      <tr>
        <th>Rank</th>
        <th>Language</th>
        <th>Data Points</th>
      </tr>
    """

    for i, (lang, count) in enumerate(sorted_langs):
        html += f"""
        <tr>
          <td>{i+1}</td>
          <td>{lang}</td>
          <td>{count}</td>
        </tr>
        """

    html += "</table>"
    return html


with gr.Blocks(css=css, js=js) as demo:
    user_consented = gr.State(value=False)
    language = gr.State(value="English")  # Default language state
    leaderboard_data = gr.State([])

    # Main application interface (initially hidden)
    with gr.Group() as main_app:
        with gr.Row():
            # Main content column (wider)
            with gr.Column(scale=3, elem_classes=["main-content"]):
                ##############################
                # Chatbot
                ##############################
                gr.Markdown("""
                # ♾️ FeeL: Improving LMs for All Languages
                """, elem_classes=["app-title"])

                with gr.Accordion("") as explanation:
                    gr.Markdown(f"""

                    **FeeL** (Feedback Loop) is a community-driven project by MIT, Hugging Face and IBM that aims to make language models better in *all languages*.

                    ### Why This Matters
                    Have you ever tried using an AI in your native language only to get responses that barely make sense? Most AI improvements prioritize widely spoken languages, while others fall behind. FeeL changes this by letting YOU shape how models respond in your language.

                    ### What You Can Do
                    1. **Select your language** from the dropdown menu (or add a new one if yours is missing)
                    2. **Chat with the model** in your language
                    3. **Provide feedback** on each response using:
                       - πŸ‘/πŸ‘Ž Like or dislike responses
                       - ✏️ Edit responses to sound more natural or correct
                       - πŸ”„ Regenerate to try another response

                    Your feedback is directly used to fine-tune the model in real-time. The more you interact, the better the model becomes for your language community.

                    All [data](https://huggingface.co./datasets/{scheduler.repo_id}), [code](https://github.com/huggingface/feel) and [models](https://huggingface.co./collections/feel-fl/feel-models-67a9b6ef0fdd554315e295e8) are publicly available for research and development.

                    """)

                chatbot = gr.Chatbot(
                    elem_id="chatbot",
                    editable="all",
                    value=format_system_message("English"),
                    type="messages",
                    feedback_options=["Like", "Dislike"],
                    height=600
                )

                chat_input = gr.Textbox(
                    interactive=True,
                    placeholder="Enter message or upload file...",
                    show_label=False,
                    submit_btn=True,
                )

                with gr.Accordion("Collected feedback", open=False):
                    dataframe = gr.Dataframe(wrap=True, label="Collected feedback")

                submit_btn = gr.Button(value="πŸ’Ύ Submit conversation", visible=False)

            # Sidebar column (narrower)
            with gr.Column(scale=1, elem_classes=["sidebar"]):
                with gr.Group(elem_classes=["language-container"]):
                    gr.Markdown("### Language Settings", elem_classes=["language-settings-header"])
                    gr.Markdown("Select your preferred language:", elem_classes=["language-instruction"])

                    with gr.Column(elem_classes=["language-dropdown"]):
                        language_dropdown = gr.Dropdown(
                            choices=list(load_languages().keys()),
                            value="English",
                            container=True,
                            show_label=False,
                        )

                    add_language_btn = gr.Button(
                        "Add New Language",
                        size="sm",
                        elem_classes=["add-language-btn"]
                    )



                # Right column with leaderboard
                with gr.Column(scale=3, elem_classes=["leaderboard-container"]):
                    gr.Markdown("# Language Leaderboard", elem_classes=["leaderboard-title"])
                    leaderboard_html = gr.HTML("Loading leaderboard...")
                    refresh_leaderboard_btn = gr.Button("Refresh Counts from Dataset")
                    leaderboard_html.value = render_leaderboard()

                    # HELPERS:
                    def update_func():
                        update_language_counts_from_dataset()
                        return render_leaderboard()


                    def set_language_count(language, count):
                        """admin function to manually set language count"""
                        if not language:
                            return render_leaderboard()

                        data_file, _ = get_persistent_storage_path("language_data_points.json")

                        if data_file.exists():
                            with open(data_file, "r", encoding="utf-8") as f:
                                try:
                                    data = json.load(f)
                                except json.JSONDecodeError:
                                    data = {}
                        else:
                            data = {}
                        data[language] = int(count)

                        with open(data_file, "w", encoding="utf-8") as f:
                            json.dump(data, f, ensure_ascii=False, indent=2)

                        return render_leaderboard()


                    refresh_leaderboard_btn.click(
                        update_func,
                        outputs=leaderboard_html
                    )




                    with gr.Accordion("Admin Controls", open=False, visible=False) as admin_panel:
                        with gr.Row():
                            admin_language = gr.Dropdown(choices=list(LANGUAGES.keys()), label="Language")
                            admin_count = gr.Number(value=0, label="Data Points")
                        set_count_btn = gr.Button("Set Count")

                    # toggle button for admin panel?
                    admin_toggle = gr.Button("Admin Controls", visible=True)
                    def toggle_admin_view():
                        return gr.update(visible=True)


                    set_count_btn.click(
                        set_language_count,
                        inputs=[admin_language, admin_count],
                        outputs=leaderboard_html
                    )

                    admin_toggle.click(
                        toggle_admin_view,
                        outputs=admin_panel
                    )


                # Create a hidden group instead of a modal
                with gr.Group(visible=False) as add_language_modal:
                    gr.Markdown("### Add New Language")
                    new_lang_name = gr.Textbox(label="Language Name", lines=1)
                    new_system_prompt = gr.Textbox(
                        label="System Prompt",
                        lines=4,
                        placeholder="Write in your own language: \"You are a helpful assistant. Always respond to requests in fluent and natural [your language], regardless of the language used by the user.\"",
                        info="The system prompt tells the AI how to behave. Make sure to write it in the language you're adding so the AI knows how to respond naturally."
                    )
                    with gr.Row():
                        save_language_btn = gr.Button("Save")
                        cancel_language_btn = gr.Button("Cancel")

        refresh_html = gr.HTML(visible=False)

        session_id = gr.Textbox(
            interactive=False,
            value=str(uuid.uuid4()),
            visible=False,
        )

        conversation_id = gr.Textbox(
            interactive=False,
            value=str(uuid.uuid4()),
            visible=False,
        )

    # Overlay for the consent modal
    with gr.Group(elem_classes=["modal-overlay"]) as consent_overlay:
        pass

    # Consent popup
    with gr.Group(elem_classes=["consent-modal"]) as consent_modal:
        gr.Markdown("# User Agreement")
        with gr.Group(elem_classes=["user-agreement-container"]):
            gr.Markdown(USER_AGREEMENT)
        consent_btn = gr.Button("I agree")

    # Add a contact footer at the bottom of the page
    with gr.Row(elem_classes=["footer-banner"]) as footer_banner:
        gr.Markdown("""
        ### Contact Us
        Have questions, requests, or ideas for how we can improve? Email us at: **[email protected]**, **[email protected]**
        """)

    # Add a subtle language management section at the bottom
    with gr.Row(elem_classes=["footer-section"]) as footer_section:
        with gr.Accordion("πŸ”§ Admin Language Management", open=False, elem_classes=["admin-tools-accordion"]):
            # Removed the "Language File Manager" headline

            # Password authentication - button below password field
            admin_password = gr.Textbox(
                type="password",
                label="Admin Password",
                placeholder="Enter admin password"
            )
            auth_button = gr.Button("Authenticate", size="sm")

            auth_status = gr.Markdown("")

            # File management (initially hidden)
            with gr.Group(visible=False) as lang_editor_group:
                gr.Markdown("Edit the languages JSON file below:", elem_classes=["edit-instructions"])

                # Language file editor
                lang_json_editor = gr.Code(
                    language="json",
                    label="Languages JSON",
                    lines=15
                )

                with gr.Row():
                    load_button = gr.Button("Load Current Languages", size="sm")
                    save_button = gr.Button("Save Changes", size="sm", elem_classes=["yellow-btn"])

                result_message = gr.Markdown("")

    # Check consent on page load and show/hide components appropriately
    def initialize_consent_status():
        # This function will be called when the app loads
        return False  # Default to not consented

    def update_visibility(has_consent):
        # Show/hide components based on consent status
        return (
            gr.Group(visible=has_consent),  # main_app
            gr.Group(visible=not has_consent),  # consent_overlay
            gr.Group(visible=not has_consent),  # consent_modal
            gr.Group(visible=has_consent),  # footer_banner
            gr.Group(visible=has_consent)   # footer_section
        )

    # Initialize app with consent checking
    demo.load(
        fn=initialize_consent_status,
        outputs=user_consented,
        js=js
    ).then(
        fn=update_visibility,
        inputs=user_consented,
        outputs=[main_app, consent_overlay, consent_modal, footer_banner, footer_section]
    )

# Update the consent button click handler
    consent_btn.click(
        fn=lambda: True,
        outputs=user_consented,
        js="() => set_cookie('feel_consent', 'true')"
    ).then(
        fn=update_visibility,
        inputs=user_consented,
        outputs=[main_app, consent_overlay, consent_modal, footer_banner, footer_section]
    )

    ##############################
    # Deal with feedback
    ##############################

    language_dropdown.change(
        fn=format_system_message,
        inputs=[language_dropdown],
        outputs=[chatbot],
    ).then(
        fn=lambda x: x,  # Update the language state
        inputs=[language_dropdown],
        outputs=[language]
    )

    chat_input.submit(
        fn=add_user_message,
        inputs=[chatbot, chat_input],
        outputs=[chatbot, chat_input],
    ).then(
        respond, 
        inputs=[chatbot, language], 
        outputs=[chatbot]
    ).then(
        lambda: gr.Textbox(interactive=True), 
        None, 
        [chat_input]
    )

    # Add a separate chain for updating the dataframe and leaderboard
    # This avoids the issue by not passing chatbot through this chain
    chatbot.change(
        fn=update_dataframe,
        inputs=[dataframe, chatbot],
        outputs=[dataframe]
    ).then(
        submit_conversation,
        inputs=[dataframe, conversation_id, session_id, language],
        outputs=[dataframe, chatbot, leaderboard_data]  # Replace None with chatbot
    ).then(
        render_leaderboard,
        inputs=[leaderboard_data],
        outputs=[leaderboard_html]
    )

    chatbot.like(
        fn=wrangle_like_data,
        inputs=[chatbot],
        outputs=[chatbot, dataframe],
        like_user_message=False,
    ).then(
        submit_conversation,
        inputs=[dataframe, conversation_id, session_id, language],
    )

    chatbot.retry(
        fn=wrangle_retry_data,
        inputs=[chatbot, dataframe, conversation_id, session_id, language],
        outputs=[chatbot, dataframe],
    )

    chatbot.edit(
        fn=wrangle_edit_data,
        inputs=[chatbot, dataframe, conversation_id, session_id, language],
        outputs=[chatbot],
    ).then(update_dataframe, inputs=[dataframe, chatbot], outputs=[dataframe])

    gr.on(
        triggers=[submit_btn.click, chatbot.clear],
        fn=submit_conversation,
        inputs=[dataframe, conversation_id, session_id, language],
        outputs=[dataframe, chatbot],
    ).then(
        fn=lambda x: str(uuid.uuid4()),
        inputs=[conversation_id],
        outputs=[conversation_id],
    )

    def on_app_load():
        global LANGUAGES
        LANGUAGES = load_languages()
        language_choices = list(LANGUAGES.keys())
        default_language = language_choices[0] if language_choices else "English"
        leaderboard_data = load_initial_language_data()

        return str(uuid.uuid4()), gr.Dropdown(choices=language_choices, value=default_language), default_language

    def toggle_admin_panel(visible):
        return gr.Accordion(visible=not visible)

    def handle_set_count(language, count):
        updated_data = set_language_data_points(language, int(count))
        save_language_data_points()
        return render_leaderboard(), updated_data

    demo.load(
        fn=on_app_load,
        inputs=None,
        outputs=[
            session_id,
            language_dropdown,
            language
        ]
    ).then(
        fn=lambda: render_leaderboard(),  # Call with no arguments
        outputs=[leaderboard_html]
    )

    add_language_btn.click(
        fn=lambda: gr.Group(visible=True),
        outputs=[add_language_modal]
    )

    cancel_language_btn.click(
        fn=lambda: gr.Group(visible=False),
        outputs=[add_language_modal]
    )

    save_language_btn.click(
        fn=save_new_language,
        inputs=[new_lang_name, new_system_prompt],
        outputs=[add_language_modal, refresh_html, language_dropdown]
    )

    # Connect the events
    # submit_email_btn.click(
    #     fn=lambda email, name, consent: "Thank you for your submission!" if consent else "Please provide consent to submit",
    #     inputs=[contributor_email, contributor_name, email_consent],
    #     outputs=[email_submit_status]
    # ).then(
    #     fn=lambda email, name, consent: save_contributor_email(email, name) if consent else None,
    #     inputs=[contributor_email, contributor_name, email_consent],
    #     outputs=None
    # )

    # Add the necessary functions
    def authenticate(password):
        """Authenticate the admin password"""
        correct_password = os.getenv("ADMIN_PASSWORD", "default_admin_password")
        if password == correct_password:
            return "βœ… Authentication successful. You can now manage languages.", gr.Group(visible=True)
        else:
            return "❌ Incorrect password. Please try again.", gr.Group(visible=False)

    def load_languages_file():
        """Load the languages file from persistent storage"""
        languages_path, _ = get_persistent_storage_path("languages.json")
        try:
            with open(languages_path, "r", encoding="utf-8") as f:
                content = f.read()
            return content, "Languages file loaded successfully."
        except Exception as e:
            return "", f"Error loading languages file: {str(e)}"

    def save_languages_file(json_content):
        """Save the languages file to persistent storage"""
        try:
            # Validate JSON format
            languages_dict = json.loads(json_content)

            # Basic validation
            if not isinstance(languages_dict, dict):
                return "Error: Content must be a JSON object (dictionary)."

            for key, value in languages_dict.items():
                if not isinstance(key, str) or not isinstance(value, str):
                    return f"Error: Keys and values must be strings. Issue with: {key}: {value}"

            # Save to file
            languages_path, _ = get_persistent_storage_path("languages.json")
            with open(languages_path, "w", encoding="utf-8") as f:
                f.write(json_content)

            return f"βœ… Languages file updated successfully with {len(languages_dict)} languages."
        except json.JSONDecodeError as e:
            return f"❌ Invalid JSON format: {str(e)}"
        except Exception as e:
            return f"❌ Error saving languages file: {str(e)}"

    # Connect the event handlers
    auth_button.click(
        fn=authenticate,
        inputs=[admin_password],
        outputs=[auth_status, lang_editor_group]
    )

    load_button.click(
        fn=load_languages_file,
        inputs=[],
        outputs=[lang_json_editor, result_message]
    )

    save_button.click(
        fn=save_languages_file,
        inputs=[lang_json_editor],
        outputs=[result_message]
    )

    admin_toggle.click(
        fn=toggle_admin_panel,
        inputs=[admin_panel],
        outputs=[admin_panel]
    )

    set_count_btn.click(
        fn=handle_set_count,
        inputs=[admin_language, admin_count],
        outputs=[leaderboard_html, leaderboard_data]
    )

demo.launch()