From 21dc55d1d79ec711f369b63526e59c3b7f39c340 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 24 Aug 2018 12:19:05 -0400 Subject: [PATCH 1/5] Chatterbot GUI Front End - Machine Learning Initial checkin of a GUI front-end to the Chatterbot Machine Learning software package. Uses graphical progress meters to show training progress Provides a "chat-window" style interface for conversing --- Demo_Chatterbot.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Demo_Chatterbot.py diff --git a/Demo_Chatterbot.py b/Demo_Chatterbot.py new file mode 100644 index 00000000..a0e69844 --- /dev/null +++ b/Demo_Chatterbot.py @@ -0,0 +1,38 @@ +import PySimpleGUI as gui +from chatterbot import ChatBot +import chatterbot.utils + +''' +Demo_Chatterbot.py +A GUI wrapped arouind the Chatterbot package. +The GUI is used to show progress bars during the training process and +to collect user input that is sent to the chatbot. The reply is displayed in the GUI window +''' + +# redefine the chatbot text based progress bar with a graphical one +def print_progress_bar(description, iteration_counter, total_items, progress_bar_length=20): + gui.EasyProgressMeter(description, iteration_counter, total_items) + +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 ################# +with gui.FlexForm('Chat Window', auto_size_text=True, default_element_size=(30, 2)) as form: + layout = [ [gui.Output(size=(80, 20))], + [gui.Multiline(size=(70, 5), enter_submits=True), + gui.ReadFormButton('SEND', bind_return_key=True), gui.SimpleButton('EXIT')]] + + form.Layout(layout) + # ---===--- Loop taking in user input and using it to query HowDoI web oracle --- # + while True: + button, (value,) = form.Read() + if button != 'SEND': + break + print(value.rstrip()) + # send the user input to chatbot to get a response + response = chatbot.get_response(value.rstrip()) + print(response) \ No newline at end of file From 8bf744689faf877f0170e4b6901d84b4d592914b Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Fri, 24 Aug 2018 23:40:14 -0400 Subject: [PATCH 2/5] Replace MAster with Dev version.... multiple progress bars --- Demo_Chatterbot.py | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/Demo_Chatterbot.py b/Demo_Chatterbot.py index a0e69844..6697335b 100644 --- a/Demo_Chatterbot.py +++ b/Demo_Chatterbot.py @@ -1,4 +1,4 @@ -import PySimpleGUI as gui +import PySimpleGUI as g from chatterbot import ChatBot import chatterbot.utils @@ -9,10 +9,37 @@ The GUI is used to show progress bars during the training process and to collect user input that is sent to the chatbot. The reply is displayed in the GUI window ''' -# redefine the chatbot text based progress bar with a graphical one -def print_progress_bar(description, iteration_counter, total_items, progress_bar_length=20): - gui.EasyProgressMeter(description, iteration_counter, total_items) +# Create the 'Trainer GUI' +MAX_PROG_BARS = 20 +bars = [] +texts = [] +training_layout = [[g.T('TRAINING PROGRESS', size=(20,1), font=('Helvetica', 17))]] +for i in range(MAX_PROG_BARS): + bars.append(g.ProgressBar(100, size=(30, 5))) + texts.append(g.T(' '*20)) + training_layout += [[texts[i], bars[i]]] +training_form = g.FlexForm('Training') +training_form.Layout(training_layout) +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_form + # update the form and the bars + button, values = training_form.ReadNonBlocking() + if button is None and values is None: + exit(69) + if bars[current_bar].UpdateBar(iteration_counter, max=total_items) is False: + exit(69) + texts[current_bar].Update(description) + if iteration_counter == total_items: + current_bar += 1 + +# 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') @@ -21,10 +48,10 @@ chatbot = ChatBot('Ron Obvious', trainer='chatterbot.trainers.ChatterBotCorpusTr chatbot.train("chatterbot.corpus.english") ################# GUI ################# -with gui.FlexForm('Chat Window', auto_size_text=True, default_element_size=(30, 2)) as form: - layout = [ [gui.Output(size=(80, 20))], - [gui.Multiline(size=(70, 5), enter_submits=True), - gui.ReadFormButton('SEND', bind_return_key=True), gui.SimpleButton('EXIT')]] +with g.FlexForm('Chat Window', auto_size_text=True, default_element_size=(30, 2)) as form: + layout = [[g.Output(size=(80, 20))], + [g.Multiline(size=(70, 5), enter_submits=True), + g.ReadFormButton('SEND', bind_return_key=True), g.SimpleButton('EXIT')]] form.Layout(layout) # ---===--- Loop taking in user input and using it to query HowDoI web oracle --- # From fad8378cb0997dbd7b3c13188079f7a5069f12ef Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Sun, 26 Aug 2018 19:58:57 -0400 Subject: [PATCH 3/5] Delete SimScript_.py --- SimScript_.py | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 SimScript_.py diff --git a/SimScript_.py b/SimScript_.py deleted file mode 100644 index 2185935b..00000000 --- a/SimScript_.py +++ /dev/null @@ -1,4 +0,0 @@ -import time - -for i in range(100): - print(i,'', end='') From 85dd1893342288b0f813e4b88f66254040048b13 Mon Sep 17 00:00:00 2001 From: "Jorj X. McKie" Date: Mon, 3 Sep 2018 10:05:33 -0400 Subject: [PATCH 4/5] Viewer for arbitrary images --- Demo_Img_Viewer.py | 114 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Demo_Img_Viewer.py diff --git a/Demo_Img_Viewer.py b/Demo_Img_Viewer.py new file mode 100644 index 00000000..67da8392 --- /dev/null +++ b/Demo_Img_Viewer.py @@ -0,0 +1,114 @@ +import PySimpleGUI as sg +import os +from PIL import Image, ImageTk +import io +""" +Simple Image Browser based on PySimpleGUI +-------------------------------------------- +There are some improvements compared to the PNG browser of the repository: +1. Paging is cyclic, i.e. automatically wraps around if file index is outside +2. Supports all file types that are valid PIL images +3. Limits the maximum form size to the physical screen +4. When selecting an image from the listbox, subsequent paging uses its index +5. Paging performance improved significantly because of using PIL + +Dependecies +------------ +Python v3 +PIL +""" +# Get the folder containing the images from the user +rc, folder = sg.GetPathBox('Image Browser', 'Image folder to open', default_path='') +if not rc or not folder: + sg.MsgBoxCancel('Cancelling') + raise SystemExit() + +# PIL supported image types +img_types = (".png", ".jpg", "jpeg", ".tiff", ".bmp") + +# get list of files in folder +flist0 = os.listdir(folder) + +# create sub list of image files (no sub folders, no wrong file types) +fnames = [f for f in flist0 if os.path.isfile(os.path.join(folder,f)) and f.lower().endswith(img_types)] + +num_files = len(fnames) # number of iamges found +if num_files == 0: + sg.MsgBox('No files in folder') + raise SystemExit() + +del flist0 # no longer needed + +#------------------------------------------------------------------------------ +# use PIL to read data of one image +#------------------------------------------------------------------------------ +def get_img_data(f, maxsize = (1200, 850), first = False): + """Generate image data using PIL + """ + img = Image.open(f) + img.thumbnail(maxsize) + if first: # tkinter is inactive the first time + bio = io.BytesIO() + img.save(bio, format = "PNG") + del img + return bio.getvalue() + return ImageTk.PhotoImage(img) +#------------------------------------------------------------------------------ + + +# create the form that also returns keyboard events +form = sg.FlexForm('Image Browser', return_keyboard_events=True, + location=(0, 0), use_default_focus=False) + +# make these 2 elements outside the layout as we want to "update" them later +# initialize to the first file in the list +filename = os.path.join(folder, fnames[0]) # name of first file in list +image_elem = sg.Image(data = get_img_data(filename, first = True)) +filename_display_elem = sg.Text(filename, size=(80, 3)) +file_num_display_elem = sg.Text('File 1 of {}'.format(num_files), size=(15,1)) + +# define layout, show and read the form +col = [[filename_display_elem], + [image_elem], + [sg.ReadFormButton('Next', size=(8,2)), sg.ReadFormButton('Prev', + size=(8,2)), file_num_display_elem]] + +col_files = [[sg.Listbox(values = fnames, size=(60,30), key='listbox')], + [sg.ReadFormButton('Read')]] + +layout = [[sg.Column(col_files), sg.Column(col)]] + +button, values = form.LayoutAndRead(layout) # Shows form on screen + +# loop reading the user input and displaying image, filename +i=0 +while True: + + # perform button and keyboard operations + if button is None: + break + elif button in ('Next', 'MouseWheel:Down', 'Down:40', 'Next:34'): + i += 1 + if i >= num_files: + i -= num_files + elif button in ('Prev', 'MouseWheel:Up', 'Up:38', 'Prior:33'): + i -= 1 + if i < 0: + i = num_files + i + + if button == 'Read': # something from the listbox + f = values["listbox"][0] # selected filename + filename = os.path.join(folder, f) # read this file + i = fnames.index(f) # update running index + else: + filename = os.path.join(folder, fnames[i]) + + # update window with new image + image_elem.Update(data=get_img_data(filename)) + # update window with filename + filename_display_elem.Update(filename) + # update page display + file_num_display_elem.Update('File {} of {}'.format(i+1, num_files)) + + # read the form + button, values = form.Read() From 5d6ae5dcdce8a9a4e28c8d7f862f99e55527956a Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Mon, 3 Sep 2018 16:51:25 -0400 Subject: [PATCH 5/5] MsgBox changed to Popup. Removed Read button. Listbox returns when selected --- Demo_Img_Viewer.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Demo_Img_Viewer.py b/Demo_Img_Viewer.py index 67da8392..ffd48d75 100644 --- a/Demo_Img_Viewer.py +++ b/Demo_Img_Viewer.py @@ -20,7 +20,7 @@ PIL # Get the folder containing the images from the user rc, folder = sg.GetPathBox('Image Browser', 'Image folder to open', default_path='') if not rc or not folder: - sg.MsgBoxCancel('Cancelling') + sg.PopupCancel('Cancelling') raise SystemExit() # PIL supported image types @@ -34,7 +34,7 @@ fnames = [f for f in flist0 if os.path.isfile(os.path.join(folder,f)) and f.lowe num_files = len(fnames) # number of iamges found if num_files == 0: - sg.MsgBox('No files in folder') + sg.Popup('No files in folder') raise SystemExit() del flist0 # no longer needed @@ -69,20 +69,21 @@ file_num_display_elem = sg.Text('File 1 of {}'.format(num_files), size=(15,1)) # define layout, show and read the form col = [[filename_display_elem], - [image_elem], - [sg.ReadFormButton('Next', size=(8,2)), sg.ReadFormButton('Prev', - size=(8,2)), file_num_display_elem]] + [image_elem]] -col_files = [[sg.Listbox(values = fnames, size=(60,30), key='listbox')], - [sg.ReadFormButton('Read')]] +col_files = [[sg.Listbox(values = fnames, select_submits=True, size=(60,30), key='listbox')], + [sg.ReadFormButton('Next', size=(8,2)), sg.ReadFormButton('Prev', + size=(8,2)), file_num_display_elem]] layout = [[sg.Column(col_files), sg.Column(col)]] -button, values = form.LayoutAndRead(layout) # Shows form on screen +form.Layout(layout) # Shows form on screen # loop reading the user input and displaying image, filename i=0 while True: + # read the form + button, values = form.Read() # perform button and keyboard operations if button is None: @@ -91,12 +92,13 @@ while True: i += 1 if i >= num_files: i -= num_files + filename = os.path.join(folder, fnames[i]) elif button in ('Prev', 'MouseWheel:Up', 'Up:38', 'Prior:33'): i -= 1 if i < 0: i = num_files + i - - if button == 'Read': # something from the listbox + filename = os.path.join(folder, fnames[i]) + elif button in ('Read', ''): # something from the listbox f = values["listbox"][0] # selected filename filename = os.path.join(folder, f) # read this file i = fnames.index(f) # update running index @@ -110,5 +112,4 @@ while True: # update page display file_num_display_elem.Update('File {} of {}'.format(i+1, num_files)) - # read the form - button, values = form.Read() +