-
Notifications
You must be signed in to change notification settings - Fork 3
/
voice_assistant_sample.py
246 lines (203 loc) · 6.85 KB
/
voice_assistant_sample.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
import os
import sys
import speech_recognition as sr
import webbrowser
from time import strftime
import pyjokes
import subprocess
import datetime
import pyttsx3
import webbrowser
from urllib.request import urlopen
from bs4 import BeautifulSoup as soup
def bot(talk):
print(talk)
engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', 160)
volume = engine.getProperty('volume')
engine.setProperty('volume', 1.0)
sound = engine.getProperty('voices')
engine.setProperty('voice', sound[33].id)
for i in str(talk).splitlines():
engine.say(talk)
engine.runAndWait()
def listen():
mic = sr.Microphone()
r = sr.Recognizer()
with mic as source:
print("say something...")
audio = r.listen(source, phrase_time_limit=5)
try:
command = r.recognize_google(audio).lower()
print("You said : " + command)
# main(command)
except sr.UnknownValueError:
print("Error occured, try again")
bot("Sorry I did not get that. Please try again.")
command = listen()
return command
# def activate(talk):
# wake_words = {'ok siri'}
# talk = talk.lower()
# for phrase in wake_words:
# if phrase in talk:
# return True
# return False
def main(command):
if "hello" in command:
current_time = int(strftime('%H'))
if current_time < 12:
bot("Hello, Good morning, this is your voice assistant.")
elif 12 <= current_time < 16:
bot("Hello, Good afternoon, this is your voice assistant.")
else:
bot("Hello, Good evening, this is your voice assistant.")
elif "who made you" in command:
bot("I was developed by a team Skydocs.")
elif "how are you" in command:
bot("I am great. Hoping the same for you.")
elif "your name" in command:
bot("My name is Bella.")
elif "feature" in command:
bot("I have lot of features, Some of my features are given below:")
bot("Greetings")
bot("Play Video")
bot("Web Search")
bot("Give Latest News")
bot("Add Notes....why not try something and get started.")
elif "joke" in command:
bot(pyjokes.get_joke())
elif "google" in command:
webbrowser.open("https://www.google.com")
bot("Check your default web browser!")
elif 'time' in command:
now = datetime.datetime.now()
bot('Current time is %d hours %d minutes' % (now.hour, now.minute))
elif "play video" in command:
bot("What to play?")
q = sr.Recognizer()
t = 0
with sr.Microphone() as source:
print("Search for the term:")
while t == 0:
audio = q.listen(source, phrase_time_limit=5)
try:
query = q.recognize_google(audio)
print('you said :{}'.format(query))
t = 1
except:
print('Not understandable')
print('Try again')
t = 0
url = "https://www.youtube.com/results?search_query=" + query
webbrowser.open(url)
elif "shop" in command:
bot("what you want to shop?")
q = sr.Recognizer()
t = 0
with sr.Microphone() as source:
print("search for the term:")
while t == 0:
audio = q.listen(source, phrase_time_limit=5)
try:
query = q.recognize_google(audio)
print('you said :{}'.format(query))
bot('Here you go')
bot('Happy shoping!')
t = 1
except:
print('Not understandable')
print('Try again')
t = 0
url = "https://www.amazon.in/s?k=" + query
webbrowser.open(url)
elif "write note" in command:
bot("What should i write ?")
note = listen()
file = open('user.txt', 'w')
bot("Should i include date and time")
snfm = listen()
if 'yes' in snfm or 'sure' in snfm:
strTime = datetime.datetime.now().strftime("% H:% M:% S")
file.write(strTime)
file.write(" :- ")
file.write(note)
else:
file.write(note)
elif "show note" in command:
bot("Showing Notes")
file = open("user.txt", "r")
print(file.read())
bot(file.read(6))
elif "gmail" in command:
bot("sure, opening gmail")
url_mail = "https://www.gmail.com"
webbrowser.open(url_mail)
elif "wikipedia" in command:
bot("Sure! Here you go.")
url_wiki = "https://www.wikipedia.org/"
webbrowser.open(url_wiki)
elif "news" in command:
try:
news_url = "https://news.google.com/news/rss"
Client = urlopen(news_url)
xml_page = Client.read()
Client.close()
soup_page = soup(xml_page, "xml")
news_list = soup_page.findAll("item")
for news in news_list[:15]:
bot(news.title.text.encode('utf-8'))
except Exception as e:
print(e)
elif "map" in command:
bot("opening maps powered by google")
maps_url = "https://www.google.co.in/maps"
webbrowser.open(maps_url)
elif "shutdown" in command:
bot("You are going to poweroff your system. Are you sure?")
listen()
if "yes" in command:
os.system("poweroff")
else:
bot("You have aborted the process. Returning back to previous state")
main(listen())
# google search
elif 'search' in command:
bot('What to search?')
# listen()
w = sr.Recognizer()
t = 0
with sr.Microphone() as source:
print('Search for the term:')
# print(t)
while t == 0:
audio = w.listen(source, phrase_time_limit=5)
try:
# print('in try block')
query = w.recognize_google(audio).lower()
print('you said :{}'.format(query))
t = 1
except:
print('Not understandable')
print('Try again')
t = 0
webbrowser.open("https://google.com/search?q=%s" % query)
elif "remind" in command:
bot("What shall I remind you about?")
text = listen()
bot("In how many minutes ?")
local_time = float(listen())
local_time = local_time * 60
time.sleep(local_time)
bot(text)
elif "bye" in command:
bot("Bye!")
sys.exit()
elif "thank you" in command:
bot("Pleasure to serve you!")
sys.exit()
else:
bot("I am sorry, I am unable to process your request.")
while True:
main(listen())