-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathV_A.py
319 lines (244 loc) · 9.56 KB
/
V_A.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
from tkinter import *
from PIL import Image,ImageTk
import speech_recognition as sr
import datetime
import pyaudio
import pyttsx3
import pywhatkit as kt
from PyDictionary import PyDictionary
from threading import Thread
import pyjokes
######## VOICE INIT ########
engine=pyttsx3.init('sapi5')
engine.setProperty("rate",145)
######## SPEAK ########
def speak(audio):
engine.say(audio)
engine.runAndWait()
######## TAKING VOICE FROM THE USER ########
def record():
r=sr.Recognizer()
with sr.Microphone() as mic:
print("Listeninng")
bot_status(True)
uservoice=r.listen(mic)
query=""
try:
print("Recognizing")
bot_status(False,True)
query=r.recognize_google(uservoice)
message(text=f"{query}")
except Exception as e:
print(e)
message(text="Could Not Hear You. Please Try Again",bot=True)
return query.lower()
######## CONVERTING VOICES TO STRINGS ########
def runAssitant():
query=record()
if "thank you" in query:
message(text="Your most welcome",bot=True)
speak("Your most welcome.")
return
else:
command(query)
return
######## ACTION TO BE TAKEN FOR THE COMMANDS TAKEN FROM THE USER ########
def having(text,subtext):
for i in subtext:
if i in text:
return True
return False
def command(query):
""" Interactions : """
if having(query,["who are you","who made you"]):
message(text="Hii, I am a voice assistant namely D.D.X made by Biswaranjan", bot=True)
speak("Hii, I am a voice assistant namely DDX made by Biswaranjan")
bot_status(False,False,True)
if having(query,["you are good","you are nice","you are awesome"]):
message(text="Aww thank you, I appreciate that", bot=True)
speak("Aww thank you, I appreciate that")
bot_status(False,False,True)
if having(query,["tell me some jokes","make me laugh"]):
jokes=pyjokes.get_joke()
message(text=f"{jokes}", bot=True)
speak(f"{jokes}")
bot_status(False,False,True)
if having(query,["how are you"]):
message(text="I am good. Thank You. What about You?", bot=True)
speak("I am good. Thank You. What about you?")
uservoice1=record()
if having(uservoice1,["not feeling good","sad","not fine","not good"]):
message("Dont worry, Everything will be fine soon",True)
speak("Dont worry, Everything will be fine soon")
else:
message("Thats really nice. Have a great day ", True)
speak("Thats really nice. Have a great day")
bot_status(False, False, True)
""" PLAYING VIDEO OR SEARCHING ON YOUTUBE """
if having(query,["on youtube","video","play"]):
if "play" in query:
query=query.replace('play','')
if "video" in query:
query=query.replace('video','')
if "on youtube" in query:
query=query.replace('on youtube','')
message(text=f"Opening Youtube and playing {query}", bot=True)
speak(f"Hold on, Loading Youtube and playing {query}.")
kt.playonyt(query)
bot_status(False, False, True)
""" SEARCHING ON GOOGLE """
if having(query,["search","google","on google"]):
if "search" in query:
query=query.replace('search','')
if "on google" in query:
query = query.replace('on google', '')
if "google" in query:
query = query.replace('google', '')
message(text=f"Searching:{query} on Google", bot=True)
speak(f"Hold on, I am searching {query} on Google.")
kt.search(query)
bot_status(False, False, True)
""" DICTIONARY USE """
if having(query,["what is the meaning of","what is the definition of","define"]):
if "what is the meaning of" in query:
query=query.replace('what is the meaning of ','')
if "what is the definition of" in query:
query = query.replace('what is the definition of ', '')
if "define " in query:
query=query.replace('define','')
message(text=f"{query.capitalize()}: It is ..", bot=True)
speak(f"{query}: It is ")
word=PyDictionary()
meaning=word.meaning(f"{query}")
count=len(meaning['Noun'])
if count>4:
count=4
for i in range(count):
message(text=f"{meaning['Noun'][i]}", bot=True)
speak(f"{meaning['Noun'][i]}")
bot_status(False, False, True)
######## MIC BUTTON TASK ########
def mic():
clearchats()
try:
t1=Thread(target=runAssitant)
t1.start()
except Exception as e:
print(e)
if "you are offline" in e.lower():
Label(text="You are Off line").pack()
######## INTERACTIONS ON SCREEN ########
def message(text,bot=False):
if bot:
ddx = Label(chat_frm, text=f"{text}.", bg="light grey", wraplength=250, justify=LEFT,font="Calisto 10 bold",fg='black')
ddx.pack(anchor="w", pady=2)
else:
usr = Label(chat_frm, text=text.capitalize(), wraplength=250, justify=LEFT, bg="black",font="Calisto 10 bold",fg='white')
usr.pack(anchor="e", pady=2)
######## CLEARING THE CHAT SCREEN ########
def clearchats():
for win in chat_frm.winfo_children():
win.destroy()
######### STATUS MESSAGE ########
def bot_status(lsn=False,recgn=False,mic=False):
if lsn:
AI_status.config(text="Listening...",fg="red")
return
if recgn:
AI_status.config(text="Recognizing...",fg="green")
return
if mic:
AI_status.config(text="",bg="white")
return
######## MAIN GUI ########
def destroyscreen():
speak("Hii, I am DDX")
splash.destroy()
def time():
lvtime = datetime.datetime.now().strftime("%I:%M:%S %p")
tm.config(text=lvtime)
gui.after(1000,time)
def guide():
guide = Tk()
guide.title("D.D.X ASSISTANT GUIDEBOOK")
wd, ht = 800, 500
guide.geometry(f"{wd}x{ht}")
guide.maxsize(width=wd, height=ht)
guide.minsize(width=wd, height=ht)
guide.configure(bg='black')
## TITLE ##
page_title = Label(guide, text="VOICE COMMANDS", fg="white", bg='black', font='Calibri 28 bold')
page_title.pack()
## ADDING FRAME ##
text_frame = Frame(guide, bg='white', width=800, height=410)
text_frame.pack()
text_frame.pack_propagate()
## ADDING SCROLLBAR ##
scl_bar = Scrollbar(text_frame, orient=VERTICAL, bg='black')
scl_bar.pack(side=RIGHT, fill=Y)
## ADDING TEXT BOX TO THE WINDOW ##
text_box = Text(text_frame, bg="black", font="Calibri 14 bold", fg='white', width=75, height=17, borderwidth=5,
wrap='word', yscrollcommand=scl_bar.set,relief="sunken")
text_box.pack(fill='both')
text_box.insert(END,open('GUIDE_BOOK', 'r').read())
scl_bar.config(command=text_box.yview)
## BUTTON TO CLOSE THE WINDOW ##
close_button = Button(guide, text='CLOSE', font='calibri 10 bold', bg='red', fg='white', command=guide.destroy)
close_button.pack(side=BOTTOM, pady=10)
guide.mainloop()
if __name__=="__main__":
# SPLASH SCREEN
splash=Tk()
splash.title("ASSISTANT - D.D.X")
w,h=500,600
splash.geometry(f"{w}x{h}")
sp_pic = Image.open("SPLASH.jpg")
sp_pic = sp_pic.resize((w, h))
sp_image = ImageTk.PhotoImage(sp_pic)
bgn = Label(splash, image=sp_image)
bgn.place(x=-2, y=0)
splash.after(100,destroyscreen)
splash.mainloop()
### MAIN WORKING INTERFACE
gui=Tk()
gui.title("ASSISTANT - D.D.X")
wd,ht=800,500
gui.geometry(f"{wd}x{ht}")
gui.maxsize(width=wd,height=ht)
gui.minsize(width=wd, height=ht)
bg_pic=Image.open("img_1.png")
bg_pic=bg_pic.resize((wd,ht))
bgn_image=ImageTk.PhotoImage(bg_pic)
bgn=Label(gui,image=bgn_image)
bgn.place(x=-2,y=0)
### ADDING LIVE TIME AND DATE ###
tm=Label(gui,text="",font="calibri 15 bold",fg="#004E7B",bg="white")
tm.place(x=472.5,y=46)
dt=datetime.datetime.now().strftime("%a, %b %d")
dte=Label(gui,text=dt,font="calibri 10 bold",fg="#004E7B",bg="white")
dte.place(x=492,y=67.5)
time()
#### GUIDE ICON ######
gd_pic = Image.open("dark-blue-book-icon_tcm12-256223.png")
gd_pic = gd_pic.resize((45,39))
gd_image = ImageTk.PhotoImage(gd_pic)
gdic=Button(gui, image=gd_image, command=guide, activebackground="white", borderwidth=0, bg="white")
gdic.place(x=392.5,y=46.5)
### CHAT FRAME ###
chat_frm = Frame(gui,bg="white",width=267.5,height=300)
chat_frm.place(x=394,y=106)
chat_frm.pack_propagate(0)
message(text="Tap the Mic to Speak. To Know more, Click 'info' button on the top left-hand side.",bot=True)
#### STATUS FRAME AND MESSAGE ####
st_frame=Frame(gui,bg="white",width=101,height=20)
st_frame.place(x=485,y=405)
st_frame.pack_propagate(0)
AI_status=Label(st_frame,text="",fg="white",bg="white",font=("Calisto MT","11","bold"))
AI_status.pack(anchor="center")
#### MIC PLACEMENT #### #004E7B
mic_pic = Image.open("img.png")
mic_pic = mic_pic.resize((65,65))
mic_image = ImageTk.PhotoImage(mic_pic)
mic_btn = Button(gui,image=mic_image,command=mic,bg="#004E7B",activebackground="#004E7B",borderwidth=0)
mic_btn.place(x=496.5, y=426)
gui.mainloop()