-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualAssistant.py
220 lines (179 loc) · 7.36 KB
/
VirtualAssistant.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
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import webbrowser
import os
import time
import subprocess
from ecapture import ecapture as ec
import wolframalpha
import json
import requests
#Initiating Speech Engine
engine = pyttsx3.init('nsss')
voices = engine.getProperty('voices')
engine.setProperty('voice','voice[1].id')
#Converting Text to Speech
def speak(text):
engine.say(text)
engine.runAndWait()
#Welcome Message
def greetMe():
hour = datetime.datetime.now().hour
if hour >= 0 and hour < 12:
speak("Hello, Good Morning")
print("Hello, Good Morning")
elif hour >= 12 and hour < 18:
speak("Hello, Good Afternoon")
print("Hello, Good Afternoon")
else:
speak("Hello, Good Evening")
print("Hello, Good Evening")
#Command Function
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Go Ahead, I'm Listening...")
audio = r.listen(source)
try:
statement = r.recognize_google(audio,language = 'en-in')
print(f"user said:{statement}\n")
except Exception as e:
speak("Sorry, I can't hear that")
return "None"
return statement
print("Loading Your Personal Assistant Mike")
speak("Loading Your Personal Assistant Mike")
greetMe()
#Main Function
if __name__=='__main__':
while True:
speak("Hi, how can I help you ?")
statement = takeCommand().lower()
if statement == 0:
continue
if "good bye" in statement or "ok bye" in statement or "stop" in statement:
speak('Until we meet again, Take care')
print('Until we meet again, Take care')
break
if 'wikipedia' in statement:
speak('Searching Wikipedia....')
statement = statement.replace("wikipedia", "")
results = wikipedia.summary(statement, sentences = 3)
speak("According to wikipedia")
print(results)
speak(results)
elif 'open youtube' in statement:
webbrowser.open_new_tab("https://www.youtube.com")
speak("Opening Youtube")
time.sleep(5)
elif 'open google' in statement:
webbrowser.open_new_tab("https://www.google.com")
speak("Google open now")
time.sleep(5)
elif 'open gmail' in statement:
webbrowser.open_new_tab("gmail.com")
speak("Opening Google Mail")
time.sleep(5)
elif 'open twitter' in statement:
webbrowser.open_new_tab("https://twitter.com/login")
speak("Opening Twitter")
time.sleep(5)
elif 'open amazon' in statement:
webbrowser.open_new_tab("https://www.amazon.in/")
speak("Opening Amazon")
time.sleep(5)
elif 'open netflix' in statement:
webbrowser.open_new_tab("https://www.netflix.com/")
speak("Opening Netflix")
time.sleep(5)
elif 'open yahoo' in statement:
webbrowser.open_new_tab("https://in.yahoo.com/")
speak("Opening Yahoo")
time.sleep(5)
elif 'open whatsapp' in statement:
webbrowser.open_new_tab("https://www.whatsapp.com/")
speak("Opening Whatsapp")
time.sleep(5)
elif 'open reddit' in statement:
webbrowser.open_new_tab("https://www.reddit.com/")
speak("Opening Reddit")
time.sleep(5)
elif 'open linkedin' in statement:
webbrowser.open_new_tab("https://www.linkedin.com/signup")
speak("Opening Linkeedin")
time.sleep(5)
elif 'open github' in statement:
webbrowser.open_new_tab("https://github.com/")
speak("Opening Github")
time.sleep(5)
elif "weather" in statement:
api_key = "8ef61edcf1c576d65d836254e11ea420"
base_url = "https://api.openweathermap.org/data/2.5/weather?"
speak("what's the city name")
city_name = takeCommand()
complete_url = base_url+"appid="+api_key+"&q="+city_name
response = requests.get(complete_url)
x=response.json()
if x["cod"] != "404":
y=x["main"]
current_temperature = y["temp"]
current_humidiy = y["humidity"]
z = x["weather"]
weather_description = z[0]["description"]
speak(" Temperature in kelvin unit is " +
str(current_temperature) +
"\n humidity in percentage is " +
str(current_humidiy) +
"\n description " +
str(weather_description))
print(" Temperature in kelvin unit = " +
str(current_temperature) +
"\n humidity (in percentage) = " +
str(current_humidiy) +
"\n description = " +
str(weather_description))
else:
speak(" Unable to find the city ")
elif 'time' in statement:
strTime=datetime.datetime.now().strftime("%H:%M:%S")
speak(f"the time is {strTime}")
elif 'who are you' in statement or 'what can you do' in statement:
speak('I am Mike your persoanl assistant. I am programmed to minor tasks like'
'opening youtube,google chrome,gmail and stackoverflow ,predict time,take a photo,search wikipedia,predict weather'
'in different cities , get top headline news from times of india and you can ask me computational or geographical questions too!')
elif "who made you" in statement or "who created you" in statement or "who discovered you" in statement:
speak("I was developed by Prashanth")
print("I was developed by Prashanth")
elif "open stackoverflow" in statement:
webbrowser.open_new_tab("https://stackoverflow.com/login")
speak("Here is stackoverflow")
elif 'news' in statement:
news = webbrowser.open_new_tab("https://timesofindia.indiatimes.com/home/headlines")
speak('Here are some headlines from the Times of India,Happy reading')
time.sleep(6)
elif "camera" in statement or "take a photo" in statement:
ec.capture(0,"robo camera","img.jpg")
elif 'search' in statement:
statement = statement.replace("search", "")
webbrowser.open_new_tab(statement)
time.sleep(5)
elif 'ask' in statement:
speak('I can answer to computational and geographical questions and what question do you want to ask now')
question=takeCommand()
app_id="54H9GA-27Y2U8444T"
client = wolframalpha.Client('54H9GA-27Y2U8444T')
res = client.query(question)
answer = next(res.results).text
speak(answer)
print(answer)
elif "log off" in statement or "sign out" in statement:
speak("Ok , your pc will log off in 10 sec make sure you exit from all applications")
subprocess.call(["shutdown", "/l"])
time.sleep(3)