-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheve_v3.py
304 lines (273 loc) · 10.3 KB
/
eve_v3.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import speech_recognition as sr
from word2number import w2n
import pyttsx3
import webbrowser
import os, sys
import time
import subprocess
import pyautogui
import psutil
import win32gui
import win32process
import win32con
# Speech engine
r = sr.Recognizer()
r.energy_threshold = 10000
# window object to focus applications
w=win32gui
# map of application name to set of application process ids.
open_applications = {}
running = True
is_executing_commands = True
def main():
w.EnumWindows(updateOpenApplications, None)
greet_me()
global globalvar
globalvar = [1, 5]
r.listen_in_background(sr.Microphone(), takeAudio, phrase_time_limit=5)
global running
while running:
time.sleep(0.01)
# Converts text to speech
def speak(text):
# eve_v1.tts(text)
print(text)
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)
engine.setProperty("rate", 250)
engine.say(text)
engine.runAndWait()
del engine
# Greeting on start up
def greet_me():
if time.localtime().tm_hour >= 12:
if time.localtime().tm_hour <= 18:
greeting = "Good afternoon Adam,"
else:
greeting = "Good evening Adam,"
else:
greeting = "Good morning Adam,"
greeting += " how may I help you?"
speak("Hello, I am Eve, Adam's Digital Assistant.")
speak(greeting)
# Recreate mapping of open applications
def updateOpenApplications( hwnd, ctx ):
if w.IsWindowVisible( hwnd ):
pid = win32process.GetWindowThreadProcessId(hwnd)
name = psutil.Process(pid[-1]).name()
if name in open_applications:
curr_processes = open_applications[name]
curr_processes.add(hwnd)
else:
open_applications[name] = {hwnd}
def takeAudio(r, audio):
try:
global open_applications
global is_executing_commands
results = r.recognize_google(audio, language="en-in").lower()
print(results)
# if "hey eve" in results:
# start_index = results.index("hey eve")
# print(results[start_index+8:])
# results = results[start_index+8:]
if results == "start executing commands" and not is_executing_commands:
is_executing_commands = True
speak("I have resumed executing commands")
elif not is_executing_commands or results == "stop executing commands":
if is_executing_commands:
is_executing_commands = False
speak("I have stopped executing commands until you request for me to start again")
return
if results == "terminate":
speak("I have stopped listening and will shut down. Goodbye.")
global running
running = False
sys.exit()
os._exit(0)
elif results == "lock" or "lock computer" in results:
cmd = "rundll32.exe user32.dll, LockWorkStation"
subprocess.call(cmd)
elif results == "rerun":
speak("I am restarting, I'll be right back.")
os.execv(sys.executable, ["python"] + sys.argv)
elif "change window" in results:
pyautogui.keyDown("alt")
time.sleep(0.2)
if "cycle" in results:
pyautogui.keyDown("shift")
time.sleep(0.2)
results = results.replace("change window cycle","")
else:
results = results.replace("change window","")
if results != "":
try:
if "to" in results or "too" in results:
value = 2
else:
value = w2n.word_to_num(results)
except Exception as e:
value = int(results)
else:
value = 1
for i in range(value):
pyautogui.press("tab")
time.sleep(0.2)
pyautogui.keyUp("alt")
pyautogui.keyUp("shift")
elif "focus" in results:
focus_command(results)
elif "search" in results:
webbrowser.open_new_tab("https://www.google.com/search?q={}".format(results.replace("search", "")))
open_applications = {}
w.EnumWindows(updateOpenApplications, None)
elif "open" in results:
open_command(results)
elif "close tab" in results:
pid = win32process.GetWindowThreadProcessId(w.GetForegroundWindow())
if psutil.Process(pid[-1]).name() == "chrome.exe":
pyautogui.hotkey("ctrl","w")
speak("Closing tab")
else:
speak("A web application is not focused")
elif "toggle music" in results:
pyautogui.press("playpause")
elif "volume up" in results:
results = results.replace("volume up","")
if results != "":
try:
value = w2n.word_to_num(results)
except Exception as e:
value = int(results)
value = int(value/2)
else:
value = 1
for i in range(value):
pyautogui.press("volumeup")
elif "volume down" in results:
results = results.replace("volume down","")
if results != "":
try:
value = w2n.word_to_num(results)
except Exception as e:
value = int(results)
value = int(value/2)
else:
value = 1
for i in range(value):
pyautogui.press("volumedown")
elif "play next song" in results:
pyautogui.press("nexttrack")
elif "play previous song" in results:
pyautogui.press("prevtrack")
pyautogui.press("prevtrack")
elif "list commands" in results:
speak("Version 3 of me can perform the following actions: \n TERMINATE myself \n LOCK computer\n RERUN myself \n CHANGE WINDOW \n FOCUS <APPLICATION NAME> \n SEARCH the internet \n OPEN <APPLICATION NAME> \n TOGGLE MUSIC \n VOLUME UP or DOWN <AMOUNT> \n PLAY PREVIOUS or NEXT SONG \n LIST COMMANDS")
except Exception as e:
# global globalvar
# sys.stdout.write(
# "\r[{0}{1}] ".format(
# "=" * globalvar[0], " " * (globalvar[1] - globalvar[0])
# )
# )
# if globalvar[0] > globalvar[1]:
# sys.stdout.write("\r[{0}] ".format(" " * (globalvar[0] - 1)))
# globalvar[0] = 1
# else:
# globalvar[0] += 1
# sys.stdout.flush()
if is_executing_commands:
print("I'm listening...")
print(e)
def open_command(results):
global open_applications
count = 0
response = "Opening "
if " chrome" in results:
webbrowser.open_new_tab("https://google.com")
response += "chrome, "
count += 1
if " spotify" in results:
os.system("Spotify")
response += "spotify, "
count += 1
if " calendar" in results:
webbrowser.open_new_tab("https://calendar.google.com")
response += "calendar, "
count += 1
if " email" in results:
webbrowser.open_new_tab("https://gmail.google.com")
response += "email, "
count += 1
if " discord" in results:
os.startfile(
"C:/Users/adamw/AppData/Local/Discord/app-1.0.9002/Discord"
)
response += "discord, "
count += 1
if " valorant" in results:
os.startfile(
"C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Riot Games/VALORANT"
)
response += "valorant, "
count += 1
if " vs code" in results:
os.startfile(
"C:/Users/adamw/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Visual Studio Code/Visual Studio Code"
)
response += "VS code, "
count += 1
if count == 0:
speak("No programs opened.")
else:
response = response.split(",")
if count > 1:
response[-2] = " and" + response[-2]
combined_response = ""
for i in range(len(response) - 1):
combined_response += response[i]
speak(combined_response)
open_applications = {}
w.EnumWindows(updateOpenApplications, None)
def focus_command(results):
pyautogui.press("alt")
if " chrome" in results:
if "chrome.exe" in open_applications:
w.ShowWindow(list(open_applications["chrome.exe"])[0], win32con.SW_SHOWMAXIMIZED)
w.SetForegroundWindow(list(open_applications["chrome.exe"])[0])
else:
speak("Failed to focus Google Chrome.")
elif " spotify" in results:
if "Spotify.exe" in open_applications:
w.ShowWindow(list(open_applications["Spotify.exe"])[0], win32con.SW_SHOWMAXIMIZED)
w.SetForegroundWindow(list(open_applications["Spotify.exe"])[0])
else:
speak("Failed to focus Spotify.")
elif " discord" in results:
if "Discord.exe" in open_applications:
w.ShowWindow(list(open_applications["Discord.exe"])[0], win32con.SW_SHOWMAXIMIZED)
w.SetForegroundWindow(list(open_applications["Discord.exe"])[0])
else:
speak("Failed to focus Discord.")
elif " valorant" in results:
if "Valorant.exe" in open_applications:
w.ShowWindow(list(open_applications["Valorant.exe"])[0], win32con.SW_SHOWMAXIMIZED)
w.SetForegroundWindow(list(open_applications["Valorant.exe"])[0])
else:
speak("Failed to focus Valorant.")
elif " vs code" in results:
if "Code.exe" in open_applications:
w.ShowWindow(list(open_applications["Code.exe"])[0], win32con.SW_SHOWMAXIMIZED)
w.SetForegroundWindow(list(open_applications["Code.exe"])[0])
else:
speak("Failed to focus VS Code.")
elif " powershell" in results:
if "powershell.exe" in open_applications:
w.ShowWindow(list(open_applications["powershell.exe"])[0], win32con.SW_SHOWMAXIMIZED)
w.SetForegroundWindow(list(open_applications["powershell.exe"])[0])
else:
speak("Failed to focus Powershell.")
else:
speak("Failed to focus specified application")
if __name__ == "__main__":
main()