Skip to content

Commit 892dd87

Browse files
Fix docs (#34)
* docs: improve stream disconnect
1 parent 38ee72a commit 892dd87

3 files changed

Lines changed: 72 additions & 49 deletions

File tree

README.md

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ result = AiolaClient.grant_token(
4040
api_key='your-api-key'
4141
)
4242

43-
access_token = result['accessToken']
43+
access_token = result['accessToken']
4444
session_id = result['sessionId']
4545
```
4646

@@ -64,21 +64,21 @@ def example():
6464
result = AiolaClient.grant_token(
6565
api_key=os.getenv('AIOLA_API_KEY')
6666
)
67-
67+
6868
# Step 2: Create client
6969
client = AiolaClient(
7070
access_token=result['accessToken']
7171
)
72-
72+
7373
# Step 3: Use client for API calls
7474
with open('path/to/your/audio.wav', 'rb') as audio_file:
7575
transcript = client.stt.transcribe_file(
7676
file=audio_file,
7777
language='en'
7878
)
79-
79+
8080
print('Transcript:', transcript)
81-
81+
8282
except Exception as error:
8383
print('Error:', error)
8484
```
@@ -118,17 +118,17 @@ def transcribe_file():
118118
result = AiolaClient.grant_token(
119119
api_key=os.getenv('AIOLA_API_KEY')
120120
)
121-
121+
122122
# Step 2: Create client
123123
client = AiolaClient(
124124
access_token=result['accessToken']
125125
)
126-
126+
127127
# Step 3: Transcribe file
128128
with open('path/to/your/audio.wav', 'rb') as audio_file:
129129
transcript = client.stt.transcribe_file(
130130
file=audio_file,
131-
language="e" # supported lan: en,de,fr,es,pr,zh,ja,it
131+
language="e" # supported lan: en,de,fr,es,pr,zh,ja,it
132132
)
133133

134134
print(transcript)
@@ -140,52 +140,72 @@ def transcribe_file():
140140

141141
```python
142142
import os
143-
import time
144-
from aiola import AiolaClient, MicrophoneStream # pip install 'aiola[mic]'
143+
from aiola import AiolaClient, MicrophoneStream
145144
from aiola.types import LiveEvents
146145

147-
148146
def live_streaming():
149147
try:
148+
# Step 1: Generate access token, save it
150149
result = AiolaClient.grant_token(
151-
api_key=os.getenv("AIOLA_API_KEY") or "YOUR_API_KEY"
150+
api_key=os.getenv('AIOLA_API_KEY') or 'YOUR_API_KEY'
152151
)
153-
client = AiolaClient(access_token=result["accessToken"])
152+
153+
# Step 2: Create client using the access token
154+
client = AiolaClient(
155+
access_token=result['accessToken']
156+
)
157+
158+
# Step 3: Start streaming
154159
connection = client.stt.stream(
155-
lang_code="en", # supported lan: en,de,fr,es,pr,zh,ja,it
156-
keywords={"<word_to_catch>": "<word_transcribe>"}
157-
)
160+
lang_code='en'
161+
)
158162

159163
@connection.on(LiveEvents.Transcript)
160164
def on_transcript(data):
161-
print("Transcript:", data.get("transcript", data))
165+
print('Transcript:', data.get('transcript', data))
162166

163167
@connection.on(LiveEvents.Connect)
164168
def on_connect():
165-
print("Connected to streaming service")
169+
print('Connected to streaming service')
166170

167171
@connection.on(LiveEvents.Disconnect)
168172
def on_disconnect():
169-
print("Disconnected from streaming service")
173+
print('Disconnected from streaming service')
170174

171175
@connection.on(LiveEvents.Error)
172176
def on_error(error):
173-
print("Streaming error:", error)
177+
print('Streaming error:', error)
174178

175179
connection.connect()
176180

177-
with MicrophoneStream(channels=1, samplerate=16000, blocksize=4096) as mic:
178-
mic.stream_to(connection)
179-
# Keep the main thread alive
180-
while True:
181-
time.sleep(0.1)
181+
try:
182+
# Capture audio from microphone using the SDK's MicrophoneStream
183+
with MicrophoneStream(
184+
channels=1,
185+
samplerate=16000,
186+
blocksize=4096,
187+
) as mic:
188+
mic.stream_to(connection)
189+
190+
# Keep the main thread alive
191+
while True:
192+
try:
193+
import time
194+
time.sleep(0.1)
195+
except KeyboardInterrupt:
196+
print('Keyboard interrupt')
197+
break
198+
199+
except KeyboardInterrupt:
200+
print('Keyboard interrupt')
182201

183-
except KeyboardInterrupt:
184-
print("Keyboard interrupt")
185202
except Exception as error:
186-
print("Error:", error)
203+
print('Error:', error)
187204
finally:
188205
connection.disconnect()
206+
207+
if __name__ == "__main__":
208+
live_streaming()
189209
```
190210

191211
### Text-to-Speech
@@ -203,7 +223,7 @@ def create_file():
203223
client = AiolaClient(
204224
access_token=result['accessToken']
205225
)
206-
226+
207227
audio = client.tts.synthesize(
208228
text='Hello, how can I help you today?',
209229
voice='jess',
@@ -213,7 +233,7 @@ def create_file():
213233
with open('./audio.wav', 'wb') as f:
214234
for chunk in audio:
215235
f.write(chunk)
216-
236+
217237
print('Audio file created successfully')
218238
except Exception as error:
219239
print('Error creating audio file:', error)
@@ -232,11 +252,11 @@ def stream_tts():
232252
result = AiolaClient.grant_token(
233253
api_key=os.getenv('AIOLA_API_KEY')
234254
)
235-
255+
236256
client = AiolaClient(
237257
access_token=result['accessToken']
238258
)
239-
259+
240260
stream = client.tts.stream(
241261
text='Hello, how can I help you today?',
242262
voice='jess',
@@ -246,7 +266,7 @@ def stream_tts():
246266
audio_chunks = []
247267
for chunk in stream:
248268
audio_chunks.append(chunk)
249-
269+
250270
print('Audio chunks received:', len(audio_chunks))
251271
except Exception as error:
252272
print('Error streaming TTS:', error)
@@ -268,15 +288,15 @@ async def transcribe_file():
268288
result = await AsyncAiolaClient.grant_token(
269289
api_key=os.getenv('AIOLA_API_KEY')
270290
)
271-
291+
272292
client = AsyncAiolaClient(
273293
access_token=result['accessToken']
274294
)
275-
295+
276296
with open('path/to/your/audio.wav', 'rb') as audio_file:
277297
transcript = await client.stt.transcribe_file(
278298
file=audio_file,
279-
language="e" # supported lan: en,de,fr,es,pr,zh,ja,it
299+
language="e" # supported lan: en,de,fr,es,pr,zh,ja,it
280300
)
281301

282302
print(transcript)
@@ -299,11 +319,11 @@ async def create_audio_file():
299319
result = await AsyncAiolaClient.grant_token(
300320
api_key=os.getenv('AIOLA_API_KEY')
301321
)
302-
322+
303323
client = AsyncAiolaClient(
304324
access_token=result['accessToken']
305325
)
306-
326+
307327
audio = client.tts.synthesize(
308328
text='Hello, how can I help you today?',
309329
voice='jess',
@@ -313,7 +333,7 @@ async def create_audio_file():
313333
with open('./audio.wav', 'wb') as f:
314334
async for chunk in audio:
315335
f.write(chunk)
316-
336+
317337
print('Audio file created successfully')
318338
except Exception as error:
319339
print('Error creating audio file:', error)
@@ -334,11 +354,11 @@ async def stream_tts():
334354
result = await AsyncAiolaClient.grant_token(
335355
api_key=os.getenv('AIOLA_API_KEY')
336356
)
337-
357+
338358
client = AsyncAiolaClient(
339359
access_token=result['accessToken']
340360
)
341-
361+
342362
stream = client.tts.stream(
343363
text='Hello, how can I help you today?',
344364
voice='jess',
@@ -348,7 +368,7 @@ async def stream_tts():
348368
audio_chunks = []
349369
async for chunk in stream:
350370
audio_chunks.append(chunk)
351-
371+
352372
print('Audio chunks received:', len(audio_chunks))
353373
except Exception as error:
354374
print('Error streaming TTS:', error)

aiola/mic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ def _stream_worker():
163163
while self._is_recording:
164164
try:
165165
audio_data = self.read(timeout=0.1)
166+
# Check if connection is still active before sending
167+
if hasattr(connection, "connected") and not connection.connected:
168+
break
166169
connection.send(audio_data)
167170
except queue.Empty:
168171
continue

examples/stt/mic_stream.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ def live_streaming():
88
result = AiolaClient.grant_token(
99
api_key=os.getenv('AIOLA_API_KEY') or 'YOUR_API_KEY'
1010
)
11-
11+
1212
# Step 2: Create client using the access token
1313
client = AiolaClient(
1414
access_token=result['accessToken']
1515
)
16-
16+
1717
# Step 3: Start streaming
1818
connection = client.stt.stream(
1919
lang_code='en'
@@ -45,7 +45,7 @@ def on_error(error):
4545
blocksize=4096,
4646
) as mic:
4747
mic.stream_to(connection)
48-
48+
4949
# Keep the main thread alive
5050
while True:
5151
try:
@@ -54,14 +54,14 @@ def on_error(error):
5454
except KeyboardInterrupt:
5555
print('Keyboard interrupt')
5656
break
57-
57+
5858
except KeyboardInterrupt:
5959
print('Keyboard interrupt')
60-
finally:
61-
connection.disconnect()
62-
60+
6361
except Exception as error:
6462
print('Error:', error)
63+
finally:
64+
connection.disconnect()
6565

6666
if __name__ == "__main__":
6767
live_streaming()

0 commit comments

Comments
 (0)