File size: 65,897 Bytes
c6fc32c
 
 
 
86cf81a
bdb3c28
c6fc32c
 
 
 
 
 
26217ec
c6fc32c
bdb3c28
c6fc32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70cc7b0
 
 
 
 
 
 
 
c6fc32c
 
 
 
 
8e1caa1
c6fc32c
 
 
 
71830dc
c6fc32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17ebb8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6fc32c
8ebff9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c574b13
 
8ebff9f
 
 
c7d38be
60fd07f
c7d38be
 
 
 
60fd07f
c7d38be
 
 
60fd07f
c7d38be
 
 
 
 
 
 
 
 
60fd07f
c7d38be
 
 
 
 
 
60fd07f
c7d38be
bdb3c28
 
 
 
26217ec
bdb3c28
 
26217ec
bdb3c28
 
 
 
 
 
 
 
 
 
 
 
 
c7d38be
bdb3c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7d38be
 
 
60fd07f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71830dc
60fd07f
 
 
 
924f5e7
60fd07f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7d38be
60fd07f
c7d38be
 
 
60fd07f
c7d38be
c6fc32c
 
 
 
 
 
71830dc
c6fc32c
 
 
 
71830dc
c6fc32c
 
 
 
 
 
 
 
 
 
71830dc
c6fc32c
5dc5cb8
c6fc32c
86cf81a
 
 
 
71830dc
86cf81a
 
 
bdb3c28
2e99fbf
 
26217ec
bdb3c28
 
 
 
26217ec
bdb3c28
 
 
8bec8f1
bdb3c28
2d16b47
bdb3c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d16b47
bdb3c28
5dc5cb8
8dd3ae7
bdb3c28
2e99fbf
 
8dd3ae7
 
 
 
 
bdb3c28
4839967
 
 
 
 
bdb3c28
 
 
 
 
 
 
4839967
bdb3c28
8dd3ae7
bdb3c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d1a43b
bdb3c28
c574b13
2d16b47
86cf81a
2d16b47
8bec8f1
86cf81a
8ebff9f
86cf81a
 
 
8ebff9f
5dc5cb8
 
86cf81a
8bec8f1
86cf81a
2d16b47
 
86cf81a
 
2d16b47
aad2e16
8dd3ae7
 
 
 
 
 
 
 
bdb3c28
aad2e16
 
 
 
5dc5cb8
 
 
86cf81a
 
 
 
 
 
 
 
 
 
 
aad2e16
86cf81a
 
 
 
 
 
 
 
 
 
 
71830dc
86cf81a
 
5dc5cb8
 
 
bdb3c28
 
 
 
 
 
 
c6fc32c
 
71830dc
 
c6fc32c
 
8ebff9f
8bec8f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ebff9f
70cc7b0
 
7ec4367
 
 
 
70cc7b0
 
 
 
 
 
 
c574b13
c6fc32c
70cc7b0
 
 
 
c6fc32c
 
70cc7b0
c7d38be
8e1caa1
 
71830dc
c6fc32c
 
ebf7d39
71830dc
ebf7d39
8bec8f1
ebf7d39
 
 
624ba32
2e99fbf
c6fc32c
ebf7d39
624ba32
 
c6fc32c
8ebff9f
8bec8f1
 
 
 
 
 
 
 
 
 
 
c6fc32c
624ba32
2e99fbf
 
 
 
 
 
 
 
 
 
 
 
 
c6fc32c
 
 
 
70cc7b0
c6fc32c
 
 
 
70cc7b0
c6fc32c
 
 
 
 
 
86cf81a
 
 
 
 
 
 
 
 
 
 
 
 
 
c6fc32c
86cf81a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6fc32c
bdb3c28
 
 
 
 
 
 
8e1caa1
 
33e11fc
 
 
 
 
 
8e1caa1
 
 
 
33e11fc
8e1caa1
 
 
 
 
 
 
 
 
 
33e11fc
8e1caa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dd3ae7
 
c6fc32c
8dd3ae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6fc32c
8bec8f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26217ec
bdb3c28
 
 
 
 
 
 
8bec8f1
bdb3c28
 
 
 
 
 
 
 
 
 
 
 
 
8bec8f1
bdb3c28
 
2d16b47
bdb3c28
8bec8f1
 
 
 
 
 
bdb3c28
 
 
 
 
 
 
8bec8f1
bdb3c28
 
 
8bec8f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdb3c28
8bec8f1
c574b13
2e99fbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6fc32c
 
 
70cc7b0
 
 
 
8e1caa1
 
71830dc
c6fc32c
 
 
 
 
 
 
 
 
 
c7d38be
 
 
 
c6fc32c
 
 
 
 
 
 
 
8ebff9f
 
c6fc32c
c7d38be
c6fc32c
 
 
 
 
624ba32
 
ebf7d39
c6fc32c
 
 
 
b3a5f93
c7d38be
 
b3a5f93
 
c6fc32c
 
 
 
 
 
 
 
c7d38be
c6fc32c
70cc7b0
c6fc32c
70cc7b0
c7d38be
 
 
 
8e1caa1
 
 
 
 
 
c6fc32c
c574b13
624ba32
c574b13
c6fc32c
 
 
 
b3a5f93
c7d38be
 
b3a5f93
 
c6fc32c
 
 
8e1caa1
 
 
b3a5f93
c7d38be
 
 
c6fc32c
c574b13
624ba32
c574b13
c6fc32c
33e11fc
 
 
 
 
 
 
 
 
 
 
 
 
 
c6fc32c
b3a5f93
c7d38be
 
b3a5f93
 
c6fc32c
 
c7d38be
c6fc32c
c574b13
624ba32
c574b13
c6fc32c
 
 
 
b3a5f93
c7d38be
 
b3a5f93
 
c6fc32c
 
c7d38be
c6fc32c
 
8dd3ae7
 
c6fc32c
 
 
 
b3a5f93
c7d38be
 
b3a5f93
 
c6fc32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# components/review_dashboard_page.py

import gradio as gr
import datetime
import sentry_sdk
from sqlalchemy import orm

from components.header import Header
from utils.logger import Logger
from utils.cloud_server_audio_loader import CloudServerAudioLoader
from config import conf
from utils.database import get_db
from data.models import Annotation, TTSData, Annotator, Validation, AnnotationInterval
from data.repository.annotator_workload_repo import AnnotatorWorkloadRepo
from sqlalchemy import and_

log = Logger()
LOADER = CloudServerAudioLoader(conf.FTP_URL)


class ReviewDashboardPage:
    def __init__(self) -> None:
        with gr.Column(visible=False) as self.container:
            self.header = Header()
            self.load_trigger = gr.Number(value=0, visible=False) # Add this hidden trigger

            # Review info banner
            with gr.Row():
                self.review_info = gr.Markdown("", elem_classes="review-banner")

            with gr.Row():
                # Left Column - Review Content
                with gr.Column(scale=3):
                    with gr.Row():
                        self.tts_id = gr.Textbox(label="ID", interactive=False, scale=1)
                        self.filename = gr.Textbox(label="Filename", interactive=False, scale=3)
                    
                    self.sentence = gr.Textbox(
                        label="Original Sentence", interactive=False, max_lines=5, rtl=True
                    )
                    
                    self.ann_sentence = gr.Textbox(
                        label="Annotated Sentence (by Original Annotator)", 
                        interactive=False, max_lines=5, rtl=True
                    )
                    
                    with gr.Row():
                        # self.annotator_name = gr.Textbox(label="Original Annotator", interactive=False, scale=1) # Removed for anonymization
                        self.annotated_at = gr.Textbox(label="Annotated At", interactive=False, scale=2)
                    
                    # Review Actions
                    with gr.Row():
                        self.btn_approve = gr.Button("βœ… Approve", variant="primary", min_width=120)
                        self.btn_reject = gr.Button("❌ Reject", variant="stop", min_width=120)
                        self.btn_skip = gr.Button("⏭️ Skip (No Decision)", min_width=150)

                    # Navigation
                    with gr.Row():
                        self.btn_prev = gr.Button("⬅️ Previous", min_width=120)
                        self.btn_next = gr.Button("Next ➑️", min_width=120)

                    # Jump controls
                    with gr.Row():
                        self.jump_data_id_input = gr.Number(
                            label="Jump to ID", 
                            value=None, 
                            precision=0,
                            interactive=True,
                            min_width=120
                        )
                        self.btn_jump = gr.Button("Go to ID", min_width=70)

                # Right Column - Audio
                with gr.Column(scale=2):
                    self.btn_load_voice = gr.Button("Load Audio & Play", min_width=150)
                    self.audio = gr.Audio(
                        label="πŸ”Š Audio", interactive=False, autoplay=True
                    )
                    
                    # Review status display
                    with gr.Group():
                        gr.Markdown("### Review Status")
                        self.current_validation_status = gr.Textbox(
                            label="Current Status", interactive=False
                        )
                        self.rejection_reason_input = gr.Textbox(
                            label="Rejection Reason",
                            placeholder="Enter reason and press Enter or click away...",
                            interactive=True,
                            visible=False,
                            max_lines=3,
                            elem_id="rejection_reason_input" # Added elem_id for clarity
                        )

            # State variables
            self.items_state = gr.State([])
            self.idx_state = gr.State(0)
            self.original_audio_state = gr.State(None)
            self.rejection_mode_active = gr.State(False)  # Track if waiting for rejection reason

            # List of interactive UI elements for enabling/disabling
            self.interactive_ui_elements = [
                self.btn_prev, self.btn_next, self.btn_approve, self.btn_reject, 
                self.btn_skip, self.btn_jump, self.jump_data_id_input, self.btn_load_voice
            ]

    def register_callbacks(self, login_page, session_state: gr.State, root_blocks: gr.Blocks):
        self.header.register_callbacks(login_page, self, session_state)

        def update_ui_interactive_state(is_interactive: bool):
            updates = []
            for elem in self.interactive_ui_elements:
                if elem == self.btn_load_voice and not is_interactive:
                    updates.append(gr.update(value="⏳ Loading Audio...", interactive=False))
                elif elem == self.btn_load_voice and is_interactive:
                    updates.append(gr.update(value="Load Audio & Play", interactive=True))
                else:
                    updates.append(gr.update(interactive=is_interactive))
            return updates

        def download_voice_fn(filename_to_load):
            if not filename_to_load:
                return None, None, gr.update(value=None, autoplay=False)
            # try:
            log.info(f"Downloading voice for review: {filename_to_load}")
            # Show progress to user
            # gr.Info(f"Loading audio file: {filename_to_load}")
            
            sr, wav = LOADER.load_audio(filename_to_load)
            
            log.info(f"Successfully loaded audio: {filename_to_load} (SR: {sr}, Length: {len(wav)} samples)")
            # gr.Info(f"βœ… Audio loaded successfully!")
            
            return (sr, wav), (sr, wav.copy()), gr.update(value=(sr, wav), autoplay=True)
            # except TimeoutError as e:
            #     log.error(f"Audio download timeout for {filename_to_load}: {e}")
            #     sentry_sdk.capture_exception(e)
            #     raise
            # except ConnectionError as e:
            #     log.error(f"Audio download connection error for {filename_to_load}: {e}")
            #     sentry_sdk.capture_exception(e)
            #     gr.Error(f"🌐 Connection error loading audio: {filename_to_load}. Please check your internet connection.")
            #     return None, None, gr.update(value=None, autoplay=False)
            # except FileNotFoundError as e:
            #     log.error(f"Audio file not found for {filename_to_load}: {e}")
            #     sentry_sdk.capture_exception(e)
            #     gr.Error(f"πŸ“ Audio file not found: {filename_to_load}")
            #     return None, None, gr.update(value=None, autoplay=False)
            # except Exception as e:
            #     log.error(f"Audio download failed for {filename_to_load}: {e}")
            #     sentry_sdk.capture_exception(e)
            #     gr.Error(f"❌ Failed to load audio: {filename_to_load}. Error: {e}")
            #     return None, None, gr.update(value=None, autoplay=False)

        def get_validation_status_for_item(db, annotation_id, user_id, annotation_obj):
            """Get validation status for a specific item - called on-demand"""
            validation = db.query(Validation).filter_by(
                annotation_id=annotation_id,
                validator_id=user_id
            ).first()
            
            # Check if annotation is deleted
            is_deleted = not annotation_obj.annotated_sentence or annotation_obj.annotated_sentence.strip() == ""
            
            validation_status = "Not Reviewed"
            if validation:
                if validation.validated:
                    validation_status = "Approved"
                else:
                    validation_status = "Rejected"
                    if validation.description:
                        validation_status += f" ({validation.description})"
            
            # For deleted annotations, show special status
            if is_deleted and validation_status == "Not Reviewed":
                validation_status = "Not Reviewed (Deleted)"
                
            return validation_status, is_deleted

        def get_review_progress_fn(session):
            """Calculate review progress for the current reviewer with beautiful tqdm-style display"""
            user_id = session.get("user_id")
            username = session.get("username")
            
            if not user_id or not username:
                return ""
            
            # Check if user is a reviewer
            if username not in conf.REVIEW_MAPPING.values():
                return ""
            
            # Find target annotator
            target_annotator = None
            for annotator_name, reviewer_name in conf.REVIEW_MAPPING.items():
                if reviewer_name == username:
                    target_annotator = annotator_name
                    break
            
            if not target_annotator:
                return ""
            
            with get_db() as db:
                try:
                    # Get target annotator's ID
                    target_annotator_obj = db.query(Annotator).filter_by(name=target_annotator).first()
                    if not target_annotator_obj:
                        return f"⚠️ **Error:** Annotator '{target_annotator}' not found"
                    
                    # Get the target annotator's assigned intervals
                    assigned_intervals = db.query(AnnotationInterval).filter(
                        AnnotationInterval.annotator_id == target_annotator_obj.id
                    ).all()
                    
                    if not assigned_intervals:
                        return f"⚠️ **Error:** No assigned intervals for annotator '{target_annotator}'"
                    
                    # Count total annotations within assigned intervals for target annotator
                    total_count = 0
                    for interval in assigned_intervals:
                        if interval.start_index is None or interval.end_index is None:
                            continue
                        interval_count = db.query(Annotation).join(
                            TTSData, Annotation.tts_data_id == TTSData.id
                        ).filter(
                            Annotation.annotator_id == target_annotator_obj.id,
                            TTSData.id >= interval.start_index,
                            TTSData.id <= interval.end_index
                        ).count()
                        total_count += interval_count
                    
                    # Count reviewed annotations within assigned intervals (have validation from this reviewer)
                    reviewed_count = 0
                    for interval in assigned_intervals:
                        if interval.start_index is None or interval.end_index is None:
                            continue
                        interval_reviewed = db.query(Annotation).join(
                            TTSData, Annotation.tts_data_id == TTSData.id
                        ).join(
                            Validation, Annotation.id == Validation.annotation_id
                        ).filter(
                            Annotation.annotator_id == target_annotator_obj.id,
                            TTSData.id >= interval.start_index,
                            TTSData.id <= interval.end_index,
                            Validation.validator_id == user_id
                        ).count()
                        reviewed_count += interval_reviewed
                    
                    if total_count > 0:
                        percentage = (reviewed_count / total_count) * 100
                        
                        # Create tqdm-style progress bar
                        bar_width = 30  # Width of the progress bar in characters
                        filled = int((percentage / 100) * bar_width)
                        empty = bar_width - filled
                        
                        # Different colors based on progress
                        if percentage < 25:
                            color = "πŸ”΄"  # Red for low progress
                            bar_color = "progress-bar-low"
                        elif percentage < 50:
                            color = "🟑"  # Yellow for medium-low progress
                            bar_color = "progress-bar-medium-low"
                        elif percentage < 75:
                            color = "🟠"  # Orange for medium progress
                            bar_color = "progress-bar-medium"
                        elif percentage < 100:
                            color = "🟒"  # Green for high progress
                            bar_color = "progress-bar-high"
                        else:
                            color = "βœ…"  # Check mark for complete
                            bar_color = "progress-bar-complete"
                        
                        # Create the visual progress bar with Unicode blocks
                        progress_bar = "β–ˆ" * filled + "β–‘" * empty
                        
                        # Estimate remaining items
                        remaining = total_count - reviewed_count
                        
                        # Create the beautiful progress display
                        progress_html = f"""
                        <div class="progress-container">
                            <div class="progress-header">
                                <span class="progress-icon">{color}</span>
                                <strong>Review Progress</strong>
                            </div>
                            <div class="progress-bar-container">
                                <span class="progress-percentage">{percentage:.1f}%</span>
                                <div class="progress-bar {bar_color}">
                                    <span class="progress-fill" style="width: {percentage:.1f}%"></span>
                                </div>
                                <span class="progress-stats">{reviewed_count}/{total_count}</span>
                            </div>
                            <div class="progress-details">
                                πŸ“Š <code>{progress_bar}</code> 
                                <span class="remaining-items">({remaining} remaining)</span>
                            </div>
                        </div>
                        """
                        
                        return progress_html
                    else:
                        return f"πŸ“­ **No items found for {target_annotator}**"
                        
                except Exception as e:
                    log.error(f"Error calculating review progress for user {user_id}: {e}")
                    return f"⚠️ **Error calculating progress**"

        def load_review_items_fn(session):
            user_id = session.get("user_id")
            username = session.get("username")
            
            if not user_id or not username:
                log.warning("load_review_items_fn: user not found in session")
                return [], 0, "", "", "", "", "", "", "", "", gr.update(value=None, autoplay=False), gr.update(visible=False, value=""), False, gr.update(value="❌ Reject")

            # Check if user is in Phase 2 (should be a reviewer)
            if username not in conf.REVIEW_MAPPING.values():
                log.warning(f"User {username} is not assigned as a reviewer")
                return [], 0, "", "", "", "", "", "", "", "", gr.update(value=None, autoplay=False), gr.update(visible=False, value=""), False, gr.update(value="❌ Reject")

            # Find which annotator this user should review
            target_annotator = None
            for annotator_name, reviewer_name in conf.REVIEW_MAPPING.items():
                if reviewer_name == username:
                    target_annotator = annotator_name
                    break

            if not target_annotator:
                log.warning(f"No target annotator found for reviewer {username}")
                return [], 0, "", "", "", "", "", "", "", "", gr.update(value=None, autoplay=False), gr.update(visible=False, value=""), False, gr.update(value="❌ Reject")

            # Load annotations from target annotator with FAST INITIAL LOADING
            with get_db() as db:
                # Get target annotator's ID
                target_annotator_obj = db.query(Annotator).filter_by(name=target_annotator).first()
                if not target_annotator_obj:
                    log.error(f"Target annotator {target_annotator} not found in database")
                    return [], 0, f"Review Target Error: Annotator '{target_annotator}' not found.", "", "", "", "", "", "", "", gr.update(value=None, autoplay=False), gr.update(visible=False, value=""), False, gr.update(value="❌ Reject")
                
                log.info(f"Found target annotator with ID: {target_annotator_obj.id}")

                # FAST INITIAL QUERY: Load only essential data without complex validation processing
                # Increased batch size for better navigation experience
                INITIAL_BATCH_SIZE = 10  # Load 10 items initially for better navigation
                
                # Get the target annotator's assigned intervals
                assigned_intervals = db.query(AnnotationInterval).filter(
                    AnnotationInterval.annotator_id == target_annotator_obj.id
                ).all()
                
                if not assigned_intervals:
                    log.warning(f"No assigned intervals found for annotator {target_annotator}")
                    return [], 0, f"Review Target Error: No assigned intervals for annotator '{target_annotator}'.", "", "", "", "", "", "", "", gr.update(value=None, autoplay=False), gr.update(visible=False, value=""), False, gr.update(value="❌ Reject")
                
                # Find the first UNREVIEWED annotation within assigned intervals for this reviewer
                all_reviewed = False
                first_unreviewed_tts_id = None
                
                # Query for the first TTS data ID within assigned intervals that has no validation by this reviewer
                for interval in assigned_intervals:
                    if interval.start_index is None or interval.end_index is None:
                        continue
                        
                    unreviewed_query = db.query(TTSData.id).join(
                        Annotation, Annotation.tts_data_id == TTSData.id
                    ).outerjoin(
                        Validation,
                        (Validation.annotation_id == Annotation.id) & (Validation.validator_id == user_id)
                    ).filter(
                        Annotation.annotator_id == target_annotator_obj.id,
                        TTSData.id >= interval.start_index,
                        TTSData.id <= interval.end_index,
                        Validation.id.is_(None)  # No validation by this reviewer (fixed SQLAlchemy syntax)
                    ).order_by(TTSData.id.asc()).first()
                    
                    if unreviewed_query:
                        first_unreviewed_tts_id = unreviewed_query[0]
                        break
                
                if first_unreviewed_tts_id is None:
                    # Everything reviewed: flag and we will load the last batch from the last interval
                    all_reviewed = True
                
                # Count total annotations within assigned intervals for progress info
                total_count = 0
                for interval in assigned_intervals:
                    if interval.start_index is None or interval.end_index is None:
                        continue
                    interval_count = db.query(Annotation).join(
                        TTSData, Annotation.tts_data_id == TTSData.id
                    ).filter(
                        Annotation.annotator_id == target_annotator_obj.id,
                        TTSData.id >= interval.start_index,
                        TTSData.id <= interval.end_index
                    ).count()
                    total_count += interval_count

                # Query to get annotations with a window around the first unreviewed item
                if not all_reviewed and first_unreviewed_tts_id:
                    # Load a larger window around the first unreviewed TTS ID for better navigation
                    WINDOW_BEFORE = 5  # Load 5 items before the first unreviewed
                    WINDOW_AFTER = INITIAL_BATCH_SIZE - WINDOW_BEFORE - 1  # Rest after
                    
                    # Get a range starting before the first unreviewed item
                    window_start_id = max(1, first_unreviewed_tts_id - WINDOW_BEFORE)
                    
                    initial_query = db.query(
                        Annotation,
                        TTSData.filename,
                        TTSData.sentence
                    ).join(
                        TTSData, Annotation.tts_data_id == TTSData.id
                    ).join(
                        AnnotationInterval,
                        and_(
                            AnnotationInterval.annotator_id == target_annotator_obj.id,
                            TTSData.id >= AnnotationInterval.start_index,
                            TTSData.id <= AnnotationInterval.end_index
                        )
                    ).filter(
                        Annotation.annotator_id == target_annotator_obj.id,
                        TTSData.id >= window_start_id
                    ).order_by(TTSData.id).limit(INITIAL_BATCH_SIZE)
                    
                    initial_results = initial_query.all()
                else:
                    # Everything reviewed or no unreviewed items: load the last batch from assigned intervals
                    all_reviewed = True
                    if assigned_intervals and total_count > 0:
                        # Find the last interval and load the last batch from there
                        last_interval = max(assigned_intervals, key=lambda x: x.end_index or 0)
                        if last_interval.start_index is not None and last_interval.end_index is not None:
                            initial_query = db.query(
                                Annotation,
                                TTSData.filename,
                                TTSData.sentence
                            ).join(
                                TTSData, Annotation.tts_data_id == TTSData.id
                            ).filter(
                                Annotation.annotator_id == target_annotator_obj.id,
                                TTSData.id >= last_interval.start_index,
                                TTSData.id <= last_interval.end_index
                            ).order_by(TTSData.id.desc()).limit(INITIAL_BATCH_SIZE)
                            
                            initial_results = initial_query.all()
                            initial_results.reverse()  # Restore ascending order
                        else:
                            initial_results = []
                    else:
                        initial_results = []

                log.info(f"Fast initial load: {len(initial_results)} annotations out of {total_count} total for target annotator ID {target_annotator_obj.id}")

                # Process items with minimal data - validation status will be loaded on-demand
                items = []
                for annotation, filename, sentence in initial_results:
                    # Check if annotation is deleted (minimal processing)
                    is_deleted = not annotation.annotated_sentence or annotation.annotated_sentence.strip() == ""
                    annotated_sentence_display = "[DELETED ANNOTATION]" if is_deleted else annotation.annotated_sentence
                    
                    items.append({
                        "annotation_id": annotation.id,
                        "tts_id": annotation.tts_data_id,
                        "filename": filename,
                        "sentence": sentence,
                        "annotated_sentence": annotated_sentence_display,
                        "is_deleted": is_deleted,
                        "annotated_at": annotation.annotated_at.isoformat() if annotation.annotated_at else "",
                        "validation_status": "Loading...",  # Will be loaded on-demand
                        "validation_loaded": False  # Track if validation status has been loaded
                    })

                # Determine initial index inside the loaded batch
                initial_idx = 0
                if items and not all_reviewed and first_unreviewed_tts_id:
                    # Find the first unreviewed item within the loaded batch
                    for i, item in enumerate(items):
                        if item["tts_id"] == first_unreviewed_tts_id:
                            initial_idx = i
                            break
                elif items and all_reviewed:
                    # All reviewed: start at last item in the batch for browsing
                    initial_idx = len(items) - 1 
                
                # Set initial display
                if items:
                    initial_item = items[initial_idx]
                    review_info_text = f"πŸ” **Phase 2 Review Mode** - Reviewing assigned annotations. Loaded {len(items)} of {total_count} total items."
                    # Ensure correct order of return values for 12 outputs
                    # items, idx, review_info, tts_id, filename, sentence, ann_sentence, annotated_at, validation_status, annotator_placeholder, audio_update, rejection_reason_update
                    rejection_reason_val = ""
                    rejection_visible_val = False
                    if initial_item["validation_status"].startswith("Rejected"):
                        start_paren = initial_item["validation_status"].find("(")
                        end_paren = initial_item["validation_status"].find(")")
                        if start_paren != -1 and end_paren != -1:
                            rejection_reason_val = initial_item["validation_status"][start_paren+1:end_paren]
                        rejection_visible_val = True
                    
                    return (
                        items, 
                        initial_idx, 
                        review_info_text,
                        str(initial_item["tts_id"]), 
                        initial_item["filename"], 
                        initial_item["sentence"], 
                        initial_item["annotated_sentence"],
                        initial_item["annotated_at"], 
                        initial_item["validation_status"], 
                        "", # Placeholder for the original annotator name (maps to header.welcome)
                        gr.update(value=None, autoplay=False), # audio_update
                        gr.update(visible=rejection_visible_val, value=rejection_reason_val), # rejection_reason_input update
                        False,  # Reset rejection mode
                        gr.update(value="❌ Reject")  # Reset reject button
                    )
                else:
                    # Ensure correct order and number of return values for empty items (14 outputs)
                    return [], 0, f"πŸ” **Phase 2 Review Mode** - No annotations found for review.", "", "", "", "", "", "", "", gr.update(value=None, autoplay=False), gr.update(visible=False, value=""), False, gr.update(value="❌ Reject")

                # except Exception as e:
                #     log.error(f"Error loading review items: {e}")
                #     sentry_sdk.capture_exception(e)
                #     gr.Error(f"Failed to load review data: {e}")
                #     # Ensure correct order and number of return values for error case (14 outputs)
                #     return [], 0, "", "", "", "", "", "", "", "", gr.update(value=None, autoplay=False), gr.update(visible=False, value=""), False, gr.update(value="❌ Reject")

        def show_current_review_item_fn(items, idx, session):
            if not items or idx >= len(items) or idx < 0:
                # tts_id, filename, sentence, ann_sentence, annotated_at, validation_status, annotator_name_placeholder, audio_update, rejection_reason_update, rejection_mode_reset, btn_reject_update
                return "", "", "", "", "", "", "", gr.update(value=None, autoplay=False), gr.update(visible=False, value=""), False, gr.update(value="❌ Reject")
            
            current_item = items[idx]
            
            # Load validation status on-demand if not already loaded
            if not current_item.get("validation_loaded", False):
                user_id = session.get("user_id")
                if user_id:
                    with get_db() as db:
                        try:
                            # Get the full annotation object for validation processing
                            annotation_obj = db.query(Annotation).filter_by(id=current_item["annotation_id"]).first()
                            if annotation_obj:
                                validation_status, is_deleted = get_validation_status_for_item(db, current_item["annotation_id"], user_id, annotation_obj)
                                current_item["validation_status"] = validation_status
                                current_item["is_deleted"] = is_deleted
                                current_item["validation_loaded"] = True
                                
                                # Update displayed annotation if deleted
                                if is_deleted:
                                    current_item["annotated_sentence"] = "[DELETED ANNOTATION]"
                                
                                log.info(f"Loaded validation status for item {idx}: {validation_status}")
                        except Exception as e:
                            log.error(f"Error loading validation status for item {idx}: {e}")
                            current_item["validation_status"] = "Error loading status"
            
            rejection_reason = ""
            rejection_visible = False
            
            # Check if this is a deleted annotation
            is_deleted = current_item.get("is_deleted", False)
            
            if current_item["validation_status"].startswith("Rejected"):
                # Extract reason from status like "Rejected (reason)" or just use empty if no parenthesis
                start_paren = current_item["validation_status"].find("(")
                end_paren = current_item["validation_status"].find(")")
                if start_paren != -1 and end_paren != -1:
                    rejection_reason = current_item["validation_status"][start_paren+1:end_paren]
                rejection_visible = True

            return (
                str(current_item["tts_id"]), 
                current_item["filename"],
                current_item["sentence"], 
                current_item["annotated_sentence"],
                current_item["annotated_at"],
                current_item["validation_status"],
                "", # Placeholder for annotator_name
                gr.update(value=None, autoplay=False),
                gr.update(visible=rejection_visible, value=rejection_reason),
                False,  # Reset rejection mode
                gr.update(value="❌ Reject")  # Reset reject button text
            )

        def update_review_info_fn(items, total_count):
            """Update the review info banner with current loaded items count"""
            if items:
                return f"πŸ” **Phase 2 Review Mode** - Reviewing assigned annotations. Loaded {len(items)} of {total_count} total items."
            else:
                return f"πŸ” **Phase 2 Review Mode** - No annotations found for review."

        def navigate_and_load_fn(items, current_idx, direction, session):
            """Combined navigation and loading function with dynamic loading in both directions"""
            if not items:
                return items, 0, ""
            
            # Navigate
            if direction == "next":
                new_idx = min(current_idx + 1, len(items) - 1)
                # Only load more items when user reaches the LAST item of a batch
                should_load_more = (new_idx == len(items) - 1 and len(items) % 5 == 0)
                if should_load_more:
                    log.info(f"User reached end of loaded items ({new_idx}/{len(items)}), will load more items")
                    # Load more items
                    updated_items, total_count = load_more_items_fn(items, session, current_batch_size=10)
                    # Update review info with new count
                    review_info = update_review_info_fn(updated_items, total_count)
                    return updated_items, new_idx, review_info
                else:
                    return items, new_idx, ""  # No review info update needed
            else:  # prev
                new_idx = max(current_idx - 1, 0)
                # Load more items when user reaches the FIRST item of the batch
                should_load_previous = (new_idx == 0 and current_idx == 0)
                if should_load_previous:
                    log.info(f"User reached beginning of loaded items, will load previous items")
                    # Load previous items
                    updated_items, total_count, loaded_count = load_previous_items_fn(items, session, current_batch_size=5)
                    # Adjust index to account for new items loaded at the beginning
                    adjusted_idx = new_idx + loaded_count
                    # Update review info with new count
                    review_info = update_review_info_fn(updated_items, total_count)
                    return updated_items, adjusted_idx, review_info
                else:
                    return items, new_idx, ""  # No review info update needed

        def save_validation_fn(items, idx, session, approved: bool, rejection_reason: str = ""):
            if not items or idx >= len(items):
                gr.Error("Invalid item index")
                return items, "Error: Invalid item index", gr.update(visible=False)

            user_id = session.get("user_id")
            if not user_id:
                gr.Error("User not logged in")
                return items, "Error: User not logged in", gr.update(visible=False)

            current_item = items[idx]
            annotation_id = current_item["annotation_id"]
            log.info(f"Saving validation for annotation_id: {annotation_id}, validator_id: {user_id}, approved: {approved}, reason: {rejection_reason}")

            with get_db() as db:
                # try:
                existing_validation = db.query(Validation).filter_by(
                    annotation_id=annotation_id,
                    validator_id=user_id
                ).first()

                if existing_validation:
                    log.info(f"Updating existing validation for annotation_id: {annotation_id}")
                    existing_validation.validated = approved
                    existing_validation.description = rejection_reason if not approved else None
                    existing_validation.validated_at = datetime.datetime.utcnow()
                else:
                    log.info(f"Creating new validation for annotation_id: {annotation_id}")
                    new_validation = Validation(
                        annotation_id=annotation_id,
                        validator_id=user_id,
                        validated=approved,
                        description=rejection_reason if not approved else None,
                        validated_at=datetime.datetime.utcnow(),
                    )
                    db.add(new_validation)
                
                db.commit()
                log.info(f"Validation saved successfully for annotation_id: {annotation_id}")
                
                items[idx]["validation_status"] = "Approved" if approved else f"Rejected ({rejection_reason})" if rejection_reason else "Rejected"
                
                # Show rejection reason input only if rejected, otherwise hide and clear
                rejection_input_update = gr.update(visible=not approved, value="" if approved else rejection_reason)
                
                return items, items[idx]["validation_status"], rejection_input_update

                # except Exception as e:
                #     db.rollback()
                #     log.error(f"Error saving validation: {e}")
                #     sentry_sdk.capture_exception(e)
                #     gr.Error(f"Failed to save validation: {e}")
                #     return items, current_item["validation_status"], gr.update(visible=False) # Return original status and hide input on error

        def handle_rejection_fn(items, idx, session, rejection_reason, rejection_mode_active):
            """Handle rejection button click - two-step process"""
            if not items or idx >= len(items):
                return items, "Error: Invalid item", gr.update(visible=False), False, gr.update(value="❌ Reject")
                
            current_item = items[idx]
            current_status = current_item["validation_status"]
            
            if not rejection_mode_active:
                # First click - show rejection reason input and change button text
                return (
                    items,  # items unchanged
                    current_status,  # Keep current validation status
                    gr.update(visible=True, value=""),  # Show rejection reason input, clear any existing value
                    True,  # Set rejection mode active
                    gr.update(value="⚠️ Confirm Reject")  # Change button text
                )
            else:
                # Second click - validate reason and save if provided
                if not rejection_reason or rejection_reason.strip() == "":
                    gr.Warning("Rejection reason cannot be empty. Please provide a reason before confirming rejection.")
                    return (
                        items,  # items unchanged
                        current_status,  # Keep current validation status
                        gr.update(visible=True, value=rejection_reason),  # Keep input visible
                        True,  # Keep rejection mode active
                        gr.update(value="⚠️ Confirm Reject")  # Keep button text
                    )
                else:
                    # Save the rejection with reason
                    updated_items, validation_status, rejection_input_update = save_validation_fn(
                        items, idx, session, approved=False, rejection_reason=rejection_reason.strip()
                    )
                    return (
                        updated_items,
                        validation_status,
                        gr.update(visible=False, value=""),  # Hide rejection input after successful save
                        False,  # Reset rejection mode
                        gr.update(value="❌ Reject")  # Reset button text
                    )

        def jump_by_data_id_fn(items, target_data_id, current_idx, session):
            """Jump to a specific TTS ID by querying the database and loading a new batch around it"""
            if not target_data_id:
                return items, current_idx, ""
            
            user_id = session.get("user_id")
            username = session.get("username")
            
            if not user_id or not username:
                gr.Warning("User session not found")
                return items, current_idx, ""
            
            # Find target annotator
            target_annotator = None
            for annotator_name, reviewer_name in conf.REVIEW_MAPPING.items():
                if reviewer_name == username:
                    target_annotator = annotator_name
                    break
                    
            if not target_annotator:
                gr.Warning("Target annotator not found for user")
                return items, current_idx, ""
            
            try:
                target_id = int(target_data_id)
                
                with get_db() as db:
                    target_annotator_obj = db.query(Annotator).filter_by(name=target_annotator).first()
                    if not target_annotator_obj:
                        gr.Warning("Target annotator not found in database")
                        return items, current_idx, ""
                    
                    # Get the target annotator's assigned intervals
                    assigned_intervals = db.query(AnnotationInterval).filter(
                        AnnotationInterval.annotator_id == target_annotator_obj.id
                    ).all()
                    
                    if not assigned_intervals:
                        gr.Warning("No assigned intervals found")
                        return items, current_idx, ""
                    
                    # Check if the target TTS ID exists within the assigned intervals
                    target_annotation = None
                    for interval in assigned_intervals:
                        if interval.start_index is None or interval.end_index is None:
                            continue
                        
                        target_annotation = db.query(Annotation).join(
                            TTSData, Annotation.tts_data_id == TTSData.id
                        ).filter(
                            Annotation.annotator_id == target_annotator_obj.id,
                            TTSData.id == target_id,
                            TTSData.id >= interval.start_index,
                            TTSData.id <= interval.end_index
                        ).first()
                        
                        if target_annotation:
                            break
                    
                    if not target_annotation:
                        gr.Warning(f"Data ID {target_id} not found in assigned review range")
                        return items, current_idx, ""
                    
                    # Load a batch around the target ID
                    BATCH_SIZE = 10
                    WINDOW_BEFORE = BATCH_SIZE // 2
                    
                    window_start_id = max(1, target_id - WINDOW_BEFORE)
                    
                    # Query for annotations in the window
                    new_query = db.query(
                        Annotation,
                        TTSData.filename,
                        TTSData.sentence
                    ).join(
                        TTSData, Annotation.tts_data_id == TTSData.id
                    ).join(
                        AnnotationInterval,
                        and_(
                            AnnotationInterval.annotator_id == target_annotator_obj.id,
                            TTSData.id >= AnnotationInterval.start_index,
                            TTSData.id <= AnnotationInterval.end_index
                        )
                    ).filter(
                        Annotation.annotator_id == target_annotator_obj.id,
                        TTSData.id >= window_start_id
                    ).order_by(TTSData.id).limit(BATCH_SIZE)
                    
                    results = new_query.all()
                    
                    # Process new items
                    new_items = []
                    target_idx = 0
                    for i, (annotation, filename, sentence) in enumerate(results):
                        # Check if annotation is deleted
                        is_deleted = not annotation.annotated_sentence or annotation.annotated_sentence.strip() == ""
                        annotated_sentence_display = "[DELETED ANNOTATION]" if is_deleted else annotation.annotated_sentence
                        
                        new_items.append({
                            "annotation_id": annotation.id,
                            "tts_id": annotation.tts_data_id,
                            "filename": filename,
                            "sentence": sentence,
                            "annotated_sentence": annotated_sentence_display,
                            "is_deleted": is_deleted,
                            "annotated_at": annotation.annotated_at.isoformat() if annotation.annotated_at else "",
                            "validation_status": "Loading...",
                            "validation_loaded": False
                        })
                        
                        # Find the target index within the new batch
                        if annotation.tts_data_id == target_id:
                            target_idx = i
                    
                    if new_items:
                        # Count total for review info
                        total_count = 0
                        for interval in assigned_intervals:
                            if interval.start_index is None or interval.end_index is None:
                                continue
                            interval_count = db.query(Annotation).join(
                                TTSData, Annotation.tts_data_id == TTSData.id
                            ).filter(
                                Annotation.annotator_id == target_annotator_obj.id,
                                TTSData.id >= interval.start_index,
                                TTSData.id <= interval.end_index
                            ).count()
                            total_count += interval_count
                        
                        review_info = f"πŸ” **Phase 2 Review Mode** - Jumped to ID {target_id}. Loaded {len(new_items)} of {total_count} total items."
                        log.info(f"Successfully jumped to TTS ID {target_id}, loaded {len(new_items)} items, target at index {target_idx}")
                        return new_items, target_idx, review_info
                    else:
                        gr.Warning(f"No items loaded around ID {target_id}")
                        return items, current_idx, ""
                        
            except ValueError:
                gr.Warning(f"Invalid Data ID format: {target_data_id}")
                return items, current_idx, ""
            except Exception as e:
                log.error(f"Error jumping to ID {target_data_id}: {e}")
                gr.Warning(f"Error jumping to ID {target_data_id}")
                return items, current_idx, ""

        def load_more_items_fn(items, session, current_batch_size=10):
            """Load more items when user needs them (pagination support)"""
            user_id = session.get("user_id")
            username = session.get("username")
            
            if not user_id or not username:
                return items, 0  # Return existing items if no user session
                
            # Find target annotator
            target_annotator = None
            for annotator_name, reviewer_name in conf.REVIEW_MAPPING.items():
                if reviewer_name == username:
                    target_annotator = annotator_name
                    break
                    
            if not target_annotator:
                return items, 0
                
            with get_db() as db:
                target_annotator_obj = db.query(Annotator).filter_by(name=target_annotator).first()
                if not target_annotator_obj:
                    return items, 0
                
                # Get the target annotator's assigned intervals
                assigned_intervals = db.query(AnnotationInterval).filter(
                    AnnotationInterval.annotator_id == target_annotator_obj.id
                ).all()
                
                if not assigned_intervals:
                    return items, 0
                    
                # Count total annotations within assigned intervals for progress info
                total_count = 0
                for interval in assigned_intervals:
                    if interval.start_index is None or interval.end_index is None:
                        continue
                    interval_count = db.query(Annotation).join(
                        TTSData, Annotation.tts_data_id == TTSData.id
                    ).filter(
                        Annotation.annotator_id == target_annotator_obj.id,
                        TTSData.id >= interval.start_index,
                        TTSData.id <= interval.end_index
                    ).count()
                    total_count += interval_count
                
                # Determine the next window based on the last loaded annotation id
                last_loaded_id = items[-1]["annotation_id"] if items else 0
                
                # FAST LOADING: Use id-based pagination within assigned intervals to continue from current position
                query = db.query(
                    Annotation,
                    TTSData.filename,
                    TTSData.sentence
                ).join(
                    TTSData, Annotation.tts_data_id == TTSData.id
                ).join(
                    AnnotationInterval,
                    and_(
                        AnnotationInterval.annotator_id == target_annotator_obj.id,
                        TTSData.id >= AnnotationInterval.start_index,
                        TTSData.id <= AnnotationInterval.end_index
                    )
                ).filter(
                    Annotation.annotator_id == target_annotator_obj.id,
                    Annotation.id > last_loaded_id
                ).order_by(Annotation.id).limit(current_batch_size)
                
                results = query.all()
                
                # Process new items with minimal data - validation status loaded on-demand
                new_items = []
                for annotation, filename, sentence in results:
                    # Check if annotation is deleted (minimal processing)
                    is_deleted = not annotation.annotated_sentence or annotation.annotated_sentence.strip() == ""
                    annotated_sentence_display = "[DELETED ANNOTATION]" if is_deleted else annotation.annotated_sentence
                    
                    new_items.append({
                        "annotation_id": annotation.id,
                        "tts_id": annotation.tts_data_id,
                        "filename": filename,
                        "sentence": sentence,
                        "annotated_sentence": annotated_sentence_display,
                        "is_deleted": is_deleted,
                        "annotated_at": annotation.annotated_at.isoformat() if annotation.annotated_at else "",
                        "validation_status": "Loading...",  # Will be loaded on-demand
                        "validation_loaded": False  # Track if validation status has been loaded
                    })
                
                # Combine with existing items
                all_items = items + new_items
                log.info(f"Loaded {len(new_items)} more items after id {last_loaded_id}, total now: {len(all_items)}")
                return all_items, total_count

        def load_previous_items_fn(items, session, current_batch_size=5):
            """Load items before the current batch when user navigates backward"""
            user_id = session.get("user_id")
            username = session.get("username")
            
            if not user_id or not username:
                return items, 0, 0  # Return existing items if no user session
                
            # Find target annotator
            target_annotator = None
            for annotator_name, reviewer_name in conf.REVIEW_MAPPING.items():
                if reviewer_name == username:
                    target_annotator = annotator_name
                    break
                    
            if not target_annotator:
                return items, 0, 0
                
            with get_db() as db:
                target_annotator_obj = db.query(Annotator).filter_by(name=target_annotator).first()
                if not target_annotator_obj:
                    return items, 0, 0
                
                # Get the target annotator's assigned intervals
                assigned_intervals = db.query(AnnotationInterval).filter(
                    AnnotationInterval.annotator_id == target_annotator_obj.id
                ).all()
                
                if not assigned_intervals:
                    return items, 0, 0
                    
                # Count total annotations within assigned intervals for progress info
                total_count = 0
                for interval in assigned_intervals:
                    if interval.start_index is None or interval.end_index is None:
                        continue
                    interval_count = db.query(Annotation).join(
                        TTSData, Annotation.tts_data_id == TTSData.id
                    ).filter(
                        Annotation.annotator_id == target_annotator_obj.id,
                        TTSData.id >= interval.start_index,
                        TTSData.id <= interval.end_index
                    ).count()
                    total_count += interval_count
                
                # Get the first loaded annotation id to load items before it
                first_loaded_id = items[0]["annotation_id"] if items else float('inf')
                
                # LOAD ITEMS BEFORE: Use id-based pagination to get previous items
                query = db.query(
                    Annotation,
                    TTSData.filename,
                    TTSData.sentence
                ).join(
                    TTSData, Annotation.tts_data_id == TTSData.id
                ).join(
                    AnnotationInterval,
                    and_(
                        AnnotationInterval.annotator_id == target_annotator_obj.id,
                        TTSData.id >= AnnotationInterval.start_index,
                        TTSData.id <= AnnotationInterval.end_index
                    )
                ).filter(
                    Annotation.annotator_id == target_annotator_obj.id,
                    Annotation.id < first_loaded_id
                ).order_by(Annotation.id.desc()).limit(current_batch_size)
                
                results = query.all()
                results.reverse()  # Restore ascending order
                
                # Process new items with minimal data - validation status loaded on-demand
                new_items = []
                for annotation, filename, sentence in results:
                    # Check if annotation is deleted (minimal processing)
                    is_deleted = not annotation.annotated_sentence or annotation.annotated_sentence.strip() == ""
                    annotated_sentence_display = "[DELETED ANNOTATION]" if is_deleted else annotation.annotated_sentence
                    
                    new_items.append({
                        "annotation_id": annotation.id,
                        "tts_id": annotation.tts_data_id,
                        "filename": filename,
                        "sentence": sentence,
                        "annotated_sentence": annotated_sentence_display,
                        "is_deleted": is_deleted,
                        "annotated_at": annotation.annotated_at.isoformat() if annotation.annotated_at else "",
                        "validation_status": "Loading...",  # Will be loaded on-demand
                        "validation_loaded": False  # Track if validation status has been loaded
                    })
                
                # Combine with existing items (new items go to the front)
                all_items = new_items + items
                loaded_count = len(new_items)
                log.info(f"Loaded {loaded_count} items before id {first_loaded_id}, total now: {len(all_items)}")
                return all_items, total_count, loaded_count

        # Output definitions
        review_display_outputs = [
            self.tts_id, self.filename, self.sentence, self.ann_sentence,
            self.annotated_at, 
            self.current_validation_status, 
            self.header.welcome, # Placeholder for anonymized annotator name
            self.audio,
            self.rejection_reason_input, # Added rejection reason input to display outputs
            self.rejection_mode_active,  # Added rejection mode state
            self.btn_reject  # Added reject button to display outputs
        ]

        # Trigger data loading when load_trigger changes (after successful login for a reviewer)
        self.load_trigger.change(
            fn=lambda: update_ui_interactive_state(False),
            outputs=self.interactive_ui_elements
        ).then(
            fn=load_review_items_fn,
            inputs=[session_state],
            outputs=[self.items_state, self.idx_state, self.review_info] + review_display_outputs
        ).then(
            fn=get_review_progress_fn,
            inputs=[session_state],
            outputs=[self.header.progress_display]
        ).then(
            fn=lambda: (None, gr.update(value=None)), # Clear audio state
            outputs=[self.original_audio_state, self.audio]
        ).then(
            fn=lambda: update_ui_interactive_state(True),
            outputs=self.interactive_ui_elements
        )

        # Audio loading is now manual only via the Load Audio button
        # Removed automatic filename.change callback to prevent slow loading during initialization

        # Navigation buttons
        for btn, direction in [(self.btn_prev, "prev"), (self.btn_next, "next")]:
            btn.click(
                fn=lambda: update_ui_interactive_state(False),
                outputs=self.interactive_ui_elements
            ).then(
                fn=lambda items, idx, session, dir=direction: navigate_and_load_fn(items, idx, dir, session),
                inputs=[self.items_state, self.idx_state, session_state],
                outputs=[self.items_state, self.idx_state, self.review_info]
            ).then(
                fn=show_current_review_item_fn,
                inputs=[self.items_state, self.idx_state, session_state],
                outputs=review_display_outputs
            ).then(
                # Auto-load audio with autoplay for smooth navigation
                fn=download_voice_fn,
                inputs=[self.filename],
                outputs=[self.audio, self.original_audio_state, self.audio]
            ).then(
                lambda: gr.update(value=None),
                outputs=self.jump_data_id_input
            ).then(
                fn=lambda: update_ui_interactive_state(True),
                outputs=self.interactive_ui_elements
            )

        # Approve/Reject buttons
        self.btn_approve.click(
            fn=lambda items, idx, session: save_validation_fn(items, idx, session, approved=True, rejection_reason=""), # Pass empty rejection_reason
            inputs=[self.items_state, self.idx_state, session_state],
            outputs=[self.items_state, self.current_validation_status, self.rejection_reason_input] 
        ).then(
            fn=get_review_progress_fn,  # Update progress after approval
            inputs=[session_state],
            outputs=[self.header.progress_display]
        ).then(
            fn=lambda: False,  # Reset rejection mode
            outputs=[self.rejection_mode_active]
        ).then(
            fn=lambda: gr.update(value="❌ Reject"),  # Reset reject button
            outputs=[self.btn_reject]
        ).then(
            fn=lambda items, idx, session: navigate_and_load_fn(items, idx, "next", session),
            inputs=[self.items_state, self.idx_state, session_state],
            outputs=[self.items_state, self.idx_state, self.review_info]
        ).then(
            fn=show_current_review_item_fn,
            inputs=[self.items_state, self.idx_state, session_state],
            outputs=review_display_outputs
        ).then(
            # Auto-load audio with autoplay after moving to next item
            fn=download_voice_fn,
            inputs=[self.filename],
            outputs=[self.audio, self.original_audio_state, self.audio]
        )

        self.btn_reject.click(
            fn=handle_rejection_fn,
            inputs=[self.items_state, self.idx_state, session_state, self.rejection_reason_input, self.rejection_mode_active],
            outputs=[self.items_state, self.current_validation_status, self.rejection_reason_input, self.rejection_mode_active, self.btn_reject]
        ).then(
            fn=lambda items, idx, session, rejection_mode: get_review_progress_fn(session) if not rejection_mode else "",  # Update progress only after successful rejection
            inputs=[self.items_state, self.idx_state, session_state, self.rejection_mode_active],
            outputs=[self.header.progress_display]
        ).then(
            fn=lambda items, idx, session, rejection_mode: navigate_and_load_fn(items, idx, "next", session) if not rejection_mode else (items, idx, ""),
            inputs=[self.items_state, self.idx_state, session_state, self.rejection_mode_active],
            outputs=[self.items_state, self.idx_state, self.review_info]
        ).then(
            fn=lambda items, idx, session, rejection_mode: show_current_review_item_fn(items, idx, session) if not rejection_mode else (
                str(items[idx]["tts_id"]) if items and idx < len(items) else "",
                items[idx]["filename"] if items and idx < len(items) else "",
                items[idx]["sentence"] if items and idx < len(items) else "",
                items[idx]["annotated_sentence"] if items and idx < len(items) else "",
                items[idx]["annotated_at"] if items and idx < len(items) else "",
                items[idx]["validation_status"] if items and idx < len(items) else "",
                "",  # annotator placeholder
                gr.update(value=None, autoplay=False),  # audio
                gr.update(),  # rejection_reason_input - don't change
                rejection_mode,  # keep rejection mode as is
                gr.update()   # btn_reject - don't change
            ),
            inputs=[self.items_state, self.idx_state, session_state, self.rejection_mode_active],
            outputs=review_display_outputs
        ).then(
            # Auto-load audio with autoplay only if we moved to next item (not in rejection mode)
            fn=lambda filename, rejection_mode: download_voice_fn(filename) if not rejection_mode else (None, None, gr.update(value=None, autoplay=False)),
            inputs=[self.filename, self.rejection_mode_active],
            outputs=[self.audio, self.original_audio_state, self.audio]
        )

        # Skip button (just navigate to next)
        self.btn_skip.click(
            fn=lambda items, idx, session: navigate_and_load_fn(items, idx, "next", session),
            inputs=[self.items_state, self.idx_state, session_state],
            outputs=[self.items_state, self.idx_state, self.review_info]
        ).then(
            fn=show_current_review_item_fn,
            inputs=[self.items_state, self.idx_state, session_state],
            outputs=review_display_outputs
        ).then(
            # Auto-load audio with autoplay after skipping
            fn=download_voice_fn,
            inputs=[self.filename],
            outputs=[self.audio, self.original_audio_state, self.audio]
        )

        # Jump button
        self.btn_jump.click(
            fn=jump_by_data_id_fn,
            inputs=[self.items_state, self.jump_data_id_input, self.idx_state, session_state],
            outputs=[self.items_state, self.idx_state, self.review_info]
        ).then(
            fn=show_current_review_item_fn,
            inputs=[self.items_state, self.idx_state, session_state],
            outputs=review_display_outputs
        ).then(
            # Auto-load audio with autoplay after jumping
            fn=download_voice_fn,
            inputs=[self.filename],
            outputs=[self.audio, self.original_audio_state, self.audio]
        ).then(
            lambda: gr.update(value=None),
            outputs=self.jump_data_id_input
        )

        # Load audio button
        self.btn_load_voice.click(
            fn=lambda: update_ui_interactive_state(False),
            outputs=self.interactive_ui_elements
        ).then(
            fn=download_voice_fn,
            inputs=[self.filename],
            outputs=[self.audio, self.original_audio_state, self.audio]
        ).then(
            fn=lambda: update_ui_interactive_state(True),
            outputs=self.interactive_ui_elements
        )

        return self.container