File size: 51,452 Bytes
bb6a3e5
8a54e54
750dde0
 
 
 
8a54e54
bb6a3e5
 
d9008cd
 
d629be6
d9008cd
 
f99daf4
bb6a3e5
d9008cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
750dde0
d9008cd
 
 
 
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9008cd
 
 
bb6a3e5
 
750dde0
bb6a3e5
d9008cd
 
750dde0
 
 
 
 
 
 
 
8a54e54
 
bb6a3e5
 
d9008cd
750dde0
 
 
 
 
bb6a3e5
 
d9008cd
750dde0
 
 
 
 
bb6a3e5
29f9c4f
d9008cd
bb6a3e5
750dde0
bb6a3e5
e692be8
a065c57
d9008cd
750dde0
 
 
 
 
 
a065c57
d9008cd
 
 
d629be6
750dde0
d9008cd
 
 
 
 
 
 
 
 
 
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a065c57
 
 
d9008cd
 
 
 
f99daf4
 
a065c57
d9008cd
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a065c57
d9008cd
 
 
d629be6
bb6a3e5
750dde0
bb6a3e5
d629be6
d9008cd
a065c57
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a065c57
 
 
d9008cd
 
 
 
750dde0
d9008cd
 
a065c57
d9008cd
750dde0
 
a065c57
750dde0
 
 
a065c57
750dde0
 
a065c57
d9008cd
 
 
a065c57
d9008cd
750dde0
 
 
a065c57
750dde0
 
a065c57
750dde0
a065c57
750dde0
 
 
 
 
 
 
 
 
 
a065c57
750dde0
 
 
 
 
a45c38f
d9008cd
bb6a3e5
750dde0
bb6a3e5
d9008cd
a065c57
d9008cd
 
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a065c57
 
 
d9008cd
 
bb6a3e5
 
750dde0
bb6a3e5
 
a065c57
d9008cd
750dde0
 
 
 
 
 
 
 
a065c57
7887502
d9008cd
750dde0
 
 
 
 
 
a065c57
d9008cd
 
750dde0
 
 
 
 
 
 
a065c57
d9008cd
 
750dde0
 
 
 
 
f99daf4
bb6a3e5
 
750dde0
bb6a3e5
 
a065c57
bb6a3e5
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a065c57
d9008cd
 
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9008cd
7887502
bb6a3e5
750dde0
bb6a3e5
f99daf4
d9008cd
8a49a9e
 
 
 
 
a065c57
750dde0
 
 
 
 
8a49a9e
750dde0
a065c57
750dde0
 
 
e692be8
750dde0
 
 
bb6a3e5
750dde0
 
 
8a49a9e
750dde0
d9008cd
750dde0
8a49a9e
bb6a3e5
750dde0
 
f99daf4
750dde0
 
 
 
 
 
8a49a9e
d16fe9c
750dde0
 
 
bb6a3e5
750dde0
 
d9008cd
8a49a9e
d9008cd
 
a065c57
8a49a9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9008cd
 
a065c57
750dde0
 
 
 
 
8a49a9e
750dde0
 
8a49a9e
750dde0
 
 
 
 
 
 
 
8a49a9e
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
8a49a9e
750dde0
 
 
 
 
 
 
 
8a49a9e
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
8a49a9e
750dde0
 
f99daf4
bb6a3e5
d9008cd
750dde0
 
 
 
8a49a9e
750dde0
 
 
8a49a9e
750dde0
a065c57
d9008cd
 
750dde0
 
 
8a49a9e
a065c57
750dde0
 
a065c57
8a49a9e
a065c57
f99daf4
d9008cd
750dde0
 
8a49a9e
a065c57
 
 
750dde0
 
 
 
 
 
 
a065c57
d2f5241
d9008cd
750dde0
 
 
 
 
 
 
 
d2f5241
 
bb6a3e5
750dde0
bb6a3e5
d9008cd
f99daf4
a065c57
d9008cd
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
d9008cd
 
a065c57
750dde0
 
 
a065c57
 
750dde0
a065c57
d9008cd
a065c57
 
750dde0
a065c57
 
750dde0
a065c57
 
 
750dde0
a065c57
d9008cd
a065c57
bb6a3e5
750dde0
 
 
 
 
 
a065c57
 
750dde0
 
a065c57
 
750dde0
a065c57
 
 
 
750dde0
 
d9008cd
 
a065c57
bb6a3e5
750dde0
 
 
 
 
bb6a3e5
d9008cd
a065c57
f99daf4
750dde0
 
 
 
 
 
 
 
 
 
d9008cd
 
a065c57
 
750dde0
 
 
 
 
 
a065c57
 
 
d9008cd
750dde0
 
 
 
 
 
 
 
 
 
d9008cd
 
a065c57
d9008cd
750dde0
 
 
 
 
 
 
d9008cd
 
a065c57
 
750dde0
a065c57
d9008cd
a065c57
 
750dde0
a065c57
 
 
d9008cd
750dde0
 
 
 
 
 
d9008cd
 
a065c57
d9008cd
750dde0
 
 
 
 
 
 
 
 
d9008cd
 
 
750dde0
 
 
 
 
 
 
 
d9008cd
 
750dde0
 
d9008cd
 
a065c57
d9008cd
750dde0
 
 
 
 
 
 
f99daf4
d9008cd
a065c57
bb6a3e5
750dde0
 
 
 
 
 
 
 
 
 
 
d9008cd
 
a065c57
 
 
750dde0
 
d9008cd
 
a065c57
d9008cd
750dde0
 
 
 
 
 
 
 
 
a065c57
 
 
 
750dde0
 
a065c57
 
750dde0
 
bb6a3e5
f99daf4
 
6875ca1
750dde0
6875ca1
453c4a3
a065c57
d9008cd
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a49a9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
750dde0
 
 
 
 
 
8a49a9e
750dde0
 
 
8a49a9e
750dde0
 
 
8a49a9e
750dde0
 
 
8a49a9e
750dde0
 
 
8a49a9e
750dde0
 
 
 
 
8a49a9e
750dde0
 
8a49a9e
750dde0
 
 
 
 
8a49a9e
750dde0
8a49a9e
 
 
 
 
 
750dde0
 
 
 
 
d9008cd
 
 
750dde0
d9008cd
bb6a3e5
e692be8
750dde0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a49a9e
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
"""
╔═══════════════════════════════════════════════════════════════════════════════════════╗
β•‘  ECHOLENS β€” Realtime Vision Assistant for Blind & Low-Vision Users             β•‘
β•‘                                                                                β•‘
β•‘  Keyboard:  D = Describe  Β·  R = Toggle realtime  Β·  Esc = Stop  Β·  P = Repeat β•‘
β•‘  Voice Commands: Click "Enable Voice Commands" for hands-free control          β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
"""

from __future__ import annotations

import asyncio
import hashlib
import io
import os
import re
import threading
import time
import warnings
from collections import deque
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import Any, Dict, List, Literal, Optional, Tuple

import gradio as gr
import numpy as np
import torch
from PIL import Image, ImageEnhance
from transformers import AutoModelForCausalLM, AutoProcessor

# ── Suppress noisy warnings ─────────────────────────────────────────────────
warnings.filterwarnings("ignore", message=".*Torch was not compiled with flash attention.*")
warnings.filterwarnings("ignore", message=".*Using the model.*inference mode.*")

# ═══════════════════════════════════════════════════════════════
#  CONFIGURATION
# ═══════════════════════════════════════════════════════════════


class Config:
    """Central configuration β€” tweak values here."""

    # Timing
    CAPTURE_INTERVAL: float = 3.0  # seconds between realtime captures
    SCENE_THRESHOLD: float = 0.10  # dHash distance to treat as "same scene"
    DEBOUNCE_MS: int = 800  # ms to debounce rapid requests
    MAX_DIM: int = 768  # downscale before inference
    HASH_SIZE: int = 16  # perceptual hash grid size

    # Audio
    TTS_TIMEOUT: float = 12.0
    TTS_RATE: str = "+8%"  # slightly faster speech
    AUDIO_FORMAT: str = "mp3"
    MAX_QUEUE_SIZE: int = 3  # max pending audio announcements

    # Model
    MODEL_NAME: str = "microsoft/Florence-2-base"
    MAX_NEW_TOKENS: Dict[str, int] = {
        "<CAPTION>": 64,
        "<DETAILED_CAPTION>": 120,
        "<MORE_DETAILED_CAPTION>": 200,
        "<OD>": 256,
        "<OCR>": 300,
    }

    # UI
    APP_NAME: str = "EchoLens"
    APP_VERSION: str = "2.0"


CONFIG = Config()

# ═══════════════════════════════════════════════════════════════
#  VOICE CONFIGURATION
# ═══════════════════════════════════════════════════════════════

VOICE_MAP: Dict[str, str] = {
    "Aria β€” Female US": "en-US-AriaNeural",
    "Guy β€” Male US": "en-US-GuyNeural",
    "Jenny β€” Female US": "en-US-JennyNeural",
    "Sonia β€” Female UK": "en-GB-SoniaNeural",
    "Ryan β€” Male UK": "en-GB-RyanNeural",
    "Emily β€” Female Australia": "en-AU-EmilyNeural",
    "William β€” Male Australia": "en-AU-WilliamNeural",
    "Natasha β€” Female Australia": "en-AU-NatashaNeural",
    "Swara β€” Female India (Hindi)": "hi-IN-SwaraNeural",
    "Madhur β€” Male India (Hindi)": "hi-IN-MadhurNeural",
}

TASKS: Dict[str, str] = {
    "Quick Caption": "<CAPTION>",
    "Describe Scene": "<DETAILED_CAPTION>",
    "Detailed Description": "<MORE_DETAILED_CAPTION>",
    "Read Text (OCR)": "<OCR>",
    "Detect Objects": "<OD>",
}

TASK_DESCRIPTIONS: Dict[str, str] = {
    "Quick Caption": "A brief one-sentence description",
    "Describe Scene": "A paragraph describing the scene",
    "Detailed Description": "A thorough multi-sentence description",
    "Read Text (OCR)": "Reads any visible text aloud",
    "Detect Objects": "Names objects and their locations",
}


# ═══════════════════════════════════════════════════════════════
#  DEVICE & MODEL LOADING
# ═══════════════════════════════════════════════════════════════


def get_device() -> str:
    """Select best available device."""
    if torch.cuda.is_available():
        return "cuda"
    elif torch.backends.mps.is_available():
        return "mps"
    return "cpu"


DEVICE: str = get_device()
DTYPE: torch.dtype = torch.float16 if DEVICE == "cuda" else torch.float32

print(f"πŸ–₯️  Device: {DEVICE.upper()}")
print(f"πŸ”’ Dtype: {DTYPE}")

# ── Model Loading ──────────────────────────────────────────────
_model_loaded = threading.Event()

processor: Optional[AutoProcessor] = None
model: Optional[AutoModelForCausalLM] = None


def _load_model():
    """Load Florence-2 model in background thread."""
    global model, processor
    try:
        model = AutoModelForCausalLM.from_pretrained(
            CONFIG.MODEL_NAME,
            trust_remote_code=True,
            torch_dtype=DTYPE,
        ).to(DEVICE).eval()

        processor = AutoProcessor.from_pretrained(
            CONFIG.MODEL_NAME,
            trust_remote_code=True,
        )
        print("βœ… Model loaded successfully")
    except Exception as e:
        print(f"❌ Model loading failed: {e}")
        raise


# Load synchronously on startup (can be made async if needed)
_load_model()
_model_loaded.set()

# ── Background Warmup ──────────────────────────────────────────
_warmup_done = threading.Event()


def _warmup_model():
    """Run a dummy inference to warm up CUDA kernels."""
    if model is None or processor is None:
        return
    try:
        dummy = Image.new("RGB", (224, 224), 128)
        inputs = processor(text="<CAPTION>", images=dummy, return_tensors="pt").to(DEVICE)
        with torch.inference_mode():
            model.generate(
                input_ids=inputs["input_ids"],
                pixel_values=inputs["pixel_values"],
                max_new_tokens=10,
                num_beams=1,
            )
        _warmup_done.set()
        print("πŸ”₯ Model warmed up")
    except Exception as e:
        print(f"Warmup warning: {e}")


threading.Thread(target=_warmup_model, daemon=True).start()


# ═══════════════════════════════════════════════════════════════
#  AUDIO QUEUE SYSTEM
# ═══════════════════════════════════════════════════════════════


class AudioQueue:
    """Thread-safe FIFO audio queue with interruption support."""

    def __init__(self, max_size: int = 3):
        self._queue: deque[Tuple[str, str]] = deque()  # (text, audio_path)
        self._current: Optional[str] = None
        self._lock = threading.Lock()
        self._counter = 0
        self._max_size = max_size

    def enqueue(self, text: str, audio_path: str) -> Optional[str]:
        """Add audio to queue. Returns the path to play (or None if queue full)."""
        with self._lock:
            if len(self._queue) >= self._max_size:
                # Remove oldest
                oldest = self._queue.popleft()
                self._safe_delete(oldest[1])
            self._queue.append((text, audio_path))
            self._counter += 1
            return audio_path

    def dequeue(self) -> Optional[Tuple[str, str]]:
        """Get next audio item."""
        with self._lock:
            if self._queue:
                item = self._queue.popleft()
                self._current = item[1]
                return item
            return None

    def clear(self):
        """Clear all queued audio and delete files."""
        with self._lock:
            for _, path in self._queue:
                self._safe_delete(path)
            self._queue.clear()
            self._current = None

    def interrupt(self):
        """Interrupt current and clear queue."""
        self.clear()

    @property
    def is_empty(self) -> bool:
        with self._lock:
            return len(self._queue) == 0

    @property
    def size(self) -> int:
        with self._lock:
            return len(self._queue)

    @staticmethod
    def _safe_delete(path: str):
        try:
            if path and os.path.exists(path):
                os.unlink(path)
        except OSError:
            pass


# Global audio queue
AUDIO_QUEUE = AudioQueue(max_size=CONFIG.MAX_QUEUE_SIZE)


# ═══════════════════════════════════════════════════════════════
#  TTS ENGINE (edge-tts)
# ═══════════════════════════════════════════════════════════════


def init_tts_loop() -> asyncio.AbstractEventLoop:
    """Create a dedicated event loop for TTS in a background thread."""
    loop = asyncio.new_event_loop()

    def _run():
        asyncio.set_event_loop(loop)
        loop.run_forever()

    threading.Thread(target=_run, daemon=True).start()
    return loop


_TTS_LOOP = init_tts_loop()


def text_to_speech(text: str, voice_id: str = "en-US-AriaNeural") -> Optional[str]:
    """Convert text to speech, returning the audio file path."""
    if not text or not text.strip():
        return None

    try:
        import tempfile

        import edge_tts

        async def _generate():
            with tempfile.NamedTemporaryFile(delete=False, suffix=f".{CONFIG.AUDIO_FORMAT}") as f:
                path = f.name
            communicate = edge_tts.Communicate(
                text.strip(),
                voice=voice_id,
                rate=CONFIG.TTS_RATE,
            )
            await communicate.save(path)
            return path

        future = asyncio.run_coroutine_threadsafe(_generate(), _TTS_LOOP)
        return future.result(timeout=CONFIG.TTS_TIMEOUT)
    except Exception as e:
        print(f"TTS error: {e}")
        return None


# ═══════════════════════════════════════════════════════════════
#  APPLICATION STATE
# ═══════════════════════════════════════════════════════════════


@dataclass
class AppState:
    """Thread-safe application state."""

    # Scene hashing
    last_hash: Optional[bytes] = None
    last_task: str = ""
    last_text: str = ""
    last_audio: Optional[str] = None

    # Realtime
    realtime_active: bool = False
    last_capture_time: float = 0.0

    # History
    history: List[Dict[str, Any]] = field(default_factory=list)
    max_history: int = 50

    # Stats
    total_describes: int = 0
    total_realtime_captures: int = 0

    # Lock
    _lock: threading.Lock = field(default_factory=threading.Lock)
    _tmp_files: List[Tuple[str, float]] = field(default_factory=list)

    def update(self, hash_val: bytes, task: str, text: str, audio: Optional[str]):
        """Update state with new capture results."""
        with self._lock:
            self._cleanup_old_files()
            if self.last_audio and os.path.exists(self.last_audio):
                try:
                    os.unlink(self.last_audio)
                except OSError:
                    pass
            self.last_hash = hash_val
            self.last_task = task
            self.last_text = text
            self.last_audio = audio
            if audio:
                self._tmp_files.append((audio, time.time()))
            # Add to history
            self.history.insert(0, {
                "time": time.strftime("%H:%M:%S"),
                "task": task,
                "text": text,
            })
            if len(self.history) > self.max_history:
                self.history = self.history[: self.max_history]

    def is_duplicate(self, hash_val: bytes, task: str) -> bool:
        """Check if this hash+task combination was already processed."""
        with self._lock:
            return (
                self.last_hash is not None
                and self.last_hash == hash_val
                and self.last_task == task
                and self.last_text != ""
            )

    def get_last(self) -> Tuple[str, Optional[str]]:
        """Get last description text and audio."""
        with self._lock:
            return self.last_text, self.last_audio

    def add_stat(self, key: str):
        with self._lock:
            if key == "describe":
                self.total_describes += 1
            elif key == "realtime":
                self.total_realtime_captures += 1

    def get_stats(self) -> Dict[str, Any]:
        with self._lock:
            return {
                "describes": self.total_describes,
                "realtime_captures": self.total_realtime_captures,
                "history_count": len(self.history),
            }

    def _cleanup_old_files(self):
        """Remove temp files older than 5 minutes."""
        now = time.time()
        keep = []
        for path, ts in self._tmp_files:
            if now - ts > 300:  # 5 minutes
                try:
                    os.unlink(path)
                except OSError:
                    pass
            else:
                keep.append((path, ts))
        self._tmp_files = keep


# Global state
APP_STATE = AppState()


# ═══════════════════════════════════════════════════════════════
#  IMAGE PROCESSING
# ═══════════════════════════════════════════════════════════════


def compute_hash(image: Image.Image, size: int = 16) -> bytes:
    """Compute difference hash (dHash) for scene change detection."""
    gray = image.resize((size + 1, size), Image.LANCZOS).convert("L")
    pixels = list(gray.getdata())
    return bytes(
        1 if pixels[y * (size + 1) + x] > pixels[y * (size + 1) + x + 1] else 0
        for y in range(size)
        for x in range(size)
    )


def hash_distance(a: Optional[bytes], b: Optional[bytes]) -> float:
    """Compute normalized Hamming distance between two hashes."""
    if a is None or b is None:
        return 1.0
    if len(a) != len(b):
        return 1.0
    return sum(x != y for x, y in zip(a, b)) / len(a)


def preprocess_image(image: Image.Image) -> Image.Image:
    """Resize image for inference while preserving aspect ratio."""
    w, h = image.size
    if max(w, h) <= CONFIG.MAX_DIM:
        return image
    scale = CONFIG.MAX_DIM / max(w, h)
    new_size = (int(w * scale), int(h * scale))
    return image.resize(new_size, Image.LANCZOS)


def auto_enhance(image: Image.Image) -> Image.Image:
    """Auto-enhance image for better vision model performance."""
    # Slight contrast boost helps Florence-2 on low-light images
    enhancer = ImageEnhance.Contrast(image)
    image = enhancer.enhance(1.1)
    return image


# ═══════════════════════════════════════════════════════════════
#  CORE VISION INFERENCE
# ═══════════════════════════════════════════════════════════════


def run_inference(image: Image.Image, task_label: str) -> str:
    """Run Florence-2 inference on an image."""
    if model is None or processor is None:
        return "Error: Model not loaded. Please wait or restart."

    task_token = TASKS.get(task_label, "<CAPTION>")
    max_tokens = CONFIG.MAX_NEW_TOKENS.get(task_token, 64)

    try:
        # Preprocess
        image = preprocess_image(image)
        image = auto_enhance(image)

        # Prepare inputs
        inputs = processor(
            text=task_token,
            images=image,
            return_tensors="pt",
        ).to(DEVICE)

        # Generate
        with torch.inference_mode():
            output_ids = model.generate(
                input_ids=inputs["input_ids"],
                pixel_values=inputs["pixel_values"],
                max_new_tokens=max_tokens,
                do_sample=False,
                num_beams=1,
                use_cache=True,
            )

        # Decode
        raw_text = processor.batch_decode(output_ids, skip_special_tokens=False)[0]
        result = processor.post_process_generation(
            raw_text,
            task=task_token,
            image_size=(image.width, image.height),
        )

        # Format output based on task
        if task_token == "<OD>":
            od_data = result.get("<OD>", {})
            return format_object_detection(od_data)
        elif task_token == "<OCR>":
            text_found = result.get("<OCR>", "").strip()
            if not text_found:
                return "No text detected in the image."
            return f"Text found: {text_found}"
        else:
            caption = result.get(task_token, "").strip()
            if not caption:
                return "I couldn't understand what's in the image. Please try again."
            return caption

    except torch.cuda.OutOfMemoryError:
        torch.cuda.empty_cache()
        return "The image is too large for memory. Try a smaller image."
    except Exception as e:
        print(f"Inference error: {e}")
        return f"Sorry, I had trouble analyzing that image. Please try again."


def format_object_detection(od_data: Dict) -> str:
    """Format object detection results into natural language."""
    if not od_data or not od_data.get("labels"):
        return "No objects detected in the image."

    labels = od_data.get("labels", [])
    bboxes = od_data.get("bboxes", [])

    if not labels:
        return "No objects detected in the image."

    # Build object list with positions
    objects: List[Tuple[str, str]] = []
    for label, bbox in zip(labels, bboxes):
        x1, _, x2, _ = bbox
        cx = (x1 + x2) / 2
        # Florence uses 0-999 coordinate space
        if cx < 333:
            pos = "on the left"
        elif cx < 666:
            pos = "in the center"
        else:
            pos = "on the right"
        objects.append((label.strip(), pos))

    # Deduplicate (keep first occurrence of each label type)
    seen: set = set()
    unique: List[Tuple[str, str]] = []
    for lbl, pos in objects:
        key = lbl.lower()
        if key and key not in seen:
            seen.add(key)
            unique.append((lbl, pos))

    if not unique:
        return "No objects detected in the image."

    # Format naturally
    if len(unique) == 1:
        lbl, pos = unique[0]
        return f"I see {lbl} {pos}."

    parts = [f"{lbl} {pos}" for lbl, pos in unique]

    if len(parts) <= 5:
        return "I see " + ", ".join(parts[:-1]) + f", and {parts[-1]}."
    else:
        summary = ", ".join(parts[:5])
        return f"I see {len(unique)} objects: {summary}, and {len(unique) - 5} more."


# ═══════════════════════════════════════════════════════════════
#  HANDLER FUNCTIONS
# ═══════════════════════════════════════════════════════════════


def _status_html(msg: str) -> str:
    """Wrap a status message in the styled status-bar div."""
    return f'<div id="echo-status" role="status" aria-live="polite">{msg}</div>'


def describe_now(image, task_label: str, voice_name: str):
    """
    Manual describe handler.
    Streams words visually, then returns final text + audio.
    """
    if image is None:
        yield "πŸ“· Please open the camera or upload an image first.", None, _status_html("Waiting for image...")
        return

    # Convert to PIL if needed
    if not isinstance(image, Image.Image):
        image = Image.fromarray(image)

    # Compute hash
    img_hash = compute_hash(image)
    task_key = TASKS.get(task_label, "<CAPTION>")

    # Check cache
    if APP_STATE.is_duplicate(img_hash, task_key):
        text, audio = APP_STATE.get_last()
        yield text, audio, _status_html(f"βœ“ Cached result β€’ {task_label}")
        return

    # Run inference
    yield "⏳ Analyzing image...", None, _status_html("⏳ Processing image...")

    caption = run_inference(image, task_label)
    APP_STATE.add_stat("describe")

    # Stream words
    words = caption.split()
    partial = ""
    for i, w in enumerate(words):
        partial += (" " if partial else "") + w
        if (i + 1) % 3 == 0 or i == len(words) - 1:
            yield partial, None, _status_html(f"⏳ Generating description... ({i + 1}/{len(words)} words)")

    # Generate TTS
    voice_id = VOICE_MAP.get(voice_name, "en-US-AriaNeural")
    audio_path = text_to_speech(caption, voice_id)

    # Update state
    APP_STATE.update(img_hash, task_key, caption, audio_path)

    yield caption, audio_path, _status_html(f"βœ… {task_label} complete β€’ {len(words)} words")


def handle_upload(image, task_label: str, voice_name: str):
    """Handle uploaded image β€” always processes fresh, bypasses scene cache."""
    if image is None:
        yield "πŸ“ Please upload an image first.", None, _status_html("Waiting for upload...")
        return

    # Convert to PIL if needed
    if not isinstance(image, Image.Image):
        image = Image.fromarray(image)

    yield "⏳ Analyzing uploaded image...", None, _status_html("⏳ Processing uploaded image...")

    caption = run_inference(image, task_label)
    APP_STATE.add_stat("describe")

    # Stream words visually
    words = caption.split()
    partial = ""
    for i, w in enumerate(words):
        partial += (" " if partial else "") + w
        if (i + 1) % 3 == 0 or i == len(words) - 1:
            yield partial, None, _status_html(f"⏳ Generating description... ({i + 1}/{len(words)} words)")

    # Generate TTS
    voice_id = VOICE_MAP.get(voice_name, "en-US-AriaNeural")
    audio_path = text_to_speech(caption, voice_id)

    # Update app state
    img_hash = compute_hash(image)
    APP_STATE.update(img_hash, TASKS.get(task_label, "<CAPTION>"), caption, audio_path)

    yield caption, audio_path, _status_html(f"βœ… Upload β€” {task_label} complete β€’ {len(words)} words")


def handle_realtime_stream(image, task_label: str, voice_name: str, rt_active: bool):
    """
    Called automatically by webcam.stream() every CAPTURE_INTERVAL seconds.
    Only processes if realtime toggle is ON.
    """
    if not rt_active:
        return gr.update(), gr.update(), _status_html("⚫ Realtime paused β€” press R to start")

    if image is None:
        return gr.update(), gr.update(), _status_html("πŸ“· No camera feed detected")

    # Convert to PIL
    if not isinstance(image, Image.Image):
        image = Image.fromarray(image)

    # Debounce check
    now = time.time()
    if now - APP_STATE.last_capture_time < 1.0:
        return gr.update(), gr.update(), _status_html("⏳ Debouncing...")
    APP_STATE.last_capture_time = now

    # Compute hash
    img_hash = compute_hash(image)
    task_key = TASKS.get(task_label, "<CAPTION>")

    # Scene change detection
    if APP_STATE.last_hash is not None:
        dist = hash_distance(img_hash, APP_STATE.last_hash)
        if dist < CONFIG.SCENE_THRESHOLD:
            return (
                gr.update(),
                gr.update(),
                _status_html(f"🟒 Realtime active β€’ Scene unchanged ({1 - dist:.0%} similar)"),
            )

    # Cache check
    if APP_STATE.is_duplicate(img_hash, task_key):
        text, audio = APP_STATE.get_last()
        return (
            text,
            audio,
            _status_html("🟒 Realtime active β€’ Used cached result"),
        )

    # Run inference
    caption = run_inference(image, task_label)
    APP_STATE.add_stat("realtime")

    # Generate TTS
    voice_id = VOICE_MAP.get(voice_name, "en-US-AriaNeural")
    audio_path = text_to_speech(caption, voice_id)

    # Update state
    APP_STATE.update(img_hash, task_key, caption, audio_path)

    dist = hash_distance(img_hash, APP_STATE.last_hash) if APP_STATE.last_hash else 1.0
    status = _status_html(f"🟒 Realtime active β€’ Scene changed β€’ {len(caption.split())} words")

    return caption, audio_path, status


def toggle_realtime(current: bool) -> Tuple[bool, str, str]:
    """Toggle realtime mode on/off."""
    new_state = not current
    if new_state:
        label = "🟒 Stop Realtime (R)"
        status = _status_html("🟒 Realtime ON β€” describing every 3 seconds")
    else:
        AUDIO_QUEUE.interrupt()
        label = "⚫ Start Realtime (R)"
        status = _status_html("⚫ Realtime OFF β€” press R or click to start")
    return new_state, label, status


def repeat_last(voice_name: str):
    """Repeat the last description."""
    text, _ = APP_STATE.get_last()
    if not text:
        return "No previous description to repeat.", None, _status_html("ℹ️ No history available")

    voice_id = VOICE_MAP.get(voice_name, "en-US-AriaNeural")
    audio_path = text_to_speech(text, voice_id)

    return text, audio_path, _status_html("πŸ” Repeated last description")


def stop_all():
    """Stop all audio and clear state."""
    AUDIO_QUEUE.interrupt()
    return "", None, _status_html("⏹ Stopped β€” press D to describe or R for realtime")


def get_history() -> str:
    """Get formatted history."""
    if not APP_STATE.history:
        return "No descriptions yet."
    lines = []
    for i, item in enumerate(APP_STATE.history[:10], 1):
        lines.append(f"{i}. [{item['time']}] {item['task']}: {item['text'][:80]}...")
    return "\n".join(lines)


def get_stats() -> str:
    """Get usage statistics."""
    stats = APP_STATE.get_stats()
    return (
        f"πŸ“Š Statistics:\n"
        f"β€’ Manual describes: {stats['describes']}\n"
        f"β€’ Realtime captures: {stats['realtime_captures']}\n"
        f"β€’ History entries: {stats['history_count']}"
    )


# ═══════════════════════════════════════════════════════════════
#  CSS STYLES
# ═══════════════════════════════════════════════════════════════

CSS = """
/* ── Base ─────────────────────────────────────────────────── */
:root {
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --success: #059669;
    --warning: #d97706;
    --danger: #dc2626;
    --bg-primary: #ffffff;
    --bg-secondary: #f8fafc;
    --bg-dark: #0f172a;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border: #e2e8f0;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-lg: 0 10px 25px -5px rgba(0,0,0,0.1), 0 8px 10px -6px rgba(0,0,0,0.1);
}

/* ── Font size modes ──────────────────────────────────────── */
body.fs-normal  { --base-size: 16px; }
body.fs-large   { --base-size: 20px; }
body.fs-xlarge  { --base-size: 26px; }

body {
    font-size: var(--base-size, 16px) !important;
}

/* ── High Contrast Mode ───────────────────────────────────── */
body.hc {
    filter: contrast(1.7) brightness(1.05);
}
body.hc .gr-button {
    border: 2px solid #000 !important;
}
body.hc .gr-input,
body.hc .gr-textbox textarea {
    border: 2px solid #000 !important;
}

/* ── Layout ───────────────────────────────────────────────── */
.gr-button {
    min-height: 52px !important;
    font-size: var(--base-size, 16px) !important;
    border-radius: var(--radius) !important;
    font-weight: 600 !important;
    transition: all 0.15s ease !important;
    box-shadow: var(--shadow) !important;
}
.gr-button:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-lg) !important;
}
.gr-button:active {
    transform: translateY(0);
}

/* Primary button */
.gr-button-primary {
    background: linear-gradient(135deg, var(--accent), var(--accent-hover)) !important;
    border: none !important;
}

/* ── Textbox ──────────────────────────────────────────────── */
.gr-textbox textarea {
    font-size: calc(var(--base-size, 16px) * 1.15) !important;
    line-height: 1.7 !important;
    border-radius: var(--radius) !important;
    padding: 14px !important;
    font-family: 'Segoe UI', system-ui, sans-serif !important;
}

/* ── Status Bar ───────────────────────────────────────────── */
#echo-status {
    background: linear-gradient(135deg, #1e293b, #0f172a);
    color: #f1f5f9;
    padding: 14px 20px;
    border-radius: var(--radius);
    font-size: calc(var(--base-size, 16px) * 0.95);
    font-weight: 600;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
    border-left: 4px solid var(--accent);
    transition: all 0.3s ease;
}

/* ── Cards ────────────────────────────────────────────────── */
.echo-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: var(--shadow);
}

/* ── Keyboard Shortcuts Display ───────────────────────────── */
.echo-kbd {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: #e2e8f0;
    border-radius: 6px;
    font-size: calc(var(--base-size, 16px) * 0.8);
    font-family: monospace;
    font-weight: 600;
    color: #334155;
}

/* ── Tips Box ─────────────────────────────────────────────── */
.echo-tips {
    background: linear-gradient(135deg, #ecfdf5, #d1fae5);
    border: 1px solid #a7f3d0;
    border-radius: var(--radius);
    padding: 18px;
    margin-top: 14px;
    font-size: calc(var(--base-size, 16px) * 0.9);
    line-height: 1.7;
}

/* ── Radio buttons ────────────────────────────────────────── */
.gr-radio {
    font-size: calc(var(--base-size, 16px) * 0.95) !important;
}

/* ── Dropdown ─────────────────────────────────────────────── */
.gr-dropdown {
    font-size: calc(var(--base-size, 16px) * 0.95) !important;
}

/* ── Section headers ──────────────────────────────────────── */
.echo-section-title {
    font-size: calc(var(--base-size, 16px) * 1.2);
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--border);
}

/* ── Accessibility Toolbar ───────────────────────────────── */
.echo-toolbar {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 16px;
    padding: 12px;
    background: var(--bg-secondary);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    align-items: center;
}

.echo-toolbar button {
    padding: 8px 16px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: white;
    cursor: pointer;
    font-weight: 600;
    font-size: calc(var(--base-size, 16px) * 0.85);
    transition: all 0.15s;
}
.echo-toolbar button:hover {
    background: #e2e8f0;
    transform: translateY(-1px);
}

/* ── Stats display ────────────────────────────────────────── */
.echo-stats {
    font-family: monospace;
    font-size: calc(var(--base-size, 16px) * 0.85);
    color: var(--text-secondary);
    background: var(--bg-secondary);
    padding: 10px 14px;
    border-radius: var(--radius);
    margin-top: 10px;
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 768px) {
    .gr-button {
        width: 100% !important;
        min-height: 56px !important;
    }
    .echo-toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    .echo-toolbar button {
        width: 100%;
    }
}

/* ── Focus indicators for accessibility ───────────────────── */
button:focus-visible,
.gr-button:focus-visible {
    outline: 3px solid var(--accent) !important;
    outline-offset: 2px !important;
}

/* ── Screen reader only ───────────────────────────────────── */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* ── Loading animation ────────────────────────────────────── */
@keyframes pulse-dot {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}
.echo-loading::after {
    content: "...";
    animation: pulse-dot 1.5s infinite;
}
"""

# ═══════════════════════════════════════════════════════════════
#  GRADIO UI
# ═══════════════════════════════════════════════════════════════


def build_ui() -> gr.Blocks:
    """Build the Gradio user interface."""

    with gr.Blocks(
        title=f"{CONFIG.APP_NAME} v{CONFIG.APP_VERSION} β€” Vision Assistant",
        css=CSS,
        theme=gr.themes.Soft(
            primary_hue="blue",
            secondary_hue="slate",
            neutral_hue="slate",
            spacing_size="md",
            radius_size="md",
        ),
        analytics_enabled=False,
    ) as demo:

        # ── Live region for screen readers ───────────────────
        gr.HTML(
            '<div class="sr-only" aria-live="assertive" aria-atomic="true" '
            'id="aria-live-region" role="status"></div>'
        )

        # ── Status Bar ───────────────────────────────────────
        status_bar = gr.HTML(
            '<div id="echo-status" role="status" aria-live="polite">'
            "βœ… Ready β€” Press D to describe what the camera sees"
            "</div>"
        )

        # ── Header ───────────────────────────────────────────
        gr.Markdown(
            f"# πŸ‘οΈ {CONFIG.APP_NAME} β€” Realtime Vision Assistant",
            elem_classes=["echo-section-title"],
        )
        gr.Markdown(
            "Helping blind and visually impaired users understand their surroundings. "
            "Press **D** to describe, **R** for realtime mode, **P** to repeat."
        )

        # ── Accessibility Toolbar ────────────────────────────
        gr.HTML("""
        <div class="echo-toolbar" role="toolbar" aria-label="Accessibility controls">
            <span style="font-weight:600;color:#475569;font-size:0.9em">Text Size:</span>
            <button onclick="document.body.classList.remove('fs-large','fs-xlarge');document.body.classList.add('fs-normal')"
                aria-label="Normal text size">A</button>
            <button onclick="document.body.classList.remove('fs-large','fs-xlarge');document.body.classList.add('fs-large')"
                aria-label="Large text size" style="font-size:1.15em">A+</button>
            <button onclick="document.body.classList.remove('fs-large','fs-xlarge');document.body.classList.add('fs-xlarge')"
                aria-label="Extra large text size" style="font-size:1.3em">A++</button>
            <button onclick="document.body.classList.toggle('hc')"
                aria-label="Toggle high contrast mode"
                style="background:#1e293b;color:white">⬛ High Contrast</button>
            <span style="margin-left:auto;font-size:0.85em;color:#6b7280;align-self:center">
                <span class="echo-kbd">D</span> describe Β·
                <span class="echo-kbd">R</span> realtime Β·
                <span class="echo-kbd">P</span> repeat Β·
                <span class="echo-kbd">Esc</span> stop
            </span>
        </div>
        """
        )

        # ── Realtime state (single source of truth) ──────────
        rt_state = gr.State(False)

        with gr.Row():
            # ════════════════════════════════════════════════
            #  LEFT COLUMN β€” Inputs
            # ════════════════════════════════════════════════
            with gr.Column(scale=1):

                # ── Camera ─────────────────────────────────
                webcam = gr.Image(
                    label="πŸ“· Camera Feed",
                    type="numpy",
                    sources=["webcam"],
                    streaming=True,
                    height=260,
                    elem_id="echo-webcam",
                )

                # ── Upload ─────────────────────────────────
                upload = gr.Image(
                    label="πŸ“ Or Upload Image",
                    type="numpy",
                    sources=["upload"],
                    height=140,
                    elem_id="echo-upload",
                )

                # ── Task Selection ─────────────────────────
                task_radio = gr.Radio(
                    choices=list(TASKS.keys()),
                    value="Quick Caption",
                    label="What should I do?",
                    info="Select the type of description you want",
                )

                # Task description
                task_info = gr.Textbox(
                    value=TASK_DESCRIPTIONS["Quick Caption"],
                    label="",
                    interactive=False,
                    max_lines=1,
                    show_label=False,
                    container=False,
                    elem_classes=["echo-stats"],
                )

                # ── Voice Selection ────────────────────────
                voice_dropdown = gr.Dropdown(
                    choices=list(VOICE_MAP.keys()),
                    value="Aria β€” Female US",
                    label="πŸ”Š Voice",
                    info="Choose a voice for spoken descriptions",
                )

                # ── Describe Button ────────────────────────
                describe_btn = gr.Button(
                    "πŸ” Describe Now (D)",
                    variant="primary",
                    size="lg",
                    elem_id="echo-describe-btn",
                )

                # ── Realtime Toggle ────────────────────────
                realtime_btn = gr.Button(
                    "⚫ Start Realtime (R)",
                    variant="secondary",
                    size="lg",
                    elem_id="echo-rt-btn",
                )

            # ════════════════════════════════════════════════
            #  RIGHT COLUMN β€” Output
            # ════════════════════════════════════════════════
            with gr.Column(scale=1):

                # ── Caption Output ─────────────────────────
                caption_box = gr.Textbox(
                    label="πŸ“ Description",
                    lines=6,
                    interactive=False,
                    show_copy_button=True,
                    placeholder="Description will appear here...",
                    elem_id="echo-caption",
                )

                # ── Audio Output ───────────────────────────
                audio_player = gr.Audio(
                    label="πŸ”Š Audio",
                    type="filepath",
                    autoplay=True,
                    elem_id="echo-audio",
                )

                # ── Action Buttons ─────────────────────────
                with gr.Row():
                    repeat_btn = gr.Button(
                        "πŸ” Repeat Last (P)",
                        variant="secondary",
                        size="lg",
                        elem_id="echo-repeat-btn",
                    )
                    stop_btn = gr.Button(
                        "⏹ Stop All (Esc)",
                        variant="stop",
                        size="lg",
                        elem_id="echo-stop-btn",
                    )

                # ── Tips ───────────────────────────────────
                gr.HTML("""
                <div class="echo-tips" role="complementary" aria-label="Tips for users">
                    <strong style="color:#065f46;font-size:1.05em">πŸ’‘ Tips:</strong><br>
                    β€’ <strong>D</strong> β€” Describe what the camera sees right now<br>
                    β€’ <strong>R</strong> β€” Start/stop auto-description every 3 seconds<br>
                    β€’ <strong>P</strong> β€” Repeat the last description<br>
                    β€’ <strong>Esc</strong> β€” Stop all audio and realtime mode<br>
                    β€’ <strong>Read Text</strong> β€” Reads signs, labels, screens (OCR)<br>
                    β€’ <strong>Detect Objects</strong> β€” Hear what's where in the scene
                </div>
                """)

        # ═══════════════════════════════════════════════════
        #  BOTTOM SECTION β€” Stats & History
        # ═══════════════════════════════════════════════════
        with gr.Accordion("πŸ“Š Session Statistics", open=False):
            stats_box = gr.Textbox(
                value="Press 'Get Stats' to see usage statistics",
                label="Statistics",
                interactive=False,
                lines=4,
            )
            stats_btn = gr.Button("Refresh Statistics", size="sm")

        # ═══════════════════════════════════════════════════
        #  EVENT WIRING
        # ═══════════════════════════════════════════════════

        # Update task description when task changes
        def update_task_info(task_label):
            return TASK_DESCRIPTIONS.get(task_label, "")

        task_radio.change(
            update_task_info,
            inputs=[task_radio],
            outputs=[task_info],
        )

        # Manual Describe
        describe_btn.click(
            describe_now,
            inputs=[webcam, task_radio, voice_dropdown],
            outputs=[caption_box, audio_player, status_bar],
            show_progress="minimal",
        )

        # Upload
        upload.change(
            handle_upload,
            inputs=[upload, task_radio, voice_dropdown],
            outputs=[caption_box, audio_player, status_bar],
            show_progress="minimal",
        )

        # Realtime Toggle
        realtime_btn.click(
            toggle_realtime,
            inputs=[rt_state],
            outputs=[rt_state, realtime_btn, status_bar],
        )

        # Realtime Stream
        webcam.stream(
            handle_realtime_stream,
            inputs=[webcam, task_radio, voice_dropdown, rt_state],
            outputs=[caption_box, audio_player, status_bar],
            stream_every=CONFIG.CAPTURE_INTERVAL,
            time_limit=None,
        )

        # Repeat
        repeat_btn.click(
            repeat_last,
            inputs=[voice_dropdown],
            outputs=[caption_box, audio_player, status_bar],
            show_progress=False,
        )

        # Stop
        stop_btn.click(
            stop_all,
            inputs=[],
            outputs=[caption_box, audio_player, status_bar],
            show_progress=False,
        )

        # Stats
        stats_btn.click(
            get_stats,
            inputs=[],
            outputs=[stats_box],
        )

        # ═══════════════════════════════════════════════════
        #  KEYBOARD SHORTCUTS (JavaScript)
        # ═══════════════════════════════════════════════════
        gr.HTML("""
        <script>
        // Helper: finds the real <button> inside a Gradio wrapper by elem_id
        function gradioBtn(id) {
            var wrapper = document.getElementById(id);
            if (!wrapper) return null;
            // Gradio wraps buttons in a div; the real <button> is inside
            return wrapper.tagName === 'BUTTON' ? wrapper : wrapper.querySelector('button');
        }

        function clickGradioBtn(id) {
            var btn = gradioBtn(id);
            if (btn) {
                btn.click();
                return true;
            }
            return false;
        }

        document.addEventListener('keydown', function(e) {
            // Don't trigger shortcuts when typing in inputs
            if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.isContentEditable) {
                return;
            }

            var key = e.key.toLowerCase();

            if (key === 'd') {
                e.preventDefault();
                clickGradioBtn('echo-describe-btn');
            }
            else if (key === 'r') {
                e.preventDefault();
                clickGradioBtn('echo-rt-btn');
            }
            else if (key === 'p') {
                e.preventDefault();
                clickGradioBtn('echo-repeat-btn');
            }
            else if (key === 'escape') {
                e.preventDefault();
                clickGradioBtn('echo-stop-btn');
            }
        });

        // Announce to screen readers
        function announce(message) {
            var live = document.getElementById('aria-live-region');
            if (live) {
                live.textContent = message;
                setTimeout(function() { live.textContent = ''; }, 1000);
            }
        }

        // Hook button clicks for announcements
        document.addEventListener('click', function(e) {
            var btn = e.target.closest('button');
            if (!btn) return;
            var wrapper = btn.closest('[id]');
            if (!wrapper) return;
            if (wrapper.id === 'echo-describe-btn') announce('Describing scene');
            if (wrapper.id === 'echo-rt-btn') announce('Toggling realtime mode');
            if (wrapper.id === 'echo-repeat-btn') announce('Repeating last description');
            if (wrapper.id === 'echo-stop-btn') announce('Stopping all audio');
        });
        </script>
        """)

    return demo


# ═══════════════════════════════════════════════════════════════
#  MAIN
# ═══════════════════════════════════════════════════════════════

if __name__ == "__main__":
    print(f"πŸš€ Starting {CONFIG.APP_NAME} v{CONFIG.APP_VERSION}")
    print(f"   Device: {DEVICE.upper()}")
    print(f"   Tasks: {list(TASKS.keys())}")
    print(f"   Voices: {list(VOICE_MAP.keys())}")
    print(f"   Realtime interval: {CONFIG.CAPTURE_INTERVAL}s")

    demo = build_ui()

    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        debug=True,
        show_error=True,
        favicon_path=None,
    )