-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassistant.py
166 lines (136 loc) Β· 5.75 KB
/
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
import pyttsx3
import datetime
import speech_recognition as speech
import time
import games
import wikipedia
import webbrowser
import os
import random
from functools import lru_cache
from Assets.webcam.webcam_openCv import __open__webcam
random_index = random.randint(1, 50)
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)
engine.setProperty("rate", 160)
@lru_cache(maxsize=30)
def speakFunc(audio):
engine.say(audio)
engine.runAndWait()
lru_cache(maxsize=100)
def give_your_command():
"""
Takes the input from the user device microphone
and convert it to a string and perform the
action according to the user command.
Just like a Google voice assistant. The Voice command
sdk is used from `google voice assistants`.
"""
recognise = speech.Recognizer() # Initialize the recognizer
with speech.Microphone() as source: # used microphone as the source from the user
print("Listening...")
recognise.pause_threshold = 1
recognise.energy_threshold = 400
audio_get = recognise.listen(source)
try: # Tries to recognise the user input from the microphone
print("Recognising...")
query = recognise.recognize_google(audio_get, language="en-in")
print(f"user said : {query} ")
except Exception as e: # If the module can't recognise the user input then this exception run
print("Say that again!")
print(e)
return "none"
# else: If the user didn't do any input then it will throw the else exception with a mess.
# print("Sorry! I didn't get anything")
return query # returns the query which was said by user
def wishMe():
"""
Wishes the user from the current time stamp according to the written given logics.
"""
current_time = int(datetime.datetime.now().hour) # converted the date time into integer
if 0 <= current_time < 12: # If time is under 12 PM
speakFunc("Good morning! boss")
elif current_time >= 12 and current_time < 12: # If time is Over 12 PM and smaller 12 AM ( 12hr Timeframe )
speakFunc("Good afternoon! boss")
else: # returns the evening.
speakFunc("Good evening! boss")
speakFunc("I'm Friday now i'm initialised, Let's begin, ")
def user_to_choice():
user_wish_statement = give_your_command().lower()
if "close" in user_wish_statement:
speakFunc("I'm exiting from program good bye")
time.sleep(5)
exit()
elif "i love you" in user_wish_statement:
speakFunc("I love you too! .")
exit()
elif "wiki" in user_wish_statement:
speakFunc("Wikiguy module is in development")
elif "load game" in user_wish_statement:
speakFunc("Which game would you want to play, ")
"""
I done changes in this block, if program fails to run then you
have to check in this block. because, it could be an error in
this block .
"""
if "sake water gun" in user_wish_statement:
games.snake_water_gun()
speakFunc("I'll loaded up snake water gun game for you. Enjoy boss")
elif "number game" in user_wish_statement:
games.choose_number_game()
speakFunc("I'll loaded up number game for you. Enjoy boss.")
# games.choose_number_game()
# time.sleep(2)
# exit()
else:
speakFunc("I didn't recognise anything properly")
time.sleep(1)
speakFunc("Speak again!")
# * give_your_command()
if __name__ == "__main__":
print(__name__)
wishMe()
while True:
# query_string = give_your_command().lower()
query_string = input("enter your request : ")
if "wikipedia" in query_string:
speakFunc("Searching wikipedia...")
query_str = query_string.replace("wikipedia", "")
results = wikipedia.summary(query_str, sentences=2)
speakFunc("According to wikipedia...")
print(results)
speakFunc(f"{results} That's which i got from wiki")
break
elif "open my friend profile" in query_string:
webbrowser.open_new_tab("https://instagram.com")
speakFunc("Opening your friend profile boss")
elif "shutdown" in query_string:
speakFunc("I'm going to sleep now")
break
elif "play music" in query_string:
music_directory = "O:\\About-me\\musics"
speakFunc("Playing music.")
music = os.listdir(music_directory)
print(music)
os.startfile(os.path.join(music_directory, music[random_index]))
elif "time" in query_string:
current_time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
speakFunc(f"Boss, the current time is : {current_time}")
elif "your god" in query_string:
speakFunc("My sir is my god who develops me")
elif "open browser computer" in query_string:
speakFunc("Opening browser computer for you boss")
webbrowser.open_new_tab("https://neverinstall.com")
elif "open camera" in query_string:
speakFunc("Opening webcam, please be patient")
__open__webcam()
elif "personal space" in query_string:
speakFunc("You are in now your personal space, what can i do for you now")
user_to_choice()
elif "news feed" in query_string:
speakFunc("Wait, i'm loading the news feed. Hang on!")
speakFunc("Under, Development")
elif "turn off" in query_string:
os.system("shutdown /s /t 1")
speakFunc("Ok then, I'm gonna shutting down your main host, See u soon")