Skip to content

Commit 4406b18

Browse files
committed
making the voice assistant usable for every one and still giving personalised results when prompted
1 parent d92ca6c commit 4406b18

File tree

2 files changed

+280
-1
lines changed

2 files changed

+280
-1
lines changed

predict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
1919

20-
DATASET_ROOT = "" #Path to dataset
20+
DATASET_ROOT = "/Users/harshitruwali/Desktop/sem3-pro/" #Path to dataset
2121
NOISE_SUBFOLDER = "noise"
2222
DATASET_NOISE_PATH = os.path.join(DATASET_ROOT, NOISE_SUBFOLDER)
2323

voice-assistant.py

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
import os
2+
clear = lambda: os.system('clear')
3+
clear()
4+
5+
print("\033[31m[*] General Voice Assistant triggered.\033[0m")
6+
7+
import sys
8+
import speech_recognition as sr
9+
import webbrowser
10+
from time import strftime
11+
import pyjokes
12+
import subprocess
13+
import datetime
14+
import pyttsx3
15+
import webbrowser
16+
from urllib.request import urlopen
17+
from bs4 import BeautifulSoup as soup
18+
from subprocess import call
19+
# import predict
20+
21+
def bot(talk):
22+
print(talk)
23+
24+
engine = pyttsx3.init()
25+
rate = engine.getProperty('rate')
26+
engine.setProperty('rate', 160)
27+
volume = engine.getProperty('volume')
28+
engine.setProperty('volume', 1.0)
29+
sound = engine.getProperty('voices')
30+
engine.setProperty('voice', sound[33].id)
31+
32+
for i in str(talk).splitlines():
33+
engine.say(talk)
34+
engine.runAndWait()
35+
36+
37+
def listen():
38+
mic = sr.Microphone()
39+
r = sr.Recognizer()
40+
with mic as source:
41+
print("\033[31m[*]\033[0m Say Something...")
42+
audio = r.listen(source, phrase_time_limit=5)
43+
44+
try:
45+
command = r.recognize_google(audio).lower()
46+
print("You said : " + command)
47+
# main(command)
48+
49+
except sr.UnknownValueError:
50+
# print("Error occured, try again")
51+
print("Sorry I did not get that. Please try again.")
52+
command = listen()
53+
54+
return command
55+
56+
# def activate(talk):
57+
# wake_words = {'ok siri'}
58+
59+
# talk = talk.lower()
60+
# for phrase in wake_words:
61+
# if phrase in talk:
62+
# return True
63+
# return False
64+
65+
66+
def main(command):
67+
if "hello" in command:
68+
current_time = int(strftime('%H'))
69+
if current_time < 12:
70+
bot("Hello, Good morning, this is your voice assistant.")
71+
elif 12 <= current_time < 16:
72+
bot("Hello, Good afternoon, this is your voice assistant.")
73+
else:
74+
bot("Hello, Good evening, this is your voice assistant.")
75+
76+
elif "who made you" in command:
77+
bot("I was developed by a team Skydocs.")
78+
79+
elif "how are you" in command:
80+
bot("I am great. Hoping the same for you.")
81+
82+
elif "your name" in command:
83+
bot("My name is Bella.")
84+
85+
elif "who am I?" in command:
86+
bot("I am the general user. I am not giving the presonalised voice assistant.")
87+
bot("To activate the presonalised voice assistant say RECOGNISE")
88+
89+
elif "feature" in command:
90+
bot("Say recognise to recognise the user and give presonalised requests")
91+
bot("I have lot of features, Some of my features are given below:")
92+
bot("Greetings")
93+
bot("Play Video")
94+
bot("Web Search")
95+
bot("Give Latest News")
96+
bot("Add Notes....why not try something and get started.")
97+
98+
elif "recognise" in command:
99+
bot("You will be redirected to the recognition part!")
100+
call(["python", "predict.py"])
101+
102+
elif "joke" in command:
103+
bot(pyjokes.get_joke())
104+
105+
elif "google" in command:
106+
webbrowser.open("https://www.google.com")
107+
bot("Check your default web browser!")
108+
109+
elif 'time' in command:
110+
now = datetime.datetime.now()
111+
bot('Current time is %d hours %d minutes' % (now.hour, now.minute))
112+
113+
elif "play video" in command:
114+
115+
bot("What to play?")
116+
q = sr.Recognizer()
117+
t = 0
118+
with sr.Microphone() as source:
119+
print("Search for the term:")
120+
while t == 0:
121+
audio = q.listen(source, phrase_time_limit=5)
122+
try:
123+
query = q.recognize_google(audio)
124+
print('you said :{}'.format(query))
125+
t = 1
126+
except:
127+
print('Not understandable')
128+
print('Try again')
129+
t = 0
130+
url = "https://www.youtube.com/results?search_query=" + query
131+
webbrowser.open(url)
132+
133+
elif "shop" in command:
134+
135+
bot("what you want to shop?")
136+
q = sr.Recognizer()
137+
t = 0
138+
with sr.Microphone() as source:
139+
print("search for the term:")
140+
while t == 0:
141+
audio = q.listen(source, phrase_time_limit=5)
142+
try:
143+
query = q.recognize_google(audio)
144+
print('you said :{}'.format(query))
145+
bot('Here you go')
146+
bot('Happy shoping!')
147+
t = 1
148+
except:
149+
print('Not understandable')
150+
print('Try again')
151+
t = 0
152+
url = "https://www.amazon.in/s?k=" + query
153+
webbrowser.open(url)
154+
155+
elif "write note" in command:
156+
bot("What should i write ?")
157+
note = listen()
158+
file = open('user.txt', 'w')
159+
bot("Should i include date and time")
160+
snfm = listen()
161+
if 'yes' in snfm or 'sure' in snfm:
162+
strTime = datetime.datetime.now().strftime("% H:% M:% S")
163+
file.write(strTime)
164+
file.write(" :- ")
165+
file.write(note)
166+
else:
167+
file.write(note)
168+
169+
elif "show notes" in command:
170+
bot("Searching for Notes")
171+
try:
172+
file = open("user.txt", "r")
173+
print(file.read())
174+
bot(file.read())
175+
except FileNotFoundError:
176+
bot("No notes are available.")
177+
bot("Want to create one now?")
178+
snfm = listen()
179+
if 'yes' in snfm or 'sure' in snfm:
180+
bot("What should i write ?")
181+
note = listen()
182+
file = open('user.txt', 'w')
183+
bot("Should i include date and time")
184+
185+
if 'yes' in snfm or 'sure' in snfm:
186+
strTime = datetime.datetime.now().strftime("% H:% M:% S")
187+
file.write(strTime)
188+
file.write(" :- ")
189+
file.write(note)
190+
else:
191+
file.write(note)
192+
193+
elif "gmail" in command:
194+
bot("sure, opening gmail")
195+
url_mail = "https://www.gmail.com"
196+
webbrowser.open(url_mail)
197+
198+
elif "wikipedia" in command:
199+
bot("Sure! Here you go.")
200+
url_wiki = "https://www.wikipedia.org/"
201+
webbrowser.open(url_wiki)
202+
203+
elif "news" in command:
204+
try:
205+
news_url = "https://news.google.com/news/rss"
206+
Client = urlopen(news_url)
207+
xml_page = Client.read()
208+
Client.close()
209+
soup_page = soup(xml_page, "xml")
210+
news_list = soup_page.findAll("item")
211+
for news in news_list[:15]:
212+
bot(news.title.text.encode('utf-8'))
213+
except Exception as e:
214+
print(e)
215+
216+
elif "map" in command:
217+
bot("opening maps powered by google")
218+
maps_url = "https://www.google.co.in/maps"
219+
webbrowser.open(maps_url)
220+
221+
elif "shutdown" in command:
222+
bot("You are going to poweroff your system. Are you sure?")
223+
listen()
224+
if "yes" in command:
225+
os.system("poweroff")
226+
else:
227+
bot("You have aborted the process. Returning back to previous state")
228+
main(listen())
229+
230+
# google search
231+
elif 'search' in command:
232+
bot('What to search?')
233+
# listen()
234+
235+
w = sr.Recognizer()
236+
t = 0
237+
238+
with sr.Microphone() as source:
239+
print('Search for the term:')
240+
# print(t)
241+
242+
while t == 0:
243+
audio = w.listen(source, phrase_time_limit=5)
244+
try:
245+
# print('in try block')
246+
query = w.recognize_google(audio).lower()
247+
print('you said :{}'.format(query))
248+
t = 1
249+
250+
except:
251+
print('Not understandable')
252+
print('Try again')
253+
t = 0
254+
255+
webbrowser.open("https://google.com/search?q=%s" % query)
256+
257+
elif "remind" in command:
258+
bot("What shall I remind you about?")
259+
text = listen()
260+
bot("In how many minutes ?")
261+
local_time = float(listen())
262+
local_time = local_time * 60
263+
time.sleep(local_time)
264+
bot(text)
265+
266+
elif "bye" in command:
267+
bot("Bye!")
268+
sys.exit()
269+
270+
elif "thank you" in command:
271+
bot("Pleasure to serve you!")
272+
sys.exit()
273+
274+
else:
275+
bot("I am sorry, I am unable to process your request.")
276+
277+
278+
while True:
279+
main(listen())

0 commit comments

Comments
 (0)