File size: 49,932 Bytes
13c3e03
 
 
 
 
 
 
 
 
 
393fe50
13c3e03
 
 
 
 
 
 
 
 
8fc7f79
 
 
 
 
 
 
 
 
13c3e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fc7f79
13c3e03
 
 
 
 
e9ee848
 
 
 
 
 
 
 
13c3e03
 
 
 
 
 
 
 
 
 
 
 
 
8fc7f79
13c3e03
 
 
e9ee848
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13c3e03
cee53d8
13c3e03
cee53d8
 
 
 
13c3e03
 
cee53d8
 
13c3e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cee53d8
13c3e03
cee53d8
 
 
 
13c3e03
cee53d8
13c3e03
cee53d8
 
13c3e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe6871a
 
13c3e03
 
fe6871a
 
 
 
 
 
 
 
13c3e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fc7f79
 
 
 
13c3e03
 
 
8fc7f79
13c3e03
 
 
8fc7f79
 
13c3e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fc7f79
 
 
13c3e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fc7f79
13c3e03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e877de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
993a840
e877de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e85681
e877de6
 
2e85681
e877de6
 
 
2e85681
e877de6
9660889
 
 
 
 
e877de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e85681
e877de6
 
 
 
 
 
 
 
 
 
 
9660889
 
 
 
e877de6
 
 
 
 
 
 
 
 
 
 
 
 
 
2e85681
e877de6
 
 
 
9660889
 
 
 
 
 
 
2e85681
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
"""
Segment processor for VAD-based audio segmentation, ASR, and text matching.

Splits audio into segments using VAD, transcribes each with Whisper,
matches to verse text using phonemizer, and returns segment info.
"""
import sys
import torch
import numpy as np
import librosa
import librosa.core.audio  # Force eager load to avoid lazy_loader bug with Gradio hot-reload
from pathlib import Path
from dataclasses import dataclass
from typing import List, Optional, Tuple

# Add paths for imports
sys.path.insert(0, str(Path(__file__).parent.parent))
from config import (
    PROJECT_ROOT, SEGMENTER_MODEL, WHISPER_MODEL,
    MIN_SILENCE_DURATION_MS, MIN_SPEECH_DURATION_MS, PAD_DURATION_MS,
    MIN_MATCH_SCORE,
)

# Centralized phonemizer utilities (single source of truth)
from utils.phonemizer_utils import (
    load_phonemizer,
    load_surah_info,
    match_text_to_verse,
    get_total_words_for_verse_range,
)

# Add data directory for recitations_segmenter
DATA_PATH = PROJECT_ROOT / "data"
sys.path.insert(0, str(DATA_PATH))


@dataclass
class VadSegment:
    """Raw VAD segment with just timing info (before Whisper processing)."""
    start_time: float
    end_time: float
    segment_idx: int


@dataclass
class SegmentInfo:
    """Information about a detected speech segment."""
    start_time: float
    end_time: float
    transcribed_text: str
    matched_text: str  # The canonical text portion matched to this segment
    matched_ref: str   # The verse reference for this segment (e.g., "1:2" or "1:2-1:3")
    word_start_idx: int  # 0-based index of first word in this segment
    word_end_idx: int    # 0-based index of last word (inclusive)
    canonical_phonemes: str
    match_score: float
    error: Optional[str] = None


@dataclass 
class SegmentationResult:
    """Result of segmenting and processing audio."""
    segments: List[SegmentInfo]
    full_coverage: bool  # True if segments cover all words
    coverage_warning: Optional[str] = None
    total_words: int = 0


@dataclass
class VadResult:
    """Result of VAD-only processing."""
    vad_segments: List[VadSegment]
    audio: np.ndarray  # Preprocessed audio (float32, mono)
    sample_rate: int
    canonical_words: List[str]
    total_words: int


# Module-level caches for models (phonemizer cache is in utils/phonemizer_utils.py)
_segmenter_cache = {"model": None, "processor": None, "loaded": False}
_whisper_cache = {"model": None, "processor": None, "gen_config": None, "prompt_ids": None, "loaded": False}


def _get_device_and_dtype():
    """Get device and dtype for model loading.

    On HF Spaces with ZeroGPU, returns CPU to defer CUDA init
    until inside a @gpu_decorator function.
    """
    from config import IS_HF_SPACE
    if IS_HF_SPACE:
        return torch.device("cpu"), torch.float32  # Defer GPU until inference
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    dtype = torch.float16 if device.type == "cuda" else torch.float32
    return device, dtype


def initialize_segment_models():
    """
    Pre-load segmenter and whisper models at app startup.
    Call this during app initialization to avoid delay on first audio processing.
    """
    print("Loading segmentation models...")
    _load_segmenter()
    _load_whisper()
    load_phonemizer()  # Uses centralized loader from phonemizer_utils
    print("Segmentation models ready.")


def move_segment_models_to_gpu():
    """Move segmenter and whisper models to GPU.

    Call this inside @gpu_decorator functions on HF Spaces.
    On ZeroGPU, models are loaded on CPU at startup to avoid CUDA init
    in the main process. This function moves them to GPU when a GPU
    lease is active.
    """
    if not torch.cuda.is_available():
        return

    device = torch.device("cuda")
    dtype = torch.float16

    # Move segmenter
    if _segmenter_cache["model"] is not None:
        model = _segmenter_cache["model"]
        current_device = next(model.parameters()).device
        if current_device.type != "cuda":
            model = model.to(device, dtype=dtype)
            _segmenter_cache["model"] = model
            print(f"Moved segmenter to {device}")

    # Move whisper
    if _whisper_cache["model"] is not None:
        model = _whisper_cache["model"]
        current_device = next(model.parameters()).device
        if current_device.type != "cuda":
            model = model.to(device, dtype=dtype)
            _whisper_cache["model"] = model
            print(f"Moved Whisper to {device}")

    # Move prompt_ids tensor if present
    if _whisper_cache["prompt_ids"] is not None:
        prompt_ids = _whisper_cache["prompt_ids"]
        if prompt_ids.device.type != "cuda":
            _whisper_cache["prompt_ids"] = prompt_ids.to(device)


def _load_segmenter():
    """Load the VAD segmenter model.

    Note: This function only loads the model, it does NOT move it between devices.
    Use move_segment_models_to_gpu() to move models to GPU inside GPU contexts.
    """
    # If already loaded, just return it (don't move it)
    if _segmenter_cache["loaded"]:
        return _segmenter_cache["model"], _segmenter_cache["processor"]

    device, dtype = _get_device_and_dtype()
    
    try:
        from transformers import AutoFeatureExtractor, AutoModelForAudioFrameClassification
        
        processor = AutoFeatureExtractor.from_pretrained(SEGMENTER_MODEL)
        model = AutoModelForAudioFrameClassification.from_pretrained(SEGMENTER_MODEL)
        model.to(device, dtype=dtype)
        model.eval()
        
        _segmenter_cache["model"] = model
        _segmenter_cache["processor"] = processor
        _segmenter_cache["loaded"] = True
        print(f"βœ“ Segmenter loaded: {SEGMENTER_MODEL}")
        return model, processor
    except Exception as e:
        print(f"βœ— Failed to load segmenter: {e}")
        return None, None


def _load_whisper():
    """Load the Whisper ASR model.

    Note: This function only loads the model, it does NOT move it between devices.
    Use move_segment_models_to_gpu() to move models to GPU inside GPU contexts.
    """
    # If already loaded, just return it (don't move it)
    if _whisper_cache["loaded"]:
        return (_whisper_cache["model"], _whisper_cache["processor"],
                _whisper_cache["gen_config"], _whisper_cache["prompt_ids"])

    device, dtype = _get_device_and_dtype()
    
    try:
        from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq, GenerationConfig
        
        processor = AutoProcessor.from_pretrained(WHISPER_MODEL)
        model = AutoModelForSpeechSeq2Seq.from_pretrained(
            WHISPER_MODEL, torch_dtype=dtype, low_cpu_mem_usage=True
        ).to(device).eval()
        
        # Build generation config
        tok = processor.tokenizer
        prompt_tokens = ["<|startoftranscript|>", "<|ar|>", "<|transcribe|>", "<|notimestamps|>"]
        prompt_ids_list = []
        for t in prompt_tokens:
            try:
                tid = tok.convert_tokens_to_ids(t)
                unk = getattr(tok, "unk_token_id", None)
                if tid is not None and (unk is None or tid != unk):
                    prompt_ids_list.append(int(tid))
            except:
                pass
        
        gen_config = GenerationConfig()
        prompt_ids_tensor = None
        if prompt_ids_list:
            prompt_ids_tensor = torch.tensor(prompt_ids_list, dtype=torch.long, device=device)
        
        _whisper_cache["model"] = model
        _whisper_cache["processor"] = processor
        _whisper_cache["gen_config"] = gen_config
        _whisper_cache["prompt_ids"] = prompt_ids_tensor
        _whisper_cache["loaded"] = True
        print(f"βœ“ Whisper loaded: {WHISPER_MODEL}")
        return model, processor, gen_config, prompt_ids_tensor
    except Exception as e:
        print(f"βœ— Failed to load Whisper: {e}")
        return None, None, None, None




def _detect_speech_segments(audio: np.ndarray, sample_rate: int) -> List[Tuple[float, float]]:
    """
    Detect speech segments in audio using VAD.
    
    Args:
        audio: Audio waveform (mono, float32)
        sample_rate: Sample rate of audio
        
    Returns:
        List of (start_time, end_time) tuples in seconds
    """
    model, processor = _load_segmenter()
    if model is None:
        return [(0, len(audio) / sample_rate)]  # Fallback: treat whole audio as one segment
    
    try:
        from recitations_segmenter import segment_recitations, clean_speech_intervals
        
        # Resample to 16kHz if needed (segmenter expects 16kHz)
        if sample_rate != 16000:
            audio = librosa.resample(audio, orig_sr=sample_rate, target_sr=16000)
            sample_rate = 16000
        
        device = next(model.parameters()).device
        dtype = next(model.parameters()).dtype
        
        # Convert numpy array to torch tensor (segmenter expects tensors)
        audio_tensor = torch.from_numpy(audio).float()
        
        # Segment the audio
        outputs = segment_recitations(
            [audio_tensor],  # List of waveforms as tensors
            model,
            processor,
            device=device,
            dtype=dtype,
            batch_size=1,
        )
        
        if not outputs:
            return [(0, len(audio) / sample_rate)]
        
        # Clean speech intervals
        clean_out = clean_speech_intervals(
            outputs[0].speech_intervals,
            outputs[0].is_complete,
            min_silence_duration_ms=MIN_SILENCE_DURATION_MS,
            min_speech_duration_ms=MIN_SPEECH_DURATION_MS,
            pad_duration_ms=PAD_DURATION_MS,
            return_seconds=True,
        )
        
        # Convert to list of tuples
        intervals = clean_out.clean_speech_intervals.tolist()
        return [(start, end) for start, end in intervals]
        
    except Exception as e:
        print(f"VAD error: {e}, using full audio as single segment")
        import traceback
        traceback.print_exc()
        return [(0, len(audio) / sample_rate)]


def _transcribe_segment(audio: np.ndarray, sample_rate: int) -> str:
    """
    Transcribe an audio segment to Arabic text using Whisper.
    
    Args:
        audio: Audio waveform (mono, float32)
        sample_rate: Sample rate
        
    Returns:
        Transcribed Arabic text
    """
    model, processor, gen_config, prompt_ids = _load_whisper()
    if model is None:
        return ""
    
    try:
        # Resample to 16kHz if needed
        if sample_rate != 16000:
            audio = librosa.resample(audio, orig_sr=sample_rate, target_sr=16000)
        
        device = next(model.parameters()).device
        dtype = model.dtype
        
        # Process audio
        feats = processor(audio=audio, sampling_rate=16000, return_tensors="pt")["input_features"]
        feats = feats.to(device=device, dtype=dtype)
        
        # Generate transcription
        with torch.no_grad():
            if prompt_ids is not None:
                out_ids = model.generate(
                    feats,
                    prompt_ids=prompt_ids,
                    generation_config=gen_config,
                    max_new_tokens=200,
                    do_sample=False,
                    num_beams=1,
                )
            else:
                out_ids = model.generate(
                    feats,
                    generation_config=gen_config,
                    max_new_tokens=200,
                    do_sample=False,
                    num_beams=1,
                )
        
        text = processor.batch_decode(out_ids, skip_special_tokens=True)[0].strip()
        return text
        
    except Exception as e:
        print(f"Whisper transcription error: {e}")
        return ""


def transcribe_segments_batched(
    segment_audios: List[np.ndarray],
    sample_rate: int
) -> List[str]:
    """
    Transcribe multiple audio segments in a single batched Whisper call.
    
    Args:
        segment_audios: List of audio waveforms (mono, float32)
        sample_rate: Sample rate of audio
        
    Returns:
        List of transcribed Arabic texts (one per segment)
    """
    import time
    
    if not segment_audios:
        return []
    
    model, processor, gen_config, prompt_ids = _load_whisper()
    if model is None:
        return [""] * len(segment_audios)
    
    try:
        batch_start = time.time()
        
        # Collect segment duration stats
        segment_lengths = [len(audio) for audio in segment_audios]
        segment_durations = [length / sample_rate for length in segment_lengths]
        min_dur, max_dur = min(segment_durations), max(segment_durations)
        avg_dur = sum(segment_durations) / len(segment_durations)
        total_audio_dur = sum(segment_durations)
        
        # Resample all to 16kHz if needed
        resampled = []
        for audio in segment_audios:
            if sample_rate != 16000:
                audio = librosa.resample(audio, orig_sr=sample_rate, target_sr=16000)
            resampled.append(audio)
        
        device = next(model.parameters()).device
        dtype = model.dtype
        
        # Process all audios - Whisper processor handles batching
        # Each audio becomes a feature tensor
        batch_features = []
        for audio in resampled:
            feats = processor(audio=audio, sampling_rate=16000, return_tensors="pt")["input_features"]
            batch_features.append(feats)
        
        # Stack into batch (all Whisper features are same size: 80 x 3000)
        batch_input = torch.cat(batch_features, dim=0).to(device=device, dtype=dtype)
        
        # Create attention mask (all ones - no padding needed for Whisper mel features)
        attention_mask = torch.ones(batch_input.shape[:2], dtype=torch.long, device=device)
        
        # Generate transcriptions in batch
        # Note: prompt_ids doesn't work well with batched inference due to dimension issues
        # For batched mode, we use standard generation without custom prompts
        inference_start = time.time()
        with torch.no_grad():
            out_ids = model.generate(
                batch_input,
                attention_mask=attention_mask,
                generation_config=gen_config,
                max_new_tokens=200,
                do_sample=False,
                num_beams=1,
            )
        inference_time = time.time() - inference_start
        
        # Decode all transcriptions
        texts = processor.batch_decode(out_ids, skip_special_tokens=True)
        texts = [t.strip() for t in texts]
        
        batch_time = time.time() - batch_start
        
        # Calculate efficiency stats
        # Whisper's mel spectrogram clips to 30s max, compute padding waste for shorter segments
        whisper_window_s = 30.0
        padded_total = len(segment_audios) * whisper_window_s
        padding_waste = padded_total - total_audio_dur
        padding_pct = (padding_waste / padded_total) * 100 if padded_total > 0 else 0
        
        # Get GPU memory stats (if CUDA available)
        mem_allocated = 0
        mem_reserved = 0
        mem_peak = 0
        gpu_name = "CPU"
        if torch.cuda.is_available() and device.type == "cuda":
            torch.cuda.synchronize()
            mem_allocated = torch.cuda.memory_allocated(device) / (1024**3)  # GB
            mem_reserved = torch.cuda.memory_reserved(device) / (1024**3)  # GB
            mem_peak = torch.cuda.max_memory_allocated(device) / (1024**3)  # GB
            try:
                gpu_name = torch.cuda.get_device_name(device)
            except:
                gpu_name = "GPU"
        
        # Log detailed stats
        print(f"[BATCHED WHISPER] ──────────────────────────────────────")
        print(f"  Segments: {len(segment_audios)} | Total audio: {total_audio_dur:.2f}s")
        print(f"  Duration range: {min_dur:.2f}s - {max_dur:.2f}s (avg: {avg_dur:.2f}s)")
        print(f"  Padding waste: {padding_waste:.2f}s ({padding_pct:.1f}% of batch capacity)")
        print(f"  Inference time: {inference_time:.2f}s | Total time: {batch_time:.2f}s")
        print(f"  Throughput: {total_audio_dur / batch_time:.2f}x realtime")
        if device.type == "cuda":
            print(f"  GPU: {gpu_name}")
            print(f"  Memory: {mem_allocated:.2f}GB allocated | {mem_reserved:.2f}GB reserved | {mem_peak:.2f}GB peak")
        print(f"[BATCHED WHISPER] ──────────────────────────────────────")
        
        return texts
        
    except Exception as e:
        print(f"Batched Whisper error: {e}")
        import traceback
        traceback.print_exc()
        return [""] * len(segment_audios)


def _match_text_to_verse(transcribed_text: str, verse_ref: str) -> Tuple[str, str, float, str]:
    """
    Match transcribed text to verse using phonemizer.

    This is a thin wrapper around the centralized match_text_to_verse() function
    from phonemizer_utils, kept for backwards compatibility with existing code.

    Args:
        transcribed_text: Arabic text from ASR
        verse_ref: Verse reference (e.g., "1:2")

    Returns:
        Tuple of (matched_text, phonemes, match_score, matched_ref)
    """
    # Use centralized helper from phonemizer_utils
    return match_text_to_verse(transcribed_text, verse_ref, stops=["compulsory_stop"])


def _parse_ref_range(ref: str) -> Tuple[int, int, int, int]:
    """
    Parse verse reference to get chapter, start verse, end verse.
    
    Args:
        ref: Reference like "1:2" or "1:2-1:5"
        
    Returns:
        Tuple of (chapter, start_verse, end_verse)
    """
    if '-' in ref:
        start_ref, end_ref = ref.split('-')
        start_ch, start_v = map(int, start_ref.split(':'))
        end_ch, end_v = map(int, end_ref.split(':'))
        return start_ch, start_v, end_ch, end_v
    else:
        ch, v = map(int, ref.split(':'))
        return ch, v, ch, v


def _parse_word_indices_from_ref(matched_ref: str, verse_ref: str) -> Tuple[Optional[int], Optional[int]]:
    """
    Parse word indices from phonemizer reference and convert to global indices.
    
    The phonemizer returns verse:word references (1-based within each verse),
    but we need global word indices (0-based across all verses in the selection).
    
    Example: matched_ref="112:1:1-112:2:2" for selection "112:1-112:4"
    - Verse 112:1 has 4 words, verse 112:2 has 2 words
    - "112:1:1" = global index 0 (first word of verse 1)
    - "112:2:2" = global index 5 (4 words from verse 1 + word 2 of verse 2)
    
    Args:
        matched_ref: Reference like "112:1:1-112:2:2" (surah:verse:word_start-surah:verse:word_end)
                     or "47:38:5" (single word) or "47:38" (just verse, no word indices)
        verse_ref: The full verse reference for the selection (e.g., "112:1-112:4")
        
    Returns:
        Tuple of (start_word_0based, end_word_0based) or (None, None) if no word indices
    """
    if not matched_ref or ':' not in matched_ref:
        return None, None

    # Load surah info to get word counts per verse (uses centralized loader)
    surah_info = load_surah_info()
    if not surah_info:
        return None, None
    
    # Parse the verse_ref to get the starting verse
    try:
        if '-' in verse_ref:
            start_verse_ref = verse_ref.split('-')[0]
        else:
            start_verse_ref = verse_ref
        
        if ':' not in start_verse_ref:
            # Whole chapter, starts at verse 1
            selection_start_surah = int(start_verse_ref)
            selection_start_verse = 1
        else:
            selection_start_surah, selection_start_verse = map(int, start_verse_ref.split(':'))
    except Exception:
        return None, None
    
    # Parse matched_ref to get verse:word positions
    if '-' in matched_ref:
        # Range format: "112:1:1-112:2:2"
        start_part, end_part = matched_ref.split('-')
        start_parts = start_part.split(':')
        end_parts = end_part.split(':')
        
        if len(start_parts) >= 3 and len(end_parts) >= 3:
            try:
                start_surah = int(start_parts[0])
                start_verse = int(start_parts[1])
                start_word_in_verse = int(start_parts[2])
                
                end_surah = int(end_parts[0])
                end_verse = int(end_parts[1])
                end_word_in_verse = int(end_parts[2])
                
                # Convert to global 0-based indices
                start_global = _verse_word_to_global_index(
                    start_surah, start_verse, start_word_in_verse,
                    selection_start_surah, selection_start_verse, surah_info
                )
                end_global = _verse_word_to_global_index(
                    end_surah, end_verse, end_word_in_verse,
                    selection_start_surah, selection_start_verse, surah_info
                )
                
                if start_global is not None and end_global is not None:
                    return start_global, end_global
            except ValueError:
                return None, None
    else:
        # Single reference: "47:38:1" or "47:38"
        parts = matched_ref.split(':')
        if len(parts) >= 3:
            try:
                surah = int(parts[0])
                verse = int(parts[1])
                word_in_verse = int(parts[2])
                
                global_idx = _verse_word_to_global_index(
                    surah, verse, word_in_verse,
                    selection_start_surah, selection_start_verse, surah_info
                )
                
                if global_idx is not None:
                    return global_idx, global_idx
            except ValueError:
                return None, None
    
    return None, None


def _verse_word_to_global_index(
    target_surah: int,
    target_verse: int,
    word_in_verse: int,
    selection_start_surah: int,
    selection_start_verse: int,
    surah_info: dict
) -> Optional[int]:
    """
    Convert a verse:word reference (1-based) to a global word index (0-based).
    
    Args:
        target_surah: Surah number of the target word
        target_verse: Verse number of the target word
        word_in_verse: Word index within the verse (1-based)
        selection_start_surah: Surah number where the selection starts
        selection_start_verse: Verse number where the selection starts
        surah_info: Surah info dictionary
        
    Returns:
        Global word index (0-based) or None if calculation fails
    """
    if target_surah != selection_start_surah:
        # Cross-surah not supported yet
        return None
    
    try:
        # Get surah data
        surah_data = surah_info.get(str(target_surah))
        if not surah_data or "verses" not in surah_data:
            return None
        
        # Count words from selection start to target verse
        word_offset = 0
        for verse_data in surah_data["verses"]:
            verse_num = verse_data["verse"]
            
            if verse_num < selection_start_verse:
                continue
            elif verse_num < target_verse:
                # Add all words from this verse
                word_offset += verse_data.get("num_words", 0)
            elif verse_num == target_verse:
                # Add the word index within this verse (convert 1-based to 0-based)
                return word_offset + (word_in_verse - 1)
            else:
                break
        
        return None
    except Exception:
        return None


def detect_vad_segments(
    audio_data: Tuple[int, np.ndarray],
    canonical_text: str,
    verse_ref: str = "",
) -> Optional[VadResult]:
    """
    Run VAD only to detect speech segments (no Whisper yet).
    
    Args:
        audio_data: Tuple of (sample_rate, audio_array) from Gradio
        canonical_text: Expected Arabic text for the verse
        verse_ref: Verse reference (e.g., "1:2") for word count lookup
        
    Returns:
        VadResult with speech intervals and preprocessed audio, or None on error
    """
    import time
    
    if audio_data is None:
        return None
    
    sample_rate, audio = audio_data
    
    # Convert to float32 if needed
    if audio.dtype == np.int16:
        audio = audio.astype(np.float32) / 32768.0
    elif audio.dtype == np.int32:
        audio = audio.astype(np.float32) / 2147483648.0
    
    # Convert stereo to mono
    if len(audio.shape) > 1:
        audio = audio.mean(axis=1)
    
    # Get canonical words for display/matching (includes verse markers, that's fine)
    canonical_words = canonical_text.split()
    
    # Get accurate word count from surah_info.json (this is the authoritative count)
    total_words = get_total_words_for_verse_range(verse_ref) if verse_ref else 0
    if total_words == 0:
        # Fallback if surah_info lookup fails or no verse_ref
        total_words = len(canonical_words)
        print(f"[VAD] Warning: Using text.split() for word count (surah_info lookup failed)")
    else:
        print(f"[VAD] Word count from surah_info.json: {total_words} words")
    
    audio_duration = len(audio) / sample_rate
    print(f"\n[VAD] Processing {audio_duration:.2f}s of audio...")
    
    # Detect speech segments using VAD
    vad_start = time.time()
    speech_intervals = _detect_speech_segments(audio, sample_rate)
    vad_time = time.time() - vad_start
    print(f"[VAD] Completed in {vad_time:.2f}s - detected {len(speech_intervals)} segments")
    
    if not speech_intervals:
        print("[VAD] No speech detected")
        return None
    
    # Create VadSegment list
    vad_segments = []
    for idx, (start_time, end_time) in enumerate(speech_intervals):
        vad_segments.append(VadSegment(
            start_time=start_time,
            end_time=end_time,
            segment_idx=idx
        ))
    
    return VadResult(
        vad_segments=vad_segments,
        audio=audio,
        sample_rate=sample_rate,
        canonical_words=canonical_words,
        total_words=total_words
    )


def process_single_segment(
    vad_segment: VadSegment,
    audio: np.ndarray,
    sample_rate: int,
    verse_ref: str,
    canonical_words: List[str],
    words_covered: set,
) -> SegmentInfo:
    """
    Process a single VAD segment: Whisper transcription + text matching.
    
    Args:
        vad_segment: VAD segment with timing info
        audio: Preprocessed audio array
        sample_rate: Audio sample rate
        verse_ref: Verse reference (e.g., "1:2")
        canonical_words: List of canonical words
        words_covered: Set to track which words are covered (modified in place)
        
    Returns:
        SegmentInfo with transcription and matching results
    """
    import time
    
    start_time = vad_segment.start_time
    end_time = vad_segment.end_time
    seg_idx = vad_segment.segment_idx
    total_words = len(canonical_words)
    
    # Extract audio segment
    start_sample = int(start_time * sample_rate)
    end_sample = int(end_time * sample_rate)
    segment_audio = audio[start_sample:end_sample]
    
    if len(segment_audio) < 1600:  # Less than 0.1s at 16kHz
        print(f"[SEG {seg_idx+1}] Skipped (too short)")
        return SegmentInfo(
            start_time=start_time,
            end_time=end_time,
            transcribed_text="",
            matched_text="",
            matched_ref="",
            word_start_idx=0,
            word_end_idx=0,
            canonical_phonemes="",
            match_score=0.0,
            error="Segment too short"
        )
    
    # Transcribe segment with Whisper
    whisper_start = time.time()
    transcribed_text = _transcribe_segment(segment_audio, sample_rate)
    whisper_time = time.time() - whisper_start
    
    if not transcribed_text:
        print(f"[SEG {seg_idx+1}] ({start_time:.1f}s-{end_time:.1f}s): Whisper {whisper_time:.2f}s - FAILED")
        return SegmentInfo(
            start_time=start_time,
            end_time=end_time,
            transcribed_text="",
            matched_text="",
            matched_ref="",
            word_start_idx=0,
            word_end_idx=0,
            canonical_phonemes="",
            match_score=0.0,
            error="Transcription failed"
        )
    
    # Match transcribed text to verse using phonemizer
    match_start = time.time()
    matched_text, phonemes, match_score, matched_ref = _match_text_to_verse(
        transcribed_text, verse_ref
    )
    match_time = time.time() - match_start
    
    # Debug logging
    print(f"[SEG {seg_idx+1}] Phonemizer results:")
    print(f"  - Transcribed: '{transcribed_text}'")
    print(f"  - Matched text: '{matched_text}'")
    print(f"  - Matched ref: '{matched_ref}'")
    print(f"  - Score: {match_score:.2f}")
    
    if match_score < MIN_MATCH_SCORE:
        print(f"[SEG {seg_idx+1}] ({start_time:.1f}s-{end_time:.1f}s): Whisper {whisper_time:.2f}s, Match {match_time:.2f}s - SCORE {match_score:.2f} (LOW)")
        return SegmentInfo(
            start_time=start_time,
            end_time=end_time,
            transcribed_text=transcribed_text,
            matched_text="",
            matched_ref="",
            word_start_idx=0,
            word_end_idx=0,
            canonical_phonemes="",
            match_score=match_score,
            error=f"Low match score ({match_score:.2f})"
        )
    
    # Parse word indices from matched_ref (phonemizer gives us this!)
    start_word_0based, end_word_0based = _parse_word_indices_from_ref(matched_ref, verse_ref)
    
    print(f"[SEG {seg_idx+1}] Word indices from phonemizer ref:")
    print(f"  - Parsed from '{matched_ref}': global indices {start_word_0based}-{end_word_0based} (0-based)")
    
    if start_word_0based is not None and end_word_0based is not None:
        # Already 0-based, just clamp to valid range
        word_start_idx = max(0, min(start_word_0based, total_words - 1))
        word_end_idx = max(0, min(end_word_0based, total_words - 1))
        
        print(f"  - Clamped indices: word_start_idx={word_start_idx}, word_end_idx={word_end_idx}")
        print(f"  - Covering words: {canonical_words[word_start_idx:word_end_idx+1]}")
    else:
        # Fallback: no word indices in ref, use matched text length
        matched_words = matched_text.split()
        num_matched = len(matched_words)
        word_start_idx = 0
        word_end_idx = min(num_matched - 1, total_words - 1) if num_matched > 0 else 0
        print(f"  - No word indices in ref, using matched text length: {num_matched} words")
        print(f"  - Default indices: word_start_idx={word_start_idx}, word_end_idx={word_end_idx}")
    
    # Mark words as covered
    for idx in range(word_start_idx, word_end_idx + 1):
        words_covered.add(idx)
    
    total_time = whisper_time + match_time
    print(f"[SEG {seg_idx+1}] ({start_time:.1f}s-{end_time:.1f}s): Whisper {whisper_time:.2f}s, Match {match_time:.2f}s, Total {total_time:.2f}s - words {word_start_idx+1}-{word_end_idx+1} - SCORE {match_score:.2f}")
    
    return SegmentInfo(
        start_time=start_time,
        end_time=end_time,
        transcribed_text=transcribed_text,
        matched_text=matched_text,
        matched_ref=matched_ref,
        word_start_idx=word_start_idx,
        word_end_idx=word_end_idx,
        canonical_phonemes=phonemes,
        match_score=match_score,
        error=None
    )


def process_audio_segments(
    audio_data: Tuple[int, np.ndarray],
    verse_ref: str,
    canonical_text: str,
    canonical_phonemes: str,
) -> SegmentationResult:
    """
    Process audio with VAD segmentation, ASR, and text matching.
    
    Args:
        audio_data: Tuple of (sample_rate, audio_array) from Gradio
        verse_ref: Verse reference (e.g., "1:2")
        canonical_text: Expected Arabic text for the verse
        canonical_phonemes: Expected phonemes for the verse
        
    Returns:
        SegmentationResult with segment info and coverage status
    """
    import time
    
    if audio_data is None:
        return SegmentationResult(segments=[], full_coverage=False, 
                                  coverage_warning="No audio provided")
    
    total_start = time.time()
    sample_rate, audio = audio_data
    
    # Convert to float32 if needed
    if audio.dtype == np.int16:
        audio = audio.astype(np.float32) / 32768.0
    elif audio.dtype == np.int32:
        audio = audio.astype(np.float32) / 2147483648.0
    
    # Convert stereo to mono
    if len(audio.shape) > 1:
        audio = audio.mean(axis=1)
    
    # Get canonical words for display/matching (includes verse markers, that's fine)
    canonical_words = canonical_text.split()
    
    # Get accurate word count from surah_info.json (this is the authoritative count)
    total_words = get_total_words_for_verse_range(verse_ref) if verse_ref else 0
    if total_words == 0:
        # Fallback if surah_info lookup fails or no verse_ref
        total_words = len(canonical_words)
        print(f"[Segmentation] Warning: Using text.split() for word count (surah_info lookup failed)")
    else:
        print(f"[Segmentation] Word count from surah_info.json: {total_words} words")
    
    audio_duration = len(audio) / sample_rate
    print(f"\n[Segmentation] Processing {audio_duration:.2f}s of audio...")
    
    # Detect speech segments using VAD
    vad_start = time.time()
    speech_intervals = _detect_speech_segments(audio, sample_rate)
    vad_time = time.time() - vad_start
    print(f"[Segmentation] VAD: {vad_time:.2f}s - detected {len(speech_intervals)} segments")
    
    if not speech_intervals:
        return SegmentationResult(
            segments=[], 
            full_coverage=False,
            coverage_warning="No speech detected in audio",
            total_words=total_words
        )
    
    # Track which words are covered
    words_covered = set()
    segments = []

    for seg_idx, (start_time, end_time) in enumerate(speech_intervals):
        seg_start = time.time()
        
        # Extract audio segment
        start_sample = int(start_time * sample_rate)
        end_sample = int(end_time * sample_rate)
        segment_audio = audio[start_sample:end_sample]
        
        if len(segment_audio) < 1600:  # Less than 0.1s at 16kHz
            print(f"[Segmentation] Segment {seg_idx+1}: skipped (too short)")
            continue
        
        # Transcribe segment with Whisper
        whisper_start = time.time()
        transcribed_text = _transcribe_segment(segment_audio, sample_rate)
        whisper_time = time.time() - whisper_start
        
        if not transcribed_text:
            print(f"[SEG {seg_idx+1}] ({start_time:.1f}s-{end_time:.1f}s): Whisper {whisper_time:.2f}s - FAILED")
            segments.append(SegmentInfo(
                start_time=start_time,
                end_time=end_time,
                transcribed_text="",
                matched_text="",
                matched_ref="",
                word_start_idx=0,
                word_end_idx=0,
                canonical_phonemes="",
                match_score=0.0,
                error="Transcription failed"
            ))
            continue
        
        # Match transcribed text to verse using phonemizer
        match_start = time.time()
        matched_text, phonemes, match_score, matched_ref = _match_text_to_verse(
            transcribed_text, verse_ref
        )
        match_time = time.time() - match_start
        
        if match_score < MIN_MATCH_SCORE:
            print(f"[Segmentation] Segment {seg_idx+1} ({start_time:.1f}s-{end_time:.1f}s): Whisper {whisper_time:.2f}s, Match {match_time:.2f}s - LOW SCORE ({match_score:.2f})")
            segments.append(SegmentInfo(
                start_time=start_time,
                end_time=end_time,
                transcribed_text=transcribed_text,
                matched_text="",
                matched_ref="",
                word_start_idx=0,
                word_end_idx=0,
                canonical_phonemes="",
                match_score=match_score,
                error=f"Low match score ({match_score:.2f})"
            ))
            continue
        
        # Parse word indices from matched_ref (phonemizer gives us this!)
        start_word_0based, end_word_0based = _parse_word_indices_from_ref(matched_ref, verse_ref)
        
        if start_word_0based is not None and end_word_0based is not None:
            # Already 0-based, just clamp to valid range
            word_start_idx = max(0, min(start_word_0based, total_words - 1))
            word_end_idx = max(0, min(end_word_0based, total_words - 1))
        else:
            # Fallback: no word indices in ref, use matched text length
            matched_words = matched_text.split()
            num_matched = len(matched_words)
            word_start_idx = 0
            word_end_idx = min(num_matched - 1, total_words - 1) if num_matched > 0 else 0
        
        # Mark words as covered
        for idx in range(word_start_idx, word_end_idx + 1):
            words_covered.add(idx)
        
        seg_total = time.time() - seg_start
        print(f"[SEG {seg_idx+1}] ({start_time:.1f}s-{end_time:.1f}s): Whisper {whisper_time:.2f}s, Match {match_time:.2f}s, Total {seg_total:.2f}s - words {word_start_idx+1}-{word_end_idx+1} - SCORE {match_score:.2f}")
        
        segments.append(SegmentInfo(
            start_time=start_time,
            end_time=end_time,
            transcribed_text=transcribed_text,
            matched_text=matched_text,
            matched_ref=matched_ref,
            word_start_idx=word_start_idx,
            word_end_idx=word_end_idx,
            canonical_phonemes=phonemes,
            match_score=match_score,
            error=None
        ))
    
    # Check coverage
    full_coverage = len(words_covered) == total_words
    coverage_warning = None
    
    if not full_coverage:
        missing_words = [i for i in range(total_words) if i not in words_covered]
        if missing_words:
            # Group consecutive missing indices
            groups = []
            start = missing_words[0]
            end = missing_words[0]
            for idx in missing_words[1:]:
                if idx == end + 1:
                    end = idx
                else:
                    groups.append((start, end))
                    start = end = idx
            groups.append((start, end))
            
            missing_ranges = ", ".join(
                f"words {s+1}-{e+1}" if s != e else f"word {s+1}"
                for s, e in groups
            )
            coverage_warning = f"⚠️ Incomplete coverage: {missing_ranges} not detected in segments"
    
    total_time = time.time() - total_start
    coverage_pct = (len(words_covered) / total_words * 100) if total_words > 0 else 0
    print(f"[SEGMENTATION] COMPLETE: {total_time:.2f}s - {len(segments)} segments, {len(words_covered)}/{total_words} words ({coverage_pct:.0f}% coverage)")
    
    return SegmentationResult(
        segments=segments,
        full_coverage=full_coverage,
        coverage_warning=coverage_warning,
        total_words=total_words
    )


def run_text_matching(transcribed_texts, vad_segments, verse_ref, total_words):
    """
    Run CPU text matching with CONSTRAINED SLIDING WINDOW.

    For continuous recitation:
    - First GLOBAL_SEGMENTS search full verse range (to establish position)
    - Subsequent segments search constrained window around last matched verse
    - Window = last_verse - LOOKBACK to last_verse + LOOKAHEAD

    This is much faster than searching full range every time.

    Args:
        transcribed_texts: List of transcribed text strings from Whisper
        vad_segments: List of VadSegment objects
        verse_ref: Full verse reference for the recitation (e.g., "1:2-1:7")
        total_words: Total number of words in the verse range

    Returns:
        Tuple of (match_results, words_covered)
        - match_results: List of tuples with matching info for each segment
        - words_covered: Set of word indices that were matched
    """
    import time
    from recitation_engine.special_segments import is_special_segment
    from config import MIN_MATCH_SCORE, GLOBAL_SEARCH_SEGMENTS, MATCH_LOOKBACK_VERSES, MATCH_LOOKAHEAD_VERSES

    match_start = time.time()
    num_segments = len(transcribed_texts)
    match_results = []
    words_covered = set()

    # Parse the full verse ref to get surah and verse range
    try:
        if '-' in verse_ref:
            start_ref, end_ref = verse_ref.split('-')
            start_surah, start_verse = map(int, start_ref.split(':'))
            end_surah, end_verse = map(int, end_ref.split(':'))
        else:
            start_surah, start_verse = map(int, verse_ref.split(':'))
            end_surah, end_verse = start_surah, start_verse
    except:
        start_surah, start_verse = 1, 1
        end_surah, end_verse = 1, 1

    # Track last matched verse for constrained search
    last_matched_verse = start_verse
    global_searches = 0
    constrained_searches = 0

    for i, (vad_seg, transcribed_text) in enumerate(zip(vad_segments, transcribed_texts)):
        if not transcribed_text:
            match_results.append((None, "", "", 0.0, ""))
            continue

        # Check if this is a special segment (Basmala/Isti'adha) to skip
        is_special, special_name = is_special_segment(transcribed_text, verse_ref)
        if is_special:
            # Mark as special segment to be filtered out later
            match_results.append((None, transcribed_text, "", 0.0, f"SKIP_SPECIAL:{special_name}"))
            continue

        # Determine search ref based on position
        if i < GLOBAL_SEARCH_SEGMENTS:
            # First N segments: search full range (global)
            search_ref = verse_ref
            global_searches += 1
        else:
            # Constrained search: window around last matched verse
            window_start = max(start_verse, last_matched_verse - MATCH_LOOKBACK_VERSES)
            window_end = min(end_verse, last_matched_verse + MATCH_LOOKAHEAD_VERSES)

            if start_surah == end_surah:
                search_ref = f"{start_surah}:{window_start}-{start_surah}:{window_end}"
            else:
                # Multi-surah: just use full ref (complex case)
                search_ref = verse_ref
            constrained_searches += 1

        # Match against verse
        matched_text, phonemes, match_score, matched_ref = _match_text_to_verse(
            transcribed_text, search_ref
        )

        # If constrained search fails, try global fallback
        if match_score < MIN_MATCH_SCORE and i >= GLOBAL_SEARCH_SEGMENTS:
            matched_text, phonemes, match_score, matched_ref = _match_text_to_verse(
                transcribed_text, verse_ref  # Full range fallback
            )
            global_searches += 1
            constrained_searches -= 1

        if match_score < MIN_MATCH_SCORE:
            match_results.append((None, transcribed_text, "", match_score, f"Low match score ({match_score:.2f})"))
            continue

        # Parse word indices and update last_matched_verse
        start_word, end_word = _parse_word_indices_from_ref(matched_ref, verse_ref)
        seg_words_covered = set()

        if start_word is not None and end_word is not None:
            word_start_idx = max(0, min(start_word, total_words - 1))
            word_end_idx = max(0, min(end_word, total_words - 1))
            for w_idx in range(word_start_idx, word_end_idx + 1):
                seg_words_covered.add(w_idx)
                words_covered.add(w_idx)

            # Update last matched verse from matched_ref
            try:
                if ':' in matched_ref:
                    parts = matched_ref.split('-')[-1].split(':')  # Get end verse
                    if len(parts) >= 2:
                        last_matched_verse = int(parts[1])
            except:
                pass
        else:
            word_start_idx = 0
            word_end_idx = 0

        match_results.append((
            (word_start_idx, word_end_idx),
            transcribed_text,
            matched_text,
            match_score,
            matched_ref,
            phonemes
        ))

    match_time = time.time() - match_start
    avg_seg = match_time / num_segments if num_segments > 0 else 0

    print(f"[CPU TEXT MATCHING] ──────────────────────────────────────")
    print(f"  Segments: {num_segments} | Total: {match_time:.2f}s | Avg: {avg_seg*1000:.0f}ms/seg")
    print(f"  Global searches: {global_searches} | Constrained: {constrained_searches}")
    print(f"  Window: {MATCH_LOOKBACK_VERSES} back / {MATCH_LOOKAHEAD_VERSES} ahead")
    print(f"[CPU TEXT MATCHING] ──────────────────────────────────────")

    return match_results, words_covered


def build_segment_infos(vad_segments, segment_audios, match_results, wav2vec_results):
    """
    Build SegmentInfo objects from match results and Wav2Vec2 transcriptions.

    Segments marked as SKIP_SPECIAL (Basmala/Isti'adha) are filtered out entirely.

    Args:
        vad_segments: List of VadSegment objects
        segment_audios: List of audio arrays for each segment
        match_results: List of match result tuples from run_text_matching
        wav2vec_results: List of phoneme transcriptions from Wav2Vec2

    Returns:
        Tuple of (segment_infos, predicted_phonemes, kept_indices)
        - segment_infos: List of SegmentInfo objects (special segments filtered out)
        - predicted_phonemes: List of phoneme strings matching segment_infos
        - kept_indices: List of original segment indices that were kept (for FA alignment)
    """
    segment_infos = []
    predicted_phonemes = []
    kept_indices = []  # Track original indices for FA result alignment
    skipped_count = 0
    empty_phoneme_count = 0

    # Diagnostic logging: verify input lengths match
    print(f"[BUILD SEGMENT] Inputs: {len(vad_segments)} VAD segments, {len(segment_audios)} audios, "
          f"{len(match_results)} matches, {len(wav2vec_results)} wav2vec results")

    for i, (vad_seg, audio, match_result) in enumerate(zip(vad_segments, segment_audios, match_results)):
        if match_result[0] is None:
            # Check if this is a special segment to skip entirely
            error_msg = match_result[4] if len(match_result) > 4 else ""
            if error_msg.startswith("SKIP_SPECIAL:"):
                special_type = error_msg.split(":")[1] if ":" in error_msg else "unknown"
                print(f"[SEGMENT FILTER] Skipping {special_type} segment ({vad_seg.start_time:.1f}s-{vad_seg.end_time:.1f}s)")
                skipped_count += 1
                continue  # Don't add to segment_infos - completely invisible

            # Error case - no valid match (display error in UI)
            segment_infos.append(SegmentInfo(
                start_time=vad_seg.start_time,
                end_time=vad_seg.end_time,
                transcribed_text=match_result[1],
                matched_text="",
                matched_ref="",
                word_start_idx=0,
                word_end_idx=0,
                canonical_phonemes="",
                match_score=match_result[3] if len(match_result) > 3 else 0.0,
                error=error_msg if error_msg else "Transcription failed"
            ))
            predicted_phonemes.append("")
            kept_indices.append(i)  # Track for FA alignment
        else:
            # Valid match - get transcription by index
            word_start_idx, word_end_idx = match_result[0]
            transcribed_text = match_result[1]
            matched_text = match_result[2]
            match_score = match_result[3]
            matched_ref = match_result[4]
            canonical_phonemes = match_result[5] if len(match_result) > 5 else ""

            # Direct index - wav2vec_results has same ordering as segments
            seg_transcription = wav2vec_results[i] if i < len(wav2vec_results) else ""
            if not seg_transcription:
                empty_phoneme_count += 1
                print(f"[BUILD SEGMENT] Segment {i+1} ({vad_seg.start_time:.1f}s-{vad_seg.end_time:.1f}s): "
                      f"Empty phonemes (matched verse: {matched_ref})")
            predicted_phonemes.append(seg_transcription)

            segment_infos.append(SegmentInfo(
                start_time=vad_seg.start_time,
                end_time=vad_seg.end_time,
                transcribed_text=transcribed_text,
                matched_text=matched_text,
                matched_ref=matched_ref,
                word_start_idx=word_start_idx,
                word_end_idx=word_end_idx,
                canonical_phonemes=canonical_phonemes,
                match_score=match_score,
                error=None
            ))
            kept_indices.append(i)  # Track for FA alignment

    if skipped_count > 0:
        print(f"[SEGMENT FILTER] Skipped {skipped_count} special segment(s) (Basmala/Isti'adha)")

    # Summary logging
    valid_phoneme_count = len(predicted_phonemes) - empty_phoneme_count
    print(f"[BUILD SEGMENT] Output: {len(segment_infos)} segments, "
          f"{valid_phoneme_count}/{len(predicted_phonemes)} with phoneme data")
    if empty_phoneme_count > 0:
        print(f"[BUILD SEGMENT] WARNING: {empty_phoneme_count} segment(s) have no phoneme data")

    return segment_infos, predicted_phonemes, kept_indices