-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesktopAssistant.py
252 lines (179 loc) · 7.12 KB
/
desktopAssistant.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import os
import pyttsx3 as tts
import speech_recognition as sr
from selenium import webdriver
import datetime
import psutil
import pyjokes
dateTime = datetime.datetime.now()
day = dateTime.strftime("%A")
month = dateTime.strftime("%B")
date = dateTime.strftime("%d")
year = dateTime.strftime("%Y")
hour = dateTime.strftime("%H")
min = dateTime.strftime("%M")
engine = tts.init()
def assistant_speak(output) :
engine.say(output)
engine.runAndWait()
def greetings():
h = int(datetime.datetime.now().hour)
if h >= 0 and h < 12:
assistant_speak("Good Morning sir.")
elif h>= 12 and h< 18:
assistant_speak("Good afternoon sir")
else:
assistant_speak("Good night sir")
def get_audio():
recognizer = sr.Recognizer()
audio = ''
with sr.Microphone() as source:
print('Recognizing..')
recognizer.pause_threshold = 1
audio = recognizer.listen(source)
print('Stop')
try:
text = recognizer.recognize_google(audio, language= 'en-US')
print('You : ', text)
return text
except:
assistant_speak("Sorry sir, Could not recognize your voice")
get_audio()
return "None"
# close app
def close_app(app_name) :
running_apps = psutil.process_iter(['pid', 'name']) # returns names of running processes
found = False
for app in running_apps :
sys_app = app.info.get('name').split('.')[0].lower()
if sys_app in app_name.split() or app_name in sys_app :
pid = app.info.get('pid') # returns PID of the given app if found running
try :
app_pid = psutil.Process(pid)
app_pid.terminate()
found = True
except :
pass
else :
pass
if not found :
assistant_speak(app_name + "not found running")
else :
assistant_speak(app_name + "closed")
def openApp(text):
text = text.lower()
if 'open chrome' in str(text) or 'open google' in str(text) or 'google chrome' in str(text):
assistant_speak('Opening Google Chrome')
os.startfile('C:\Program Files\Google\Chrome\Application\chrome.exe')
return
elif 'open file' in str(text) or 'files' in str(text).lower() or 'file' in str(text):
assistant_speak('Opening file explorer')
os.startfile("C:\Windows\explorer.exe")
return
elif 'open youtube' in str(text) or 'youtube' in str(text).lower():
assistant_speak('Opening Youtube')
os.startfile("C:\wssistantApps\YouTube.lnk")
return
elif 'gmail' in str(text) or 'email' in str(text) or 'my email' in str(text) or 'gmail' in str(text) :
assistant_speak('Opening Gmail')
os.startfile("C:\wssistantApps\Gmail.lnk")
return
elif 'vscode' in str(text) or 'visual studio' in str(text) or 'visual studio code' in str(text) or 'code' in str(text) :
assistant_speak('Opening Visual studio code')
os.startfile("C:\Program Files\Microsoft VS Code\Code.exe")
return
elif 'terminal' in str(text) or 'cmd' in str(text) or 'black screen' in str(text) or 'command prompt' in str(text):
os.startfile("C:\wssistantApps\Command Prompt.lnk")
assistant_speak('Opening Command Prompt')
return
elif 'telegram' in str(text) or 'tele' in str(text) or 'open telegram' in str(text):
os.startfile("C:\wssistantApps\Telegram.lnk")
assistant_speak('Opening Telegram')
return
else:
assistant_speak('Cannot find that application')
return
def closeApp(text):
text = text.lower()
if "chrome" in text or "google" in text:
assistant_speak("Closing Chrome")
close_app("chrome")
return
elif "spotify" in text:
assistant_speak("Closing Spotify")
close_app("Spotify)
return
elif "youtube" in text or "tube" in text :
assistant_speak("Closing Youtube")
close_app("youtube")
return
elif "telegram" in text or "tele" in text :
assistant_speak("Closing Telegram")
close_app("telegram")
return
elif "terminal" in text or "cmd" in text :
assistant_speak("Closing command prompt")
close_app("command prompt")
return
elif "pycharm" in text or "pycharm" in text :
assistant_speak("Closing Pycharm")
close_app("pycharm")
return
elif "vscode" in text or "visual studio" in text :
assistant_speak("Closing visual studio code")
close_app("visual studio code")
return
def jokes():
jokes = pyjokes.get_joke(language= 'en', category= 'neutral')
assistant_speak(jokes)
assistant_speak("How is that need another one")
def process_text(text):
try:
if 'search' in text or 'play' in text:
pass
return
elif "who are you" in text or "define yourself" in text:
about = '''Hello i am Jarvis, Iam your personal assistant created by indrajith,
I can help you with whatever you ask or to do.'''
assistant_speak(about)
return
elif 'how are you' in text:
assistant_speak("I am fine, Thank you")
assistant_speak("How are you, Sir")
elif 'fine' in text or "good" in text:
assistant_speak("It's good to know that your fine")
elif "what's the time" in text or "time" in text:
assistant_speak(hour + min)
return
elif "what's the date today" in text or "date" in text:
assistant_speak(date + month)
return
elif "date and year" in text or "year" in text:
assistant_speak(day + date + month + year)
return
elif "tell me a joke" in text or "jokes" in text or "joke" in text or 'tell joke' in text or "make me laugh" in text:
assistant_speak("Ok joke coming")
jokes()
return
elif "open" in text:
openApp(text)
return
elif "close" in text or "stop" in text or "kill" in text or "exit" in text:
closeApp(text)
return
except :
assistant_speak("I don't understand, I can search the web for you, Do you want to continue?")
if __name__ == "__main__" :
greetings()
text = get_audio()
if "hey jarvis" in str(text).lower() or "jarvis" in str(text).lower() or "hello jarvis" in str(text).lower() or "hello" in text.lower() or "hi" in text.lower() or "hey" in text.lower():
assistant_speak("yes sir")
assistant_speak("What can i do for you?")
while (1) :
text = get_audio()
if text == 0 :
continue
if "bye" in str(text).lower() or "sleep" in str(text).lower() :
assistant_speak("See you later sir, take care.")
break
process_text(text)