File size: 86,719 Bytes
0696f29
5010f3b
5be28d9
 
 
 
 
f916692
5be28d9
 
 
 
 
ab056f9
 
aedcd2d
ead8900
147e59b
2feb639
91eb6ba
4bfc93b
 
147e59b
d289a58
ab056f9
3192180
5be28d9
 
 
 
ab056f9
 
 
5be28d9
ab056f9
5be28d9
ab056f9
 
5be28d9
ab056f9
1e59d95
b837f42
f916692
1e59d95
ab056f9
a86070e
 
923c7d2
ab056f9
5be28d9
28a6133
ab056f9
5be28d9
 
1e59d95
b837f42
f916692
1e59d95
5be28d9
a86070e
 
923c7d2
5be28d9
 
ab056f9
1eb736f
ab056f9
 
a74365f
 
 
 
 
 
cb4287f
 
a74365f
 
 
 
 
 
 
 
1e59d95
a74365f
 
 
 
 
b837f42
a74365f
 
f916692
a74365f
 
f916692
a74365f
 
1e59d95
a74365f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6e31a
e070bfe
a74365f
a86070e
446c43b
f58043d
87bb9af
5be28d9
ab056f9
 
 
eb6fa00
09da72d
b5dbaa7
eb6fa00
 
b5dbaa7
eb6fa00
3288bca
 
 
eb6fa00
 
 
544f2be
eb6fa00
 
 
544f2be
eb6fa00
 
 
 
 
 
 
544f2be
eb6fa00
 
 
 
 
ee617d9
 
 
 
 
 
b5dbaa7
09da72d
 
 
 
 
 
 
 
 
 
eb6fa00
cfbfa67
eb6fa00
 
544f2be
eb6fa00
2e79c6a
ee617d9
eb6fa00
ee617d9
 
eb6fa00
 
 
2e79c6a
 
ee617d9
eb6fa00
 
ee617d9
eb6fa00
 
09da72d
 
eb6fa00
ee617d9
544f2be
 
b5dbaa7
34d917f
 
ee617d9
eb6fa00
ee617d9
 
c21c060
544f2be
b5dbaa7
eb6fa00
cfbfa67
ead8900
544f2be
2e28b84
1eb736f
ead8900
b5dbaa7
544f2be
ead8900
b5dbaa7
cfbfa67
c21c060
 
 
 
 
 
ab056f9
c21c060
 
 
 
 
 
 
 
ab056f9
c21c060
ab056f9
 
 
 
c21c060
 
f17e5ba
c21c060
 
 
 
 
 
 
f17e5ba
c21c060
f17e5ba
ab056f9
 
f17e5ba
ab056f9
c21c060
ab056f9
c21c060
ab056f9
 
c21c060
 
2feb639
91eb6ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d62d3a9
2feb639
 
 
 
 
91eb6ba
 
2feb639
 
 
 
 
c21c060
91eb6ba
6beb359
 
91eb6ba
 
 
 
 
 
 
c21c060
147e59b
91eb6ba
2feb639
 
147e59b
2feb639
 
 
 
 
 
 
 
147e59b
91eb6ba
 
d62d3a9
 
 
5f3e869
 
7ddc969
d62d3a9
91eb6ba
 
 
 
4bfc93b
d62d3a9
c21c060
d62d3a9
 
91eb6ba
d62d3a9
55f2f7b
d62d3a9
 
91eb6ba
2feb639
91eb6ba
 
c21c060
2feb639
c21c060
 
ab056f9
78dc7f6
1eb736f
 
5be28d9
 
 
 
 
1eb736f
9514504
 
1eb736f
 
 
9514504
e73fa5c
 
 
 
 
 
 
 
 
 
 
 
09da72d
ead8900
 
 
 
afbfe50
428b100
283e770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3406c39
283e770
 
 
 
 
 
 
 
 
 
 
 
 
 
1e59d95
283e770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb6fa00
283e770
 
 
 
 
1e59d95
283e770
1e59d95
283e770
 
 
 
 
 
1e59d95
283e770
 
3406c39
 
eb6fa00
283e770
 
 
1e59d95
283e770
79e9e1f
283e770
 
 
eb6fa00
283e770
eb6fa00
283e770
3406c39
 
283e770
 
 
 
 
 
 
 
 
 
428b100
283e770
 
 
 
 
 
 
 
 
 
 
 
1e59d95
eb6fa00
283e770
 
 
 
 
 
37d4f71
eb6fa00
283e770
 
 
428b100
 
 
 
 
 
 
 
 
283e770
 
 
 
 
 
 
 
 
 
 
 
 
 
797a77d
 
283e770
 
035b684
37d4f71
 
 
b71a811
37d4f71
 
283e770
 
 
035b684
37d4f71
 
 
b71a811
37d4f71
 
 
886ae8c
283e770
886ae8c
 
 
 
 
37d4f71
886ae8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37d4f71
1eb736f
283e770
886ae8c
 
 
9ebd21e
283e770
886ae8c
 
 
 
37d4f71
886ae8c
 
 
 
 
37d4f71
428b100
886ae8c
 
 
 
283e770
886ae8c
 
283e770
886ae8c
 
 
 
 
283e770
e73fa5c
283e770
e73fa5c
 
 
 
 
283e770
e73fa5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428b100
 
e73fa5c
283e770
e73fa5c
 
283e770
e73fa5c
 
 
 
 
aa3ec2e
1eb736f
283e770
e73fa5c
 
 
 
37d4f71
e73fa5c
 
 
 
 
37d4f71
e73fa5c
 
283e770
e73fa5c
 
283e770
e73fa5c
 
 
 
 
283e770
eb6fa00
283e770
e73fa5c
 
 
 
76dfd30
e73fa5c
 
 
 
 
76dfd30
 
e73fa5c
283e770
5ab4c0c
 
 
 
 
 
 
283e770
e73fa5c
 
79cc7bc
e73fa5c
 
 
283e770
e73fa5c
 
 
5ab4c0c
e73fa5c
283e770
 
 
 
 
 
 
 
eb6fa00
283e770
 
 
eb6fa00
283e770
 
 
 
 
78bc188
283e770
 
 
 
 
 
 
 
428b100
283e770
 
 
 
 
428b100
283e770
 
 
78bc188
283e770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3406c39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6beb359
3406c39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4dc66a
0696f29
3406c39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2feb639
3406c39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760cdf1
3406c39
 
 
 
 
 
 
16ee3b6
 
 
 
 
 
 
 
 
 
 
 
283e770
 
 
 
 
 
 
 
 
 
 
 
5be28d9
283e770
 
 
 
 
70c8d2e
b701f1e
283e770
 
 
be848c7
283e770
 
 
 
cfeb2ce
283e770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7142a63
5398c45
7142a63
 
 
5398c45
283e770
 
 
 
be848c7
283e770
 
cb4287f
 
283e770
cfeb2ce
283e770
 
 
2aeb160
283e770
 
 
 
 
 
 
 
 
 
 
4e4391c
 
 
 
7142a63
 
 
 
283e770
 
 
 
be848c7
283e770
cb4287f
 
283e770
cfeb2ce
283e770
 
 
2aeb160
283e770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be848c7
283e770
cb4287f
 
283e770
cfeb2ce
283e770
923c7d2
 
2aeb160
923c7d2
283e770
 
 
 
 
 
 
 
923c7d2
 
 
 
cb4287f
 
923c7d2
 
 
 
 
 
 
 
 
 
cb4287f
923c7d2
 
 
cb4287f
 
923c7d2
 
 
 
 
 
 
 
 
 
 
 
 
283e770
 
1e24622
283e770
 
2feb639
283e770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923c7d2
 
 
283e770
 
 
 
be848c7
283e770
cb4287f
 
283e770
 
 
 
7885f4f
16ee3b6
0feda92
 
 
2feb639
0feda92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283e770
e63c537
 
16ee3b6
e63c537
 
 
 
 
 
 
 
 
 
 
2feb639
e63c537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97084a1
 
 
e63c537
 
 
97084a1
 
16ee3b6
da04045
16ee3b6
 
e63c537
 
 
 
 
 
 
 
283e770
37d4f71
 
 
54f412c
37d4f71
 
 
 
 
 
 
 
5be28d9
72d7234
5be28d9
a74365f
1eb736f
a74365f
72d7234
 
48b2f3d
 
09da72d
48b2f3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9839822
48b2f3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4dc66a
8b6802b
72d7234
48b2f3d
 
 
 
 
 
 
72d7234
48b2f3d
 
 
 
 
 
3c4b3d6
 
709f12d
3c4b3d6
872fcee
72d7234
48b2f3d
872fcee
2cd905e
 
72d7234
48b2f3d
72d7234
 
 
 
 
 
5be28d9
2cd905e
54f412c
 
ddb33e4
8b6802b
 
 
 
 
 
 
 
923c7d2
5be28d9
 
72d7234
5be28d9
72d7234
5be28d9
72d7234
 
5be28d9
2206e72
54f412c
 
5be28d9
8b6802b
 
 
 
 
 
 
5be28d9
 
 
72d7234
 
 
 
 
5be28d9
eb6fa00
09da72d
5be28d9
09da72d
 
5be28d9
09da72d
82f7457
 
 
 
5be28d9
 
72d7234
 
 
 
5be28d9
72d7234
5be28d9
b7e4638
48b2f3d
1e24622
48b2f3d
b7e4638
37d4f71
b7e4638
 
 
3406c39
830b3ab
37d4f71
 
 
b7e4638
37d4f71
 
 
 
 
ead8900
3402f84
afbfe50
37d4f71
 
 
f46fb70
37d4f71
 
283e770
6a59fab
54f412c
b7e4638
5ea9fbd
72d7234
5be28d9
 
 
 
72d7234
2cd905e
 
 
872fcee
 
 
 
 
 
 
 
2feb639
2cd905e
2feb639
 
 
 
2cd905e
72d7234
2cd905e
72d7234
2cd905e
72d7234
 
 
 
2cd905e
 
72d7234
2cd905e
72d7234
 
 
 
 
 
923c7d2
137efe0
 
 
 
 
 
 
 
923c7d2
137efe0
78dc7f6
923c7d2
78dc7f6
72d7234
78dc7f6
2cd905e
72d7234
 
78dc7f6
 
5be28d9
 
fd0fb1c
923c7d2
247b89d
25a185e
72d7234
 
25a185e
247b89d
5be28d9
 
72d7234
 
2a84939
 
 
 
 
 
 
 
 
 
78dc7f6
 
 
0696f29
760cdf1
0696f29
 
 
 
78dc7f6
0696f29
 
 
 
 
2a84939
78dc7f6
72d7234
78dc7f6
72d7234
2a84939
 
 
5be28d9
 
72d7234
1eb736f
247b89d
78dc7f6
72d7234
 
b221c0f
5be28d9
 
78dc7f6
cdcb85c
b221c0f
 
cdcb85c
247b89d
cdcb85c
247b89d
cdcb85c
247b89d
cdcb85c
78dc7f6
 
 
 
cdcb85c
 
 
78dc7f6
 
72d7234
cdcb85c
 
72d7234
 
cdcb85c
 
 
0aaeda6
834500a
1eb736f
247b89d
b221c0f
72d7234
 
78dc7f6
0aaeda6
f571fdc
bf3b5df
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

import os
import openai
import gradio as gr
import pandas as pd
from Chatbot_new import (
    generate_first_reveal, generate_internal_external_options,
    generate_second_reveal, generate_stable_unstable_options, generate_global_specific_options,
    generate_vignette, interpret_response, save_data_to_pickle,
    standardize_response, save_data_to_excel
)
import uuid
from dotenv import load_dotenv
from huggingface_hub import HfApi, HfFolder
import datetime
import logging
import asyncio
import threading
import time
import boto3
from botocore.config import Config

file_lock = threading.Lock()
#logging.basicConfig(level=logging.DEBUG)

tiliah_write1 = os.getenv("tiliah_write1")

# Load environment variables
load_dotenv()

##############################################################################
# File paths & Initialization
##############################################################################

excel_file = "assessment_data.xlsx"
pickle_file = "assessment_data.pkl"

# Make sure your Excel file has the PHQ/GAD columns:
if not os.path.exists(excel_file):
    pd.DataFrame(columns=[
        'Prolific ID', 'Age', 'Profession', 'Interests', 'New Interest', 'Topic Choice',
        'Vignette Number', 'Vignette', 'Tone', 'First Reveal', 'Raw Internal_External', 'Initial Internal',
        'Initial External', 'Raw Stable_Unstable', 'Initial Stable', 'Initial Unstable', 'Raw Global_Specific', 'Initial Global',
        'Initial Specific', 'Raw Reassessment', 'Reassessment Internal', 'Reassessment External',
        'Initial Relevance Rating', 'Final Relevance Rating', 'Emotional Relevance',
        'Positive Valence', 'Negative Valence', 'Likelihood of Change', "Ease of Use",
        "Clarity of Instructions", "Engagement Level", "Satisfaction with Assessment",
        "Cognitive Load", "Additional Comments", 'PHQ-9 Score', 'GAD-7 Score', "Attention Check 1", "Attention Check 2"
    ]).to_excel(excel_file, index=False, engine="openpyxl")


# Same for the pickle file:
if not os.path.exists(pickle_file):
    pd.DataFrame(columns=[
        'Prolific ID', 'Age', 'Profession', 'Interests', 'New Interest', 'Topic Choice',
        'Vignette Number', 'Vignette', 'Tone', 'First Reveal', 'Raw Internal_External', 'Initial Internal',
        'Initial External', 'Raw Stable_Unstable', 'Initial Stable', 'Initial Unstable', 'Raw Global_Specific', 'Initial Global',
        'Initial Specific', 'Raw Reassessment', 'Reassessment Internal', 'Reassessment External',
        'Initial Relevance Rating', 'Final Relevance Rating', 'Emotional Relevance',
        'Positive Valence', 'Negative Valence', 'Likelihood of Change', "Ease of Use",
        "Clarity of Instructions", "Engagement Level", "Satisfaction with Assessment",
        "Cognitive Load", "Additional Comments", 'PHQ-9 Score', 'GAD-7 Score', "Attention Check 1", "Attention Check 2"
    ]).to_pickle(pickle_file)

##############################################################################
# Shared user_state & Constants
##############################################################################

def get_default_app_state():
    return {
        "prolific_id": None,
        "phq_score": 0,
        "gad_score": 0,
        "eligible": False,
        "Attention Check 1": None,
        "Attention Check 2": None,
        "standardized_data": {
            "Prolific ID": None,
            "Raw Age": None,
            "Raw Profession": None,
            "Raw Interests": None,
            "Age": None,
            "Profession": None,
            "Interests": None,
            'New Interest': None,
            "Topic Choice": None,
            "Vignette Number": None,
            "Vignette": None,
            "Tone": None,
            "First Reveal": None,
            'Raw Internal_External': None,
            "Initial Internal": None,
            'Initial External': None,
            'Raw Stable_Unstable': None,
            'Initial Stable': None,
            'Initial Unstable': None,
            'Raw Global_Specific': None,
            'Initial Global': None,
            'Initial Specific': None,
            'Raw Reassessment': None,
            'Reassessment Internal': None,
            'Reassessment External': None,
            'Initial Relevance Rating': None,
            'Final Relevance Rating': None,
            'Emotional Relevance': None,
            'Positive Valence': None,
            'Negative Valence': None,
            'Likelihood of Change': None,
            'PHQ-9 Score': None,
            'GAD-7 Score': None,
            "Second Reveal": None,
            "Explanation Response": None,
            "Assessment Internal External Response": None,
            "Evolved Internal External Response": None,
        },
        "stage": "prestart",
        "user_responses": [],
        "vignette_counter": 1,
        "topic_choice": None,
        "options_text": None,
        "desired_tone": None,
        "usability_question_counter": 0,
        "usability_feedback": {
            "Ease of Use": None,
            "Clarity of Instructions": None,
            "Engagement Level": None,
            "Satisfaction with Assessment": None,
            "Cognitive Load": None,
            "Additional Comments": None,
            "welcome_message": False
        }
    }


EXCLUSION_THRESHOLD = 20

##############################################################################
# Storing / Uploading Functions
##############################################################################

def generate_custom_vignette(chat_history, topic_choice, desired_tone, user_state, feedback=None):
    """
    Generates a personalized vignette for psychological assessment based on user preferences, ensuring
    that each vignette exclusively reflects the chosen topic (profession or interest) without mixing them.
    """
    # Retrieve stored user info
    user_age = user_state["standardized_data"].get("Age", "Unknown")
    user_profession = user_state["standardized_data"].get("Profession", "Unknown")
    user_interests = user_state["standardized_data"].get("Interests", "Unknown")
    new_interest = user_state["standardized_data"].get("New Interest", None)

    # Identify the relevant topic context based on topic_choice
    if topic_choice == "1":
        # Focus on profession only
        chosen_topic_text = f"Profession: {user_profession}"
        topic_context = f"a scenario directly related to teaching or performing tasks typical of {user_profession}"
    elif topic_choice == "2":
        # Focus on personal interests only
        chosen_topic_text = f"Interests: {user_interests}"
        topic_context = f"a scenario reflecting one of the user's personal interests ({user_interests})"
    elif topic_choice == "3" and new_interest:
        # Use the newly added interest
        chosen_topic_text = f"New Interest: {new_interest}"
        topic_context = f"a scenario inspired by the newly mentioned interest ({new_interest})"
    else:
        # Fallback for any other case
        chosen_topic_text = "General context"
        topic_context = "a general scenario based on the user's profile"
    
    # Decide the tone
    if desired_tone == "Positive":
        tone_context = "a pleasant and rewarding situation, highlighting success, collaboration, or enjoyable experiences."
    elif desired_tone == "Negative":
        tone_context = "an unpleasant or confrontational situation, involving conflict, challenges, or setbacks."
    else:
        tone_context = "a neutral situation."

    # If feedback is provided, embed it in the prompt
    feedback_instructions = ""
    if feedback and feedback.strip():
        feedback_instructions = f"""
        The user has provided additional information or critiques about the previous scenario:
        "{feedback}"

        Please incorporate this feedback to make the scenario more aligned with the user's context.
        """
    
    # Build a prompt that references ONLY the chosen topic
    prompt = f"""
    You are tasked with generating a short, personalized vignette for a psychological assessment.
    The user is {user_age} years old.

    {chosen_topic_text}

    Scenario Details:
    - The vignette must strictly focus on {topic_context}.
    - The tone of the vignette should reflect {tone_context}.
    - The vignette should include:
        - A clear context type (e.g., work, social, physical, educational, creative).
        - A specific interaction (e.g., feedback from authority, peer collaboration, group dynamics).
        - A defined event or action (e.g., presenting an idea, resolving a conflict, completing a project).

    Guidelines:
    - The vignette must describe external events only, without referencing thoughts, emotions, or internal reflections.
    - Use second-person language ("You...").
    - Keep the vignette concise (under 50 tokens) and neutral/professional.
    - Do not include personal identifying information.
    - Do not mention both profession and personal interests together; focus exclusively on the chosen topic.
    - Do not mix multiple interests or contexts; stay consistent with the single chosen topic.

    {feedback_instructions}
    
    Now generate the vignette:
    """

    try:
        response = openai.chat.completions.create(
            model="gpt-4o",
            messages=[
                {"role": "system", "content": "You are an assistant generating short, topic-focused scenarios without mixing contexts."},
                {"role": "user", "content": prompt},
            ],
            temperature=0.3,
            max_tokens=100
        )

        content = response.choices[0].message.content.strip()
        return content, desired_tone

    except KeyError as ke:
        print(f"KeyError accessing user_state: {ke}")
        return None, None
    except Exception as e:
        print(f"Error generating vignette: {e}")
        return None, None


def upload_file_to_repo(
    local_file_path, 
    repo_id, 
    hf_token, 
    path_in_repo=None, 
    repo_type="space"):
    """
    Uploads `local_file_path` to a Hugging Face repo (dataset/space/model).
    Deletes existing versions of the file first.

    :param local_file_path: local file (e.g. "partial_data.xlsx")
    :param repo_id: e.g. "mojakob/user_data" or "mojakob/my-space"
    :param hf_token: huggingface token with write access
    :param path_in_repo: file path in the repo (defaults to local_file_path)
    :param repo_type: "dataset", "space", or "model"
    """

    api = HfApi()
    if path_in_repo is None:
        path_in_repo = local_file_path

    # 1. Delete previous file(s) of same name
    all_files = api.list_repo_files(repo_id, repo_type=repo_type)
    for file in all_files:
        if file.startswith(path_in_repo):  # match by filename prefix
            api.delete_file(
                path_in_repo=file, 
                repo_id=repo_id, 
                repo_type=repo_type, 
                token=hf_token
            )
            print(f"Deleted old file: {file}")

    # 2. Upload the new version
    api.upload_file(
        path_or_fileobj=local_file_path,
        path_in_repo=path_in_repo,
        repo_id=repo_id,
        repo_type=repo_type,
        token=hf_token,
        commit_message="Upload latest version of data file"
    )

    print(f"Uploaded {local_file_path} -> {repo_id}/{path_in_repo} [repo_type={repo_type}]")


# def save_to_excel(new_row, user_state):
#     """
#     Appends a single row (new_row) to the user's unique file 
#     stored in user_state["excel_file"].

#     Then uploads that updated file to Hugging Face, preserving concurrency 
#     by never touching a shared file.
#     """
#     hf_token = os.getenv("tiliah_write1")
#     dataset_repo_id = "Tiliah/user_data" 
#     local_file = user_state.get("excel_file")

#     if not local_file:
#         print("Error: No user_state['excel_file'] found.")
#         return

#     usability_data = user_state.get("usability_feedback", {})
#     new_row.update(usability_data)
    
#     try:
#         with file_lock:
#             # 1. If the unique user file doesn't exist yet, create a new one with appropriate columns
#             if not os.path.exists(local_file):
#                 df = pd.DataFrame(columns=new_row.keys())
#             else:
#                 df = pd.read_excel(local_file, engine="openpyxl")
            
#             # 2. Append the new row
#             df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
#             print("SAVING row to unique user file:", new_row)
            
#             # 3. Save locally
#             df.to_excel(local_file, index=False, engine="openpyxl")

#         # 4. Upload to Hugging Face (outside the lock)
#         if hf_token and dataset_repo_id:
#             api = HfApi()
#             path_in_repo = f"Cond1/{local_file}"
#             future_upload = api.upload_file(
#                 path_or_fileobj=local_file,
#                 path_in_repo=path_in_repo,  # same name in the repo
#                 repo_id=dataset_repo_id,
#                 repo_type="dataset",
#                 token=hf_token,
#                 run_as_future=True
#             )
#             future_upload
#             future_upload.result()            
#             print(f"Uploaded {local_file} to HF dataset.")
#         else:
#             print("No HF token or dataset repo provided, skipping HF upload.")
#     except Exception as e:
#         print(f"Error in save_to_excel: {e}")
#         raise


def save_to_excel(new_row, user_state):
    """
    Appends a single row (new_row) to the user's unique file 
    stored in user_state["excel_file"].

    Then uploads that updated file to S3 under "Cond1/<filename>",
    preserving concurrency by never touching a shared file.
    """
    local_file = user_state.get("excel_file")
    if not local_file:
        print("Error: No user_state['excel_file'] found.")
        return

    # Merge in usability feedback if present
    usability_data = user_state.get("usability_feedback", {})
    new_row.update(usability_data)

    # Read S3 credentials from environment
    s3_access_key = os.getenv("HETZNER_ACCESS_KEY_ID")
    s3_secret_key = os.getenv("HETZNER_SECRET_ACCESS_KEY")
    s3_endpoint   = os.getenv("HETZNER_S3_ENDPOINT")
    bucket_name   = os.getenv("HETZNER_BUCKET_NAME")

    try:
        with file_lock:
            # 1. Create or load existing local file
            if not os.path.exists(local_file):
                df = pd.DataFrame(columns=new_row.keys())
            else:
                df = pd.read_excel(local_file, engine="openpyxl")
            
            # 2. Append the new row
            df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
            print("SAVING row to unique user file:", new_row)
            
            # 3. Save locally
            df.to_excel(local_file, index=False, engine="openpyxl")

        # 4. Upload to S3 (outside the lock), if credentials are present
        if s3_access_key and s3_secret_key and s3_endpoint and bucket_name:
            # Configure S3 client for Hetzner's S3 (path-style addressing required)
            my_config = Config(
                signature_version="s3v4",
                s3={"addressing_style": "path",
                   "payload_signing_enabled": False
                }               
            )
            s3_client = boto3.client(
                "s3",
                aws_access_key_id=s3_access_key,
                aws_secret_access_key=s3_secret_key,
                endpoint_url=s3_endpoint,
                config=my_config
            )
            # Use the basename to avoid including unwanted local path info
            s3_key = f"Cond1/{os.path.basename(local_file)}"
            
            # Read file content for upload
            with open(local_file, "rb") as f:
                file_content = f.read()
                s3_client.put_object(Bucket=bucket_name, Key=s3_key, Body=file_content)
            print(f"Uploaded {local_file} to S3 bucket '{bucket_name}' at key: {s3_key}")
        else:
            print("No S3 credentials or bucket provided, skipping S3 upload.")

    except Exception as e:
        print(f"Error in save_to_excel: {e}")
        raise


# Processes usability questions
def process_feedback(ease, clarity, engagement, satisfaction, cognitive_load, user_state):  
    user_state["usability_feedback"] = {
        "Ease of Use": ease,
        "Clarity of Instructions": clarity,
        "Engagement Level": engagement,
        "Satisfaction with Assessment": satisfaction,
        "Cognitive Load": cognitive_load,
#        "Additional Comments": comments
    }

    response = "Thank you for your feedback! It has been recorded.", user_state
    chat_history[-1] = (user_input, response)        
    return chat_history, user_state

def validate_numeric_input(user_input, min_val=1, max_val=10):
    """
    Attempts to convert user_input to an integer and ensures it's between min_val and max_val.
    Returns (value, None) if valid, or (None, error_message) if invalid.
    """
    try:
        value = int(user_input.strip())
        if value < min_val or value > max_val:
            return None, f"Please provide a number between {min_val} and {max_val}."
        return value, None
    except ValueError:
        return None, f"Please provide a number between {min_val} and {max_val}."

#############################################################################
# Chat Flow
#############################################################################

def new_chat_input(chat_history, user_state, user_input, *args, **kwargs):
        
    if not user_input.strip():
        return (
            chat_history,                 # Updated chat history
            user_state,                   # Updated user state
            gr.update(visible=True, value=""),  # Textbox
            gr.update(visible=False),     # Slider
            gr.update(visible=False)      # Confirm button
        )
    chat_history.append((user_input, ""))  
    
    vignette_tones = [
        'Positive', 'Negative', 'Negative', 'Positive', 'Positive', 'Negative', 'Negative', 'Positive',
        'Positive', 'Negative', 'Negative', 'Positive', 'Positive', 'Negative', 'Negative', 'Positive']  # Total of 16 vignettes
    
    print(f"user_state at start of new_chat_input: {user_state}")
    print(f"input received: {user_input}")
    print(f"history received: {chat_history}")
                    
    if user_state["stage"] == "prestart":
        user_state["standardized_data"]['Prolific ID'] = user_state["prolific_id"]
        # Retrieve the "Raw" fields first
        raw_age = user_state["standardized_data"].get("Raw Age", "Unknown")
        raw_profession = user_state["standardized_data"].get("Raw Profession", "Unknown")
        raw_interests = user_state["standardized_data"].get("Raw Interests", "Unknown")

        required_fields = ["Age", "Profession", "Interests"]
        for field in required_fields:
            if field not in user_state["standardized_data"]:
                raise KeyError(f"Missing field '{field}' in user_state['standardized_data'].")

        user_state["stage"] = "topic_choice"
        print(f"user_state during initial topic_choice: {user_state}")
        
        response = """
        Let's create a scenario for you.
        Which topic would you prefer?
        1. A situation related to your profession.
        2. A situation related to your personal interests.
        3. Something else about me. (Add another activity or interest that is important to you by saying "I also like...")
        """

        if user_state["vignette_counter"] == 1 and not user_state.get("welcome_message", False):
            welcome_message = "Alright then! "
            response = welcome_message + response
            user_state["welcome_message"] = True        
        
        chat_history[-1] = (user_input, response)
        return (
            chat_history,                 # Updated chat history
            user_state,                   # Updated user state
            gr.update(visible=True, value=""),  # Textbox
            gr.update(visible=False),     # Slider
            gr.update(visible=False)      # Confirm button
        )
    
    elif user_state["stage"] == "topic_choice":
        print("topic_choice block successfully entered")
        print(f"User entered input: {user_input}")
        
        # Define the options text
        user_state['options_text'] = (
            '1. A situation related to your profession.\n'
            '2. A situation related to your personal interests.\n'
            '3. Something else about me (Add another activity or interest that is important to you by saying "I also like..." or including "3" in your response)'
        )
        
        # Store the user's response
        user_state["user_responses"].append({"stage": "topic_choice", "response": user_input})
        print(f"user input was: {user_input}, user responses after storing it are: {user_state['user_responses']}")
        
        user_input_lower = user_input.lower().strip()
        
        mapped_option1 = standardize_response("Please extract the user's chosen topic.", user_input)
        mapped_option2 = interpret_response(mapped_option1, user_state['options_text'], user_state)
    
        # Check if user typed "3" anywhere in their input
        if "3" in user_input_lower:
            mapped_option2 = "3"
            # Attempt to parse the new interest from any text after "3"
            # e.g. "So I'd prefer 3 traveling"
            split_on_3 = user_input_lower.split("3", 1)
            new_interest_raw = split_on_3[1].strip() if len(split_on_3) > 1 else ""
            
            # If user included more text after "3", treat that as the new interest
            if new_interest_raw:
                new_interest = new_interest_raw
            else:
                new_interest = "unspecified"  # fallback
            
            user_state["standardized_data"]["New Interest"] = new_interest
            
            # Update the user's interests
            existing_interests = user_state["standardized_data"].get("Interests", "")
            if existing_interests:
                user_state["standardized_data"]["Interests"] = existing_interests + ", " + new_interest
            else:
                user_state["standardized_data"]["Interests"] = new_interest
            
            # Store the new interest as the topic choice for vignette generation
            topic_choice_response = new_interest
            user_state["standardized_data"]["Topic Choice"] = "3"
            user_state["topic_choice"] = "3"
        
        elif "i also like" in user_input_lower:
            mapped_option2 = "3"
            new_interest = user_input_lower.split("i also like", 1)[1].strip()
            
            user_state["standardized_data"]["New Interest"] = new_interest
            
            existing_interests = user_state["standardized_data"].get("Interests", "")
            if existing_interests:
                user_state["standardized_data"]["Interests"] = existing_interests + ", " + new_interest
            else:
                user_state["standardized_data"]["Interests"] = new_interest
            
            topic_choice_response = new_interest
            user_state["standardized_data"]["Topic Choice"] = "3"
            user_state["topic_choice"] = "3"
        
        else:
            # No explicit "3" or "i also like", rely on interpret_response for '1' or '2'
            print(f"Before Topic Choice Update: {user_state['standardized_data']}")
            user_state["standardized_data"]["Topic Choice"] = mapped_option2
            user_state["topic_choice"] = mapped_option2
            print(f"After Topic Choice Update: {user_state['standardized_data']}")
        
        print(f"User Input: {user_input}")
        print(f"Mapped Option: {mapped_option2}")
        
        if mapped_option2 in ['1', '2', '3']:
            topic_choice_response = mapped_option2
            user_state["standardized_data"]['Vignette Number'] = user_state["vignette_counter"]
            user_state["desired_tone"] = vignette_tones[user_state["vignette_counter"] - 1] if user_state["vignette_counter"] - 1 < len(vignette_tones) else "Positive"
            
            if mapped_option2 == "3":
                # Generate vignette using the newly extracted new_interest (if any)
                vignette, tone = generate_custom_vignette(
                    chat_history=chat_history,
                    topic_choice=user_state["topic_choice"],
                    desired_tone=user_state["desired_tone"],
                    user_state=user_state
                )
            else:
                # Normal path for options 1 or 2
                vignette, tone = generate_custom_vignette(
                    chat_history=chat_history,
                    topic_choice=user_state["topic_choice"],
                    desired_tone=user_state["desired_tone"],
                    user_state=user_state
                )
            
            if vignette is None:
                user_state["stage"] = "error"
                response = "Error generating vignette. Please restart."
                chat_history[-1] = (user_input, response)        
                return (
                    chat_history,                 # Updated chat history
                    user_state,                   # Updated user state
                    gr.update(visible=True, value=""),  # Textbox
                    gr.update(visible=False),     # Slider
                    gr.update(visible=False)      # Confirm button
                )
            
            user_state["vignette"] = vignette
            user_state["tone"] = tone
            user_state["user_responses"].append({
                'stage': 'vignette', 
                'content': user_state["vignette"], 
                'tone': user_state["tone"]
            })
            
            user_state["standardized_data"]['Vignette'] = user_state["vignette"]
            user_state["standardized_data"]['Tone'] = user_state["tone"]
            user_state["stage"] = "vignette_usability_assessment"
            print(f"Stage updated to: {user_state['stage']}")
    
            response = (
                f"Alright, please imagine you are witnessing the following scene:\n\n{user_state['vignette']}\n\n"
                "Before we proceed: Have you personally experienced something similar to this scenario before? "
                "Please rate the similarity on a scale from 1 to 10 (1 = Not at all similar, 10 = Extremely similar)."
            )
            chat_history[-1] = (user_input, response)        
            return (
                chat_history,                 # Updated chat history
                user_state,                   # Updated user state
                gr.update(visible=True, value=""),  # Textbox
                gr.update(visible=False),     # Slider
                gr.update(visible=False)      # Confirm button
            )          
        else:
            response = "Sorry, I couldn't understand your response. Could you please rephrase or specify your choice?"
            chat_history[-1] = (user_input, response)        
            return (
                chat_history,                 # Updated chat history
                user_state,                   # Updated user state
                gr.update(visible=True, value=""),  # Textbox
                gr.update(visible=False),     # Slider
                gr.update(visible=False)      # Confirm button
            )
        
    elif user_state["stage"] == "vignette_usability_assessment":
        try:
            relevance_rating_int = int(user_input.strip())
        except ValueError:
            response = "Please provide a number between 1 and 10."
            chat_history[-1] = (user_input, response)
            return (
                chat_history,
                user_state,
                gr.update(visible=True, value=""),
                gr.update(visible=False),
                gr.update(visible=False)
            )
        if relevance_rating_int not in range(1, 11):
            response = "Please provide a number between 1 and 10."
            chat_history[-1] = (user_input, response)
            return (
                chat_history,
                user_state,
                gr.update(visible=True, value=""),
                gr.update(visible=False),
                gr.update(visible=False)
            )
        
        # Store the initial relevance rating if not already stored
        if 'Initial Relevance Rating' not in user_state["standardized_data"]:
            user_state["standardized_data"]['Initial Relevance Rating'] = relevance_rating_int
        
        if relevance_rating_int < 6:
            user_state["stage"] = "vignette_regeneration"
            response = ("I'm sorry to hear that the scenario wasn't quite fitting. "
                        "I might have misunderstood some of your responses.\n\n"
                        "Could you please briefly clarify why the scenario felt off, so we can come up with a better one?")
            chat_history[-1] = (user_input, response)
            return (
                chat_history,
                user_state,
                gr.update(visible=True, value=""),
                gr.update(visible=False),
                gr.update(visible=False)
            )
        
        # Accept the vignette and move on
        user_state["standardized_data"]['Vignette'] = user_state["vignette"]
        user_state["standardized_data"]['Tone'] = user_state["tone"]
        user_state["standardized_data"]['Final Relevance Rating'] = relevance_rating_int
        user_state["stage"] = "emotional_relevance"
        response = "How much emotional impact did this scenario have on you? (1 = None, 10 = A lot)"
        chat_history[-1] = (user_input, response)
        return (
            chat_history,
            user_state,
            gr.update(visible=True, value=""),
            gr.update(visible=False),
            gr.update(visible=False)
        )
            
    elif user_state["stage"] == "emotional_relevance":
        try:
            emotional_relevance_int = int(user_input.strip())
        except ValueError:
            response = "Please provide a number between 1 and 10."
            chat_history[-1] = (user_input, response)
            return (
                chat_history,
                user_state,
                gr.update(visible=True, value=""),
                gr.update(visible=False),
                gr.update(visible=False)
            )
        if emotional_relevance_int not in range(1, 11):
            response = "Please provide a number between 1 and 10."
            chat_history[-1] = (user_input, response)
            return (
                chat_history,
                user_state,
                gr.update(visible=True, value=""),
                gr.update(visible=False),
                gr.update(visible=False)
            )
        
        user_state["standardized_data"]["Emotional Relevance"] = emotional_relevance_int
        user_state["stage"] = "positive_valence"
        response = "How much joy, excitement, or satisfaction did this scenario evoke in you? (1 = None, 10 = A lot)"
        chat_history[-1] = (user_input, response)
        return (
            chat_history,
            user_state,
            gr.update(visible=True, value=""),
            gr.update(visible=False),
            gr.update(visible=False)
        )
    
    elif user_state["stage"] == "positive_valence":
        positive_valence, error = validate_numeric_input(user_input, 1, 10)
        if error:
            response = error
            chat_history[-1] = (user_input, response)
            return (
                chat_history,
                user_state,
                gr.update(visible=True, value=""),
                gr.update(visible=False),
                gr.update(visible=False)
            )
        
        user_state["standardized_data"]['Positive Valence'] = positive_valence
        user_state["stage"] = "negative_valence"
        response = "How much discomfort or tension did you feel while imagining this scenario? (1 = None, 10 = A lot)"
        chat_history[-1] = (user_input, response)
        return (
            chat_history,
            user_state,
            gr.update(visible=True, value=""),
            gr.update(visible=False),
            gr.update(visible=False)
        )
    
    elif user_state["stage"] == "negative_valence":
        negative_valence, error = validate_numeric_input(user_input, 1, 10)
        if error:
            response = error
            chat_history[-1] = (user_input, response)
            return (
                chat_history,
                user_state,
                gr.update(visible=True, value=""),
                gr.update(visible=False),
                gr.update(visible=False)
            )
        
        user_state["standardized_data"]['Negative Valence'] = negative_valence
        user_state["stage"] = "internal_external"

        user_state['options_text'] = generate_internal_external_options(
            chat_history,
            user_state["standardized_data"]["Vignette"],
            user_state
        )        
        
        response = (
            f"Please read the scenario again and use the slider below to answer the next few questions:\n"
            f"{user_state['vignette']}\n"
            "\nImagine this situation is happening to you personally. \nFrom 1 to 10, which of the following statements do you think is more likely (1 = Definitely Option A, 10 = Definitely Option B):\n"
            f"{user_state['options_text']}"
        )
        chat_history[-1] = (user_input, response)
        return (
            chat_history,
            user_state,
            gr.update(visible=False),
            gr.update(visible=True, value=1),
            gr.update(visible=True)
        )
        
    elif user_state["stage"] == "vignette_regeneration":
        additional_info = user_input
        user_state["user_responses"].append({
            "user_state['stage']": "vignette_regeneration",
            "feedback": additional_info
        })
    
        # Retrieve current topic choice
        topic_choice = user_state["standardized_data"].get("Topic Choice", user_state["topic_choice"])
        new_interest = user_state["standardized_data"].get("New Interest", None)
    
        # If topic_choice was "3" + new_interest exists, use that as the topic; else use topic_choice
        if topic_choice == "3" and new_interest:
            topic_choice_for_vignette = new_interest
        else:
            topic_choice_for_vignette = topic_choice
    
        # Regenerate the vignette, passing user feedback
        user_state["vignette"], user_state["tone"] = generate_custom_vignette(
            chat_history=chat_history,
            topic_choice=topic_choice_for_vignette,
            desired_tone=user_state["desired_tone"],
            user_state=user_state,
            feedback=additional_info
        )
    
        user_state["user_responses"].append({
            'user_state["stage"]': "vignette_regenerated",
            'content': user_state["vignette"],
            'tone': user_state["tone"]
        })
    
        # Temporarily store the new vignette and tone
        user_state["standardized_data"]['Vignette'] = user_state["vignette"]
        user_state["standardized_data"]['Tone'] = user_state["tone"]
    
        # Go back to usability assessment
        user_state["stage"] = "vignette_usability_assessment"
        response = (
            "Okay, let's try again. Please imagine you are witnessing the following scene:\n\n"
            f"{user_state['vignette']}\n\n"
            "Before we proceed: Have you personally experienced something similar to this scenario before? "
            "Please rate the similarity on a scale from 1 to 10 (1 = Not at all similar, 10 = Extremely similar)."
        )

        chat_history[-1] = (user_input, response)        
        return (
            chat_history,                 # Updated chat history
            user_state,                   # Updated user state
            gr.update(visible=True, value=""),  # Textbox
            gr.update(visible=False),     # Slider
            gr.update(visible=False)      # Confirm button
        )


    elif user_state["stage"] == "usability_feedback":
        print("Transitioning to usability_feedback stage.")
        #save_to_pickle(user_state["standardized_data"])
        #save_to_excel(user_state["standardized_data"])
        feedback_questions = [
            """Thank you for completing all the scenarios. We just have a few more questions before you leave. 
            
            How easy was it to complete the assessment? (1 = Not at all, 10 = Very much)""",
            "How clear were the instructions? (1 = Not at all, 10 = Very much)",
            "How engaging was the assessment experience? (1 = Not at all, 10 = Very much)",
            "How satisfied are you with this assessment? (1 = Not at all, 10 = Very much)",
            "How mentally demanding was the assessment? (1 = Not at all, 10 = Very much)",
            None
        ]
        
        if user_state["usability_question_counter"] < 5:
            # ----------------------------------------------------------
            # Handling numeric questions (1..5)
            try:
                numeric_response = int(user_input)
            except ValueError:
                response = "Please provide a number between 1 and 10."
                chat_history[-1] = (user_input, response)
                return (
                    chat_history,
                    user_state,
                    gr.update(visible=True, value=""),
                    gr.update(visible=False),
                    gr.update(visible=False)
                )
        
            if numeric_response not in range(1, 11):
                response = "Please provide a number between 1 and 10."
                chat_history[-1] = (user_input, response)
                return (
                    chat_history,
                    user_state,
                    gr.update(visible=True, value=""),
                    gr.update(visible=False),
                    gr.update(visible=False)
                )
        
            # Store numeric response
            question_key = [
                "Ease of Use",
                "Clarity of Instructions",
                "Engagement Level",
                "Satisfaction with Assessment",
                "Cognitive Load"
            ][user_state["usability_question_counter"]]
            user_state["usability_feedback"][question_key] = numeric_response
        
            user_state["usability_question_counter"] += 1
        
            # If we still have more numeric questions
            if user_state["usability_question_counter"] < 5:
                response = feedback_questions[user_state["usability_question_counter"]]
                chat_history[-1] = (user_input, response)
                return (
                    chat_history,
                    user_state,
                    gr.update(visible=True, value=""),
                    gr.update(visible=False),
                    gr.update(visible=False)
                )
        
            # else we are now at the Additional Comments question:
            # response = "Do you have any additional comments about your experience? (Type 'no' if none.)"
            response = "Lastly, do you have any additional comments or feedback regarding your experience? (Type 'no' if none.)"
            chat_history[-1] = (user_input, response)
            user_state["usability_question_counter"] = 5
            return (
                chat_history,
                user_state,
                gr.update(visible=True, value=""),  # Let them type text
                gr.update(visible=False),
                gr.update(visible=False)
            )
                
    
        # Check if we've finished all questions
        elif user_state["usability_question_counter"] == 5:
            user_state["usability_feedback"]["Additional Comments"] = user_input.strip()
            user_state["stage"] = "completed"
            save_to_excel({
                'Prolific ID': user_state["prolific_id"],
                'Age': user_state["standardized_data"].get("Age", None),
                'Profession': user_state["standardized_data"].get("Profession", None),
                'Interests': user_state["standardized_data"].get("Interests", None),
                'New Interest': user_state["standardized_data"].get("New Interest", None),
                'Topic Choice': user_state["standardized_data"].get("Topic Choice", None),
                'Vignette Number': user_state["standardized_data"].get("Vignette Number", None),
                'Vignette': user_state["standardized_data"].get("Vignette", None),
                'Tone': user_state["standardized_data"].get("Tone", None),
                'First Reveal': user_state["standardized_data"].get("First Reveal", None),
                'Raw Internal_External': user_state["standardized_data"].get("Raw Internal_External", None),
                'Initial Internal': user_state["standardized_data"].get("Initial Internal", None),
                'Initial External': user_state["standardized_data"].get("Initial External", None),
                'Raw Stable_Unstable': user_state["standardized_data"].get("Raw Stable_Unstable", None),
                'Initial Stable': user_state["standardized_data"].get("Initial Stable", None),
                'Initial Unstable': user_state["standardized_data"].get("Initial Unstable", None),
                'Raw Global_Specific': user_state["standardized_data"].get("Raw Global_Specific", None),
                'Initial Global': user_state["standardized_data"].get("Initial Global", None),
                'Initial Specific': user_state["standardized_data"].get("Initial Specific", None),
                'Raw Reassessment': user_state["standardized_data"].get("Raw Reassessment", None),
                'Reassessment Internal': user_state["standardized_data"].get("Reassessment Internal", None),
                'Reassessment External': user_state["standardized_data"].get("Reassessment External", None),
                'Initial Relevance Rating': user_state["standardized_data"].get("Initial Relevance Rating", None),
                'Final Relevance Rating': user_state["standardized_data"].get("Final Relevance Rating", None),
                'Emotional Relevance': user_state["standardized_data"].get("Emotional Relevance", None),
                'Positive Valence': user_state["standardized_data"].get("Positive Valence", None),
                'Negative Valence': user_state["standardized_data"].get("Negative Valence", None),
                'Likelihood of Change': user_state["standardized_data"].get("Likelihood of Change", None),
                "Ease of Use": user_state["standardized_data"].get("Ease of Use", None),
                "Clarity of Instructions": user_state["standardized_data"].get("Clarity of Instructions", None),
                "Engagement Level": user_state["standardized_data"].get("Engagement Level", None),
                "Satisfaction with Assessment": user_state["standardized_data"].get("Satisfaction with Assessment", None),
                "Cognitive Load": user_state["standardized_data"].get("Cognitive Load", None),
                "Additional Comments": user_state["standardized_data"].get("Additional Comments", None),
                'PHQ-9 Score': user_state["phq_score"],
                'GAD-7 Score': user_state["gad_score"],
                "Attention Check 1": user_state.get("Attention Check 1", None),
                "Attention Check 2": user_state.get("Attention Check 2", None)
            }, user_state)
            
            response = "Thank you for your feedback! Your responses have been saved. Please use the following link to submit your results: https://app.prolific.com/submissions/complete?cc=C1ECU6HA"
            chat_history[-1] = (user_input, response)        
            return (
                chat_history,                 # Updated chat history
                user_state,                   # Updated user state
                gr.update(visible=True, value=""),  # Textbox
                gr.update(visible=False),     # Slider
                gr.update(visible=False)      # Confirm button
            )  
            
    elif user_state["stage"] == "completed":
            response = "Thank you for your feedback! Your responses have been saved. Please use the following link to submit your results: https://app.prolific.com/submissions/complete?cc=C1ECU6HA"
            chat_history[-1] = (user_input, response)        
            return (
                chat_history,                 # Updated chat history
                user_state,                   # Updated user state
                gr.update(visible=True, value=""),  # Textbox
                gr.update(visible=False),     # Slider
                gr.update(visible=False)      # Confirm button
            )            
    
    else:
        print(f"Unexpected user_state or input: stage={user_state['stage']}, message={user_input}")
        response = "An unexpected error occurred. Please try again or contact support."
        chat_history[-1] = (user_input, response)        
        return (
            chat_history,                 # Updated chat history
            user_state,                   # Updated user state
            gr.update(visible=True, value=""),  # Textbox
            gr.update(visible=False),     # Slider
            gr.update(visible=False)      # Confirm button
        )

##################################################################################
# Sliders
##################################################################################

def slider_input(chat_history, user_state, attribution_slider, *args, **kwargs):
    if 1 <= attribution_slider <= 10:
        chat_history.append((str(attribution_slider), ""))    
    
    if attribution_slider < 1 or attribution_slider > 10:
        response = "Please pick a number between 1 and 10."
        chat_history[-1] = (str(attribution_slider), response)
        return (
            chat_history,                # Updated chat history
            user_state,                  # Updated user state
            gr.update(visible=False),    # Hide the textbox
            gr.update(visible=True, value=1),     # Show the slider
            gr.update(visible=True)      # Show the Confirm button
        )
    
    # For internal/external
    if user_state["stage"] == "internal_external":
        user_state["standardized_data"]['Raw Internal_External'] = attribution_slider
        # 1–5 => '1', 6–10 => '2'
        user_state["explanation_response"] = '1' if attribution_slider <= 5 else '2'

        # Store numeric
        if user_state["explanation_response"] == '1':
            user_state["standardized_data"]['Initial Internal'] = 1
            user_state["standardized_data"]['Initial External'] = 0
        else:
            user_state["standardized_data"]['Initial Internal'] = 0
            user_state["standardized_data"]['Initial External'] = 1

        # Next stage => stable_unstable
        user_state["stage"] = "stable_unstable"
        # Generate stable/unstable text
        user_state['options_text'] = generate_stable_unstable_options(
            user_state["standardized_data"]["Vignette"], user_state["explanation_response"], user_state
        )
        # global_specific_output = generate_global_specific_options(
        #     user_state["standardized_data"]["Vignette"], user_state["explanation_response"], user_state
        # )
        
        response = (
            "Now, from 1 to 10, which do you think is more likely? (1 = Definitely Option A, 10 = Definitely Option B)\n\n"
            f"{user_state['options_text']}"
        )
        chat_history[-1] = (str(attribution_slider), response)
        # Return the chat + user_state, and keep the slider visible
        return (
            chat_history,                
            user_state,                  
            gr.update(visible=False),    # Hide the textbox
            gr.update(visible=True, value=1),     # Show the slider
            gr.update(visible=True)      # Show the Confirm button
        )

    elif user_state["stage"] == "stable_unstable":
        user_state["standardized_data"]['Raw Stable_Unstable'] = attribution_slider
        user_state["explanation_stable_unstable_response"] = '1' if attribution_slider <= 5 else '2'
        if user_state["explanation_stable_unstable_response"] == '1':
            user_state["standardized_data"]['Initial Stable'] = 1
            user_state["standardized_data"]['Initial Unstable'] = 0
        else:
            user_state["standardized_data"]['Initial Stable'] = 0
            user_state["standardized_data"]['Initial Unstable'] = 1
        
        # Move on to global_specific
        user_state["stage"] = "global_specific"
        
        print("explanation_response value passed to global_specific stage:")
        print(user_state["explanation_response"])
        
        user_state['options_text'] = generate_global_specific_options(
            user_state["standardized_data"]["Vignette"], user_state["explanation_response"], user_state
        )
        
        response = (
            "Next, from 1 to 10, which do you think is more likely? (1 = Definitely Option A, 10 = Definitely Option B)\n\n"
            f"{user_state['options_text']}"
        )
        chat_history[-1] = (str(attribution_slider), response)
        return (
            chat_history,                
            user_state,                  
            gr.update(visible=False),    # Hide the textbox
            gr.update(visible=True, value=1),     # Show the slider
            gr.update(visible=True)      # Show the Confirm button
        )

    elif user_state["stage"] == "global_specific":
        user_state["standardized_data"]['Raw Global_Specific'] = attribution_slider
        user_state["explanation_global_specific_response"] = '1' if attribution_slider <= 5 else '2'
        if user_state["explanation_global_specific_response"] == '1':
            user_state["standardized_data"]['Initial Global'] = 1
            user_state["standardized_data"]['Initial Specific'] = 0
        else:
            user_state["standardized_data"]['Initial Global'] = 0
            user_state["standardized_data"]['Initial Specific'] = 1
        
        # Move to next stage => internal_external_reassessment
        user_state["stage"] = "internal_external_reassessment"
        user_state["first_reveal"] = generate_first_reveal(
            user_state["vignette"], user_state
        )
        user_state['options_text'] = generate_internal_external_options(
            chat_history,
            user_state["standardized_data"]["Vignette"] + " " + user_state["first_reveal"],
            user_state
        )
        response = (
            f"Alright, now please imagine:\n\n{user_state['first_reveal']}\n\n"
            "On a scale from 1 to 10, which is more likely? (1 = Definitely Option A, 10 = Definitely Option B)\n"
            f"{user_state['options_text']}"
        )
        chat_history[-1] = (str(attribution_slider), response)
        return (
            chat_history,                
            user_state,                  
            gr.update(visible=False),    # Hide the textbox
            gr.update(visible=True, value=1),     # Show the slider
            gr.update(visible=True)      # Show the Confirm button
            )
    
    elif user_state["stage"] == "internal_external_reassessment":
        # store Reassessment
        user_state["standardized_data"]['Raw Reassessment'] = attribution_slider
        user_state["assessment_internal_external_response"] = '1' if attribution_slider <= 5 else '2'
        if user_state["assessment_internal_external_response"] == '1':
            user_state["standardized_data"]['Reassessment Internal'] = 1
            user_state["standardized_data"]['Reassessment External'] = 0
        else:
            user_state["standardized_data"]['Reassessment Internal'] = 0
            user_state["standardized_data"]['Reassessment External'] = 1
    
        # Next vignette
        user_state["vignette_counter"] += 1
    
        # Attention check 1 at vignette 3
        if user_state["vignette_counter"] == 3 and user_state["Attention Check 1"] is None:
            user_state["stage"] = "Attention Check 1"
            response = (
                "ATTENTION CHECK: \n\n"
                "Please set the slider to 10 (option on the very right) to show you're paying attention."
            )
            chat_history[-1] = (str(attribution_slider), response)
            return (
                chat_history,
                user_state,
                gr.update(visible=False),
                gr.update(visible=True, value=1), 
                gr.update(visible=True)           # show confirm button
            )
    
        # Attention check 2 at vignette 9
        if user_state["vignette_counter"] == 9 and user_state["Attention Check 2"] is None:
            user_state["stage"] = "Attention Check 2"
            response = (
                "ATTENTION CHECK: \n\n"
                "Once again, please set the slider to 10 (option on the very right) to confirm you're paying attention."
            )
            chat_history[-1] = (str(attribution_slider), response)
            return (
                chat_history,
                user_state,
                gr.update(visible=False),
                gr.update(visible=True, value=1),
                gr.update(visible=True)
            )
        
        # If all vignettes are complete, transition to usability feedback
        if user_state["vignette_counter"] > 12:
            print("All vignettes completed. Moving to usability feedback.")
           # save_to_pickle(user_state["standardized_data"])
            save_to_excel({
                'Prolific ID': user_state["prolific_id"],
                'Age': user_state["standardized_data"].get("Age", None),
                'Profession': user_state["standardized_data"].get("Profession", None),
                'Interests': user_state["standardized_data"].get("Interests", None),
                'New Interest': user_state["standardized_data"].get("New Interest", None),
                'Topic Choice': user_state["standardized_data"].get("Topic Choice", None),
                'Vignette Number': user_state["standardized_data"].get("Vignette Number", None),
                'Vignette': user_state["standardized_data"].get("Vignette", None),
                'Tone': user_state["standardized_data"].get("Tone", None),
                'First Reveal': user_state["standardized_data"].get("First Reveal", None),
                'Raw Internal_External': user_state["standardized_data"].get("Raw Internal_External", None),
                'Initial Internal': user_state["standardized_data"].get("Initial Internal", None),
                'Initial External': user_state["standardized_data"].get("Initial External", None),
                'Raw Stable_Unstable': user_state["standardized_data"].get("Raw Stable_Unstable", None),
                'Initial Stable': user_state["standardized_data"].get("Initial Stable", None),
                'Initial Unstable': user_state["standardized_data"].get("Initial Unstable", None),
                'Raw Global_Specific': user_state["standardized_data"].get("Raw Global_Specific", None),
                'Initial Global': user_state["standardized_data"].get("Initial Global", None),
                'Initial Specific': user_state["standardized_data"].get("Initial Specific", None),
                'Raw Reassessment': user_state["standardized_data"].get("Raw Reassessment", None),
                'Reassessment Internal': user_state["standardized_data"].get("Reassessment Internal", None),
                'Reassessment External': user_state["standardized_data"].get("Reassessment External", None),
                'Initial Relevance Rating': user_state["standardized_data"].get("Initial Relevance Rating", None),
                'Final Relevance Rating': user_state["standardized_data"].get("Final Relevance Rating", None),
                'Emotional Relevance': user_state["standardized_data"].get("Emotional Relevance", None),
                'Positive Valence': user_state["standardized_data"].get("Positive Valence", None),
                'Negative Valence': user_state["standardized_data"].get("Negative Valence", None),
                'Likelihood of Change': user_state["standardized_data"].get("Likelihood of Change", None),
                "Ease of Use": user_state["standardized_data"].get("Ease of Use", None),
                "Clarity of Instructions": user_state["standardized_data"].get("Clarity of Instructions", None),
                "Engagement Level": user_state["standardized_data"].get("Engagement Level", None),
                "Satisfaction with Assessment": user_state["standardized_data"].get("Satisfaction with Assessment", None),
                "Cognitive Load": user_state["standardized_data"].get("Cognitive Load", None),
                "Additional Comments": user_state["standardized_data"].get("Additional Comments", None),
                'PHQ-9 Score': user_state["phq_score"],
                'GAD-7 Score': user_state["gad_score"],
                "Attention Check 1": user_state.get("Attention Check 1", None),
                "Attention Check 2": user_state.get("Attention Check 2", None)                
            }, user_state)
            user_state["stage"] = "usability_feedback"
            user_state["usability_question_counter"] = 0
            response = "Thank you for completing all the scenarios. We just have a few more questions before you leave.\n \n How easy was it to complete the assessment? (1 = Not at all, 10 = Very much)"
            chat_history[-1] = (str(attribution_slider), response)        
            return (
                chat_history,                 
                user_state,                   
                gr.update(visible=True, value=""),  # Textbox
                gr.update(visible=False),     # Slider
                gr.update(visible=False)      # Confirm button
            )

        else:
            # Save data
            user_state["stage"] = "topic_choice"
            # save_to_pickle(user_state["standardized_data"])
            save_to_excel({
                'Prolific ID': user_state["prolific_id"],
                'Age': user_state["standardized_data"].get("Age", None),
                'Profession': user_state["standardized_data"].get("Profession", None),
                'Interests': user_state["standardized_data"].get("Interests", None),
                'New Interest': user_state["standardized_data"].get("New Interest", None),
                'Topic Choice': user_state["standardized_data"].get("Topic Choice", None),
                'Vignette Number': user_state["standardized_data"].get("Vignette Number", None),
                'Vignette': user_state["standardized_data"].get("Vignette", None),
                'Tone': user_state["standardized_data"].get("Tone", None),
                'First Reveal': user_state["standardized_data"].get("First Reveal", None),
                'Raw Internal_External': user_state["standardized_data"].get("Raw Internal_External", None),
                'Initial Internal': user_state["standardized_data"].get("Initial Internal", None),
                'Initial External': user_state["standardized_data"].get("Initial External", None),
                'Raw Stable_Unstable': user_state["standardized_data"].get("Raw Stable_Unstable", None),
                'Initial Stable': user_state["standardized_data"].get("Initial Stable", None),
                'Initial Unstable': user_state["standardized_data"].get("Initial Unstable", None),
                'Raw Global_Specific': user_state["standardized_data"].get("Raw Global_Specific", None),
                'Initial Global': user_state["standardized_data"].get("Initial Global", None),
                'Initial Specific': user_state["standardized_data"].get("Initial Specific", None),
                'Raw Reassessment': user_state["standardized_data"].get("Raw Reassessment", None),
                'Reassessment Internal': user_state["standardized_data"].get("Reassessment Internal", None),
                'Reassessment External': user_state["standardized_data"].get("Reassessment External", None),
                'Initial Relevance Rating': user_state["standardized_data"].get("Initial Relevance Rating", None),
                'Final Relevance Rating': user_state["standardized_data"].get("Final Relevance Rating", None),
                'Emotional Relevance': user_state["standardized_data"].get("Emotional Relevance", None),
                'Positive Valence': user_state["standardized_data"].get("Positive Valence", None),
                'Negative Valence': user_state["standardized_data"].get("Negative Valence", None),
                'Likelihood of Change': user_state["standardized_data"].get("Likelihood of Change", None),
                "Ease of Use": user_state["standardized_data"].get("Ease of Use", None),
                "Clarity of Instructions": user_state["standardized_data"].get("Clarity of Instructions", None),
                "Engagement Level": user_state["standardized_data"].get("Engagement Level", None),
                "Satisfaction with Assessment": user_state["standardized_data"].get("Satisfaction with Assessment", None),
                "Cognitive Load": user_state["standardized_data"].get("Cognitive Load", None),
                "Additional Comments": user_state["standardized_data"].get("Additional Comments", None),
                'PHQ-9 Score': user_state["phq_score"],
                'GAD-7 Score': user_state["gad_score"],
                "Attention Check 1": user_state.get("Attention Check 1", None),
                "Attention Check 2": user_state.get("Attention Check 2", None)                
            }, user_state)
            response = f"Thank you! We are now finished with scene {user_state['vignette_counter'] - 1}. In total you will be seeing 12 scenes.\n\nWhat would you like for your next round?\n1. A situation related to your profession.\n2. A situation related to your personal interests.\n3. Something else about me. (Add another activity or interest that is important to you by saying 'I also like...')"
            chat_history[-1] = (str(attribution_slider), response)        
            return (
                chat_history,                 
                user_state,                   
                gr.update(visible=True, value=""),  # Textbox
                gr.update(visible=False),     # Slider
                gr.update(visible=False)      # Confirm button
            )        
        
    elif user_state["stage"] in ["Attention Check 1", "Attention Check 2"]:
        # Determine which attention check is being handled).
        check_id = user_state["stage"][-1]  # Either '1' or '2'
        
        # Evaluate pass/fail based on slider
        if attribution_slider == 10:
            user_state[f"Attention Check {check_id}"] = True
        else:
            user_state[f"Attention Check {check_id}"] = False
        
        # Advance to the next stage or vignette
        user_state["stage"] = "topic_choice"
        
        # Save partial data
        save_to_excel({
            'Prolific ID': user_state["prolific_id"],
            'Age': user_state["standardized_data"].get("Age", None),
            'Profession': user_state["standardized_data"].get("Profession", None),
            'Interests': user_state["standardized_data"].get("Interests", None),
            'New Interest': user_state["standardized_data"].get("New Interest", None),
            'Topic Choice': user_state["standardized_data"].get("Topic Choice", None),
            'Vignette Number': user_state["standardized_data"].get("Vignette Number", None),
            'Vignette': user_state["standardized_data"].get("Vignette", None),
            'Tone': user_state["standardized_data"].get("Tone", None),
            'First Reveal': user_state["standardized_data"].get("First Reveal", None),
            'Raw Internal_External': user_state["standardized_data"].get("Raw Internal_External", None),
            'Initial Internal': user_state["standardized_data"].get("Initial Internal", None),
            'Initial External': user_state["standardized_data"].get("Initial External", None),
            'Raw Stable_Unstable': user_state["standardized_data"].get("Raw Stable_Unstable", None),
            'Initial Stable': user_state["standardized_data"].get("Initial Stable", None),
            'Initial Unstable': user_state["standardized_data"].get("Initial Unstable", None),
            'Raw Global_Specific': user_state["standardized_data"].get("Raw Global_Specific", None),
            'Initial Global': user_state["standardized_data"].get("Initial Global", None),
            'Initial Specific': user_state["standardized_data"].get("Initial Specific", None),
            'Raw Reassessment': user_state["standardized_data"].get("Raw Reassessment", None),
            'Reassessment Internal': user_state["standardized_data"].get("Reassessment Internal", None),
            'Reassessment External': user_state["standardized_data"].get("Reassessment External", None),
            'Initial Relevance Rating': user_state["standardized_data"].get("Initial Relevance Rating", None),
            'Final Relevance Rating': user_state["standardized_data"].get("Final Relevance Rating", None),
            'Emotional Relevance': user_state["standardized_data"].get("Emotional Relevance", None),
            'Positive Valence': user_state["standardized_data"].get("Positive Valence", None),
            'Negative Valence': user_state["standardized_data"].get("Negative Valence", None),
            'Likelihood of Change': user_state["standardized_data"].get("Likelihood of Change", None),
            "Ease of Use": user_state["standardized_data"].get("Ease of Use", None),
            "Clarity of Instructions": user_state["standardized_data"].get("Clarity of Instructions", None),
            "Engagement Level": user_state["standardized_data"].get("Engagement Level", None),
            "Satisfaction with Assessment": user_state["standardized_data"].get("Satisfaction with Assessment", None),
            "Cognitive Load": user_state["standardized_data"].get("Cognitive Load", None),
            "Additional Comments": user_state["standardized_data"].get("Additional Comments", None),
            'PHQ-9 Score': user_state["phq_score"],
            'GAD-7 Score': user_state["gad_score"],
            "Attention Check 1": user_state.get("Attention Check 1", None),
            "Attention Check 2": user_state.get("Attention Check 2", None)                
        }, user_state)

        response = f"Thank you! We are now finished with scene {user_state['vignette_counter'] - 1}. In total you will be seeing 12 scenes.\n\nWhat would you like for your next round?\n1. A situation related to your profession.\n2. A situation related to your personal interests.\n3. Something else about me. (Add another activity or interest that is important to you by saying 'I also like...')"
        chat_history[-1] = (str(attribution_slider), response)
        
        return (
            chat_history,
            user_state,
            gr.update(visible=True, value=""),  # Textbox
            gr.update(visible=False),           # Slider
            gr.update(visible=False)            # Confirm button
        )
            
    else:  
        print(f"Unexpected user_state or input: stage={user_state['stage']}, message={attribution_slider}")
        response = "An unexpected error occurred. Please try again or contact support."
        chat_history[-1] = (str(attribution_slider), response)        
        return (
            chat_history,                 
            user_state,                   
            gr.update(visible=True, value=""),  # Textbox
            gr.update(visible=False),     # Slider
            gr.update(visible=False)      # Confirm button
        )





### Gradio app #############################################################

css = """
.custom-slider input[type=range] {
    accent-color: #1a73e8; /* Change this to your desired color */
}
"""

with gr.Blocks(theme= gr.themes.Glass(), css=css) as app:
    ########################################################################
    # Page 1: Briefing & Informed Consent
    ########################################################################

    user_state = gr.State(get_default_app_state())
    
    with gr.Row(visible=True) as page_1: 
        with gr.Column():
            gr.Markdown("""
            # **Information for Participants and Declaration of Consent**  
            ## **Evaluating the Validity of LLM-Based Psychological Assessments**
            
            ---
            
            ## **Dear Participant,**  
            ### We invite you to participate in the study described above. Your participation is entirely voluntary. You may refuse to participate or withdraw at any time without needing to provide a reason, and doing so will have no negative consequences for you.  
            
            ###  This study is designed to contribute to academic research by exploring a novel type of psychological assessment using advanced AI technology.  
            
            ###  Please read the following information carefully before deciding whether to participate. You may proceed only after acknowledging that you understand the study and your rights as a participant.  
            
            ---
            
            ## **Purpose of the Study**  
            ### This study aims to evaluate whether an AI-based psychological assessment can provide insights into cognitive patterns comparable to traditional methods.
            
            ---
            
            ## **Study Procedure**  
            ### - The study consists of three online sessions. Each session lasts approximately 20–30 minutes. The assessment will be conducted over the course of one week with three days in between each session. You must complete all three sessions to receive full compensation.  
            ### - You will reflect on various scenarios and provide feedback on your experience.  
            
            ---
            
            ## **Potential Benefits**  
            ### While there are no direct personal benefits, your participation will contribute to research that could help improve psychological diagnostic tools using AI.  
            
            ---
            
            ## **Potential Risks**  
            ### - The study presents minimal risks.  
            ### - Some scenarios may ask you to reflect on personal responsibility, which could cause minor emotional discomfort.  
            ### - If you feel uncomfortable, you can withdraw at any time.  
            
            ## **Eligibility Criteria:**  
            ### - Participants must be 18 or older, fluent in English, and score below the clinical threshold on pre-screening mental health questionnaires.  
            ### - Participants with diagnosed mental health disorders or high scores on these questionnaires are excluded.  
            
            ---
            
            ## **Data Handling and Privacy**  
            ### - Your data will be anonymised and used only for statistical purposes.  
            ### - No personal identifying information will be collected.  
            ### - The data will be securely stored and accessed only by authorised study personnel.
            
            ---
            
            ## **Reimbursement**  
            ### - You will not incur any costs for participating.  
            ### - Compensation will be provided through Prolific based on the time spent participating.
            ### - You will receive a partial completion code after each session, but full payment will only be processed upon completing all three sessions.

            
            ---
            
            ## **Withdrawal and Contact Information**  
            ### - You may withdraw at any time without providing a reason.  
            ### - If you experience any discomfort or have questions about the study, please contact the coordinators:  
            ####  - **Nace Mikus**: nace.mikus@univie.ac.at  
            ####  - **Jakob Möller**: a12028569@unet.univie.ac.at  
            
            ---
            
            ## **Acknowledgment**  
            ### By clicking "I Acknowledge and Consent" you confirm that:  
            ### - You have read and understood the information provided.  
            ### - You are aware of your right to withdraw at any time without consequences.  
            ### - You agree to participate in the study and for your anonymised data to be used for academic research.

            ---

            ### The study has been concluded but remains open for testing. No ID needs to be entered.
            """)

            pid_input = gr.Textbox(label="Prolific ID")
            pid_output = gr.Markdown()
            
            # Consent by pressing the button
            briefing_start_button = gr.Button("I Acknowledge and Consent")
    
    ########################################################################
    # Page 2: Pretest: PHQ-9
    ########################################################################

    with gr.Row(visible=False) as page_2:
        with gr.Column():
            # PHQ-9           
            gr.Markdown("""#### Over the last 2 weeks, how often have you been bothered by 
            #### any of the following problems (0 = "Never", 3 = "Very often")?""")
            
            q1 = gr.Radio(choices=["0", "1", "2", "3"], label="Little interest or pleasure in doing things?", value = "0")
            q2 = gr.Radio(choices=["0", "1", "2", "3"], label="Feeling down, depressed, or hopeless?", value = "0")
            q3 = gr.Radio(choices=["0", "1", "2", "3"], label="Trouble falling or staying asleep, or sleeping too much?", value = "0")
            q4 = gr.Radio(choices=["0", "1", "2", "3"], label="Feeling tired or having little energy?", value = "0")
            q5 = gr.Radio(choices=["0", "1", "2", "3"], label="Poor appetite or overeating?", value = "0")
            q6 = gr.Radio(choices=["0", "1", "2", "3"], label="Feeling bad about yourself?", value = "0")
            q7 = gr.Radio(choices=["0", "1", "2", "3"], label="Trouble concentrating?", value = "0")
            q8 = gr.Radio(choices=["0", "1", "2", "3"], label="Moving or speaking so slowly people noticed?", value = "0")
            # q9 = gr.Radio(choices=["0", "1", "2", "3"], label="Thoughts that you would be better off dead?", value = "0")
            phq_result = gr.Markdown()
            next_button = gr.Button("Next")
    
    ########################################################################
    # Page 3: Pretest: GAD-7
    ########################################################################

    with gr.Row(visible=False) as page_3:
        with gr.Column():
            gr.Markdown(""" 
            #### Over the last 2 weeks, how often have you been bothered by
            #### any of the following problems (0 = "Never", 3 = "Very often")?
            """)
            g1 = gr.Radio(choices=["0", "1", "2", "3"], label="Feeling nervous, anxious, or on edge?", value = "0")
            g2 = gr.Radio(choices=["0", "1", "2", "3"], label="Not being able to stop or control worrying?", value = "0")
            g3 = gr.Radio(choices=["0", "1", "2", "3"], label="Worrying too much about different things?", value = "0")
            g4 = gr.Radio(choices=["0", "1", "2", "3"], label="Trouble relaxing?", value = "0")
            g5 = gr.Radio(choices=["0", "1", "2", "3"], label="Being so restless that it’s hard to sit still?", value = "0")
            g6 = gr.Radio(choices=["0", "1", "2", "3"], label="Becoming easily annoyed or irritable?", value = "0")
            g7 = gr.Radio(choices=["0", "1", "2", "3"], label="Feeling afraid as if something awful might happen?", value = "0")
            gad_result = gr.HTML()
            finish_button = gr.Button("Finish")
    
    #######################################################################
    # Page 4: User Profile
    #######################################################################

    with gr.Row(visible=False) as page_4:
        with gr.Column():
            gr.Markdown("# Tell Us About Yourself")
            gr.Markdown("""### Great! During the course of this assessment you will be confronted with descriptions of fictional everyday situations. Please try to immerse yourself in these scenarios as if they were happening to you personally.
        
        ### In order to provide you with situations that might be more relevant to you specifically, 
        ### we need some general information about you. Don't worry, you will remain anonymous!
        
        ### Please tell us your age, profession and one of your personal interests.""")
            age_input = gr.Textbox(label="Age", placeholder="Enter your age (e.g., 25)", lines=1)
            profession_input = gr.Textbox(label="Profession", placeholder="Enter your profession (e.g., Engineer)", lines=1)
            interest_input = gr.Textbox(label="One personal interest", placeholder="Enter a hobby or interest (e.g., Painting)", lines=1)
        
            user_profile_button = gr.Button("Submit Profile")
            user_profile_output = gr.Markdown()
    
    #######################################################################
    # Page 5: Chatbot Assessment
    #######################################################################

    with gr.Row(visible=False) as page_5:
        with gr.Column():
            # Existing chatbot code
            gr.Markdown("""### Engage with the chatbot by typing your messages below and submitting with **Enter**. Don't worry, this chatbot does not have live access to the internet. Your data will only be processed locally.""")
            chatbot = gr.Chatbot(value = [(None, "Hello! I'm Evaline, I will guide you through a total of 12 scenarios during this assessment. This will approximately take 20-30 minutes. Are you ready to get started?")], label= "Evaline")
            user_message = gr.Textbox(placeholder="Type your message here...", label= "Your Message", lines=1)
    
            # Slider for Attributions (Initially Hidden)
            attribution_slider = gr.Slider(
                minimum=1,
                maximum=10,
                step=0.1,
                label="1 = Definitely Option A,                                 10 = Definitely Option B",
                visible=False,
                interactive=True,
                elem_classes=["custom-slider"]  # Apply custom CSS class
            )
            
            # Confirm Button for Slider (Initially Hidden)
            confirm_button = gr.Button("Confirm", visible=False)
            
            # Submit handler for text input
            user_message.submit(
                fn=new_chat_input,
                inputs=[chatbot, user_state, user_message],
                outputs=[chatbot, user_state, user_message, attribution_slider, confirm_button],
                queue=False
            )
         
            # Confirm handler for slider
            confirm_button.click(
                fn=slider_input,
                inputs=[chatbot, user_state, attribution_slider],
                outputs=[chatbot, user_state, user_message, attribution_slider, confirm_button],
                queue=False
            )
    
    ######################################################################
    # Transition Logic
    ######################################################################
    
    # 1) After Briefing, go to PHQ-9
    def store_pid_and_show_phq9(pid, user_state):
        # 1) Validate the PID
        pid = pid.strip()
        # if not pid:
        #     return (
        #         "Error: Please enter your Prolific ID.",
        #         gr.update(visible=True),   # keep page_1 visible
        #         gr.update(visible=False),  # keep page_2 hidden
        #         user_state
        #     )
        
        # 2) Store the PID and create unique file name
        user_state["prolific_id"] = pid
        
        timestamp = time.strftime("%Y%m%d_%H%M%S")
        user_state["excel_file"] = f"Cond1_{pid}_{timestamp}.xlsx"  
        
        # 3) Hide page_1, show page_2
        return (
            f"Prolific ID {pid} recorded, now proceed to PHQ-9.",
            gr.update(visible=False),  # hide page_1
            gr.update(visible=True),   # show page_2
            user_state
        )
    
    briefing_start_button.click(
        fn=store_pid_and_show_phq9,
        inputs=[pid_input, user_state],
        outputs=[
            pid_output,
            page_1,    # hide page_1 (Briefing)
            page_2,    # show page_2 (PHQ-9)
            user_state]
    )
    
    # 2) After finishing PHQ-9, go to GAD-7
    def process_phq9_and_show_gad7(q1, q2, q3, q4, q5, q6, q7, q8, user_state):
        q1 = int(q1) if q1 is not None else 0
        q2 = int(q2) if q2 is not None else 0
        q3 = int(q3) if q3 is not None else 0
        q4 = int(q4) if q4 is not None else 0
        q5 = int(q5) if q5 is not None else 0
        q6 = int(q6) if q6 is not None else 0
        q7 = int(q7) if q7 is not None else 0
        q8 = int(q8) if q8 is not None else 0
       # q9 = int(q9) if q9 is not None else 0        
        
        # Store ID & compute PHQ-9
        user_state["phq_score"] = q1 + q2 + q3 + q4 + q5 + q6 + q7 + q8
    
        # On success, hide Page 2 & show Page 3
        return (
            f"PHQ-9 total = {user_state['phq_score']}. Proceed to GAD-7.",
            gr.update(visible=False),  # Hide page_2
            gr.update(visible=True),   # Show page_3
            user_state
        )
    
    next_button.click(
        fn=process_phq9_and_show_gad7,
        inputs=[q1, q2, q3, q4, q5, q6, q7, q8, user_state],
        outputs=[
            phq_result, 
            page_2,    # hide page_2 (PHQ-9)
            page_3,    # show page_3 (GAD-7)    
            user_state 
        ]
    )
    
    # 3) After finishing GAD-7, go to User Profile
    def process_gad7_and_show_user_profile(g1, g2, g3, g4, g5, g6, g7, user_state):
        # Ensure all GAD-7 inputs are integers
        g1 = int(g1) if g1 is not None else 0
        g2 = int(g2) if g2 is not None else 0
        g3 = int(g3) if g3 is not None else 0
        g4 = int(g4) if g4 is not None else 0
        g5 = int(g5) if g5 is not None else 0
        g6 = int(g6) if g6 is not None else 0
        g7 = int(g7) if g7 is not None else 0
    
        # Calculate combined score
        user_state["gad_score"] = g1 + g2 + g3 + g4 + g5 + g6 + g7
        combined_score = user_state["phq_score"] + user_state["gad_score"]
    
        if combined_score > 25:
            rejection_link = "https://app.prolific.com/submissions/complete?cc=C11ALNAE"
            rejection_message = (
                f"Unfortunately, your combined score indicates that you do not meet the inclusion criteria for this study. "
                f"Please complete your submission on Prolific using the following link: {rejection_link}"
            )
            return (
                rejection_message,
                gr.update(visible=True),   # page_3
                gr.update(visible=False),  # page_4
                user_state,
            )
    
        user_state["eligible"] = True
    
        return (
            f"GAD-7 completed, total = {user_state['gad_score']}. Proceeding to user profile.",
            gr.update(visible=False),  # Hide page_3 (GAD-7)
            gr.update(visible=True),   # Show page_4 (User Profile)
            user_state,)
    
    finish_button.click(
        fn=process_gad7_and_show_user_profile,  # Updated function name
        inputs=[g1, g2, g3, g4, g5, g6, g7, user_state],
        outputs=[
            gad_result, 
            page_3,        # hide page_3 (GAD-7)
            page_4,       # show page_4 (User Profile)
            user_state]
    )
    
    # 4) After User Profile, go to Chatbot   
    def process_user_profile(age, profession, interest, user_state):
        
        # 1) Validate user input (check required fields)
        if not age.strip():
            return "Error: Please enter your age.", gr.update(visible=True), gr.update(visible=False), user_state
        if not profession.strip():
            return "Error: Please enter your profession.", gr.update(visible=True), gr.update(visible=False), user_state
        if not interest.strip():
            return "Error: Please enter a personal interest.", gr.update(visible=True), gr.update(visible=False), user_state
    
        # 2) Store in user_state
        user_state["standardized_data"]["Raw Age"] = age.strip()
        user_state["standardized_data"]["Raw Profession"] = profession.strip()
        user_state["standardized_data"]["Raw Interests"] = interest.strip()
        user_state["standardized_data"]["Age"] = age.strip()
        user_state["standardized_data"]["Profession"] = profession.strip()
        user_state["standardized_data"]["Interests"] = interest.strip()
        
        print(f"user_state at the end of show_chatbot_after_profile: {user_state}")
    
        return (
            "Profile submitted successfully. Proceeding to the chatbot.",
            gr.update(visible=False),  # Hide page_4 (User Profile)
            gr.update(visible=True),   # Show page_5 (Chatbot)
            user_state
        )
                
    user_profile_button.click(
        fn=process_user_profile,
        inputs=[age_input, profession_input, interest_input, user_state],
        outputs=[
            user_profile_output,
            page_4,     # hide page_4 (User Profile)
            page_5,      # show page_5 (Chatbot)
            user_state]
    )
            
app.launch()