-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
415 lines (329 loc) · 15.7 KB
/
gui.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from tkinter import *
import time
import tkinter.messagebox
#from bot import chat
#from chatgui import send
import pyttsx3
import threading
import nltk
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
import pickle
import numpy as np
from keras.models import load_model
model = load_model('chatbot_model.h5')
import json
import random
intents = json.loads(open('intents.json').read()) # loads converts json to python dictionary
words = pickle.load(open('words.pkl','rb'))
classes = pickle.load(open('classes.pkl','rb'))
saved_username = ["You"]
#ans=["jeff"]
window_size="400x400"
def clean_up_sentence(sentence):
# tokenize the pattern - split words into array
sentence_words = nltk.word_tokenize(sentence)
# stem each word - create short form for word example rocks-->rock
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
return sentence_words
# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
def bow(sentence, words, show_details=True):
# tokenize the pattern
#Tokenizing is the process of breaking the whole text into small parts like words.
sentence_words = clean_up_sentence(sentence)
# bag of words - matrix of N words, vocabulary matrix
bag = [0]*len(words)
for s in sentence_words:
for i,w in enumerate(words):
if w == s:
# assign 1 if current word is in the vocabulary position
bag[i] = 1
if show_details:
print ("found in bag: %s" % w)
return(np.array(bag))
def predict_class(sentence, model):
# filter out predictions below a threshold
p = bow(sentence, words,show_details=False)
res = model.predict(np.array([p]))[0]
ERROR_THRESHOLD = 0.25
results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD]
# sort by strength of probability
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
return return_list
def getResponse(ints, intents_json):
tag = ints[0]['intent']
list_of_intents = intents_json['intents']
for i in list_of_intents:
if(i['tag']== tag):
result = random.choice(i['responses'])
break
return result
def chatbot_response(msg):
ints = predict_class(msg, model)
res = getResponse(ints, intents)
return res
#Creating GUI with tkinter
def send(msg):
if msg != '':
return chatbot_response(msg)
else:
return "Sorry, I am afraid I don't Understand."
class ChatInterface(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
# sets default bg for top level windows
self.tl_bg = "#EEEEEE"
self.tl_bg2 = "#EEEEEE"
self.tl_fg = "#000000"
self.font = "Verdana 10"
menu = Menu(self.master)
self.master.config(menu=menu, bd=5)
# Menu bar
# File
file = Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=file)
# file.add_command(label="Save Chat Log", command=self.save_chat)
file.add_command(label="Clear Chat", command=self.clear_chat)
# file.add_separator()
file.add_command(label="Exit",command=self.chatexit)
# Options
options = Menu(menu, tearoff=0)
menu.add_cascade(label="Options", menu=options)
# username
# font
font = Menu(options, tearoff=0)
options.add_cascade(label="Font", menu=font)
font.add_command(label="Default",command=self.font_change_default)
font.add_command(label="Times",command=self.font_change_times)
font.add_command(label="System",command=self.font_change_system)
font.add_command(label="Helvetica",command=self.font_change_helvetica)
font.add_command(label="Fixedsys",command=self.font_change_fixedsys)
# color theme
color_theme = Menu(options, tearoff=0)
options.add_cascade(label="Color Theme", menu=color_theme)
color_theme.add_command(label="Default",command=self.color_theme_default)
# color_theme.add_command(label="Night",command=self.)
color_theme.add_command(label="Grey",command=self.color_theme_grey)
color_theme.add_command(label="Blue",command=self.color_theme_dark_blue)
color_theme.add_command(label="Torque",command=self.color_theme_turquoise)
color_theme.add_command(label="Hacker",command=self.color_theme_hacker)
# color_theme.add_command(label='Mkbhd',command=self.MKBHD)
help_option = Menu(menu, tearoff=0)
menu.add_cascade(label="Help", menu=help_option)
#help_option.add_command(label="Features", command=self.features_msg)
help_option.add_command(label="About jeff", command=self.msg)
help_option.add_command(label="Developers", command=self.about)
self.text_frame = Frame(self.master, bd=6)
self.text_frame.pack(expand=True, fill=BOTH)
# scrollbar for text box
self.text_box_scrollbar = Scrollbar(self.text_frame, bd=0)
self.text_box_scrollbar.pack(fill=Y, side=RIGHT)
# contains messages
self.text_box = Text(self.text_frame, yscrollcommand=self.text_box_scrollbar.set, state=DISABLED,
bd=1, padx=6, pady=6, spacing3=8, wrap=WORD, bg=None, font="Verdana 10", relief=GROOVE,
width=10, height=1)
self.text_box.pack(expand=True, fill=BOTH)
self.text_box_scrollbar.config(command=self.text_box.yview)
# frame containing user entry field
self.entry_frame = Frame(self.master, bd=1)
self.entry_frame.pack(side=LEFT, fill=BOTH, expand=True)
# entry field
self.entry_field = Entry(self.entry_frame, bd=1, justify=LEFT)
self.entry_field.pack(fill=X, padx=6, pady=6, ipady=3)
# self.users_message = self.entry_field.get()
# frame containing send button and emoji button
self.send_button_frame = Frame(self.master, bd=0)
self.send_button_frame.pack(fill=BOTH)
# send button
self.send_button = Button(self.send_button_frame, text="Send", relief=GROOVE, bg='white',
bd=1,width=5, command=lambda: self.send_message_insert(None), activebackground="#FFFFFF",
activeforeground="#000000")
self.send_button.pack(side=LEFT, ipady=8)
self.master.bind("<Return>", self.send_message_insert)
self.last_sent_label(date="No messages sent.")
#t2 = threading.Thread(target=self.send_message_insert(, name='t1')
#t2.start()
def playResponce(self,responce):
x=pyttsx3.init()
#print(responce)
li = []
if len(responce) > 100:
if responce.find('--') == -1:
b = responce.split('--')
#print(b)
x.setProperty('rate',120)
x.setProperty('volume',100)
x.say(responce)
x.runAndWait()
#print("Played Successfully......")
def last_sent_label(self, date):
try:
self.sent_label.destroy()
except AttributeError:
pass
self.sent_label = Label(self.entry_frame, font="Verdana 7", text=date, bg=self.tl_bg2, fg=self.tl_fg)
self.sent_label.pack(side=LEFT, fill=X, padx=3)
def clear_chat(self):
self.text_box.config(state=NORMAL)
self.last_sent_label(date="No messages sent.")
self.text_box.delete(1.0, END)
self.text_box.delete(1.0, END)
self.text_box.config(state=DISABLED)
def chatexit(self):
exit()
def msg(self):
tkinter.messagebox.showinfo("jeff v1.1",'jeff is a chatbot for Making a general conversation\nIt is based on retrival-based NLP using pythons NLTK tool-kit module\nGUI is based on Tkinter\nIt can answer questions regarding many things')
def about(self):
tkinter.messagebox.showinfo("jeff Developers","Affan Murtaza")
def send_message_insert(self, message):
user_input = self.entry_field.get()
pr1 = "Human : " + user_input + "\n"
self.text_box.configure(state=NORMAL)
self.text_box.insert(END, pr1)
self.text_box.configure(state=DISABLED)
self.text_box.see(END)
#t1 = threading.Thread(target=self.playResponce, args=(user_input,))
#t1.start()
#time.sleep(1)
ob=send(user_input)
pr="jeff : " + ob + "\n"
self.text_box.configure(state=NORMAL)
self.text_box.insert(END, pr)
self.text_box.configure(state=DISABLED)
self.text_box.see(END)
self.last_sent_label(str(time.strftime( "Last message sent: " + '%B %d, %Y' + ' at ' + '%I:%M %p')))
self.entry_field.delete(0,END)
time.sleep(0)
t2 = threading.Thread(target=self.playResponce, args=(ob,))
t2.start()
#return ob
def font_change_default(self):
self.text_box.config(font="Verdana 10")
self.entry_field.config(font="Verdana 10")
self.font = "Verdana 10"
def font_change_times(self):
self.text_box.config(font="Times")
self.entry_field.config(font="Times")
self.font = "Times"
def font_change_system(self):
self.text_box.config(font="System")
self.entry_field.config(font="System")
self.font = "System"
def font_change_helvetica(self):
self.text_box.config(font="helvetica 10")
self.entry_field.config(font="helvetica 10")
self.font = "helvetica 10"
def font_change_fixedsys(self):
self.text_box.config(font="fixedsys")
self.entry_field.config(font="fixedsys")
self.font = "fixedsys"
def color_theme_default(self):
self.master.config(bg="#EEEEEE")
self.text_frame.config(bg="#EEEEEE")
self.entry_frame.config(bg="#EEEEEE")
self.text_box.config(bg="#FFFFFF", fg="#000000")
self.entry_field.config(bg="#FFFFFF", fg="#000000", insertbackground="#000000")
self.send_button_frame.config(bg="#EEEEEE")
self.send_button.config(bg="#FFFFFF", fg="#000000", activebackground="#FFFFFF", activeforeground="#000000")
#self.emoji_button.config(bg="#FFFFFF", fg="#000000", activebackground="#FFFFFF", activeforeground="#000000")
self.sent_label.config(bg="#EEEEEE", fg="#000000")
self.tl_bg = "#FFFFFF"
self.tl_bg2 = "#EEEEEE"
self.tl_fg = "#000000"
# Dark
def color_theme_dark(self):
self.master.config(bg="#2a2b2d")
self.text_frame.config(bg="#2a2b2d")
self.text_box.config(bg="#212121", fg="#FFFFFF")
self.entry_frame.config(bg="#2a2b2d")
self.entry_field.config(bg="#212121", fg="#FFFFFF", insertbackground="#FFFFFF")
self.send_button_frame.config(bg="#2a2b2d")
self.send_button.config(bg="#212121", fg="#FFFFFF", activebackground="#212121", activeforeground="#FFFFFF")
# self.emoji_button.config(bg="#212121", fg="#FFFFFF", activebackground="#212121", activeforeground="#FFFFFF")
self.sent_label.config(bg="#2a2b2d", fg="#FFFFFF")
self.tl_bg = "#212121"
self.tl_bg2 = "#2a2b2d"
self.tl_fg = "#FFFFFF"
# Grey
def color_theme_grey(self):
self.master.config(bg="#444444")
self.text_frame.config(bg="#444444")
self.text_box.config(bg="#4f4f4f", fg="#ffffff")
self.entry_frame.config(bg="#444444")
self.entry_field.config(bg="#4f4f4f", fg="#ffffff", insertbackground="#ffffff")
self.send_button_frame.config(bg="#444444")
self.send_button.config(bg="#4f4f4f", fg="#ffffff", activebackground="#4f4f4f", activeforeground="#ffffff")
#self.emoji_button.config(bg="#4f4f4f", fg="#ffffff", activebackground="#4f4f4f", activeforeground="#ffffff")
self.sent_label.config(bg="#444444", fg="#ffffff")
self.tl_bg = "#4f4f4f"
self.tl_bg2 = "#444444"
self.tl_fg = "#ffffff"
def color_theme_turquoise(self):
self.master.config(bg="#003333")
self.text_frame.config(bg="#003333")
self.text_box.config(bg="#669999", fg="#FFFFFF")
self.entry_frame.config(bg="#003333")
self.entry_field.config(bg="#669999", fg="#FFFFFF", insertbackground="#FFFFFF")
self.send_button_frame.config(bg="#003333")
self.send_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
#self.emoji_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
self.sent_label.config(bg="#003333", fg="#FFFFFF")
self.tl_bg = "#669999"
self.tl_bg2 = "#003333"
self.tl_fg = "#FFFFFF"
# Blue
def color_theme_dark_blue(self):
self.master.config(bg="#263b54")
self.text_frame.config(bg="#263b54")
self.text_box.config(bg="#1c2e44", fg="#FFFFFF")
self.entry_frame.config(bg="#263b54")
self.entry_field.config(bg="#1c2e44", fg="#FFFFFF", insertbackground="#FFFFFF")
self.send_button_frame.config(bg="#263b54")
self.send_button.config(bg="#1c2e44", fg="#FFFFFF", activebackground="#1c2e44", activeforeground="#FFFFFF")
#self.emoji_button.config(bg="#1c2e44", fg="#FFFFFF", activebackground="#1c2e44", activeforeground="#FFFFFF")
self.sent_label.config(bg="#263b54", fg="#FFFFFF")
self.tl_bg = "#1c2e44"
self.tl_bg2 = "#263b54"
self.tl_fg = "#FFFFFF"
# Torque
def color_theme_turquoise(self):
self.master.config(bg="#003333")
self.text_frame.config(bg="#003333")
self.text_box.config(bg="#669999", fg="#FFFFFF")
self.entry_frame.config(bg="#003333")
self.entry_field.config(bg="#669999", fg="#FFFFFF", insertbackground="#FFFFFF")
self.send_button_frame.config(bg="#003333")
self.send_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
#self.emoji_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
self.sent_label.config(bg="#003333", fg="#FFFFFF")
self.tl_bg = "#669999"
self.tl_bg2 = "#003333"
self.tl_fg = "#FFFFFF"
# Hacker
def color_theme_hacker(self):
self.master.config(bg="#0F0F0F")
self.text_frame.config(bg="#0F0F0F")
self.entry_frame.config(bg="#0F0F0F")
self.text_box.config(bg="#0F0F0F", fg="#33FF33")
self.entry_field.config(bg="#0F0F0F", fg="#33FF33", insertbackground="#33FF33")
self.send_button_frame.config(bg="#0F0F0F")
self.send_button.config(bg="#0F0F0F", fg="#FFFFFF", activebackground="#0F0F0F", activeforeground="#FFFFFF")
#self.emoji_button.config(bg="#0F0F0F", fg="#FFFFFF", activebackground="#0F0F0F", activeforeground="#FFFFFF")
self.sent_label.config(bg="#0F0F0F", fg="#33FF33")
self.tl_bg = "#0F0F0F"
self.tl_bg2 = "#0F0F0F"
self.tl_fg = "#33FF33"
# Default font and color theme
def default_format(self):
self.font_change_default()
self.color_theme_default()
root=Tk()
a = ChatInterface(root)
root.geometry(window_size)
root.title("jeff")
#root.iconbitmap('i.ico')
root.mainloop()