-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
225 lines (184 loc) · 6.78 KB
/
Copy pathcli.py
File metadata and controls
225 lines (184 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""Voice Agent CLI example with local audio (PyAudio).
Connects to a Voice Agent server via WHIP, uses system microphone/speaker
via PyAudio, and prints transcripts directly to the terminal.
"""
import asyncio
import logging
import os
import signal
import sys
# Allow importing the SDK from the sibling python-sdk directory when
# running this example directly (without pip-installing the SDK).
sys.path.insert(
0, os.path.join(os.path.dirname(__file__), "..", "..", "python-sdk", "src")
)
import numpy as np
import pyaudio
import streamcore
logging.basicConfig(level=logging.ERROR, format="%(message)s")
def terminal_listener(is_muted_ref, loop, stop_event):
"""Reads raw keystrokes from the terminal without breaking asyncio."""
if sys.platform == "win32":
import msvcrt
while not stop_event.is_set():
if msvcrt.kbhit():
char = msvcrt.getch()
if char == b" ":
is_muted_ref[0] = not is_muted_ref[0]
_print_mute(is_muted_ref[0])
elif char in (b"\x03", b"c"): # Ctrl+C
loop.call_soon_threadsafe(stop_event.set)
break
else:
import time
time.sleep(0.05)
else:
import tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setcbreak(fd)
while not stop_event.is_set():
import select
r, _, _ = select.select([sys.stdin], [], [], 0.1)
if r:
char = sys.stdin.read(1)
if char == " ":
is_muted_ref[0] = not is_muted_ref[0]
_print_mute(is_muted_ref[0])
elif char == "\x03": # Ctrl+C
loop.call_soon_threadsafe(stop_event.set)
break
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
def _print_mute(muted: bool) -> None:
if muted:
print("\r\n[mic] 🔴 Muted. Press Space to talk... ", end="", flush=True)
else:
print("\r\n[mic] 🟢 Unmuted. Agent is listening... ", end="", flush=True)
async def main() -> None:
whip_url = os.environ.get("WHIP_URL", "http://localhost:8080/whip")
token_url = os.environ.get("TOKEN_URL", "")
api_key = os.environ.get("API_KEY", "")
# Keep track of the last length to clear "partial" outputs cleanly
last_print_len = 0
is_muted = [True] # List so the background thread can mutate it
def on_status(status: streamcore.ConnectionStatus) -> None:
print(f"\n[status] {status.name}")
def on_transcript(
entry: streamcore.TranscriptEntry,
_all: list[streamcore.TranscriptEntry],
) -> None:
nonlocal last_print_len
tag = "agent" if entry.role == "assistant" else "user"
# Clear the current line
print("\r" + " " * last_print_len + "\r", end="", flush=True)
if entry.partial:
msg = f"[{tag}] (partial) {entry.text}"
print(msg, end="", flush=True)
last_print_len = len(msg)
else:
print(f"[{tag}] {entry.text}")
last_print_len = 0
def on_error(err: Exception) -> None:
print(f"\n[error] {err}")
client = streamcore.Client(
config=streamcore.Config(
whip_endpoint=whip_url,
token_url=token_url,
api_key=api_key,
),
events=streamcore.EventHandler(
on_status_change=on_status,
on_transcript=on_transcript,
on_error=on_error,
),
)
print(f"Connecting to {whip_url} ...")
await client.connect()
print(
"\nConnected! Microphone is 🔴 MUTED. Press Spacebar to talk. (Ctrl+C to quit)\n"
)
# Open mic and speaker streams via PyAudio
pa = pyaudio.PyAudio()
mic_stream = pa.open(
format=pyaudio.paInt16,
channels=streamcore.CHANNELS,
rate=streamcore.SAMPLE_RATE,
input=True,
frames_per_buffer=streamcore.FRAME_SIZE,
start=True,
)
speaker_stream = pa.open(
format=pyaudio.paInt16,
channels=streamcore.CHANNELS,
rate=streamcore.SAMPLE_RATE,
output=True,
)
stop = asyncio.Event()
loop = asyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(sig, stop.set)
async def mic_loop():
while not stop.is_set():
data = await loop.run_in_executor(
None,
lambda: mic_stream.read(
streamcore.FRAME_SIZE, exception_on_overflow=False
),
)
if is_muted[0]:
pcm = np.zeros(streamcore.FRAME_SIZE, dtype=np.int16)
else:
pcm = np.frombuffer(data, dtype=np.int16)
await client.send_pcm(pcm)
async def speaker_loop():
while not stop.is_set():
try:
pcm = await client.recv_pcm()
await loop.run_in_executor(
None, lambda d=pcm.tobytes(): speaker_stream.write(d)
)
except Exception:
break
mic_task = asyncio.create_task(mic_loop())
speaker_task = asyncio.create_task(speaker_loop())
# Spawn terminal reader in a background thread to prevent blocking the async loop
import threading
t = threading.Thread(
target=terminal_listener, args=(is_muted, loop, stop), daemon=True
)
t.start()
await stop.wait()
print("\nShutting down...")
# On second Ctrl+C, force-exit immediately.
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(sig, lambda: os._exit(0))
# Cancel speaker (may block in recv_pcm forever).
speaker_task.cancel()
# Let mic exit naturally (~20 ms for the current read to finish).
# Do NOT cancel it — we need the executor thread to complete its
# mic_stream.read() call before we touch PyAudio, otherwise
# PortAudio deadlocks on macOS.
done, pending = await asyncio.wait([mic_task, speaker_task], timeout=0.5)
for t in pending:
t.cancel()
await asyncio.gather(mic_task, speaker_task, return_exceptions=True)
# Brief pause so any in-flight executor thread can finish.
await asyncio.sleep(0.05)
# Close PyAudio — safe now, executor is done.
try:
mic_stream.stop_stream()
mic_stream.close()
speaker_stream.stop_stream()
speaker_stream.close()
pa.terminate()
except Exception:
pass
# Disconnect WebRTC.
try:
await asyncio.wait_for(client.disconnect(), timeout=5)
except (asyncio.TimeoutError, Exception):
pass
if __name__ == "__main__":
asyncio.run(main())