-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (87 loc) · 3.56 KB
/
main.py
File metadata and controls
114 lines (87 loc) · 3.56 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
import os
import pyttsx3#librabry for text to speech
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import pywhatkit #librabry for youtube
engine = pyttsx3.init('espeak') #for windows use 'sapi5' and voice id 0(M) or 1(F)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices)
engine.setProperty("rate", 170) #voice speed
def speak(audio):
engine.say(audio)
engine.runAndWait()
def greet():
#upon starting it will greet you.
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak("Good Morning!")
elif hour>=12 and hour<18:
speak("Good Afternoon!")
else:
speak("Good Evening!")
speak("How may I help you?")
def tellDay():
#This function is for telling the day of the week
day = datetime.datetime.today().weekday() + 1
Day_dict = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday',
5: 'Friday', 6: 'Saturday', 7: 'Sunday'}
if day in Day_dict.keys():
day_of_the_week = Day_dict[day]
print(day_of_the_week)
speak("The day is " + day_of_the_week)
def command():
#It takes microphone input from the user and returns string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...") #prints listening so that we'll know its listening
r.pause_threshold = 1
r.energy_threshold = 300
voice = r.listen(source, 0, 5) #for 0 to 5 sec it will take the query
try:
print("Recognizing...") #prints recognizing so that we'll know its processing
query = r.recognize_google(voice, language='en-in') #you can change language accordingly
print(f"You said: {query}\n")
except Exception as e:
speak("Pardon me master, I don't get you. Could you please repeat?") #when it does not understand the query
return "None"
return query
if __name__ == "__main__":
greet()
while True:
query = command().lower() #most queries match to lower case.
# Logic for executing tasks based on queries
if 'open google' in query:
speak("opening google")
webbrowser.open("google.com")
continue
elif 'what day' in query:
tellDay()
continue
elif 'tell me time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"master, the time is {strTime}")
continue
elif 'who are you' in query:
speak("I am Alfred. Your desktop assistant.")
elif 'play music' in query:
os.system("rhythmbox-client --play") #linux command for playing music
elif 'stop' in query:
speak("very well master. Should you need my help, I am at your service.")
exit()
elif 'youtube' in query:
query = query.replace("youtube", "")
print('playing' + query + 'On Youtube')
speak('playing' + query + 'On Youtube')
pywhatkit.playonyt(query)
elif 'recite quran' in query: #plays quran recitations videos from youtube
speak("Excellent choice Master.")
webbrowser.open("https://www.youtube.com/watch?v=UDvh63xHVa0&list=PLoqNzfHlA__knCeUoKUHjQfZpUL6mj64w")
elif 'wikipedia' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=4) # prints & speaks 4 sentences
speak("According to Wikipedia")
print(results)
speak(results)