|
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 |
|
|
|
|
|
model = load_whisper() |
|
|
|
|
|
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 |
|
|
|
|
|
audio = AudioSegment.from_file(BytesIO(audio_chunk)) |
|
audio.export("temp_audio_chunk.wav", format="wav") |
|
|
|
|
|
text = transcribe_audio(model, "temp_audio_chunk.wav") |
|
if text: |
|
print(f"User: {text}") |
|
|
|
|
|
response_llm = get_response_llm(user_question=text) |
|
|
|
|
|
play_text_to_speech(text=response_llm) |
|
|
|
|
|
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() |
|
|
|
if __name__ == "__main__": |
|
asyncio.run(main()) |
|
|