@@ -35,21 +35,22 @@ def get_model_name(value):
3535soniox_supported_languages = ['multi' ,'en' , 'af' , 'sq' , 'ar' , 'az' , 'eu' , 'be' , 'bn' , 'bs' , 'bg' , 'ca' , 'zh' , 'hr' , 'cs' , 'da' , 'nl' , 'et' , 'fi' , 'fr' , 'gl' , 'de' , 'el' , 'gu' , 'he' , 'hi' , 'hu' , 'id' , 'it' , 'ja' , 'kn' , 'kk' , 'ko' , 'lv' , 'lt' , 'mk' , 'ms' , 'ml' , 'mr' , 'no' , 'fa' , 'pl' , 'pt' , 'pa' , 'ro' , 'ru' , 'sr' , 'sk' , 'sl' , 'es' , 'sw' , 'sv' , 'tl' , 'ta' , 'te' , 'th' , 'tr' , 'uk' , 'ur' , 'vi' , 'cy' ]
3636soniox_multi_languages = soniox_supported_languages
3737
38- # Languages supported by Deepgram, nova-2-general model
38+ # Languages supported by Deepgram, nova-2/nova-3 model
3939deepgram_supported_languages = {'multi' ,'bg' ,'ca' , 'zh' , 'zh-CN' , 'zh-Hans' , 'zh-TW' , 'zh-Hant' , 'zh-HK' , 'cs' , 'da' , 'da-DK' , 'nl' , 'en' , 'en-US' , 'en-AU' , 'en-GB' , 'en-NZ' , 'en-IN' , 'et' , 'fi' , 'nl-BE' , 'fr' , 'fr-CA' , 'de' , 'de-CH' , 'el' 'hi' , 'hu' , 'id' , 'it' , 'ja' , 'ko' , 'ko-KR' , 'lv' , 'lt' , 'ms' , 'no' , 'pl' , 'pt' , 'pt-BR' , 'pt-PT' , 'ro' , 'ru' , 'sk' , 'es' , 'es-419' , 'sv' , 'sv-SE' , 'th' , 'th-TH' , 'tr' , 'uk' , 'vi' }
40- deepgram_multi_languages = ['en' , 'es' ]
40+ deepgram_nova2_multi_languages = ['en' , 'es' ]
41+ deepgram_multi_languages = ["en" , "en-US" , "en-AU" , "en-GB" , "en-NZ" , "en-IN" , "es" , "es-419" , "fr" , "fr-CA" , "de" , "hi" , "ru" , "pt" , "pt-BR" , "pt-PT" , "ja" , "it" , "nl" , "nl-BE" ]
4142
4243def get_stt_service_for_language (language : str ):
4344 # Deepgram's 'multi'
4445 if language in deepgram_multi_languages :
45- return STTService .deepgram , 'multi'
46+ return STTService .deepgram , 'multi' , 'nova-3'
4647
4748 # Deepgram
4849 if language in deepgram_supported_languages :
49- return STTService .deepgram , language
50+ return STTService .deepgram , language , 'nova-2-general'
5051
51- # Default to Soniox
52- return None , None
52+ # Fallback to Deepgram en
53+ return STTService . deepgram , 'en' , 'nova-2-general'
5354
5455
5556async def send_initial_file_path (file_path : str , transcript_socket_async_send ):
@@ -86,19 +87,23 @@ async def send_initial_file(data: List[List[int]], transcript_socket):
8687is_dg_self_hosted = os .getenv ('DEEPGRAM_SELF_HOSTED_ENABLED' , '' ).lower () == 'true'
8788deepgram_options = DeepgramClientOptions (options = {"keepalive" : "true" , "termination_exception_connect" : "true" })
8889
90+ deepgram_beta_options = DeepgramClientOptions (options = {"keepalive" : "true" , "termination_exception_connect" : "true" })
91+ deepgram_beta_options .url = "https://api.beta.deepgram.com"
92+
8993if is_dg_self_hosted :
9094 dg_self_hosted_url = os .getenv ('DEEPGRAM_SELF_HOSTED_URL' )
9195 if not dg_self_hosted_url :
9296 raise ValueError ("DEEPGRAM_SELF_HOSTED_URL must be set when DEEPGRAM_SELF_HOSTED_ENABLED is true" )
9397 # Override only the URL while keeping all other options
9498 deepgram_options .url = dg_self_hosted_url
99+ deepgram_beta_options .url = dg_self_hosted_url
95100 print (f"Using Deepgram self-hosted at: { dg_self_hosted_url } " )
96101
97102deepgram = DeepgramClient (os .getenv ('DEEPGRAM_API_KEY' ), deepgram_options )
98-
103+ deepgram_beta = DeepgramClient ( os . getenv ( 'DEEPGRAM_API_KEY' ), deepgram_beta_options )
99104
100105async def process_audio_dg (
101- stream_transcript , language : str , sample_rate : int , channels : int , preseconds : int = 0 ,
106+ stream_transcript , language : str , sample_rate : int , channels : int , preseconds : int = 0 , model : str = 'nova-2-general' ,
102107):
103108 print ('process_audio_dg' , language , sample_rate , channels , preseconds )
104109
@@ -146,7 +151,7 @@ def on_error(self, error, **kwargs):
146151 print (f"Error: { error } " )
147152
148153 print ("Connecting to Deepgram" ) # Log before connection attempt
149- return connect_to_deepgram_with_backoff (on_message , on_error , language , sample_rate , channels )
154+ return connect_to_deepgram_with_backoff (on_message , on_error , language , sample_rate , channels , model )
150155
151156
152157# Calculate backoff with jitter
@@ -156,11 +161,11 @@ def calculate_backoff_with_jitter(attempt, base_delay=1000, max_delay=32000):
156161 return backoff
157162
158163
159- def connect_to_deepgram_with_backoff (on_message , on_error , language : str , sample_rate : int , channels : int , retries = 3 ):
164+ def connect_to_deepgram_with_backoff (on_message , on_error , language : str , sample_rate : int , channels : int , model : str , retries = 3 ):
160165 print ("connect_to_deepgram_with_backoff" )
161166 for attempt in range (retries ):
162167 try :
163- return connect_to_deepgram (on_message , on_error , language , sample_rate , channels )
168+ return connect_to_deepgram (on_message , on_error , language , sample_rate , channels , model )
164169 except Exception as error :
165170 print (f'An error occurred: { error } ' )
166171 if attempt == retries - 1 : # Last attempt
@@ -172,10 +177,14 @@ def connect_to_deepgram_with_backoff(on_message, on_error, language: str, sample
172177 raise Exception (f'Could not open socket: All retry attempts failed.' )
173178
174179
175- def connect_to_deepgram (on_message , on_error , language : str , sample_rate : int , channels : int ):
176- # 'wss://api.deepgram.com/v1/listen?encoding=linear16&sample_rate=8000&language=$recordingsLanguage&model=nova-2-general&no_delay=true&endpointing=100&interim_results=false&smart_format=true&diarize=true'
180+ def connect_to_deepgram (on_message , on_error , language : str , sample_rate : int , channels : int , model : str ):
177181 try :
178- dg_connection = deepgram .listen .websocket .v ("1" )
182+ # get connection by model
183+ if model == "nova-3" :
184+ dg_connection = deepgram_beta .listen .websocket .v ("1" )
185+ else :
186+ dg_connection = deepgram .listen .websocket .v ("1" )
187+
179188 dg_connection .on (LiveTranscriptionEvents .Transcript , on_message )
180189 dg_connection .on (LiveTranscriptionEvents .Error , on_error )
181190
@@ -215,7 +224,7 @@ def on_unhandled(self, unhandled, **kwargs):
215224 filler_words = False ,
216225 channels = channels ,
217226 multichannel = channels > 1 ,
218- model = 'nova-2-general' ,
227+ model = model ,
219228 sample_rate = sample_rate ,
220229 encoding = 'linear16'
221230 )
0 commit comments