forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_recorded.py
More file actions
1230 lines (1067 loc) · 43.7 KB
/
Copy pathpre_recorded.py
File metadata and controls
1230 lines (1067 loc) · 43.7 KB
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
import logging
import os
import wave as _wave
from abc import ABC, abstractmethod
from collections import defaultdict
from io import BytesIO
from math import ceil
from threading import RLock
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union, cast
import httpx
import numpy as np
from deepgram import DeepgramClient, DeepgramClientOptions
from pydub import AudioSegment # pydub is untyped
from config.prerecorded_stt import (
PrerecordedSTTConfigurationError as _PrerecordedSTTConfigurationError,
PrerecordedSTTService,
TranscriptionOutcome,
get_prerecorded_models,
require_provider_environment,
)
from config.stt_provider_policy import (
MODULATE_PROVIDER,
PARAKEET_PROVIDER,
STTServingSurface,
default_models_for_surface,
normalized_stt_language,
parakeet_supports_language,
provider_is_enabled,
)
from models.transcript_segment import TranscriptSegment
from utils.byok import get_byok_key
from utils.observability.fallback import record_fallback
from utils.other.endpoints import timeit
from utils.stt.outcomes import TranscriptionFailure
from utils.stt.speaker_clustering import select_speaker_cluster
from utils.stt.speaker_embedding import compare_embeddings, extract_embedding_from_bytes
_DG_TIMEOUT = httpx.Timeout(connect=10.0, read=120.0, write=30.0, pool=10.0)
_MODULATE_TIMEOUT = httpx.Timeout(connect=10.0, read=300.0, write=30.0, pool=10.0)
_MAX_PRE_RECORDED_SEGMENT_DURATION_SECONDS = 30.0
logger = logging.getLogger(__name__)
# Public compatibility export used by chat/router boundaries.
PrerecordedSTTConfigurationError = _PrerecordedSTTConfigurationError
# ---------------------------------------------------------------------------
# Provider-agnostic ABC — mirrors STTSocket for streaming
# ---------------------------------------------------------------------------
class PrerecordedSTTProvider(ABC):
@abstractmethod
def transcribe_url(
self,
audio_url: str,
speakers_count: Optional[int] = None,
attempts: int = 0,
return_language: bool = False,
diarize: bool = True,
language: Optional[str] = None,
keywords: Optional[Sequence[str]] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]: ...
@abstractmethod
def transcribe_bytes(
self,
audio_bytes: bytes,
sample_rate: int = 16000,
diarize: bool = True,
attempts: int = 0,
encoding: Optional[str] = None,
channels: int = 1,
language: Optional[str] = None,
return_language: bool = False,
keywords: Optional[Sequence[str]] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]: ...
def get_prerecorded_service(language: Optional[str] = 'en') -> Tuple[str, Optional[str], str]:
"""Route pre-recorded STT based on STT_PRERECORDED_MODEL env var.
Iterates comma-separated models (same pattern as STT_SERVICE_MODELS for streaming).
First model allowed by the central serving policy that supports the language
wins. Disabled-provider tokens are ignored, then policy-owned defaults provide
the serving fallback. A language no capability map claims falls through to Velma
rather than failing selection.
"""
base_lang = normalized_stt_language(language) or 'en'
def select(models: Sequence[str]) -> Optional[Tuple[str, Optional[str], str]]:
for m in models:
m = m.strip()
if m == 'modulate-velma-2' and provider_is_enabled(MODULATE_PROVIDER, STTServingSurface.PRERECORDED):
if base_lang in {'en', 'es', 'fr', 'de', 'it', 'pt', 'nl', 'ja', 'ko', 'zh'}:
return PrerecordedSTTService.MODULATE, base_lang, 'velma-2'
continue
if m == 'parakeet' and provider_is_enabled(PARAKEET_PROVIDER, STTServingSurface.PRERECORDED):
if parakeet_supports_language(STTServingSurface.PRERECORDED, base_lang):
return PrerecordedSTTService.PARAKEET, base_lang, 'parakeet'
return None
selected = select(get_prerecorded_models())
if selected is not None:
return selected
# A disabled/unknown preference must not become a provider call. Use the
# deployment-validated, policy-owned defaults instead.
selected = select(default_models_for_surface(STTServingSurface.PRERECORDED))
if selected is not None:
return selected
# Velma's batch API detects the language itself — we never send a code — so it can
# serve languages the capability maps omit, and values that are not codes at all.
if provider_is_enabled(MODULATE_PROVIDER, STTServingSurface.PRERECORDED):
return PrerecordedSTTService.MODULATE, 'multi', 'velma-2'
# Only reachable with every pre-recorded provider disabled, which no retry resolves.
raise TranscriptionFailure(TranscriptionOutcome.CONFIG_ERROR, retryable=False)
# Lazily initialized because constructing the SDK client at import makes every
# backend consumer credential-dependent, including schema export and unit discovery.
_deepgram_client: Optional[DeepgramClient] = None
_deepgram_client_lock = RLock()
def _deepgram_options() -> DeepgramClientOptions:
"""Build fresh options per client.
DeepgramClient.__init__ calls config.set_apikey(), so a cached options
object shared with a BYOK client rewrites the credential the managed
client still holds — every later request would bill that user's key.
"""
return DeepgramClientOptions(options={"keepalive": "true"})
def _get_deepgram_client() -> DeepgramClient:
global _deepgram_client
if _deepgram_client is None:
with _deepgram_client_lock:
if _deepgram_client is None:
api_key = os.getenv('DEEPGRAM_API_KEY')
if not api_key:
raise PrerecordedSTTConfigurationError(PrerecordedSTTService.DEEPGRAM, 'DEEPGRAM_API_KEY')
_deepgram_client = DeepgramClient(api_key, _deepgram_options())
return _deepgram_client
def _deepgram_client_for_request() -> DeepgramClient:
"""Route to BYOK Deepgram key when set; otherwise use the process-wide client."""
byok = get_byok_key('deepgram')
if byok:
return DeepgramClient(byok, _deepgram_options())
return _get_deepgram_client()
# Languages supported by nova-3
_deepgram_nova3_languages = {
"ar",
"ar-AE",
"ar-SA",
"ar-QA",
"ar-KW",
"ar-SY",
"ar-LB",
"ar-PS",
"ar-JO",
"ar-EG",
"ar-SD",
"ar-TD",
"ar-MA",
"ar-DZ",
"ar-TN",
"ar-IQ",
"ar-IR",
"be",
"bg",
"bn",
"bs",
"ca",
"cs",
"da",
"da-DK",
"de",
"de-CH",
"el",
"en",
"en-US",
"en-AU",
"en-GB",
"en-IN",
"en-NZ",
"es",
"es-419",
"et",
"fa",
"fi",
"fr",
"fr-CA",
"he",
"hi",
"hr",
"hu",
"id",
"it",
"ja",
"kn",
"ko",
"ko-KR",
"lt",
"lv",
"mk",
"mr",
"ms",
"nl",
"nl-BE",
"no",
"pl",
"pt",
"pt-BR",
"pt-PT",
"ro",
"ru",
"sk",
"sl",
"sr",
"sv",
"sv-SE",
"ta",
"te",
"th",
"th-TH",
"tl",
"tr",
"uk",
"ur",
"vi",
"zh",
"zh-CN",
"zh-Hans",
"zh-HK",
"zh-Hant",
"zh-TW",
}
def get_deepgram_model_for_language(language: str) -> Tuple[str, str]:
"""
Determine the appropriate Deepgram model and language for pre-recorded transcription.
Args:
language: The requested language code or 'multi' for auto-detection
Returns:
Tuple of (language_to_use, model_name)
"""
# For multi-language mode
if language == 'multi':
return 'multi', 'nova-3'
# Languages supported by nova-3
if language in _deepgram_nova3_languages:
return language, 'nova-3'
# Unsupported language - fall back to multi for auto-detection
return 'multi', 'nova-3'
@timeit
def deepgram_prerecorded(
audio_url: str,
speakers_count: Optional[int] = None,
attempts: int = 0,
return_language: bool = False,
diarize: bool = True,
language: Optional[str] = None,
model: str = "nova-3",
keywords: Optional[Sequence[str]] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
"""
Transcribe audio using Deepgram's pre-recorded API.
Returns words in same format as prerecorded for compatibility with existing postprocessing.
Args:
audio_url: URL to the audio file
speakers_count: Hint for number of speakers (not used by Deepgram, kept for API compatibility)
attempts: Current retry attempt number
return_language: If True, returns (words, language) tuple
language: Language code to force, or 'multi' for multilingual auto-detection
diarize: If True, enable speaker diarization
keywords: Custom vocabulary words to boost transcription accuracy
Returns:
List of word dicts with format: {'timestamp': [start, end], 'speaker': 'SPEAKER_XX', 'text': 'word'}
Or tuple of (words, language) if return_language=True
"""
logger.info(
'deepgram_prerecorded url_len=%s speakers_count=%s attempt=%s',
len(audio_url),
speakers_count,
attempts,
)
try:
# 'multi' language means auto-detection
is_multi = language == 'multi'
should_detect_language = return_language or is_multi
options: Dict[str, Any] = {
"model": model,
"smart_format": True,
"punctuate": True,
"diarize": diarize,
"detect_language": should_detect_language,
"utterances": True,
}
if language and not is_multi:
options["language"] = language
if keywords:
if model in ('nova-3',):
options["keyterm"] = list(keywords)
else:
options["keywords"] = list(keywords)
rest_client: Any = _deepgram_client_for_request().listen.rest.v("1")
response = rest_client.transcribe_url({"url": audio_url}, options, timeout=_DG_TIMEOUT)
# Extract words from response
result: Dict[str, Any] = response.to_dict()
channels = result.get('results', {}).get('channels', [])
if not channels:
raise Exception('No channels found in response')
alternatives = channels[0].get('alternatives', [])
if not alternatives:
raise Exception('No alternatives found in response')
dg_words = alternatives[0].get('words', [])
if not dg_words:
if return_language:
detected_lang = channels[0].get('detected_language', 'en')
if detected_lang and '-' in detected_lang:
detected_lang = detected_lang.split('-')[0]
return [], detected_lang or 'en'
return []
# Convert Deepgram format to prerecorded compatible format
# Deepgram: {word, start, end, confidence, punctuated_word, speaker (int)}
# Expected: {timestamp: [start, end], speaker: 'SPEAKER_XX', text: 'word'}
words: List[Dict[str, Any]] = []
for w in dg_words:
speaker_id = w.get('speaker', 0)
words.append(
{
'timestamp': [w['start'], w['end']],
'speaker': f"SPEAKER_{speaker_id:02d}" if speaker_id is not None else None,
'text': w.get('punctuated_word', w['word']),
}
)
if return_language:
# Deepgram returns detected_language in the channel
detected_lang = channels[0].get('detected_language', 'en')
# Normalize language code (Deepgram might return 'en-US', we want 'en')
if detected_lang and '-' in detected_lang:
detected_lang = detected_lang.split('-')[0]
return words, detected_lang or 'en'
return words
except Exception as e:
logger.error('Deepgram prerecorded error exception_type=%s attempt=%s', type(e).__name__, attempts + 1)
if attempts < 1:
return deepgram_prerecorded(
audio_url,
speakers_count,
attempts + 1,
return_language,
diarize,
language,
model,
keywords,
)
raise RuntimeError(f'Deepgram transcription failed after {attempts + 1} attempts') from e
@timeit
def deepgram_prerecorded_from_bytes(
audio_bytes: bytes,
sample_rate: int = 16000,
diarize: bool = True,
attempts: int = 0,
encoding: Optional[str] = None,
channels: int = 1,
language: Optional[str] = None,
model: str = "nova-3",
return_language: bool = False,
keywords: Optional[Sequence[str]] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
"""
Transcribe audio bytes using Deepgram's pre-recorded API.
Returns words with speaker labels when diarize=True.
Supports both WAV format (default) and raw PCM audio.
For raw PCM, pass encoding='linear16' with appropriate sample_rate and channels.
Args:
audio_bytes: Audio bytes (WAV format or raw PCM)
sample_rate: Audio sample rate in Hz (required for raw PCM, ignored for WAV)
diarize: If True, enable speaker diarization
attempts: Current retry attempt number
encoding: Audio encoding format (e.g. 'linear16' for raw PCM). None for WAV.
channels: Number of audio channels (default 1 for mono)
language: Language code for transcription, or None for auto-detect
model: Deepgram model name (default 'nova-3')
return_language: If True, returns (words, language) tuple
keywords: Custom vocabulary words to boost transcription accuracy
Returns:
List of word dicts with format: {'timestamp': [start, end], 'speaker': 'SPEAKER_XX', 'text': 'word'}
Or tuple of (words, language) if return_language=True
"""
logger.info(
f'deepgram_prerecorded_from_bytes bytes_len={len(audio_bytes)} {sample_rate} {diarize} {attempts} encoding={encoding} language={language} model={model}'
)
try:
is_multi = language == 'multi'
should_detect_language = return_language or is_multi
options: Dict[str, Any] = {
"model": model,
"smart_format": True,
"punctuate": True,
"diarize": diarize,
"utterances": True,
"detect_language": should_detect_language,
}
if language and not is_multi:
options["language"] = language
if keywords:
if str(model).startswith("nova-3"):
options["keyterm"] = list(keywords)
else:
options["keywords"] = list(keywords)
# For raw PCM, Deepgram needs encoding + sample_rate to interpret the bytes
if encoding:
options["encoding"] = encoding
options["sample_rate"] = sample_rate
options["channels"] = channels
# Wrap bytes in BytesIO for Deepgram client
audio_buffer = BytesIO(audio_bytes)
mimetype = "audio/raw" if encoding else "audio/wav"
source: Dict[str, Any] = {"buffer": audio_buffer, "mimetype": mimetype}
rest_client: Any = _deepgram_client_for_request().listen.rest.v("1")
response = rest_client.transcribe_file(source, options, timeout=_DG_TIMEOUT)
# Extract words from response
result: Dict[str, Any] = response.to_dict()
result_channels = result.get('results', {}).get('channels', [])
if not result_channels:
raise Exception('No channels found in response')
alternatives = result_channels[0].get('alternatives', [])
if not alternatives:
raise Exception('No alternatives found in response')
dg_words = alternatives[0].get('words', [])
if not dg_words:
if return_language:
detected_lang = result_channels[0].get('detected_language', 'en')
if detected_lang and '-' in detected_lang:
detected_lang = detected_lang.split('-')[0]
return [], detected_lang or 'en'
return []
# Convert Deepgram format to standard format
# Deepgram: {word, start, end, confidence, punctuated_word, speaker (int)}
# Expected: {timestamp: [start, end], speaker: 'SPEAKER_XX', text: 'word'}
words: List[Dict[str, Any]] = []
for w in dg_words:
speaker_id = w.get('speaker', 0)
words.append(
{
'timestamp': [w['start'], w['end']],
'speaker': f"SPEAKER_{speaker_id:02d}" if speaker_id is not None else None,
'text': w.get('punctuated_word', w['word']),
}
)
if return_language:
detected_lang = result_channels[0].get('detected_language', 'en')
if detected_lang and '-' in detected_lang:
detected_lang = detected_lang.split('-')[0]
return words, detected_lang or 'en'
return words
except Exception as e:
logger.error(
'Deepgram prerecorded from bytes error exception_type=%s attempt=%s',
type(e).__name__,
attempts + 1,
)
if attempts < 1:
return deepgram_prerecorded_from_bytes(
audio_bytes,
sample_rate,
diarize,
attempts + 1,
encoding,
channels,
language,
model,
return_language,
keywords,
)
raise RuntimeError(f'Deepgram transcription failed after {attempts + 1} attempts') from e
@timeit
def modulate_prerecorded_from_bytes(
audio_bytes: bytes,
sample_rate: int = 16000,
diarize: bool = True,
attempts: int = 0,
return_language: bool = False,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
logger.info(f'modulate_prerecorded_from_bytes bytes_len={len(audio_bytes)} {sample_rate} {diarize} {attempts}')
require_provider_environment(PrerecordedSTTService.MODULATE)
api_key = os.environ['MODULATE_API_KEY']
try:
url = 'https://modulate-developer-apis.com/api/velma-2-stt-batch'
headers = {'X-API-Key': api_key}
files = {'upload_file': ('audio.wav', BytesIO(audio_bytes), 'audio/wav')}
data = {'speaker_diarization': str(diarize).lower()}
with httpx.Client(timeout=300) as client:
response = client.post(url, headers=headers, files=files, data=data)
response.raise_for_status()
result = response.json()
utterances = result.get('utterances', [])
if not utterances:
if return_language:
return [], 'en'
return []
words: List[Dict[str, Any]] = []
detected_language = 'en'
for utt in utterances:
text = utt.get('text', '').strip()
if not text:
continue
start_ms = utt.get('start_ms', 0)
duration_ms = utt.get('duration_ms', 0)
start = start_ms / 1000.0
end = (start_ms + duration_ms) / 1000.0
raw_speaker = utt.get('speaker')
if isinstance(raw_speaker, int) and raw_speaker >= 1:
speaker_idx = raw_speaker - 1
else:
speaker_idx = 0
speaker = f'SPEAKER_{speaker_idx:02d}'
words.append({'timestamp': [start, end], 'speaker': speaker, 'text': text})
lang = utt.get('language')
if lang:
detected_language = lang
if return_language:
return words, detected_language
return words
except Exception as e:
logger.error('Modulate prerecorded error exception_type=%s attempt=%s', type(e).__name__, attempts + 1)
if attempts < 2:
return modulate_prerecorded_from_bytes(audio_bytes, sample_rate, diarize, attempts + 1, return_language)
raise RuntimeError(f'Modulate transcription failed after {attempts + 1} attempts') from e
@timeit
def modulate_prerecorded(
audio_url: str,
speakers_count: Optional[int] = None,
attempts: int = 0,
return_language: bool = False,
diarize: bool = True,
language: Optional[str] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
logger.info(
'modulate_prerecorded url_len=%s speakers_count=%s attempt=%s', len(audio_url), speakers_count, attempts
)
try:
with httpx.Client(timeout=_MODULATE_TIMEOUT) as client:
resp = client.get(audio_url)
resp.raise_for_status()
audio_bytes = resp.content
return modulate_prerecorded_from_bytes(
audio_bytes, diarize=diarize, attempts=attempts, return_language=return_language
)
except Exception as e:
logger.error(
'Modulate prerecorded (url) error exception_type=%s attempt=%s',
type(e).__name__,
attempts + 1,
)
if attempts < 1:
return modulate_prerecorded(audio_url, speakers_count, attempts + 1, return_language, diarize, language)
raise RuntimeError(f'Modulate transcription (url) failed after {attempts + 1} attempts') from e
# ---------------------------------------------------------------------------
# Provider implementations
# ---------------------------------------------------------------------------
class DeepgramPrerecordedProvider(PrerecordedSTTProvider):
def __init__(self, model: str = 'nova-3'):
self._model = model
def transcribe_url(
self,
audio_url: str,
speakers_count: Optional[int] = None,
attempts: int = 0,
return_language: bool = False,
diarize: bool = True,
language: Optional[str] = None,
keywords: Optional[Sequence[str]] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
lang = language if (language is None or language in _deepgram_nova3_languages) else 'multi'
return deepgram_prerecorded(
audio_url,
speakers_count=speakers_count,
attempts=attempts,
return_language=return_language,
diarize=diarize,
language=lang,
model=self._model,
keywords=keywords,
)
def transcribe_bytes(
self,
audio_bytes: bytes,
sample_rate: int = 16000,
diarize: bool = True,
attempts: int = 0,
encoding: Optional[str] = None,
channels: int = 1,
language: Optional[str] = None,
return_language: bool = False,
keywords: Optional[Sequence[str]] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
lang = language if (language is None or language in _deepgram_nova3_languages) else 'multi'
return deepgram_prerecorded_from_bytes(
audio_bytes,
sample_rate=sample_rate,
diarize=diarize,
attempts=attempts,
encoding=encoding,
channels=channels,
language=lang,
model=self._model,
return_language=return_language,
keywords=keywords,
)
class ModulatePrerecordedProvider(PrerecordedSTTProvider):
def _normalize_lang(self, language: Optional[str]) -> str:
if not language:
return 'en'
return language.split('-')[0].split('_')[0].lower()
def transcribe_url(
self,
audio_url: str,
speakers_count: Optional[int] = None,
attempts: int = 0,
return_language: bool = False,
diarize: bool = True,
language: Optional[str] = None,
keywords: Optional[Sequence[str]] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
return modulate_prerecorded(
audio_url,
speakers_count=speakers_count,
attempts=attempts,
return_language=return_language,
diarize=diarize,
language=self._normalize_lang(language),
)
def transcribe_bytes(
self,
audio_bytes: bytes,
sample_rate: int = 16000,
diarize: bool = True,
attempts: int = 0,
encoding: Optional[str] = None,
channels: int = 1,
language: Optional[str] = None,
return_language: bool = False,
keywords: Optional[Sequence[str]] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
if encoding:
audio_bytes = _wrap_pcm_as_wav(audio_bytes, sample_rate, channels)
return modulate_prerecorded_from_bytes(
audio_bytes,
sample_rate=sample_rate,
diarize=diarize,
attempts=attempts,
return_language=return_language,
)
_PARAKEET_TIMEOUT = httpx.Timeout(connect=10.0, read=120.0, write=30.0, pool=10.0)
_PARAKEET_URL_DOWNLOAD_TIMEOUT = httpx.Timeout(connect=10.0, read=60.0, write=10.0, pool=10.0)
_PARAKEET_MAX_DOWNLOAD_BYTES = 100 * 1024 * 1024 # 100 MB
_PARAKEET_CONTAINER_MIME_FORMATS = {
'audio/webm': 'webm',
'video/webm': 'webm',
'audio/mp4': 'mp4',
'video/mp4': 'mp4',
}
class ParakeetAudioDecodeError(ValueError):
"""A downloaded browser container cannot be made safe for Parakeet."""
def _normalize_parakeet_download(audio_bytes: bytes, content_type: str | None) -> bytes:
"""Convert browser containers to WAV before Parakeet's WAV-only batch path."""
media_type = (content_type or '').split(';', 1)[0].strip().lower()
container_format = _PARAKEET_CONTAINER_MIME_FORMATS.get(media_type)
if container_format is None:
return audio_bytes
try:
decoded_audio = AudioSegment.from_file(BytesIO(audio_bytes), format=container_format)
wav_buffer = BytesIO()
decoded_audio.export(wav_buffer, format='wav')
wav_bytes = wav_buffer.getvalue()
del decoded_audio
del wav_buffer
return wav_bytes
except Exception as error:
raise ParakeetAudioDecodeError('Browser audio container could not be decoded') from error
@timeit
def parakeet_prerecorded_from_bytes(
audio_bytes: bytes,
sample_rate: int = 16000,
diarize: bool = True,
attempts: int = 0,
encoding: Optional[str] = None,
channels: int = 1,
language: Optional[str] = None,
return_language: bool = False,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
logger.info(
f'parakeet_prerecorded_from_bytes bytes_len={len(audio_bytes)} {sample_rate} {diarize} {attempts} encoding={encoding}'
)
require_provider_environment(PrerecordedSTTService.PARAKEET)
api_url = os.environ['HOSTED_PARAKEET_API_URL']
try:
if encoding:
audio_bytes = _wrap_pcm_as_wav(audio_bytes, sample_rate, channels)
files = {'file': ('audio.wav', BytesIO(audio_bytes), 'audio/wav')}
use_v2 = diarize and os.getenv('PARAKEET_USE_V2', '1') == '1'
if use_v2:
url = api_url.rstrip('/') + '/v2/transcribe'
data = {'diarize': 'true'}
else:
url = api_url.rstrip('/') + '/v1/transcribe'
data = {}
with httpx.Client(timeout=_PARAKEET_TIMEOUT) as client:
response = client.post(url, files=files, data=data if data else None)
if response.status_code == 404 and use_v2:
url = api_url.rstrip('/') + '/v1/transcribe'
response = client.post(url, files={'file': ('audio.wav', BytesIO(audio_bytes), 'audio/wav')})
use_v2 = False
response.raise_for_status()
payload: Any = response.json()
# A Parakeet result always carries both keys, even for silence ({"text": "",
# "segments": []}). A 200 body with neither key is a degraded or foreign
# responder (misrouted ILB, proxy error shell), not a no-speech verdict. Raise
# so the sync job stays truthful and clients keep the audio as retry material
# instead of marking the WAL synced and discarding it. See #9586.
if not isinstance(payload, dict) or ('segments' not in payload and 'text' not in payload):
raise RuntimeError('Parakeet response contained neither segments nor text')
result: Dict[str, Any] = cast(Dict[str, Any], payload)
raw_segments = result.get('segments', [])
segments: List[Dict[str, Any]] = list(raw_segments) if isinstance(raw_segments, list) else [] # type: ignore[reportUnknownArgumentType] # untyped external JSON
full_text = (result.get('text') or '').strip()
if not segments and not full_text:
if return_language:
return [], language or 'en'
return []
spk_centroids: List[np.ndarray[Any, Any]] = []
spk_counts: List[int] = []
words: List[Dict[str, Any]] = []
for seg in segments:
text = (seg.get('text') or '').strip()
if not text:
continue
start = float(seg.get('start', 0.0))
end = float(seg.get('end', start))
speaker_label = seg.get('speaker', '') if use_v2 else ''
if not speaker_label:
speaker_label = 'SPEAKER_00'
if diarize:
speaker_label = _parakeet_assign_speaker_sync(
audio_bytes, sample_rate, start, end, spk_centroids, spk_counts
)
if not speaker_label.startswith('SPEAKER_'):
speaker_label = f'SPEAKER_{speaker_label}'
words.append({'timestamp': [start, end], 'speaker': speaker_label, 'text': text})
if not words and full_text:
words.append({'timestamp': [0.0, 0.0], 'speaker': 'SPEAKER_00', 'text': full_text})
if return_language:
detected = result.get('detected_language') or language or 'en'
return words, detected
return words
except Exception as e:
logger.error('Parakeet prerecorded error exception_type=%s attempt=%s', type(e).__name__, attempts + 1)
if attempts < 1:
return parakeet_prerecorded_from_bytes(
audio_bytes, sample_rate, diarize, attempts + 1, None, channels, language, return_language
)
raise RuntimeError(f'Parakeet transcription failed after {attempts + 1} attempts') from e
@timeit
def parakeet_prerecorded(
audio_url: str,
speakers_count: Optional[int] = None,
attempts: int = 0,
return_language: bool = False,
diarize: bool = True,
language: Optional[str] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
logger.info(f'parakeet_prerecorded url_len={len(audio_url)} {speakers_count} {attempts}')
try:
with httpx.Client(timeout=_PARAKEET_URL_DOWNLOAD_TIMEOUT) as client:
with client.stream('GET', audio_url) as resp:
resp.raise_for_status()
content_length = resp.headers.get('content-length')
if content_length and int(content_length) > _PARAKEET_MAX_DOWNLOAD_BYTES:
raise ValueError(
f'Audio file too large: {content_length} bytes (max {_PARAKEET_MAX_DOWNLOAD_BYTES})'
)
chunks: List[bytes] = []
total = 0
for chunk in resp.iter_bytes(chunk_size=1024 * 1024):
total += len(chunk)
if total > _PARAKEET_MAX_DOWNLOAD_BYTES:
raise ValueError(f'Audio download exceeded {_PARAKEET_MAX_DOWNLOAD_BYTES} bytes')
chunks.append(chunk)
audio_bytes = b''.join(chunks)
del chunks
audio_bytes = _normalize_parakeet_download(audio_bytes, resp.headers.get('content-type'))
return parakeet_prerecorded_from_bytes(
audio_bytes, diarize=diarize, attempts=attempts, return_language=return_language, language=language
)
except ParakeetAudioDecodeError:
raise
except Exception as e:
logger.error(
'Parakeet prerecorded (url) error exception_type=%s attempt=%s',
type(e).__name__,
attempts + 1,
)
if attempts < 1:
return parakeet_prerecorded(audio_url, speakers_count, attempts + 1, return_language, diarize, language)
raise RuntimeError(f'Parakeet transcription (url) failed after {attempts + 1} attempts') from e
def _parakeet_assign_speaker_sync(
wav_bytes: bytes,
sample_rate: int,
seg_start: float,
seg_end: float,
centroids: List[np.ndarray[Any, Any]],
counts: List[int],
) -> str:
if seg_end - seg_start < 0.6:
return 'SPEAKER_00'
try:
seg_pcm = _extract_pcm_segment_from_wav(wav_bytes, seg_start, seg_end)
if len(seg_pcm) < int(sample_rate * 2 * 0.6):
return 'SPEAKER_00'
seg_wav = _wrap_pcm_as_wav(seg_pcm, sample_rate, 1)
emb = extract_embedding_from_bytes(seg_wav)
best_i, create_new, _, capped = select_speaker_cluster(emb, centroids, compare_embeddings)
if not create_new:
if capped:
# Forced by the cap: the embedding missed every centroid, so keep
# it out of the running mean and report the degraded merge.
record_fallback(
component='other',
from_mode='new_speaker_centroid',
to_mode='nearest_centroid',
reason='capacity_full',
outcome='degraded',
log=logger,
)
return f'SPEAKER_{best_i:02d}'
n = counts[best_i]
centroids[best_i] = (centroids[best_i] * n + emb) / (n + 1)
counts[best_i] = n + 1
return f'SPEAKER_{best_i:02d}'
centroids.append(emb)
counts.append(1)
return f'SPEAKER_{best_i:02d}'
except Exception as e:
logger.warning(f'Parakeet batch diarization failed, defaulting to SPEAKER_00: {e}')
return 'SPEAKER_00'
def _extract_pcm_segment_from_wav(wav_bytes: bytes, start: float, end: float) -> bytes:
buf = BytesIO(wav_bytes)
with _wave.open(buf, 'rb') as wf:
sr = wf.getframerate()
start_frame = int(start * sr)
end_frame = int(end * sr)
wf.setpos(start_frame)
return wf.readframes(end_frame - start_frame)
def _wrap_pcm_as_wav(pcm_bytes: bytes, sample_rate: int, channels: int, bits_per_sample: int = 16) -> bytes:
buf = BytesIO()
with _wave.open(buf, 'wb') as wf:
wf.setnchannels(channels)
wf.setsampwidth(bits_per_sample // 8)
wf.setframerate(sample_rate)
wf.writeframes(pcm_bytes)
return buf.getvalue()
class ParakeetPrerecordedProvider(PrerecordedSTTProvider):
def transcribe_url(
self,
audio_url: str,
speakers_count: Optional[int] = None,
attempts: int = 0,
return_language: bool = False,
diarize: bool = True,
language: Optional[str] = None,
keywords: Optional[Sequence[str]] = None,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], str]]:
return parakeet_prerecorded(
audio_url,
speakers_count=speakers_count,
attempts=attempts,
return_language=return_language,
diarize=diarize,
language=language,
)
def transcribe_bytes(
self,