voice-ai / utils.py
Adipta's picture
init
27c3220 verified
import sys
from langchain_client import LangchainClient
from tts import TextToSpeech
langchain_client = LangchainClient()
conversation_chain_history = langchain_client.create_model()
tts_client = TextToSpeech()
async def listen_print_loop(responses: object) -> str:
num_chars_printed = 0
for response in responses:
if not response.results:
continue
result = response.results[0]
if not result.alternatives:
continue
transcript = result.alternatives[0].transcript
overwrite_chars = " " * (num_chars_printed - len(transcript))
if not result.is_final:
sys.stdout.write(transcript + overwrite_chars + "\r")
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
print(transcript + overwrite_chars)
result = langchain_client.invoke_llm(conversation_chain_history, transcript)
return result, tts_client.text_to_speech(result)