/
voice assistant.py
217 lines (167 loc) · 8.25 KB
/
voice assistant.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
from __future__ import unicode_literals
import pyttsx3
import webbrowser
import smtplib
import random
import speech_recognition as sr
import wikipedia
import datetime
import wolframalpha
import os
import sys
import youtube_dl
import urllib
import shutil
from pathlib import Path
import requests
import time
engine = pyttsx3.init()
'''len(voices)-1'''
engine=pyttsx3.init('sapi5')
voices=engine.getProperty('voices')
engine.setProperty('voice','voices[0].id')
def speak(audio):
print('Chitty ' + audio)
engine.say(audio)
engine.runAndWait()
def greetMe():
currentH = int(datetime.datetime.now().hour)
if currentH >= 10 and currentH < 3:
speak('Hi Pavi,Good Morning! The time is',currentH)
if currentH >= 3 and currentH < 15:
speak('Hi Pavi,Good Afternoon!')
if currentH >= 15 and currentH !=19:
speak('Hi Pavi,Good Evening!')
if currentH >= 19 and currentH !=10:
speak('Good Night Pavi! It is night...Sweet Dreams!!')
speak ('May i help you?')
greetMe()
speak('I am your assistant Chitty ,How may I help you?')
def myCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
r.pause_threshold = 0.5
try:
query = r.recognize_google(audio).lower()
print('User: ' + query + '\n')
except sr.UnknownValueError:
speak('Sorry Pavi! TRY Writing your command Pavi')
query = str(input('Command: '))
return query
if __name__ == '__main__':
while True:
query = myCommand();
query = query.lower()
if 'open youtube' in query:
speak('okay')
webbrowser.open('www.youtube.com')
elif 'open google' in query:
speak('okay')
webbrowser.open('www.google.co.in')
elif 'open gmail' in query:
speak('okay')
webbrowser.open('www.gmail.com')
elif "what\'s up" in query or 'how are you' in query:
stMsgs = ['Just doing my thing!', 'I am fine!', 'Nice!', 'I am nice and full of energy']
speak(random.choice(stMsgs))
elif 'email' in query:
speak('Who is the recipient? ')
recipient = myCommand()
if 'me' in recipient:
try:
speak('What should I say? ')
content = myCommand()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login("Your_Username", 'Your_Password')
server.sendmail('Your_Username', "Recipient_Username", content)
server.close()
speak('Email sent!')
except:
speak('Sorry Pavi! I am unable to send your message at this moment!')
elif 'nothing' in query or 'abort' in query or 'stop' in query or 'bye' in query:
speak('okay')
speak('Bye Pavi, have a good day.')
sys.exit()
elif 'what time is it' in query or 'what is the time Chitty' in query or 'what time is it' in query or 'time' in query or 'current time' in query:
speak('The current time is '+(str(datetime.datetime.now().strftime('%H:%M:%S'))))
elif 'what date is it' in query or 'what is the date Chitty' in query or 'what date is it' in query or 'date' in query or 'current date' in query:
speak('The current time is '+(str(datetime.datetime.now().strftime('%H:%M:%S'))))
elif 'hello' in query:
speak('Hello Pavi')
elif 'play music' in query or 'play songs' in query or 'play songs Chitty' in query:
music_folder = 'S:\\AI\\music\\'
music = ['Alex_Sparrow-sheiscrazybutsheismine','wedont','Konjam','stressedout']
random_music = music_folder + random.choice(music) + '.mp3'
speak('Okay, here is your music! Enjoy!')
os.system(random_music)
elif 'play english music' in query or 'play english songs' in query or 'play english songs Chitty' in query:
music_folder = 'S:\\AI\\music\\'
music = ['Alex_Sparrow-sheiscrazybutsheismine','wedont','Konjam','stressedout']
random_music = music_folder + random.choice(music) + '.mp3'
speak('Okay, here is your music! Enjoy!')
os.system(random_music)
elif 'play tamil music' in query or 'play tamil songs' in query or 'play tamil songs skava' in query:
music_folder = 'S:\\AI\\music\\'
music = ['Konjam']
random_music = music_folder + random.choice(music) + '.mp3'
speak('Okay, here is your music! Enjoy!')
os.system(random_music)
elif 'play hindi music' in query or 'play hindi songs' in query or 'play hindi songs skava' in query:
music_folder = 'S:\\AI\\music\\'
music = ['ikvaariaa']
random_music = music_folder + random.choice(music) + '.mp3'
speak('Okay, here is your music! Enjoy!')
os.system(random_music)
elif 'download youtube' in query:
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([input()])
elif 'download pdf' in query:
filename = Path('DAA.pdf')
url = input("Enter the link")
res= requests.get(url)
filename.write_bytes(res.content)
elif "log off" in query or "sign out" in query:
speak("Ok , your pc will log off in 10 sec make sure you exit from all applications")
subprocess.call(["shutdown", "/l"])
elif "Tell me motivation Quotes" in query or 'motivate me' or 'say motivational quotes' or 'sad' or 'unhappy' in query:
stMsgs = ['Failure will never overtake me if my determination to succeed is strong enough',
'The past cannot be changed. The future is yet in your power',
'Only I can change my life. No one can do it for me',
'Change your life today. Don\'t gamble on the future, act now, without delay',
'Do the difficult things while they are easy and do the great things while they are small. A journey of a thousand miles must begin with a single step',
'Either I will find a way, or I will make one',
'Our greatest weakness lies in giving up. The most certain way to succeed is always to try just one more time',
'Good, better, best. Never let it rest. Till your good is better and your better is best']
highMsgs = ['Dont worry dude,every hard time comes to an end']
speak(random.choice(stMsgs))
speak('I think thins Motivated You Pavi ... if Not')
speak(random.choice(highMsgs))
speak('You will be happy in few minutes watch this')
webbrowser.open('https://www.youtube.com/watch?v=20p6inVwjbI')
time.sleep(60)
speak('I think you cool now!!')
else:
query = query
speak('Searching...')
try:
try:
res = client.query(query)
results = next(res.results).text
speak('google says - ')
speak('Got it.')
speak(results)
except:
results = wikipedia.summary(query, sentences=2)
speak('Got it.')
speak('WIKIPEDIA says - ')
speak(results)
except:
webbrowser.open('www.google.com')
speak('any Command! Pavi!')