Skip to content

Commit

Permalink
text input chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
0Xiaohei0 committed Mar 29, 2023
1 parent 1054869 commit 777a23c
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ VOICEVOX/*
*.wav
models--staka--fugumt-en-ja/
*.exe
*.wav
*.wav
!lore.txt
142 changes: 134 additions & 8 deletions UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import time
import subLocal as SUB
import translator
import chatbot


class Pages(Enum):
AUDIO_INPUT = 0
TEXT_INPUT = 1
SETTINGS = 2
SUBTITLE = 3
CHAT = 4


current_page = Pages.AUDIO_INPUT
Expand Down Expand Up @@ -66,6 +68,18 @@ def __init__(self, master, **kwargs):
)
subtitles_button.pack(anchor="s")

chat_button = customtkinter.CTkButton(master=self,
width=120,
height=32,
border_width=0,
corner_radius=0,
text="Chat",
command=lambda: self.change_page(
Pages.CHAT),
fg_color='grey'
)
chat_button.pack(anchor="s")

button = customtkinter.CTkButton(master=self,
width=120,
height=32,
Expand Down Expand Up @@ -163,6 +177,100 @@ def log_message_on_console(self, message_text):
self.textbox.configure(state="disabled")


class ChatFrame(customtkinter.CTkFrame):
def __init__(self, master, **kwargs):
super().__init__(master, **kwargs)
self.isRecording = False
self.thread = Thread(target=STTS.start_record_auto)
# add widgets onto the frame...
self.textbox = customtkinter.CTkTextbox(self, width=400, height=400)
self.textbox.grid(row=0, column=0, rowspan=4, columnspan=4)
# configure textbox to be read-only
self.textbox.configure(state="disabled")
chatbot.logging_eventhandlers.append(self.log_message_on_console)

self.user_input_var = customtkinter.StringVar(self, '')
self.voicevox_api_key_input = customtkinter.CTkEntry(
master=self, textvariable=self.user_input_var, width=300)
self.voicevox_api_key_input.grid(
row=4, column=0, padx=10, pady=10, sticky='W', columnspan=3)
self.send_button = customtkinter.CTkButton(master=self,
width=32,
height=32,
border_width=0,
corner_radius=8,
text="send",
command=self.send_user_input,
fg_color='grey'
)
self.send_button.grid(row=4, column=3, pady=10)
# self.recordButton = customtkinter.CTkButton(master=self,
# width=120,
# height=32,
# border_width=0,
# corner_radius=8,
# text="Start Recording",
# command=self.recordButton_callback,
# fg_color='grey'
# )
# self.recordButton.grid(row=3, column=0, pady=10)

# self.playOriginalButton = customtkinter.CTkButton(master=self,
# width=120,
# height=32,
# border_width=0,
# corner_radius=8,
# text="Play original",
# command=self.play_original_callback,
# fg_color='grey'
# )
# self.playOriginalButton.grid(row=3, column=1, pady=10)

# self.clearConsoleButton = customtkinter.CTkButton(master=self,
# width=32,
# height=32,
# border_width=0,
# corner_radius=8,
# text="X",
# command=self.clear_console,
# fg_color='grey'
# )
# self.clearConsoleButton.grid(row=3, column=2, padx=10, pady=10)

# def clear_console(self):
# self.textbox.configure(state="normal")
# self.textbox.delete('1.0', customtkinter.END)
# self.textbox.configure(state="disabled")

# def recordButton_callback(self):
# if (self.isRecording):
# self.recordButton.configure(
# text="Start Recording", fg_color='grey')
# self.isRecording = False
# STTS.stop_record_auto()
# else:
# self.recordButton.configure(
# text="Stop Recording", fg_color='#fc7b5b')
# self.isRecording = True
# STTS.start_record_auto()
# self.recordButton.grid(row=3, column=0, pady=10)

# def play_original_callback(self):
# thread = Thread(target=STTS.playOriginal())
# thread.start()
def send_user_input(self):
text = self.user_input_var.get()
self.user_input_var.set('')
thread = Thread(target=chatbot.send_user_input, args=[text,])
thread.start()

def log_message_on_console(self, message_text):
# insert at line 0 character 0
self.textbox.configure(state="normal")
self.textbox.insert(customtkinter.INSERT, message_text+'\n')
self.textbox.configure(state="disabled")


class TextBoxFrame(customtkinter.CTkFrame):

def __init__(self, master, **kwargs):
Expand Down Expand Up @@ -409,7 +517,7 @@ def __init__(self):


class OptionsFrame(customtkinter.CTkFrame):
def __init__(self, master, **kwargs):
def __init__(self, master, enable_micmeter=True, **kwargs):
super().__init__(master, **kwargs)
self.speaker_names = STTS.get_speaker_names()
self.default_speaker = self.speaker_names[0]
Expand Down Expand Up @@ -458,13 +566,15 @@ def __init__(self, master, **kwargs):
variable=self.style_combobox_var)
self.style_combobox.pack(padx=20, pady=0)

label_mic = customtkinter.CTkLabel(
master=self, text='Mic activity: ')
label_mic.pack(padx=20, pady=10)
self.progressbar = customtkinter.CTkProgressBar(master=self, width=100)
self.progressbar.pack(padx=20, pady=0)
thread = Thread(target=self.update_mic_meter)
thread.start()
if (enable_micmeter):
label_mic = customtkinter.CTkLabel(
master=self, text='Mic activity: ')
label_mic.pack(padx=20, pady=10)
self.progressbar = customtkinter.CTkProgressBar(
master=self, width=100)
self.progressbar.pack(padx=20, pady=0)
thread = Thread(target=self.update_mic_meter)
thread.start()

def update_mic_meter(self):
global audio_level
Expand Down Expand Up @@ -529,6 +639,17 @@ def __init__(self, *args, **kwargs):
subtitles_frame.pack(padx=0, pady=0)


class ChatPage(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
chat_frame = ChatFrame(
master=self, width=500, corner_radius=8)
chat_frame.grid(row=0, column=1, padx=20, pady=20,
sticky="nswe")
options = OptionsFrame(master=self, enable_micmeter=False)
options.grid(row=0, column=2, padx=20, pady=20, sticky="nswe")


class SettingsPage(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
Expand Down Expand Up @@ -634,13 +755,15 @@ def __init__(self):
textInputPage = TextInputPage(self)
settingsPage = SettingsPage(self)
subtitlesPage = SubtitlesPage(self)
chatPage = ChatPage(self)
container = customtkinter.CTkFrame(
self, width=700, height=700, bg_color='#fafafa')
container.grid(row=0, column=1, padx=20, pady=20, sticky="nswe")

audioInputPage.place(in_=container, x=0, y=0)
textInputPage.place(in_=container, x=0, y=0)
subtitlesPage.place(in_=container, x=0, y=0)
chatPage.place(in_=container, x=0, y=0)
settingsPage.place(in_=container, x=0, y=0)

audioInputPage.show()
Expand All @@ -660,6 +783,9 @@ def showPage():
elif (current_page == Pages.SETTINGS):
container.lift()
settingsPage.show()
elif (current_page == Pages.CHAT):
container.lift()
chatPage.show()
pageChange_eventhandlers.append(showPage)


Expand Down
Binary file modified audioResponse.wav
Binary file not shown.
54 changes: 54 additions & 0 deletions chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
import traceback
import openai
import STTSLocal as STTS

lore = ''
try:
with open('./lore.txt', 'r', encoding='utf-8') as file:
lore = file.read()
except Exception:
print("error when reading lore.txt")
print(traceback.format_exc())
lore = lore.replace('\n', '')

message_log = [
{"role": "system", "content": lore},
{"role": "user", "content": lore},
]

logging_eventhandlers = []


def send_user_input(user_input):
log_message(f'user: {user_input}')
global message_log
api_key = os.getenv("OPENAI_API_KEY")

openai.api_key = api_key
print(f"Sending: {user_input} with api key :{api_key}")
print(message_log)
message_log.append({"role": "user", "content": user_input})
response = None
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=message_log
)
except Exception:
log_message("Error when loading api key from environment variable")
log_message(
"You need an API key from https://platform.openai.com/ stored in an environment variable with name \"OPENAI_API_KEY\" to use the chat feature")
print(traceback.format_exc())
return
text_response = response['choices'][0]['message']['content']
message_log.append({"role": "assistant", "content": text_response})
log_message(f'AI: {text_response}')
STTS.start_TTS_pipeline(text_response)


def log_message(message_text):
print(message_text)
global logging_eventhandlers
for eventhandler in logging_eventhandlers:
eventhandler(message_text)
3 changes: 3 additions & 0 deletions lore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You are not an assistant, your task is to roleplay as a character.
Can you talk to me as if you are this character? Please only provide short answers and try not to break out of character. Here's her description:
A shrine maiden of a Shinto shrine.

0 comments on commit 777a23c

Please sign in to comment.