Skip to content

Commit 6683121

Browse files
makelinuxclaude
andcommitted
Add input/output transcription to live audio example
Enable text transcription alongside audio streaming. Output transcription prints inline, input transcription in italic. Newlines separate input from output and after sentence-ending punctuation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>
1 parent 0a2e0f0 commit 6683121

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

command-line/python/main.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
CONFIG = {
1919
"response_modalities": ["AUDIO"],
2020
"system_instruction": "You are a helpful and friendly AI assistant.",
21+
"output_audio_transcription": {},
22+
"input_audio_transcription": {},
2123
}
2224

2325
audio_queue_output = asyncio.Queue()
@@ -50,13 +52,33 @@ async def send_realtime(session):
5052

5153
async def receive_audio(session):
5254
"""Receives responses from GenAI and puts audio data into the speaker audio queue."""
55+
last_was_input = False
5356
while True:
5457
turn = session.receive()
5558
async for response in turn:
56-
if (response.server_content and response.server_content.model_turn):
57-
for part in response.server_content.model_turn.parts:
59+
sc = response.server_content
60+
if not sc:
61+
continue
62+
if sc.model_turn:
63+
for part in sc.model_turn.parts:
5864
if part.inline_data and isinstance(part.inline_data.data, bytes):
5965
audio_queue_output.put_nowait(part.inline_data.data)
66+
if sc.output_transcription:
67+
if last_was_input:
68+
print()
69+
last_was_input = False
70+
t = sc.output_transcription.text
71+
print(t, end="", flush=True)
72+
if t.rstrip()[-1:] in '.!?':
73+
print()
74+
if sc.input_transcription:
75+
if not last_was_input:
76+
print()
77+
last_was_input = True
78+
t = sc.input_transcription.text
79+
print(f"\033[3m{t}\033[0m", end="", flush=True)
80+
if t.rstrip()[-1:] in '.!?':
81+
print()
6082

6183
# Empty the queue on interruption to stop playback
6284
while not audio_queue_output.empty():

0 commit comments

Comments
 (0)