listen > assistant

pull/8/head
killian 11 months ago
parent c5590871a6
commit da87851e80

@ -53,7 +53,7 @@ async def websocket_endpoint(websocket: WebSocket):
message = to_user.get() message = to_user.get()
await websocket.send_json(message) await websocket.send_json(message)
audio_chunks = [] audio_file = bytearray()
def queue_listener(): def queue_listener():
while True: while True:
@ -65,11 +65,11 @@ def queue_listener():
# Hold the audio in a buffer. If it's ready (we got end flag, stt it) # Hold the audio in a buffer. If it's ready (we got end flag, stt it)
if message["type"] == "audio": if message["type"] == "audio":
if "content" in message: if "content" in message:
audio_chunks.append(message) audio_file.extend(message["content"])
if "end" in message: if "end" in message:
text = stt(audio_chunks) content = stt(audio_file, message["format"])
audio_chunks = [] audio_file = bytearray()
message = {"role": "user", "type": "message", "content": text} message = {"role": "user", "type": "message", "content": content}
else: else:
continue continue

@ -52,7 +52,7 @@ async def start_recording():
print("Recording started...") print("Recording started...")
async with websockets.connect("ws://localhost:8000/user") as websocket: async with websockets.connect("ws://localhost:8000/user") as websocket:
# Send the start command with mime type # Send the start command with mime type
await websocket.send(json.dumps({"action": "command", "state": "start", "mimeType": "audio/wav"})) await websocket.send(json.dumps({"role": "user", "type": "audio", "format": "audio/wav", "start": True}))
while recording: while recording:
data = stream.read(chunk) data = stream.read(chunk)
frames.append(data) frames.append(data)
@ -65,13 +65,13 @@ async def start_recording():
with open(file_path, 'rb') as audio_file: with open(file_path, 'rb') as audio_file:
byte_chunk = audio_file.read(ws_chunk_size) byte_chunk = audio_file.read(ws_chunk_size)
while byte_chunk: while byte_chunk:
await websocket.send(byte_chunk) await websocket.send({"role": "user", "type": "audio", "format": "audio/wav", "content": byte_chunk})
byte_chunk = audio_file.read(ws_chunk_size) byte_chunk = audio_file.read(ws_chunk_size)
finally: finally:
os.remove(file_path) os.remove(file_path)
# Send the end command # Send the end command
await websocket.send(json.dumps({"action": "command", "state": "end"})) await websocket.send(json.dumps({"role": "user", "type": "audio", "format": "audio/wav", "end": True}))
# Receive a json message and then close the connection # Receive a json message and then close the connection
message = await websocket.recv() message = await websocket.recv()

Loading…
Cancel
Save