-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
178 lines (162 loc) · 6.36 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
from functions.chatgpt_interaction import make_respond
from functions.record_voice import record_voice
from functions.text_processing_utils import read_text_from_file, read_config
from functions.voice_recognition import voice_recognition
from functions.condition import check_condition, check_if_in_condition
from functions.text_to_speech_utils import test_voice_vox
# Constance for retry mechanism
MAX_RETRIES = 3 # How many time you want to retry before error text displayed
WAIT_TIME = 1 # Delay time before next recall api
# You can change chatGPT model here,
# For more information go to "https://platform.openai.com/docs/models/overview"
OPENAI_MODEL = "gpt-3.5-turbo"
# Set the maximum width for each line of the subtitle
max_line_width = 40
# File path
directory_path = os.path.dirname(os.path.realpath(__file__))
config_file_path = directory_path + r"\config.txt"
answer_file_path = directory_path + r"\text_log\answer.txt"
answer_en_file_path = directory_path + r"\text_log\answer_en.txt"
answer_jp_file_path = directory_path + r"\text_log\answer_jp.txt"
subtitle_file_path = directory_path + r"\text_log\subtitle.txt"
chat_log_file_path = directory_path + r"\text_log\chat_log.txt"
prompt_file_path = directory_path + r"\prompt.txt"
voice_recognition_file_path = directory_path + r"\recording.wav.txt"
# Read every config and store it as constant
# For OpenAI API key, you can get it here: "https://platform.openai.com/api-keys"
# For AssemblyAI API key, you can get it here: "https://www.assemblyai.com/app/account"
# For more information go to 'speaker.json' and "https://voicevox.hiroshiba.jp/"
# Format of config.txt should be like this:
# openAI_api_key:YOUR_OPENAI_KEY
# assembly_api_key:YOUR_ASSEMBLYAI_KEY
# assistant_name:YOUR_DESIRE_ASSISTANT_NAME
# voice_vox_text_to_speech_model:YOUR_DESIRE_VOICE_VOX_TEXT_TO_SPEECH_MODEL
(
OPENAI_KEY,
ASSEMBLY_KEY,
ASSISTANT_NAME,
VOICE_VOX_TEXT_TO_SPEECH_MODEL,
) = read_config(config_file_path)
# Use test_voice_vox function to check if VoiceVox engine is open
# and clear all file for the first time use
test_voice_vox(
answer_file_path,
answer_en_file_path,
answer_jp_file_path,
chat_log_file_path,
voice_recognition_file_path,
VOICE_VOX_TEXT_TO_SPEECH_MODEL,
subtitle_file_path,
)
# Ask the user to choose between text chat or voice chat for input
chat_or_voice_input = input("Do you want to use text chat[1] or voice chat[2]: ")
# Use the make_respond function with the provided user_input to generate a greeting for the user
user_input = "User just open the program! greeting your user!"
make_respond(
user_input,
ASSISTANT_NAME,
max_line_width,
VOICE_VOX_TEXT_TO_SPEECH_MODEL,
answer_file_path,
answer_en_file_path,
answer_jp_file_path,
subtitle_file_path,
chat_log_file_path,
prompt_file_path,
MAX_RETRIES,
WAIT_TIME,
OPENAI_MODEL,
OPENAI_KEY,
)
# If user choose to input with text it'll excute code as followed
if chat_or_voice_input == "1":
while True:
# Get input from user
user_input = input("User: ")
# Add user input to chat log
with open(chat_log_file_path, "a", encoding="utf-8") as chat_log_file:
chat_log_file.write(f"\nUser have said: {user_input}")
# If check_if_in_condition function is return false
# execute the script using check_condition function
if check_if_in_condition(user_input):
check_condition(
user_input,
chat_or_voice_input,
answer_jp_file_path,
answer_en_file_path,
subtitle_file_path,
chat_log_file_path,
voice_recognition_file_path,
VOICE_VOX_TEXT_TO_SPEECH_MODEL,
max_line_width,
ASSEMBLY_KEY,
)
# If check_if_in_condition function is return false,
# generate a response using make_respond function
elif not check_if_in_condition(user_input):
make_respond(
user_input,
ASSISTANT_NAME,
max_line_width,
VOICE_VOX_TEXT_TO_SPEECH_MODEL,
answer_file_path,
answer_en_file_path,
answer_jp_file_path,
subtitle_file_path,
chat_log_file_path,
prompt_file_path,
MAX_RETRIES,
WAIT_TIME,
OPENAI_MODEL,
OPENAI_KEY,
)
# If user choose to input with voice it'll excute code as followed
elif chat_or_voice_input == "2":
while True:
# Record user input
record_voice()
# Use a voice recognition function to convert speech into text
voice_recognition(ASSEMBLY_KEY)
# Read the English answer from the file ans store it to answer_en variable
user_input = read_text_from_file(voice_recognition_file_path)
print(f"User: {user_input}")
# Add user input to chat log
with open(chat_log_file_path, "a", encoding="utf-8") as chat_log_file:
chat_log_file.write(f"\nUser have said: {user_input}")
# If check_if_in_condition function is return false,
# execute the script using check_condition function
if check_if_in_condition(user_input):
check_condition(
user_input,
chat_or_voice_input,
answer_jp_file_path,
answer_en_file_path,
subtitle_file_path,
chat_log_file_path,
voice_recognition_file_path,
VOICE_VOX_TEXT_TO_SPEECH_MODEL,
max_line_width,
ASSEMBLY_KEY,
)
# If check_if_in_condition function is return false
# generate a response using make_respond function
elif not check_if_in_condition(user_input):
make_respond(
user_input,
ASSISTANT_NAME,
max_line_width,
VOICE_VOX_TEXT_TO_SPEECH_MODEL,
answer_file_path,
answer_en_file_path,
answer_jp_file_path,
subtitle_file_path,
chat_log_file_path,
prompt_file_path,
MAX_RETRIES,
WAIT_TIME,
OPENAI_MODEL,
OPENAI_KEY,
)
else:
print("ERROR invaid input")