PySimpleGUI/DemoPrograms/Demo_Chatterbot.py

76 lines
2.6 KiB
Python
Raw Normal View History

2018-09-27 20:24:09 +00:00
#!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
2018-09-27 20:24:09 +00:00
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
2018-09-27 20:24:09 +00:00
2018-08-24 16:20:01 +00:00
from chatterbot import ChatBot
import chatterbot.utils
2018-09-27 20:24:09 +00:00
2018-08-24 16:20:01 +00:00
'''
Demo_Chatterbot.py
A GUI wrapped arouind the Chatterbot package.
The GUI is used to show progress bars during the training process and
2018-08-24 16:20:01 +00:00
to collect user input that is sent to the chatbot. The reply is displayed in the GUI window
'''
# Create the 'Trainer GUI'
2018-09-03 02:53:11 +00:00
# The Trainer GUI consists of a lot of progress bars stacked on top of each other
sg.ChangeLookAndFeel('GreenTan')
2018-10-29 00:01:03 +00:00
# sg.DebugWin()
2018-09-03 02:53:11 +00:00
MAX_PROG_BARS = 20 # number of training sessions
bars = []
texts = []
training_layout = [[sg.T('TRAINING PROGRESS', size=(20, 1), font=('Helvetica', 17))], ]
for i in range(MAX_PROG_BARS):
bars.append(sg.ProgressBar(100, size=(30, 4)))
texts.append(sg.T(' ' * 20, size=(20, 1), justification='right'))
2018-09-03 02:53:11 +00:00
training_layout += [[texts[i], bars[i]],] # add a single row
training_window = sg.Window('Training').Layout(training_layout)
current_bar = 0
# callback function for training runs
2018-08-24 16:20:01 +00:00
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
2018-10-29 00:01:03 +00:00
button, values = training_window.Read(timeout=0)
if button is None: # if user closed the window on us, exit
sys.exit(69)
if bars[current_bar].UpdateBar(iteration_counter, max=total_items) is False:
sys.exit(69)
2018-09-03 02:53:11 +00:00
texts[current_bar].Update(description) # show the training dataset name
if iteration_counter == total_items:
current_bar += 1
2018-08-24 16:20:01 +00:00
# redefine the chatbot text based progress bar with a graphical one
2018-08-24 16:20:01 +00:00
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 #################
2018-09-23 16:16:50 +00:00
layout = [[sg.Output(size=(80, 20))],
[sg.Multiline(size=(70, 5), enter_submits=True),
2018-10-29 00:01:03 +00:00
sg.Button('SEND', bind_return_key=True), sg.Button('EXIT')]]
2018-09-23 16:16:50 +00:00
window = sg.Window('Chat Window', auto_size_text=True, default_element_size=(30, 2)).Layout(layout)
2018-09-23 16:16:50 +00:00
# ---===--- Loop taking in user input and using it to query HowDoI web oracle --- #
while True:
event, (value,) = window.Read()
if event != 'SEND':
2018-09-23 16:16:50 +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)