Skip to content

Commit 25d4457

Browse files
authored
feat(audio): add PyAudio initialization check and error handling (#355)
This commit introduces proper initialization checks for PyAudio in both realtime transcription examples. It adds: 1. A new utility function `load_pyaudio()` to handle PyAudio initialization 2. Error handling for cases where PyAudio cannot be initialized 3. Early termination with error message when PyAudio is not available 4. Consistent error handling across both microphone examples The changes ensure the applications fail gracefully when PyAudio is not available or cannot be initialized, providing better user feedback.
1 parent d6a7fb5 commit 25d4457

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

examples/mistral/audio/async_realtime_transcription_dual_delay_microphone.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
TranscriptionStreamTextDelta,
3636
)
3737

38+
from pyaudio_utils import load_pyaudio
39+
3840
console = Console()
3941

4042

@@ -188,7 +190,7 @@ async def iter_microphone(
188190
Yield microphone PCM chunks using PyAudio (16-bit mono).
189191
Encoding is always pcm_s16le.
190192
"""
191-
import pyaudio
193+
pyaudio = load_pyaudio()
192194

193195
p = pyaudio.PyAudio()
194196
chunk_samples = int(sample_rate * chunk_duration_ms / 1000)
@@ -373,6 +375,12 @@ async def main() -> int:
373375
args = parse_args()
374376
api_key = args.api_key or os.environ["MISTRAL_API_KEY"]
375377

378+
try:
379+
load_pyaudio()
380+
except RuntimeError as exc:
381+
console.print(str(exc), style="red")
382+
return 1
383+
376384
state = DualTranscriptState()
377385
display = DualTranscriptDisplay(
378386
model=args.model,
@@ -431,6 +439,16 @@ async def main() -> int:
431439
try:
432440
while True:
433441
await asyncio.sleep(0.1)
442+
for task in (broadcaster, fast_task, slow_task):
443+
if not task.done():
444+
continue
445+
exc = task.exception()
446+
if exc:
447+
state.set_error(str(exc))
448+
if update_queue.empty():
449+
update_queue.put_nowait(None)
450+
stop_event.set()
451+
break
434452
if state.error:
435453
stop_event.set()
436454
break

examples/mistral/audio/async_realtime_transcription_microphone.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
TranscriptionStreamTextDelta,
3434
)
3535

36+
from pyaudio_utils import load_pyaudio
37+
3638
console = Console()
3739

3840

@@ -131,7 +133,7 @@ async def iter_microphone(
131133
Yield microphone PCM chunks using PyAudio (16-bit mono).
132134
Encoding is always pcm_s16le.
133135
"""
134-
import pyaudio
136+
pyaudio = load_pyaudio()
135137

136138
p = pyaudio.PyAudio()
137139
chunk_samples = int(sample_rate * chunk_duration_ms / 1000)
@@ -189,6 +191,12 @@ async def main() -> int:
189191
args = parse_args()
190192
api_key = args.api_key or os.environ["MISTRAL_API_KEY"]
191193

194+
try:
195+
load_pyaudio()
196+
except RuntimeError as exc:
197+
console.print(str(exc), style="red")
198+
return 1
199+
192200
client = Mistral(api_key=api_key, server_url=args.base_url)
193201

194202
# microphone is always pcm_s16le here
@@ -231,6 +239,10 @@ async def main() -> int:
231239
except KeyboardInterrupt:
232240
display.status = "⏹️ Stopped"
233241
live.update(display.render())
242+
except Exception as exc:
243+
display.set_error(str(exc))
244+
live.update(display.render())
245+
return 1
234246

235247
return 0
236248

0 commit comments

Comments
 (0)