2018-11-05 17:54:37 +00:00
|
|
|
#!/usr/bin/env python
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-11-05 17:54:37 +00:00
|
|
|
from chatterbot import ChatBot
|
|
|
|
import chatterbot.utils
|
|
|
|
from gtts import gTTS
|
|
|
|
from pygame import mixer
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
|
|
|
|
'''
|
|
|
|
Demo_Chatterbot.py
|
|
|
|
A GUI wrapped arouind the Chatterbot package.
|
2019-06-26 15:09:42 +00:00
|
|
|
The GUI is used to show progress bars during the training process and
|
2018-11-05 17:54:37 +00:00
|
|
|
to collect user input that is sent to the chatbot. The reply is displayed in the GUI window
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Create the 'Trainer GUI'
|
|
|
|
# The Trainer GUI consists of a lot of progress bars stacked on top of each other
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.change_look_and_feel('NeutralBlue')
|
2018-11-05 17:54:37 +00:00
|
|
|
# sg.DebugWin()
|
|
|
|
MAX_PROG_BARS = 20 # number of training sessions
|
|
|
|
bars = []
|
|
|
|
texts = []
|
2019-10-23 20:10:03 +00:00
|
|
|
training_layout = [[sg.Text('TRAINING PROGRESS', size=(20, 1), font=('Helvetica', 17))], ]
|
2018-11-05 17:54:37 +00:00
|
|
|
for i in range(MAX_PROG_BARS):
|
|
|
|
bars.append(sg.ProgressBar(100, size=(30, 4)))
|
2019-10-23 20:10:03 +00:00
|
|
|
texts.append(sg.Text(' ' * 20, size=(20, 1), justification='right'))
|
2018-11-05 17:54:37 +00:00
|
|
|
training_layout += [[texts[i], bars[i]],] # add a single row
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
training_window = sg.Window('Training', training_layout)
|
2018-11-05 17:54:37 +00:00
|
|
|
current_bar = 0
|
|
|
|
|
|
|
|
# callback function for training runs
|
|
|
|
def print_progress_bar(description, iteration_counter, total_items, progress_bar_length=20):
|
|
|
|
global current_bar
|
|
|
|
global bars
|
|
|
|
global texts
|
|
|
|
global training_window
|
|
|
|
# update the window and the bars
|
2019-10-23 20:10:03 +00:00
|
|
|
button, values = training_window.read(timeout=0)
|
2018-11-05 17:54:37 +00:00
|
|
|
if button is None: # if user closed the window on us, exit
|
2019-10-23 20:10:03 +00:00
|
|
|
return
|
|
|
|
if bars[current_bar].update_bar(iteration_counter, max=total_items) is False:
|
|
|
|
return
|
|
|
|
texts[current_bar].update(description) # show the training dataset name
|
2018-11-05 17:54:37 +00:00
|
|
|
if iteration_counter == total_items:
|
|
|
|
current_bar += 1
|
|
|
|
|
|
|
|
def speak(text):
|
|
|
|
global i
|
|
|
|
tts = gTTS(text=text, lang='en',slow=False)
|
|
|
|
tts.save('speech{}.mp3'.format(i%2))
|
|
|
|
# playback the speech
|
|
|
|
mixer.music.load('speech{}.mp3'.format(i%2))
|
|
|
|
mixer.music.play()
|
|
|
|
# wait for playback to end
|
|
|
|
while mixer.music.get_busy():
|
|
|
|
time.sleep(.1)
|
|
|
|
mixer.stop()
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
mixer.init()
|
|
|
|
|
|
|
|
# redefine the chatbot text based progress bar with a graphical one
|
|
|
|
chatterbot.utils.print_progress_bar = print_progress_bar
|
|
|
|
|
|
|
|
chatbot = ChatBot('Ron Obvious', trainer='chatterbot.trainers.ChatterBotCorpusTrainer')
|
|
|
|
|
|
|
|
# Train based on the english corpus
|
|
|
|
chatbot.train("chatterbot.corpus.english")
|
|
|
|
|
|
|
|
################# GUI #################
|
|
|
|
|
|
|
|
layout = [[sg.Output(size=(80, 20))],
|
2019-10-23 20:10:03 +00:00
|
|
|
[sg.MLine(size=(70, 5), enter_submits=True),
|
2018-11-05 17:54:37 +00:00
|
|
|
sg.Button('SEND', bind_return_key=True), sg.Button('EXIT')]]
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Chat Window', layout, default_element_size=(30, 2))
|
2018-11-05 17:54:37 +00:00
|
|
|
|
|
|
|
# ---===--- Loop taking in user input and using it to query HowDoI web oracle --- #
|
|
|
|
while True:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, (value,) = window.read()
|
2019-06-26 15:09:42 +00:00
|
|
|
if event != 'SEND':
|
2018-11-05 17:54:37 +00:00
|
|
|
break
|
|
|
|
string = value.rstrip()
|
|
|
|
print(' '+string)
|
|
|
|
# send the user input to chatbot to get a response
|
|
|
|
response = chatbot.get_response(value.rstrip())
|
|
|
|
print(response)
|
2019-10-23 20:10:03 +00:00
|
|
|
speak(str(response))
|
|
|
|
|
|
|
|
window.close()
|