-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
463 lines (357 loc) · 12.7 KB
/
main.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
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
################################################### modules
from msilib import text
from winsound import PlaySound
from texttospeech import *
from processtext import *
from threading import *
import speech_recognition as sr
from playsound import playsound
from tkinter import *
import sys
# for settings
from cgitb import text
from ctypes import windll
from tkinter import *
from tkinter import ttk
from soupsieve import select
from tkinter import messagebox
import json
################################################### modules
infi_listen_bool = True
##############################################################settings window
def settings_window():
# loading config_data into data dict
file = open("config_data.json", "r")
data = json.loads(file.read())
file.close()
# initial value for option selected in drop down
dropdownstring = None
# filling drop down menu using data dict by returning a tuple
def fill_drop_down():
values = ()
for i in data.keys():
values = values + (i,)
return values
# fetching drop down menu's selected option and returning it to current value box
def option_selected(event):
nonlocal dropdownstring
dropdownstring = s.get()
enternewvaluebox.delete("1.0", "end")
insert_current_value(dropdownstring)
# displaying cuurent values of selected option in dropdown menu
def insert_current_value(key):
currentvaluebox.configure(state="normal")
currentvaluebox.delete("1.0", "end")
currentvaluebox.insert("end", data[key])
currentvaluebox.configure(state="disabled")
# when update button is pressed
def update_pressed():
nonlocal dropdownstring
if dropdownstring != None:
key = dropdownstring
newval = enternewvaluebox.get("1.0", END).strip()
if len(newval) > 0:
# updating data dict
data[key] = newval
# dumping data dict to config_data.json
newdata = json.dumps(data)
file = open("config_data.json", "w")
file.write(newdata)
file.close()
# showing success message, changing current value
messagebox.showinfo(
"", f"{key} has been successfully updated to \n{newval}"
)
insert_current_value(key)
enternewvaluebox.delete("1.0", "end")
else:
messagebox.showwarning("", f"{key} can't be empty")
#
################ window and combobox string declaration, window styling
window = Toplevel()
s = StringVar()
window.geometry("500x500")
window.maxsize(500, 500)
window.title("SETTINGS")
window.configure(bg="white")
window.option_add("*font", "Segoe_UI 10")
window.iconbitmap("assets\settings_icon.ico")
window.attributes("-topmost", True)
################ window and combobox string declaration, window styling
#
#####################################################name frame
nameframe = Frame(window)
Label(nameframe, text="NAME : ", bg="white", width=25).pack(side=LEFT)
combobox = ttk.Combobox(nameframe, width=30, textvariable=s)
combobox.pack(side=RIGHT)
combobox["state"] = "readonly"
combobox.current(None)
# filling data dict values
combobox["values"] = fill_drop_down()
# when an option is selected
combobox.bind("<<ComboboxSelected>>", option_selected)
nameframe.pack(pady=(50, 0))
#####################################################name frame
#
#####################################################current value frame
currentvalueframe = Frame(window)
currentvalueframe.configure(bg="white")
Label(currentvalueframe, text="CURRENT VALUE : ", bg="white", width=25).pack(
side=LEFT
)
currentvaluebox = Text(currentvalueframe, bg="white", width=33, height=5, wrap=WORD)
currentvaluebox.pack(side=RIGHT)
currentvaluebox.configure(state="disabled")
currentvalueframe.pack(pady=(10, 0))
#####################################################current value frame
#
#####################################################enter new value frame
enternewvalueframe = Frame(window)
enternewvalueframe.configure(bg="white")
Label(enternewvalueframe, text="ENTER NEW VALUE : ", bg="white", width=25).pack(
side=LEFT
)
enternewvaluebox = Text(
enternewvalueframe, bg="white", width=33, height=5, wrap=WORD
)
enternewvaluebox.pack(side=RIGHT)
enternewvalueframe.pack(pady=(10, 0))
#####################################################enter new value frame
#
# update button
Button(window, text="UPDATE", bg="black", fg="white", command=update_pressed).pack(
pady=(20, 0)
)
#
window.mainloop()
##############################################################settings window
################################ global threads
def program_thread_settings():
thread2 = Thread(target=settings_window)
thread2.start()
def program_thread_listen():
thread1 = Thread(target=listen)
thread1.start()
################################ global threads
# when enter key is pressed in entry box
def enter_key_pressed(event):
program_thread_listen()
# speech to text
def speechtotext():
r = sr.Recognizer()
texttospeech_stop("event")
with sr.Microphone() as source:
# printing "STARTED Listening" in console
print()
print("Listening.. Ask me anything.. ")
# playing start sound
try:
playsound("assets\start_sound.mp3")
except:
pass
# storing listened text
try:
listened_text = r.listen(source, timeout=5, phrase_time_limit=5)
except:
pass
# printing "FINISHED Listening" in console
print("Finished Listening.. ")
print()
# playing end sound
try:
playsound("assets\end_sound.mp3")
except:
pass
# result
try:
detection_result = r.recognize_google(listened_text)
except:
detection_result = "NO AUDIO DETECTED"
return detection_result
def listen():
global infi_listen_bool
query_input = input_box.get()
input_box.delete(0, END)
query_input = query_input.lstrip()
query_input = query_input.rstrip()
if len(query_input) == 0:
query_input = speechtotext()
# priting mic detected text in console and output box
print("Mic detected text: ", query_input)
output_box.configure(state="normal")
output_box.insert("end", "\n\nYOU: " + query_input)
output_box.configure(state="disabled")
# sending query to process function
if query_input != "NO AUDIO DETECTED":
query_success_code, query_output = processtext(query_input)
else:
query_success_code, query_output = (
"Response <501>",
"Sorry, I was not able to detect your voice",
)
# printing process result in console and output box
print("Query Success Code: ", query_success_code)
print("Final Answer: ", query_output)
output_box.configure(state="normal")
# loading config_data into data
file = open("config_data.json", "r")
data = json.loads(file.read())
file.close()
output_box.insert("end", f"\n{data['WAKE WORD'].upper()}: " + query_output.upper())
output_box.configure(state="disabled")
output_box.see(END)
# speaking result
texttospeech(query_output)
# checking for bye code
if query_success_code == "bye code":
root.destroy()
infi_listen_bool = False
sys.exit(0)
elif query_success_code == "settings code":
program_thread_settings()
######################################### loading screen
def loading_screen():
# creating and styling screen
root = Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry(f"{screen_width}x{screen_height}")
root.minsize(screen_width, screen_height)
root.state("zoomed")
root.title("LAUNCHING") # title
root.option_add("*font", "Segoe 15") # font
root.configure(bg="white") # bg color
root.iconbitmap("assets\logo_icon.ico") # icon
logoimg1 = PhotoImage(file="assets\logo_image.png").subsample(5, 5)
logoimg2 = PhotoImage(file="assets\logo_image2.png").subsample(5, 5)
logoimg3 = PhotoImage(file="assets\logo_image3.png").subsample(5, 5)
logoimg4 = PhotoImage(file="assets\logo_image4.png").subsample(5, 5)
logoimg5 = PhotoImage(file="assets\logo_image5.png").subsample(5, 5)
logo = Button(root, image=logoimg1, bg="white", highlightthickness=0, bd=0)
logo.pack(pady=(300, 0))
logo["state"] = "disabled"
logotext = Label(text="Loading Scripts ...", bg="white", fg="grey", width=125)
logotext.pack(pady=20)
def image_change():
counter = 0
list_of_texts = [
"Scripts Loaded",
"Connecting to Server ...",
"Loading Config Data ...",
"Config Data Loaded",
"Initialising UI ...",
"Loading Assets ...",
"Assets Loaded",
"Starting September ...",
]
for i in [
logoimg2,
logoimg3,
logoimg4,
logoimg5,
logoimg2,
logoimg3,
logoimg4,
logoimg5,
]:
if counter < 4:
logo.after(100, None)
else:
logo.after(250, None)
logo.configure(image=i)
logotext.configure(text=list_of_texts[counter])
counter += 1
subthread = Thread(target=image_change)
subthread.start()
logo.after(2000, root.destroy)
root.mainloop()
######################################### loading screen
######################################### infinite listen
r = sr.Recognizer()
def infinite_listen():
global infi_listen_bool
while infi_listen_bool:
with sr.Microphone() as source:
print("Listening")
# r.adjust_for_ambient_noise(source)
try:
listened_text = r.listen(source, timeout=5, phrase_time_limit=5)
except:
listened_text = ""
try:
detection_result = r.recognize_google(listened_text)
except:
detection_result = ""
# print(detection_result, end = " ")
# loading config_data into data
file = open("config_data.json", "r")
data = json.loads(file.read())
file.close()
if data["WAKE WORD"] in detection_result.lower():
listen()
sys.exit(0)
######################################### infinite listen
################################################## main program
# call loading screen
loading_screen()
# call infinte listen thread
infthread = Thread(target=infinite_listen)
infthread.start()
# creating screen
root = Tk()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# root.resizable(0, 0)
# setting screen size
root.geometry(f"{screen_width}x{screen_height}")
root.minsize(screen_width, screen_height)
root.state("zoomed")
# styling root screen
root.title("SEPTEMBER ASSISTANT")
root.option_add("*font", "Segoe 15")
root.configure(bg="white")
root.iconbitmap("assets\logo_icon.ico")
##########################################
#######screen elements declaration
################# output box
# output box creation
output_box = Text(root, width=130, height=25, wrap="word", bd=0)
output_box.pack(pady=(40, 0), padx=50)
# output box properties
output_box.configure(state="disabled")
output_box.see(END)
################## output box
################## frame for input box, speak button, settings button
# frame creation
frame = Frame(root)
frame.configure(bg="white")
frame.pack(pady = (30,0))
# input box
input_box = Entry(frame, width=100)
input_box.bind("<Return>", enter_key_pressed)
input_box.pack(side=LEFT)
# mic button
photo = PhotoImage(file="assets\mic_image.png")
photoimage = photo.subsample(2, 2)
mic_button = Button(
frame, image=photoimage, bd=0, highlightthickness=0, command=program_thread_listen
)
mic_button.pack(side=LEFT, padx=10)
# settings button
photo1 = PhotoImage(file="assets\settings_image.png")
photoimage1 = photo1.subsample(11, 11)
settings_button = Button(
frame,
image=photoimage1,
bd=0,
highlightthickness=0,
bg="white",
command=settings_window,
)
settings_button.pack(side=RIGHT)
################## frame for input box, speak button, settings button
# press escape to stop speaking
root.bind("<Escape>", texttospeech_stop)
root.mainloop()
################################################## main program
sys.exit(0)