-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
480 lines (379 loc) · 18.3 KB
/
app.py
File metadata and controls
480 lines (379 loc) · 18.3 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import tkinter as tk
from tkinter import *
from typing import final
from response import bot_name, response
from os import sys
import os
import tkinter
from tkinter import font
from tkinter.font import BOLD
from PIL import ImageTk, Image
from tkinter import messagebox
import pyttsx3
import pickle
from datetime import datetime
#os.remove('Transcript.dat')
engine = pyttsx3.init()
a = None
def bot():
root.destroy()
BG_GRAY = "#ABB2B9"
BG_COLOR = "#17202A"
TEXT_COLOR = "#EAECEE"
FONT = "Helvetica 14"
FONT_BOLD = "Helvetica 13 bold"
class ChatApplication:
def __init__(self):
self.window = Tk()
self._setup_main_window()
def run(self):
self.window.mainloop()
def _setup_main_window(self):
self.window.title("KlayBot")
self.window.resizable(width=False, height=False)
self.window.configure(width=1300, height=650, bg= "#1e1e1e")
# head label
head_label = Label(self.window, bg="#1e1e1e",anchor='w',justify="left", fg=TEXT_COLOR,text="Klay: Hello " + contents +", I am KlayBot and I have skills and capabilities to help make you feel better.\nPress OK to continue...", font=("Courier New", 14))
head_label.pack()
head_label.place(x=0, y = 10)
# tiny divider
#line = Label(self.window, width=50, bg=BG_GRAY)
#line.place(relwidth=1, rely=0.07, relheight=0.012)
# text widget
self.text_widget = Text(self.window, width=20, height=2, bg="#1e1e1e", fg=TEXT_COLOR,
font=FONT, padx=5, pady=5)
self.text_widget.place(relheight=0.745, relwidth=1, rely=0.08)
self.text_widget.configure(cursor="arrow", state=DISABLED, insertbackground="red")
# scroll bar
scrollbar = Scrollbar(self.text_widget)
scrollbar.place(relheight=1, relx=0.984)
scrollbar.configure(command=self.text_widget.yview, background='#36393f')
# bottom label
bottom_label = Label(self.window, bg="#252526", height=80)
bottom_label.place(relwidth=1, rely=0.825)
# message entry box
self.msg_entry = Entry(bottom_label, bg="#36393f", fg=TEXT_COLOR, font=FONT)
self.msg_entry.place(relwidth=0.74, relheight=0.06, rely=0.008, relx=0.011)
self.msg_entry.focus()
self.msg_entry.bind("<Return>", self._on_enter_pressed)
# send button
send_button = Button(bottom_label, text="Send", font=FONT_BOLD, width=20, bg=BG_GRAY,
command=lambda: self._on_enter_pressed(NONE))
send_button.place(relx=0.77, rely=0.008, relheight=0.06, relwidth=0.22)
def _on_enter_pressed(self, event):
try:
msg = self.msg_entry.get()
self._insert_message(msg, "You")
except:
return None
def _insert_message(self, msg, sender):
global a
global kek
if not msg:
return
if msg.lower() == "save":
kek = msg
if msg.lower() == "discard":
a=msg
if "bye" in msg.lower():
if a == "discard":
os.remove('Transcript.dat')
elif kek == "save":
now = datetime.now()
format = now.strftime("%Y-%m-%d----%H-%M-%S")
txt = './Transcripts/Transcript '+ format + '.txt'
binary = open('Transcript.dat','rb')
l = True
while l:
try:
decode = pickle.load(binary)
final = open(txt,'a')
final.write(decode)
final.close()
except EOFError:
l = False
binary.close()
os.remove('Transcript.dat')
self.window.destroy()
main_window()
content = response(msg)
self.msg_entry.delete(0, END)
msg1 = f"{sender}: {msg}\n\n"
self.text_widget.configure(cursor="arrow", state=NORMAL, wrap=WORD)
self.text_widget.insert(END, msg1)
self.text_widget.configure(cursor="arrow", state=DISABLED, wrap=WORD)
msg2 = f"{bot_name}: {content}\n\n"
self.text_widget.configure(cursor="arrow", state=NORMAL)
self.text_widget.insert(END, msg2)
self.text_widget.configure(cursor="arrow", state=DISABLED)
self.text_widget.see(END)
message = msg1 +"\n"+msg2
with open("Transcript.dat","ab") as file:
pickle.dump(message, file)
if __name__ == "__main__":
app = ChatApplication()
app.run()
def voicebot(x):
root.destroy()
BG_GRAY = "#ABB2B9"
BG_COLOR = "#17202A"
TEXT_COLOR = "#EAECEE"
FONT = "Helvetica 14"
FONT_BOLD = "Helvetica 13 bold"
class ChatApplication:
def __init__(self):
self.window = Tk()
self._setup_main_window()
def run(self):
self.window.mainloop()
def _setup_main_window(self):
self.window.title("KlayBot")
self.window.resizable(width=False, height=False)
self.window.configure(width=1300, height=650, bg="#1e1e1e")
head_label = Label(self.window, bg="#1e1e1e",anchor='w',justify="left", fg=TEXT_COLOR,text="Klay: Hello " + contents +",I am KlayBot and I have skills and capabilities to help you make you feel better.\nPress OK to continue...", font=("Courier New", 14))
head_label.pack()
head_label.place(x=0, y = 10)
# tiny divider
#line = Label(self.window, width=50, bg=BG_GRAY)
#line.place(relwidth=1, rely=0.07, relheight=0.012)
# text widget
self.text_widget = Text(self.window, width=20, height=2,fg=TEXT_COLOR,bg="#1e1e1e",
font=FONT, padx=5, pady=5)
self.text_widget.place(relheight=0.745, relwidth=1, rely=0.08)
self.text_widget.configure(cursor="arrow", state=DISABLED)
# scroll bar
scrollbar = Scrollbar(self.text_widget)
scrollbar.place(relheight=1, relx=0.984)
scrollbar.configure(command=self.text_widget.yview, background='#36393f')
# bottom label
bottom_label = Label(self.window, bg="#252526", height=80)
bottom_label.place(relwidth=1, rely=0.825)
# message entry box
self.msg_entry = Entry(bottom_label, bg="#36393f", fg=TEXT_COLOR, font=FONT)
self.msg_entry.place(relwidth=0.74, relheight=0.06, rely=0.008, relx=0.011)
self.msg_entry.focus()
self.msg_entry.bind("<Return>", self._on_enter_pressed)
# send button
send_button = Button(bottom_label, text="Send", font=FONT_BOLD, width=20, bg=BG_GRAY,
command=lambda: self._on_enter_pressed(NONE))
send_button.place(relx=0.77, rely=0.008, relheight=0.06, relwidth=0.22)
def _on_enter_pressed(self, event):
msg = self.msg_entry.get()
self._insert_message(msg, "You")
def _insert_message(self, msg, sender):
global a
global kek
if not msg:
return
if msg.lower() == "save":
kek = msg
if msg.lower() == "discard":
a=msg
if "bye" in msg.lower():
if a == "discard":
os.remove('Transcript.dat')
elif kek == "save":
now = datetime.now()
format = now.strftime("%Y-%m-%d----%H-%M-%S")
txt = './Transcripts/Transcript '+ format + '.txt'
binary = open('Transcript.dat','rb')
l = True
while l:
try:
decode = pickle.load(binary)
final = open(txt,'a')
final.write(decode)
final.close()
except EOFError:
l = False
binary.close()
os.remove('Transcript.dat')
self.window.destroy()
main_window()
content = response(msg)
self.msg_entry.delete(0, END)
msg1 = f"{sender}: {msg}\n\n"
self.text_widget.configure(cursor="arrow", state=NORMAL)
self.text_widget.insert(END, msg1)
self.text_widget.configure(cursor="arrow", state=DISABLED)
msg2 = f"{bot_name}: {content}\n\n"
self.text_widget.configure(cursor="arrow", state=NORMAL)
self.text_widget.insert(END, msg2)
self.text_widget.configure(cursor="arrow", state=DISABLED)
self.text_widget.see(END)
message = msg1 +"\n"+msg2
with open("Transcript.dat","ab") as file:
pickle.dump(message, file)
rate = engine.getProperty('rate')
voices = engine.getProperty('voices')
engine.setProperty('rate', 175)
engine.setProperty('voice', voices[x].id)
engine.say(content)
engine.runAndWait()
engine.stop()
if __name__ == "__main__":
app = ChatApplication()
app.run()
def voice_final():
male = (f"{male_value.get()}")
female = (f"{female_value.get()}")
print("Happy conversation! Just remember, my devs are always with you, headover to discord.gg/klayspg")
with open("./vars/male.txt","wb") as m:
pickle.dump(male,m)
with open("./vars/female.txt","wb") as f:
pickle.dump(female, f)
male_file = open("./vars/male.txt", "rb")
male_file_no = pickle.load(male_file)
male_file = int(male_file_no)
female_file = open("./vars/female.txt", "rb")
female_file_no = pickle.load(female_file)
female_file = int(female_file_no)
if male_file == 1 and female_file == 0:
voicebot(2)
# rate = engine.getProperty('rate')
# voices = engine.getProperty('voices')
# engine.setProperty('rate', 175)
# engine.setProperty('voice', voices[0].id)
# engine.say("Klay: Hello,"+contents+" I am KlayBot and I have skills and capabilities to help make you feel better.\nPress OK to continue...")
# engine.stop()
if male_file == 0 and female_file == 1:
voicebot(1)
# rate = engine.getProperty('rate')
# voices = engine.getProperty('voices')
# engine.setProperty('rate', 175)
# engine.setProperty('voice', voices[1].id)
# engine.say("Klay: Hello,"+contents+" I am KlayBot and I have skills and capabilities to help make you feel better.\nPress OK to continue...")
# engine.stop()
if male_file == 0 and female_file == 0:
messagebox.showerror('Choice error', 'Hey, choose atleast one type of voice ( ͡° ͜ʖ ͡°)')
if male_file == 1 and female_file == 1:
messagebox.showerror('Choice error', 'Hey, choose at max one type of voice ( ͡° ͜ʖ ͡°)')
def voice_choice():
global male_value
global female_value
mywindow = Toplevel()
mywindow.title("Voice type")
mywindow.geometry("450x150")
mywindow.resizable(False,False)
male_value = IntVar()
female_value = IntVar()
title=Label(mywindow,text="Please choice voice type:",font=("Terminal",15),fg="#C85417").place(x=80,y=20)
male_check = Checkbutton(mywindow, text="Male",font=("Terminal", 13), height=2, width = 10,variable=male_value)
male_check.place(x=60, y= 60)
female_check = Checkbutton(mywindow, text="Female",font=("Terminal", 13), height=2, width = 10,variable=female_value)
female_check.place(x=220, y= 60)
submit_button=Button(mywindow, text="Submit",fg="black",font=("Courier New",18),command=voice_final).place(x=160,y=100)
mywindow.mainloop()
def getvals():
global contents
name = (f"{namevalue.get()} ")
age = (f"{agevalue.get()} ")
ver = (f"{verificationvalue.get()}")
voice = (f"{voicevalue.get()}")
text = (f"{textvalue.get()}")
with open("./vars/ver.txt", "wb") as i:
pickle.dump(ver, i)
with open("./assets/age.txt", "wb") as f:
pickle.dump(age,f)
with open("./assets/name.txt","wb") as n:
pickle.dump(name, n)
with open("./vars/voice.txt","wb") as n:
pickle.dump(voice, n)
with open("./assets/text.txt","wb") as n:
pickle.dump(text, n)
file = open("./assets/age.txt", 'rb')
file = pickle.load(file)
try:
file = int(file)
except:
messagebox.showerror('Type error', 'Breh, Age is a number!')
folder = open("./vars/ver.txt", 'rb')
folder = pickle.load(folder)
folder = int(folder)
v = open("./vars/voice.txt", 'rb')
v = pickle.load(v)
v = int(v)
t = open("./assets/text.txt", 'rb')
t = pickle.load(t)
t = int(t)
f = open("./assets/name.txt", 'rb')
contents = pickle.load(f)
try:
if file >= 15 and file <= 25 and folder == 1:
if t == 1 and v == 0:
bot()
elif t ==0 and v == 1:
voice_choice()
else:
messagebox.showerror('Choice error', 'Hey! Choose either of the ways you wanna comunicate :/')
except:
return None
if folder != 1:
messagebox.showerror('Error', 'Please accept the T&C to continue...')
if file < 15 or file > 25:
messagebox.showerror('Age error', 'We currently dont support age groups more than 25 or under 15.')
def main_window():
global root
class Login:
def __init__(self,root):
global namevalue
global agevalue
global verificationvalue
global voicevalue
global textvalue
self.root=root
self.root.title("Klay Bot")
self.root.geometry("1068x600+100+50")
self.root.resizable(False,False)
image1 = Image.open("./assets/bg3.png")
test = ImageTk.PhotoImage(image1)
label1 = tkinter.Label(image=test)
label1.image = test
# Position image
label1.place(x=450, y=-175)
FONT_BOLD = "Helvetica 13 bold"
FONT = "Helvetica 14"
#Frame
Frame_login=Frame(self.root,bg="white")
Frame_login.place(x=0,y=0,height=1200,width=500)
namevalue = StringVar()
agevalue = StringVar()
verificationvalue = IntVar()
textvalue = IntVar()
voicevalue = IntVar()
#Warning
title=Label(Frame_login,text="Klaybot",font=("Terminal",35),fg="#C85417",bg="white").place(x=130,y=50)
title=Label(Frame_login,text='KlayBot is not a crisis service. KlayBot is a ',font=("Courier New",11,"bold"),fg="#808080",bg="white").place(x=40,y=130)
title=Label(Frame_login,text='self-help tool that is not intended to be a ',font=("Courier New",11,"bold"),fg="#808080",bg="white").place(x=60,y=155)
title=Label(Frame_login,text='medical intervention. No human is monitoring your ',font=("Courier New",11,"bold"),fg="#808080",bg="white").place(x=60,y=180)
title=Label(Frame_login,text='live conversations with KlayBot. If you are in a ',font=("Courier New",11,"bold"),fg="#808080",bg="white").place(x=40,y=205)
title=Label(Frame_login,text='crisis, type ',font=("Courier New",11,"bold"),fg="#808080",bg="white").place(x=60,y=230)
title=Label(Frame_login,text='"SOS" ',font=("Courier New",11,"bold"),fg="red",bg="white").place(x=175,y=230)
title=Label(Frame_login,text='in the app and KlayBot will',font=("Courier New",11,"bold"),fg="#808080",bg="white").place(x=228,y=230)
title=Label(Frame_login,text='suggest external resources that you can use.',font=("Courier New",11,"bold"),fg="#808080",bg="white").place(x=60,y=255)
title=Label(Frame_login,text='⊂(◉‿◉)つ',font=("Courier New",19,"bold"),fg="#808080",bg="white").place(x=180,y=285)
#name and age
title=Label(Frame_login,text="Name",font=("Courier New",11,"bold"),fg="#000000",bg="white").place(x=40,y=335)
self_txt_user=Entry(Frame_login,font=("Terminal",11),bg="lightgray",textvariable=namevalue)
self_txt_user.place(x=150,y=340,width=190,height=20)
title=Label(Frame_login,text="Age",font=("Courier New",11,"bold"),fg="#000000",bg="white").place(x=40,y=375)
self_txt_pass=Entry(Frame_login,font=("Terminal",11),bg="lightgray",textvariable=agevalue)
self_txt_pass.place(x=150,y=385,width=190,height=20)
#text
title=Label(Frame_login,text="Way of communication:",font=("Terminal",12,"bold"),fg="#000000",bg="white").place(x=80,y=420)
checkbutton_variable = IntVar()
#voice
voice_check = Checkbutton(text="Voice (Beta)",font=("Terminal", 9),bg = "white", height=2, width = 10,variable=voicevalue)
voice_check.place(x=120, y= 460)
#text
text_check = Checkbutton(text="Text",font=("Terminal", 9),bg = "white", height=2, width = 10,variable=textvalue)
text_check.place(x=240, y= 460)
#T&C
verification = Checkbutton(text="I have read and agree to the T&C",font=("Terminal", 9),bg = "white", height=2, width = 50,variable=verificationvalue)
verification.place(x=20, y= 500)
#Submit
submit_button=Button(Frame_login,text="Submit",fg="black",bg="#FFFFFF",font=("Courier New",18),command=getvals).place(x=170,y=550)
root = Tk()
obj = Login(root)
root.mainloop()
main_window()