-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
109 lines (94 loc) · 3.02 KB
/
main.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
import speech_recognition as sr
import pyttsx3 as t2s
import pywhatkit as pwk
import datetime
import wikipedia as wiki
import pyjokes as joke
listener = sr.Recognizer()
engine = t2s.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
# function to play voice and print
def talk(text):
print(text)
engine.say(text)
engine.runAndWait()
# function to take(listen) command
def takeCmd():
try:
with sr.Microphone() as src:
listener.pause_threshold = 1
listener.adjust_for_ambient_noise(src)
voice = listener.listen(src)
cmd = listener.recognize_google(voice)
if 'jenny' in cmd:
cmd.replace('jenny', '')
cmd = cmd.lower()
print(cmd)
except:
# pass
talk("Sorry, I can't understand!")
# print('Listening...')
# run_Jenny()
return 0
return cmd
# function to execute the command
def run_Jenny():
command = takeCmd()
if command == 0:
return
if 'play' in command:
song = command[command.find('play')+4:]
# song = command.replace('play', '')
talk('Playing' + song + '...')
pwk.playonyt(song)
elif ('who are you' in command) or ('what are you' in command):
talk("I'am Jenny, your personal Assisstant..!")
elif ('tell me a joke' in command) or ('give me a joke' in command):
talk(joke.get_joke())
elif ('the time' in command):
time = datetime.datetime.now().strftime('%I:%M %p')
talk("Current Time is: " + time)
elif ('date' in command) and ('what' in command):
date = datetime.date.today()
talk(date)
elif 'what' in command:
search = command[command.find('what')+8:]
# print(search)
res = wiki.summary(search, 2)
talk(res)
elif 'who' in command:
search = command[command.find('who')+7:]
# print(search)
res = wiki.summary(search, 2)
talk(res)
elif 'find' in command:
search = command[command.find('find')+5:]
# print(search)
res = wiki.summary(search, 2)
talk(res)
elif ('power off' in command) or ('shutdown' in command) or ('shut down' in command):
talk('Shutting Down..., Good Bye!')
exit()
else:
talk("Sorry, I can't understand!")
# print('Listening...')
# functoin for initial greeting
def greet():
hrs = int(datetime.datetime.now().hour)
if hrs < 4 and hrs >= 0:
greeting = 'Good Night!,'
if hrs >= 4 and hrs < 12:
greeting = 'Good Morning!,'
elif hrs >= 12 and hrs > 17:
greeting = 'Good Afternoon!,'
else:
greeting = 'Good Evning!,'
talk(greeting + " I'am Jenny your voice assistant!")
talk("What can I do for you?")
# print('Listening...')
if __name__ == "__main__":
greet()
while 1:
print('Listining...')
run_Jenny()