-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_assistant.py
More file actions
177 lines (153 loc) · 5.65 KB
/
virtual_assistant.py
File metadata and controls
177 lines (153 loc) · 5.65 KB
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
import speech_recognition as sr
import os
from gtts import gTTS
import datetime
import warnings
import calendar
import pyaudio
from playsound import playsound
import random
import webbrowser
import wolframalpha
'''ignore any warnings'''
warnings.filterwarnings('ignore')
'''wolframalpha'''
APP_ID = ''
def assistanceResponse(text) :
'''convert text to speach'''
output = gTTS(text=text, lang='en', slow=False)
'''save the file'''
output.save('response.mp3')
'''play the converted file'''
playsound('response.mp3')
os.remove('response.mp3')
'''record audio and return text'''
def recordAudio(ask = False) :
'''record the audio'''
#r = sr.Recognizer()
'''creating a recognizer object'''
'''open mic and start recording'''
r.adjust_for_ambient_noise(source, duration=0.5)
if ask :
assistanceResponse(ask)
else:
assistanceResponse('how can I help you?')
print('Listening...')
audio = r.listen(source,phrase_time_limit= 5)
'''use googles speech rocognition'''
input_text = ''
try :
input_text = r.recognize_google(audio)
print('You said : ' + input_text)
except sr.UnknownValueError :
'''Check for unknown errors'''
print('Google speech recognition could not understand your audio, unknown error.')
assistanceResponse('sorry, I did not get that.')
except sr.RequestError as e :
print('Request results from google speech recognition error : ' + e)
assistanceResponse('sorry, my services seems to be down at the moment.')
return input_text.lower()
class assistFunctions :
'''Functions of the AI'''
def __init__(self,AudioText):
self.AudioText = AudioText
def nameCheck(self) :
responses = ['What do you care?','My name is Eera']
rand = random.randint(0,len(responses)-1)
assistanceResponse(responses[rand])
def showDate(self) :
x = datetime.datetime.now()
print(x.strftime("%A, %m/%d/%y"))
response = x.strftime("%A %d %B %Y")
assistanceResponse("Today is " + response)
def showTime(self) :
current_time = datetime.datetime.now().strftime("%H:%M")
current_time = current_time.split(':')
if int(current_time[0]) < 12 :
current_time = f'{current_time[0]}:{current_time[1]} AM'
elif int(current_time[0]) == 12 :
current_time = f'{current_time[0]}:{current_time[1]} PM'
else :
current_time = f'{int(current_time[0])-12}:{current_time[1]} PM'
print(current_time)
assistanceResponse('It is currently ' + current_time)
def search(self) :
search = recordAudio('what would you like to search for?')
url = 'https://google.com/search?q=' + search
webbrowser.open_new_tab(url)
assistanceResponse('showing results for ' + search)
def playYoutube(self) :
search = recordAudio('what would you like to play on youtube?')
url = 'https://www.youtube.com/results?search_query=' + search
webbrowser.open_new_tab(url)
assistanceResponse('showing results for ' + search)
def makeNote(self) :
text = recordAudio('What should I write?')
now = datetime.datetime.now()
currentDate = str(now.month) + "-" + str(now.day) + "_" + str(now.hour) + "-" + str(now.minute) + "-" + str(now.second)
file_path = 'note-{}.txt'.format(currentDate)
file = open(file_path, 'w')
file.write(text)
file.close()
assistanceResponse("Noted. Here it is.")
os.system(file_path)
def wolf(self) :
client = wolframalpha.Client(APP_ID)
res = client.query(self.AudioText)
answer = next(res.results).text
print(answer)
assistanceResponse(answer)
def runFunction(self) :
if 'your name' in self.AudioText :
self.nameCheck()
return
'''
for phrase in ["what's the time", 'what time is it','tell me the time','give me the time'] :
if phrase in self.AudioText :
self.showTime()
return
'''
for phrase in ["what's the date", 'what day is it', 'tell me the date',"today's date"]:
if phrase in self.AudioText:
self.showDate()
return
if 'search' in self.AudioText :
self.search()
return
for phrase in ['play on youtube','open in youtube','search on youtube'] :
if phrase in self.AudioText :
self.playYoutube()
return
for phrase in ['make note of','take a note','make a note'] :
if phrase in self.AudioText :
self.makeNote()
return
self.wolf()
currentPath = os.path.abspath(__file__).rstrip('virtual_assistant.py')
'''MAIN'''
assistanceResponse('Starting up.')
playsound(currentPath + 'PowerUp - Sound Effects.mp3')
assistanceResponse('System online.')
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 0.5
r.adjust_for_ambient_noise(source)
while True :
print('Listening...')
audio = r.listen(source,timeout=None, phrase_time_limit=3)
try:
wake_text = r.recognize_google(audio)
print(wake_text)
except:
print('exception')
continue
if 'hey' in wake_text:
textAudio = recordAudio()
main = assistFunctions(textAudio)
main.runFunction()
if 'exit' in wake_text :
assistanceResponse('Shutting down.')
playsound(currentPath + 'Shut-down-sound-effect.mp3')
break
print('next loop')
print('end')