-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxxxxx_voiceassistant.py
119 lines (89 loc) · 3.3 KB
/
xxxxx_voiceassistant.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
import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import pyjokes
import webbrowser
listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.setProperty('rate', 150)
def talk(text):
engine.say(text)
engine.runAndWait()
def take_command():
try:
with sr.Microphone() as source:
print('I am listening, Say something...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'alexa' in command:
command = command.replace('alexa', '')
return command
except sr.RequestError:
print('Sorry, my speech service is down')
talk('Sorry, my speech service is down')
except sr.UnknownValueError:
print('Sorry, I did not catch that')
talk('Sorry, I did not catch that')
return ""
def run_alexa():
command = take_command()
if not command:
return
print('Me:', command)
if 'play' in command:
song = command.replace('play', '')
talk('Playing' + song)
print('Playing' + song)
pywhatkit.playonyt(song)
elif 'search' in command:
search = command.replace('search', '')
url = 'https://google.com/search?q=' + search
webbrowser.get().open(url)
print('Here is what I found for ' + search)
talk('Here is what I found for ' + search)
elif 'map' in command:
location = command.replace('map', '')
url = 'https://google.nl/maps/place/' + location
webbrowser.get().open(url)
print('Here is the map of ' + location)
talk('Here is the map of ' + location)
elif 'time' in command:
time = datetime.datetime.now().strftime('%I:%M %p')
print(time)
talk('Current time is ' + time)
elif 'tell me about' in command:
ask = command.replace('tell me about', '')
try:
info = wikipedia.summary(ask, sentences=2)
print(info)
talk(info)
except wikipedia.exceptions.PageError:
print('Sorry, I could not find information on that topic.')
talk('Sorry, I could not find information on that topic.')
except wikipedia.exceptions.DisambiguationError as e:
print('There are multiple results for this topic. Please be more specific.')
talk('There are multiple results for this topic. Please be more specific.')
elif 'sex' in command:
talk('Hmm...yes we will do sex at night.')
elif 'joke' in command:
joke = pyjokes.get_joke()
print(joke)
talk(joke)
elif 'who are you' in command:
talk('I am Alexa, your 24/7 virtual assistant. If you are bored, I can tell you a joke, play YouTube videos for you, and much more.')
elif 'thank you' in command:
talk('You are welcome. Anything else you want me to do?')
elif 'goodbye' in command or 'exit' in command:
talk('Goodbye, Smarty. See you later, Alligator.')
exit()
else:
print('Sorry, I did not get that')
talk('Sorry, I did not get that')
print("sorry can you speak again")
while True:
run_alexa()