1010
1111from deepgram import DeepgramClient
1212from deepgram .helpers import TextBuilder , add_pronunciation , ssml_to_deepgram
13- from deepgram .speak .v1 .audio .types import (
14- AudioGenerateRequestEncoding ,
15- AudioGenerateRequestModel ,
16- )
1713
1814
1915def example_basic_text_builder ():
2016 """Example 1: Basic TextBuilder usage with pronunciations and pauses"""
2117 print ("Example 1: Basic TextBuilder Usage" )
2218 print ("-" * 50 )
2319
24- # Build text with pronunciations and pauses
20+ # Build text with pronunciations
21+ # Note: .pause() is supported in streaming (WebSocket) mode.
22+ # For REST API, use plain text between pronunciations.
2523 text = (
2624 TextBuilder ()
2725 .text ("Take " )
2826 .pronunciation ("azathioprine" , "ˌæzəˈθaɪəpriːn" )
2927 .text (" twice daily with " )
3028 .pronunciation ("dupilumab" , "duːˈpɪljuːmæb" )
31- .text (" injections" )
32- .pause (500 )
33- .text (" Do not exceed prescribed dosage." )
29+ .text (" injections. Do not exceed prescribed dosage." )
3430 .build ()
3531 )
3632
@@ -42,15 +38,16 @@ def example_basic_text_builder():
4238 client = DeepgramClient (api_key = api_key )
4339
4440 # Generate speech with custom pronunciations
45- response = client .speak .v1 .generate (
46- text ,
47- model = AudioGenerateRequestModel . AURA_ASTERIA_EN ,
48- encoding = AudioGenerateRequestEncoding . LINEAR16 ,
41+ response = client .speak .v1 .audio . generate (
42+ text = text ,
43+ model = "aura-2-asteria-en" ,
44+ encoding = "linear16" ,
4945 )
5046
5147 # Save to file
5248 with open ("output_example1.wav" , "wb" ) as f :
53- f .write (response )
49+ for chunk in response :
50+ f .write (chunk )
5451
5552 print ("✓ Audio saved to output_example1.wav" )
5653 else :
@@ -75,13 +72,14 @@ def example_add_pronunciation_function():
7572 if api_key :
7673 client = DeepgramClient (api_key = api_key )
7774
78- response = client .speak .v1 .generate (
79- text ,
80- model = AudioGenerateRequestModel . AURA_ASTERIA_EN ,
75+ response = client .speak .v1 .audio . generate (
76+ text = text ,
77+ model = "aura-2-asteria-en" ,
8178 )
8279
8380 with open ("output_example2.wav" , "wb" ) as f :
84- f .write (response )
81+ for chunk in response :
82+ f .write (chunk )
8583
8684 print ("✓ Audio saved to output_example2.wav" )
8785 else :
@@ -96,10 +94,8 @@ def example_ssml_migration():
9694 # Existing SSML from another TTS provider
9795 ssml = """<speak>
9896 Welcome to your medication guide.
99- <break time="500ms"/>
100- Take <phoneme alphabet="ipa" ph="ˌæzəˈθaɪəpriːn">azathioprine</phoneme>
97+ Take <phoneme alphabet="ipa" ph="ˌæzəˈθaɪəpriːn">azathioprine</phoneme>
10198 as prescribed.
102- <break time="1000ms"/>
10399 Contact your doctor if you experience side effects.
104100 </speak>"""
105101
@@ -112,13 +108,14 @@ def example_ssml_migration():
112108 if api_key :
113109 client = DeepgramClient (api_key = api_key )
114110
115- response = client .speak .v1 .generate (
116- text ,
117- model = AudioGenerateRequestModel . AURA_ASTERIA_EN ,
111+ response = client .speak .v1 .audio . generate (
112+ text = text ,
113+ model = "aura-2-asteria-en" ,
118114 )
119115
120116 with open ("output_example3.wav" , "wb" ) as f :
121- f .write (response )
117+ for chunk in response :
118+ f .write (chunk )
122119
123120 print ("✓ Audio saved to output_example3.wav" )
124121 else :
@@ -137,9 +134,7 @@ def example_mixed_ssml_and_builder():
137134 text = (
138135 TextBuilder ()
139136 .from_ssml (ssml )
140- .pause (500 )
141137 .text (" Store at room temperature." )
142- .pause (500 )
143138 .text (" Keep out of reach of children." )
144139 .build ()
145140 )
@@ -150,13 +145,14 @@ def example_mixed_ssml_and_builder():
150145 if api_key :
151146 client = DeepgramClient (api_key = api_key )
152147
153- response = client .speak .v1 .generate (
154- text ,
155- model = AudioGenerateRequestModel . AURA_ASTERIA_EN ,
148+ response = client .speak .v1 .audio . generate (
149+ text = text ,
150+ model = "aura-2-asteria-en" ,
156151 )
157152
158153 with open ("output_example4.wav" , "wb" ) as f :
159- f .write (response )
154+ for chunk in response :
155+ f .write (chunk )
160156
161157 print ("✓ Audio saved to output_example4.wav" )
162158 else :
@@ -172,19 +168,15 @@ def example_pharmacy_instructions():
172168 TextBuilder ()
173169 .text ("Prescription for " )
174170 .pronunciation ("lisinopril" , "laɪˈsɪnəprɪl" )
175- .pause (300 )
176- .text (" Take one tablet by mouth daily for hypertension." )
177- .pause (500 )
171+ .text (". Take one tablet by mouth daily for hypertension." )
178172 .text (" Common side effects may include " )
179173 .pronunciation ("hypotension" , "ˌhaɪpoʊˈtɛnʃən" )
180174 .text (" or dizziness." )
181- .pause (500 )
182175 .text (" Do not take with " )
183176 .pronunciation ("aliskiren" , "əˈlɪskɪrɛn" )
184177 .text (" or " )
185178 .pronunciation ("sacubitril" , "səˈkjuːbɪtrɪl" )
186- .pause (500 )
187- .text (" Call your doctor if symptoms worsen." )
179+ .text (". Call your doctor if symptoms worsen." )
188180 .build ()
189181 )
190182
@@ -194,14 +186,15 @@ def example_pharmacy_instructions():
194186 if api_key :
195187 client = DeepgramClient (api_key = api_key )
196188
197- response = client .speak .v1 .generate (
198- text ,
199- model = AudioGenerateRequestModel . AURA_ASTERIA_EN ,
200- encoding = AudioGenerateRequestEncoding . LINEAR16 ,
189+ response = client .speak .v1 .audio . generate (
190+ text = text ,
191+ model = "aura-2-asteria-en" ,
192+ encoding = "linear16" ,
201193 )
202194
203195 with open ("output_example5.wav" , "wb" ) as f :
204- f .write (response )
196+ for chunk in response :
197+ f .write (chunk )
205198
206199 print ("✓ Audio saved to output_example5.wav" )
207200 else :
0 commit comments