Audio Streaming
Mono (Change to 2 if Stereo).
Audio Streaming.
1"""2Audio Streaming3=============================45Audio Streaming.6"""78import base649import wave10from typing import Iterator1112from kern.agent import Agent, RunOutputEvent13from kern.models.openai import OpenAIChat1415# Audio Configuration16SAMPLE_RATE = 24000 # Hz (24kHz)17CHANNELS = 1 # Mono (Change to 2 if Stereo)18SAMPLE_WIDTH = 2 # Bytes (16 bits)1920# Provide the agent with the audio file and audio configuration and get result as text + audio21# ---------------------------------------------------------------------------22# Create Agent23# ---------------------------------------------------------------------------24agent = Agent(25 model=OpenAIChat(26 id="gpt-4o-audio-preview",27 modalities=["text", "audio"],28 audio={29 "voice": "alloy",30 "format": "pcm16",31 }, # Only pcm16 is supported with streaming32 ),33)3435# ---------------------------------------------------------------------------36# Run Agent37# ---------------------------------------------------------------------------38if __name__ == "__main__":39 output_stream: Iterator[RunOutputEvent] = agent.run(40 "Tell me a 10 second story", stream=True41 )4243 filename = "tmp/response_stream.wav"4445 # Open the file once in append-binary mode46 with wave.open(str(filename), "wb") as wav_file:47 wav_file.setnchannels(CHANNELS)48 wav_file.setsampwidth(SAMPLE_WIDTH)49 wav_file.setframerate(SAMPLE_RATE)5051 # Iterate over generated audio52 for response in output_stream:53 response_audio = response.response_audio # type: ignore54 if response_audio:55 if response_audio.transcript:56 print(response_audio.transcript, end="", flush=True)57 if response_audio.content:58 try:59 pcm_bytes = base64.b64decode(response_audio.content)60 wav_file.writeframes(pcm_bytes)61 except Exception as e:62 print(f"Error decoding audio: {e}")63 print()Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/12_multimodal45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python audio_streaming.py