File size: 1,433 Bytes
a7a28b1 |
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 |
import asyncio
import websockets
import os
from io import BytesIO
from pydub import AudioSegment
from utils import transcribe_audio, get_response_llm, play_text_to_speech, load_whisper
# Load Whisper Model
model = load_whisper()
# Store connected clients
clients = set()
async def handle_audio_stream(websocket):
"""Handles incoming live audio stream from users."""
clients.add(websocket)
try:
while True:
audio_chunk = await websocket.recv()
if not audio_chunk:
break
# Convert received bytes to WAV file
audio = AudioSegment.from_file(BytesIO(audio_chunk))
audio.export("temp_audio_chunk.wav", format="wav")
# Transcribe
text = transcribe_audio(model, "temp_audio_chunk.wav")
if text:
print(f"User: {text}")
# Generate response
response_llm = get_response_llm(user_question=text)
# Play response
play_text_to_speech(text=response_llm)
# Send response to WebSocket client
await websocket.send(response_llm)
except:
pass
finally:
clients.remove(websocket)
async def main():
async with websockets.serve(handle_audio_stream, "0.0.0.0", 8765):
await asyncio.Future() # Keeps the server running
if __name__ == "__main__":
asyncio.run(main())
|