forked from PromtEngineer/Verbi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_voice_assistant.py
80 lines (62 loc) · 2.93 KB
/
run_voice_assistant.py
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
# voice_assistant/main.py
import logging
import time
from colorama import Fore, init
from voice_assistant.audio import record_audio, play_audio
from voice_assistant.transcription import transcribe_audio
from voice_assistant.response_generation import generate_response
from voice_assistant.text_to_speech import text_to_speech
from voice_assistant.utils import delete_file
from voice_assistant.config import Config
from voice_assistant.api_key_manager import get_transcription_api_key, get_response_api_key, get_tts_api_key
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Initialize colorama
init(autoreset=True)
def main():
"""
Main function to run the voice assistant.
"""
chat_history = [
{"role": "system", "content": "You are a helpful Assistant. Keep your answers short and concise."}
]
while True:
try:
# Record audio from the microphone and save it as 'test.wav'
record_audio('test.wav')
# Get the API key for transcription
transcription_api_key = get_transcription_api_key()
# Transcribe the audio file
user_input = transcribe_audio(Config.TRANSCRIPTION_MODEL, transcription_api_key, 'test.wav', Config.LOCAL_MODEL_PATH)
logging.info(Fore.GREEN + "You said: " + user_input)
# Append the user's input to the chat history
chat_history.append({"role": "user", "content": user_input})
# Get the API key for response generation
response_api_key = get_response_api_key()
# Generate a response
response_text = generate_response(Config.RESPONSE_MODEL, response_api_key, chat_history, Config.LOCAL_MODEL_PATH)
logging.info(Fore.CYAN + "Response: " + response_text)
# Append the assistant's response to the chat history
chat_history.append({"role": "assistant", "content": response_text})
# Determine the output file format based on the TTS model
if Config.TTS_MODEL == 'openai':
output_file = 'output.mp3'
else:
output_file = 'output.wav'
# Get the API key for TTS
tts_api_key = get_tts_api_key()
# Convert the response text to speech and save it to the appropriate file
text_to_speech(Config.TTS_MODEL, tts_api_key, response_text, output_file, Config.LOCAL_MODEL_PATH)
# Play the generated speech audio
play_audio(output_file)
# Clean up audio files
# delete_file('test.wav')
# delete_file(output_file)
except Exception as e:
logging.error(Fore.RED + f"An error occurred: {e}")
delete_file('test.wav')
if 'output_file' in locals():
delete_file(output_file)
time.sleep(1)
if __name__ == "__main__":
main()