-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoiceAssitantPi.py
416 lines (365 loc) · 19.4 KB
/
VoiceAssitantPi.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
# importing packages
import speech_recognition as sr
import win32com.client as wincl
import os
import sys
import re
import subprocess
import requests
import json
from bs4 import BeautifulSoup
import time
import datetime
from time import strftime
import calendar
import webbrowser
from googlesearch import search
import pyjokes
from pyowm import OWM
import wikipedia
from newsapi import NewsApiClient
from win10toast import ToastNotifier
import wolframalpha
import oxforddictionaries
import tkinter as tk
from tkinter import ttk
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
import io
import base64
class PCAssistantGUI:
def __init__(self, root):
self.root = root
self.root.title("PC Assistant")
# Create a notebook to organize different functionalities
self.notebook = ttk.Notebook(root)
# Page 1: Smart Assistant
self.page1 = ttk.Frame(self.notebook)
self.create_smart_assistant_page()
# Add pages to the notebook
self.notebook.add(self.page1, text="Smart Assistant")
self.notebook.pack(expand=1, fill="both")
def create_smart_assistant_page(self):
# Create widgets for the Smart Assistant page
label = ttk.Label(self.page1, text="PC Assistant", font=("Helvetica", 16))
label.pack(pady=10)
button_open_app = ttk.Button(self.page1, text="Open Application", command=self.open_application)
button_open_app.pack(pady=10)
button_control_notif = ttk.Button(self.page1, text="Control Notifications", command=self.control_notifications)
button_control_notif.pack(pady=10)
# Add more buttons or widgets as needed
def open_application(self):
# Implement the functionality to open applications
# Example: Open a web browser
import webbrowser
webbrowser.open("https://www.example.com")
def control_notifications(self):
# Implement the functionality to control notifications
# Example: Display a notification
from tkinter import messagebox
messagebox.showinfo("Notification", "This is a notification!")
if __name__ == "__main__":
# Create a new Tkinter window
root_tkinter = tk.Tk()
# Create an instance of PCAssistantGUI and pass the Tkinter window
app = PCAssistantGUI(root_tkinter)
# Start the Tkinter event loop
root_tkinter.mainloop()
# Invoking SAPI [Microsoft Speech API]
speak = wincl.Dispatch("SAPI.SpVoice")
# Converts Text-To-Speech; Converts only string value
def PiSays(str):
try:
speak.Speak(str)
except:
print('Error')
# Greets the User upon start-up with respect to the time of the time
def GreetUser():
try:
day_time = int(strftime('%H'))
if day_time < 12:
PiSays("Hello Ps, Good morning, Its Good To Have You Back")
print('Pi :' + ' Hello Ps, Good morning, Its Good To Have You Back')
elif 12 <= day_time < 18:
PiSays("Hello Ps, Good afternoon, Its Good To Have You Back")
print('Pi :' + ' Hello Ps, Good afternoon, Its Good To Have You Back')
else:
PiSays("Hello Ps, Good evening, Its Good To Have You Back")
print('Pi :' + ' Hello Ps, Good evening, Its Good To Have You Back')
except:
print("error")
PiSays(".. I am Pi, Your Personal Voice Assistant ..")
print('Pi :' + ' I am Pi, Your Personal Voice Assistant')
PiSays(".. How can I Assist You .. ")
print('Pi :' + ' How can I Assist You')
# Checks for any birthday event for the current day
def BirthdayNotifier():
file_read = open(BirthdayFile, 'r') # opens the file read mode
today = time.strftime('%d%m')
i = 0
for line in file_read:
if today in line:
line = line.split(' ')
print("Its " + line[1] + '\'s' + " Birthday Today")
PiSays("Its " + line[1] + '\'s' + " Birthday Today")
bday_notification.show_toast("Birthday Notification", "Its " + line[1] + '\'s' + " Birthday Today",duration=4) # Invokes Desktop Notification
i += 1
if i == 0:
time.sleep(2)
print("No Birthdays Today!")
PiSays("No Birthday's Today")
bday_notification.show_toast("Birthday Notification", "No Birthday's Today",duration=4) # Invokes Desktop Notification
# Checks for any reminder for the cureent day
def ReminderNotifier():
file_read = open(ReminderFile, 'r') # opens the file read mode
today = time.strftime('%d%m')
i = 0
for line in file_read:
if today in line:
line = line.split(' ')
print("Pi : " + line[1] + ' ' + line[2] + ' ' + line[3] + ' ' + line[4] + ' ' + line [5] + ' ' + line [6])
PiSays(line[1] + ' ' + line[2] + ' ' + line[3] + ' ' + line[4] + ' ' + line [5] + ' ' + line [6])
reminder_notification.show_toast("Reminder", line[1] + ' ' + line[2] + ' ' + line[3] + ' ' +line[4] + ' ' + line [5] + ' ' + line [6], duration=4) # Invokes Desktop Notification
i += 1
if i == 0:
time.sleep(2)
print("Pi : No Reminders Today!")
PiSays("No Reminders Today!")
reminder_notification.show_toast("Reminder", "No Reminders For Today", duration=4) # Invokes Desktop Notification
# Invoking/Calling GreetUser() Function
GreetUser()
# Calling BirthdayNotifier() Function
BirthdayFile = 'F:\BirthdayFile.txt' # File Location
bday_notification = ToastNotifier()
PiSays("Let me Check for Any Birthdays Today !")
print("Pi : Let me Check for Any Birthdays Today !")
BirthdayNotifier()
# Calling ReminderNotifier() Function
ReminderFile = 'F:\ReminderFile.txt' # File Location
reminder_notification = ToastNotifier()
PiSays("Let me Check for Any Reminders Today !")
print("Pi : Let me Check for Any Reminders Today !")
ReminderNotifier()
# Define a Function to Recognize Input Speech
def RecognizeSpeech():
r = sr.Recognizer() # an instance of speech_recognition; to recognize speech
with sr.Microphone() as source:
print('...')
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
r.energy_threshold = 2000
audio = r.listen(source, phrase_time_limit=5) # listens to the User
try:
UserCommand = r.recognize_google(audio).lower() # Recognizes Listened Speech Using Google Speech API
print('You said: ' + UserCommand + '\n')
except sr.UnknownValueError:
print('Your last command couldn\'t be heard')
UserCommand = RecognizeSpeech(); # If speech unrecognized, the RecognizeSpeech() is Invoked again and again until recognized
return UserCommand
# Defining a Function of a Set of Predefined tasks Using if..elif Statements
def SelectTask(UserCommand):
# Water-reminder notification every hour at '00' Minutes
time_now = time.strftime('%M') # returns only minute value
if time_now == 0:
water_notftn = ToastNotifier()
water_notftn.show_toast("Water Reminder 🥛",duration=4)
try:
# 1. Tells the Cureent Day and Date
if "date" in UserCommand:
def todays_date(date):
day, month, year = (int(i) for i in date.split(' '))
dayNumber = calendar.weekday(year, month, day)
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
return (days[dayNumber])
day_object = datetime.date.today().strftime("%d %m %y")
print('Today is ' + todays_date(day_object) + ' And the Date is ' + day_object)
PiSays(" Today is " + todays_date(day_object) + "And the Date is " + day_object)
# 2. Opens the Desired Website
elif "open" in UserCommand:
website_request = re.search('open (.+)', UserCommand)
if website_request:
domain = website_request.group(1)
print('The Requested Website is https://www.' + domain + '.com')
url = 'https://www.' + domain + '.com'
webbrowser.open(url)
print('The website you have requested has been opened for you.')
PiSays("The website you have requested has been opened for you.")
# 3. carry outs a search request using googlesearch
elif 'search' in UserCommand:
search_request = re.search('search (.+)', Usercommand)
if search_request:
query = search_request.group(1)
print('Your Search Results are :')
PiResponse("Your Search Results are :")
for url in search(query, stop=3):
print(url)
webbrowser.open(url)
print("Your Search Request is Completed")
PiSays("Your Search Request is Completed")
# 4. Tells any random joke from the pyjokes module
elif "tell me a joke" in UserCommand:
joke = pyjokes.get_joke()
print(joke)
PiSays(joke)
# 5. Gives Weather Updates
elif 'weather' in UserCommand:
owm = OWM(API_key='39150c1224ea99f8dcc8ccbd7403afd5') # PyOWM API_key
obs = owm.weather_at_place('Hyderabad,India') # location of required weather updates
w = obs.get_weather()
w.get_reference_time(timeformat='iso')
x = w.get_detailed_status() # returns Atmosphere status
y = w.get_humidity() # returns humidity
z = w.get_temperature(unit='celsius') # returns temperatutre
print('The Atmosphere Has ' + x)
PiSays('The Atmosphere Has ' + x)
print('The Maximum Temperature For Today is %d degree celsius and the Minimum Temperature is %d degree celsius' % (z['temp_max'], z['temp_min']))
PiSays('The Maximum Temperature is %d degree celsius and the Minimum Temperature is %d degree celsius' % (z['temp_max'], z['temp_min']))
print('And The Humidity Outside is %d percentage.' % (y))
PiSays('And The Humidity Outside is %d percentage.' % (y))
if z['temp'] >= 30:
print('The Weather Outside is Very Hot, I Suggest You Stay Home and Stay Hydrated ')
PiSays('The Weather Outside is Very Hot, I Suggest You Stay Home')
elif z['temp'] > 24 and z['temp'] < 30:
print('The Weather Outside is Moderately Hot, I Suggest You Wear Light Cloths and a Hat to avoid Heat')
PiSays('The Weather Outside is Moderately Hot, I Suggest You Wear Light Cloths and a Hat to avoid Heat')
elif z['temp'] > 16 and z['temp'] <= 24:
print('The Weather Outside is Warm, I Suggest You Wear Sunglasses and a Hat')
PiSays('The Weather Outside is Warm, I Suggest You Wear Sunglasses and a Hat')
elif z['temp'] < 16:
print('The Weather Outside is Cold, I Suggest You Wear Warm Clothes')
PiSays('The Weather Outside is Cold, I Suggest You Wear Warm Clothes')
# 6. Comes up with latest top 5 news headlines from India
elif 'news' in UserCommand:
newsapi = NewsApiClient(api_key='d444d79c864946ea9661ac0bd781aa47') # Enter Your API Key from NewsAPI
top_headlines = newsapi.get_top_headlines(q='India', language='en', )
for article in top_headlines['articles'][:5]:
print('Title : ' + article['title'])
PiSays('Title : ' + article['title'])
print('Description : ' + article['description'], '\n')
PiSays('Description : ' + article['description'])
# 7. Tells the possible meanings of the word or phrase
elif 'meaning' in UserCommand:
reg_ex = re.search('Tell me the meaning of (.+)', UserCommand)
if reg_ex:
word_id = reg_ex.group(1)
app_id = '4ea13c64' # app_id from oxforddictionaries
app_key = 'ee330a18c6d1b84caa707af849421635' #app_key from oxforddictionaries
language = 'en'
url = 'https://od-api.oxforddictionaries.com:443/api/v2/entries/' + language + '/' + word_id.lower()
urlFR = 'https://od-api.oxforddictionaries.com:443/api/v2/stats/frequency/word/' + language + '/?corpus=nmc&lemma=' + word_id.lower()
r = requests.get(url, headers={'app_id': app_id, 'app_key': app_key})
name_json = r.json()
name_list = []
for name in name_json['results']:
name_list.append(name['word'])
print("You searched for the word : " + word_id)
PiSays("You searched for the word : " + word_id)
url_mean = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/' + language + '/' + word_id.lower()
mean_json = r.json()
mean_list = []
PiSays("I can Read it out for you")
for result in mean_json['results']:
for lexicalEntry in result['lexicalEntries']:
for entry in lexicalEntry['entries']:
for sense in entry['senses']:
mean_list.append(sense['definitions'][0])
for i in mean_list:
print(word_id + " : " + i)
PiSays(word_id + " : " + i)
# 8. Gives the latest updates on coronavirus in India using Web-Scraping
elif 'corona' or 'virus' in UserCommand:
url = "https://www.worldometers.info/coronavirus/country/india/" # Website URL address used for web-scraping
req = requests.get(url)
bsObj = BeautifulSoup(req.text, "html.parser")
data = bsObj.find_all("div", class_="maincounter-number")
print('Give me Moment Ps, I am Scraping data for you')
PiSays("Give me Moment Ps, I am Scraping data for you")
print("Total Cases: ", data[0].text.strip())
PiSays("Total Cases: " + data[0].text.strip())
print("Total Deaths: ", data[1].text.strip())
PiSays("Total Deaths: " + data[1].text.strip())
print("Total Recovered: ", data[2].text.strip())
PiSays("Total Recovered: " + data[2].text.strip())
# 9. Suggests some of the most read books in the current week (Updates weekly)
elif "book" or "suggest" in UserCommand:
url = "https://www.goodreads.com/book/most_read" # Website URL address used for web-scraping
req = requests.get(url)
bsObj = BeautifulSoup(req.text, "html.parser")
book_title = bsObj.find_all(class_="bookTitle")
author_name = bsObj.find_all(class_="authorName")
print('Here are the 5 Most Read Books This Week In India : \n')
print(book_title[0].text.strip() + " by " + author_name[0].text.strip())
PiSays(book_title[0].text.strip() + " by " + author_name[0].text.strip())
print(book_title[1].text.strip() + " by " + author_name[1].text.strip())
PiSays(book_title[1].text.strip() + " by " + author_name[1].text.strip())
print(book_title[2].text.strip() + " by " + author_name[2].text.strip())
PiSays(book_title[2].text.strip() + " by " + author_name[2].text.strip())
print(book_title[3].text.strip() + " by " + author_name[3].text.strip())
PiSays(book_title[3].text.strip() + " by " + author_name[3].text.strip())
print(book_title[4].text.strip() + " by " + author_name[4].text.strip())
PiSays(book_title[4].text.strip() + " by " + author_name[4].text.strip())
# 10. Launch's any application preceeded with 'launch' in UserCommand
elif 'launch' in UserCommand:
launch_application = re.search('launch (.+)', UserCommand)
application = launch_application.group(1)
if "chrome" in application: # Launches Google Chrome
print("Openning Google Chrome")
PiSays("Openning Google Chrome")
os.startfile('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe') # File location
return
elif "word" in application: # Launches Microsoft Word
print("Opening Microsoft Word")
PiSays("Opening Microsoft Word")
os.startfile('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\\Word.lnk') # File location
return
elif "excel" in application: # Launches Microsoft Word
print("Opening Microsoft Excel")
PiSays("Opening Microsoft Excel")
os.startfile('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\\Excel.lnk') # File location
return
elif "vlc" in application: # Launches VLC
print("Opening VLC Media Player")
PiSays("Opening VLC Media Player")
os.startfile('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\VideoLAN\\VLC media player.lnk') # File location
return
else:
PiSays("Application not available")
return
# 11. Calucates simple mathematical expressions (basic calculations like a calculator); preceeded with 'pi' in UserCommand
# 12. Answers any basic general question; preceeded with 'pi' in UserCommand
elif "pi" in UserCommand:
app_id = "5A773V-U2P37TE6VH" # wolframalpha app_id
client = wolframalpha.Client(app_id)
indx = UserCommand.lower().split().index('pi')
query = UserCommand.split()[indx + 1:]
res = client.query(' '.join(query))
answer = next(res.results).text
print("The answer is " + answer)
PiSays("The answer is " + answer)
return
# 13. Returns some of the top movies to watch from IMDB, using web-scraping
elif "watch" or "movie" in UserCommand:
url = "https://www.imdb.com/chart/moviemeter/?ref_=nv_mv_mpm" # Website URL address used for web-scraping
req = requests.get(url)
bsObj = BeautifulSoup(req.text, "html.parser")
data = bsObj.find_all("<a>")
print('Give me Moment Ps, I am Scraping data for you')
PiSays("Give me Moment Ps, I am Scraping data for you")
print("Total Cases: ", data[0].text.strip())
PiSays("Total Cases: " + data[0].text.strip())
print("Total Deaths: ", data[1].text.strip())
PiSays("Total Deaths: " + data[1].text.strip())
print("Total Recovered: ", data[2].text.strip())
PiSays("Total Recovered: " + data[2].text.strip())
# 14. Shut's down Voice Assistant
elif "shut" or "down" in UserCommand:
print('Bye Bye Ps, Have a good day')
PiSays('Bye Bye Ps, Have a good day')
sys.exit()
except:
print('I am Sorry I Could not Find any Match Can You Repeat your UserCommand')
PiSays("I am Sorry I Could not Understand You, Could You Please Repeat Your UserCommand")
# Calling The SelectTask() Function with RecognizeSpeech() as its argument, Resulting in Infinite loop
while True:
SelectTask(RecognizeSpeech())