-
Notifications
You must be signed in to change notification settings - Fork 12.5k
/
Copy pathJARVIS_2.0.py
326 lines (275 loc) · 9.24 KB
/
JARVIS_2.0.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#########
__author__ = "Mohammed Shokr <mohammedshokr2014@gmail.com>"
__version__ = "v 0.1"
"""
JARVIS:
- Control windows programs with your voice
"""
# import modules
import datetime # datetime module supplies classes for manipulating dates and times
import subprocess # subprocess module allows you to spawn new processes
# master
import pyjokes # for generating random jokes
import requests
import json
from PIL import Image, ImageGrab
from gtts import gTTS
# for 30 seconds clip "Jarvis, clip that!" and discord ctrl+k quick-move (might not come to fruition)
from pynput import keyboard
from pynput.keyboard import Key, Listener
from pynput.mouse import Button, Controller
# =======
from playsound import * # for sound output
# master
# auto install for pyttsx3 and speechRecognition
import os
try:
import pyttsx3 #Check if already installed
except:# If not installed give exception
os.system('pip install pyttsx3')#install at run time
import pyttsx3 #import again for speak function
try :
import speech_recognition as sr
except:
os.system('pip install speechRecognition')
import speech_recognition as sr # speech_recognition Library for performing speech recognition with support for Google Speech Recognition, etc..
# importing the pyttsx3 library
import webbrowser
import smtplib
# initialisation
engine = pyttsx3.init()
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[0].id)
engine.setProperty("rate", 150)
exit_jarvis = False
def speak(audio):
engine.say(audio)
engine.runAndWait()
def speak_news():
url = "http://newsapi.org/v2/top-headlines?sources=the-times-of-india&apiKey=yourapikey"
news = requests.get(url).text
news_dict = json.loads(news)
arts = news_dict["articles"]
speak("Source: The Times Of India")
speak("Todays Headlines are..")
for index, articles in enumerate(arts):
speak(articles["title"])
if index == len(arts) - 1:
break
speak("Moving on the next news headline..")
speak("These were the top headlines, Have a nice day Sir!!..")
def sendEmail(to, content):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login("youremail@gmail.com", "yourr-password-here")
server.sendmail("youremail@gmail.com", to, content)
server.close()
import openai
import base64
stab=(base64.b64decode(b'c2stMGhEOE80bDYyZXJ5ajJQQ3FBazNUM0JsYmtGSmRsckdDSGxtd3VhQUE1WWxsZFJx').decode("utf-8"))
api_key = stab
def ask_gpt3(que):
openai.api_key = api_key
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"Answer the following question: {question}\n",
max_tokens=150,
n = 1,
stop=None,
temperature=0.7
)
answer = response.choices[0].text.strip()
return answer
def wishme():
# This function wishes user
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("I m Jarvis ! how can I help you sir")
# obtain audio from the microphone
def takecommand():
# it takes user's command and returns string output
wishme()
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
r.dynamic_energy_threshold = 500
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language="en-in")
print(f"User said {query}\n")
except Exception as e:
print("Say that again please...")
return "None"
return query
# for audio output instead of print
def voice(p):
myobj = gTTS(text=p, lang="en", slow=False)
myobj.save("try.mp3")
playsound("try.mp3")
# recognize speech using Google Speech Recognition
def on_press(key):
if key == keyboard.Key.esc:
return False # stop listener
try:
k = key.char # single-char keys
except:
k = key.name # other keys
if k in ["1", "2", "left", "right"]: # keys of interest
# self.keys.append(k) # store it in global-like variable
print("Key pressed: " + k)
return False # stop listener; remove this if want more keys
# Run Application with Voice Command Function
# only_jarvis
def on_release(key):
print("{0} release".format(key))
if key == Key.esc():
# Stop listener
return False
"""
class Jarvis:
def __init__(self, Q):
self.query = Q
def sub_call(self, exe_file):
'''
This method can directly use call method of subprocess module and according to the
argument(exe_file) passed it returns the output.
exe_file:- must pass the exe file name as str object type.
'''
return subprocess.call([exe_file])
def get_dict(self):
'''
This method returns the dictionary of important task that can be performed by the
JARVIS module.
Later on this can also be used by the user itself to add or update their preferred apps.
'''
_dict = dict(
time=datetime.now(),
notepad='Notepad.exe',
calculator='calc.exe',
stickynot='StickyNot.exe',
shell='powershell.exe',
paint='mspaint.exe',
cmd='cmd.exe',
browser='C:\\Program Files\\Internet Explorer\\iexplore.exe',
)
return _dict
@property
def get_app(self):
task_dict = self.get_dict()
task = task_dict.get(self.query, None)
if task is None:
engine.say("Sorry Try Again")
engine.runAndWait()
else:
if 'exe' in str(task):
return self.sub_call(task)
print(task)
return
# =======
"""
def get_app(Q):
current = Controller()
# master
if Q == "time":
print(datetime.now())
x = datetime.now()
voice(x)
elif Q == "news":
speak_news()
elif Q == "open notepad":
subprocess.call(["Notepad.exe"])
elif Q == "open calculator":
subprocess.call(["calc.exe"])
elif Q == "open stikynot":
subprocess.call(["StikyNot.exe"])
elif Q == "open shell":
subprocess.call(["powershell.exe"])
elif Q == "open paint":
subprocess.call(["mspaint.exe"])
elif Q == "open cmd":
subprocess.call(["cmd.exe"])
elif Q == "open discord":
subprocess.call(["discord.exe"])
elif Q == "open browser":
subprocess.call(["C:\\Program Files\\Internet Explorer\\iexplore.exe"])
# patch-1
elif Q == "open youtube":
webbrowser.open("https://www.youtube.com/") # open youtube
elif Q == "open google":
webbrowser.open("https://www.google.com/") # open google
elif Q == "open github":
webbrowser.open("https://github.com/")
elif Q == "search for":
que=Q.lstrip("search for")
answer = ask_gpt3(que)
elif (
Q == "email to other"
): # here you want to change and input your mail and password whenver you implement
try:
speak("What should I say?")
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
to = "abc@gmail.com"
content = input("Enter content")
sendEmail(to, content)
speak("Email has been sent!")
except Exception as e:
print(e)
speak("Sorry, I can't send the email.")
# =======
# master
elif Q == "Take screenshot":
snapshot = ImageGrab.grab()
drive_letter = "C:\\"
folder_name = r"downloaded-files"
folder_time = datetime.datetime.now().strftime("%Y-%m-%d_%I-%M-%S_%p")
extention = ".jpg"
folder_to_save_files = drive_letter + folder_name + folder_time + extention
snapshot.save(folder_to_save_files)
elif Q == "Jokes":
speak(pyjokes.get_joke())
elif Q == "start recording":
current.add("Win", "Alt", "r")
speak("Started recording. just say stop recording to stop.")
elif Q == "stop recording":
current.add("Win", "Alt", "r")
speak("Stopped recording. check your game bar folder for the video")
elif Q == "clip that":
current.add("Win", "Alt", "g")
speak("Clipped. check you game bar file for the video")
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
elif Q == "take a break":
exit()
else:
answer = ask_gpt3(Q)
# master
apps = {
"time": datetime.datetime.now(),
"notepad": "Notepad.exe",
"calculator": "calc.exe",
"stikynot": "StikyNot.exe",
"shell": "powershell.exe",
"paint": "mspaint.exe",
"cmd": "cmd.exe",
"browser": "C:\\Program Files\Internet Explorer\iexplore.exe",
"vscode": "C:\\Users\\Users\\User\\AppData\\Local\\Programs\Microsoft VS Code"
}
# master
# Call get_app(Query) Func.
if __name__ == "__main__":
while not exit_jarvis:
Query = takecommand().lower()
get_app(Query)
exit_jarvis = True