Spaces:
Runtime error
Runtime error
Create jarvis.py
Browse files
jarvis.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pip install pyaudio
|
| 2 |
+
|
| 3 |
+
import pyttsx3 #pip install pyttsx3
|
| 4 |
+
import speech_recognition as sr #pip install speechRecognition
|
| 5 |
+
import datetime
|
| 6 |
+
import wikipedia #pip install wikipedia
|
| 7 |
+
import webbrowser
|
| 8 |
+
import os
|
| 9 |
+
import smtplib
|
| 10 |
+
|
| 11 |
+
engine = pyttsx3.init('sapi5')
|
| 12 |
+
voices = engine.getProperty('voices')
|
| 13 |
+
# print(voices[1].id)
|
| 14 |
+
engine.setProperty('voice', voices[0].id)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def speak(audio):
|
| 18 |
+
engine.say(audio)
|
| 19 |
+
engine.runAndWait()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def wishMe():
|
| 23 |
+
hour = int(datetime.datetime.now().hour)
|
| 24 |
+
if hour>=0 and hour<12:
|
| 25 |
+
speak("Good Morning!")
|
| 26 |
+
|
| 27 |
+
elif hour>=12 and hour<18:
|
| 28 |
+
speak("Good Afternoon!")
|
| 29 |
+
|
| 30 |
+
else:
|
| 31 |
+
speak("Good Evening!")
|
| 32 |
+
|
| 33 |
+
speak("I am Jarvis Sir. Please tell me how may I help you")
|
| 34 |
+
|
| 35 |
+
def takeCommand():
|
| 36 |
+
#It takes microphone input from the user and returns string output
|
| 37 |
+
|
| 38 |
+
r = sr.Recognizer()
|
| 39 |
+
with sr.Microphone() as source:
|
| 40 |
+
print("Listening...")
|
| 41 |
+
r.pause_threshold = 1
|
| 42 |
+
audio = r.listen(source)
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
print("Recognizing...")
|
| 46 |
+
query = r.recognize_google(audio, language='en-in')
|
| 47 |
+
print(f"User said: {query}\n")
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
# print(e)
|
| 51 |
+
print("Say that again please...")
|
| 52 |
+
return "None"
|
| 53 |
+
return query
|
| 54 |
+
|
| 55 |
+
def sendEmail(to, content):
|
| 56 |
+
server = smtplib.SMTP('smtp.gmail.com', 587)
|
| 57 |
+
server.ehlo()
|
| 58 |
+
server.starttls()
|
| 59 |
+
server.login('youremail@gmail.com', 'your-password')
|
| 60 |
+
server.sendmail('youremail@gmail.com', to, content)
|
| 61 |
+
server.close()
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
wishMe()
|
| 65 |
+
while True:
|
| 66 |
+
# if 1:
|
| 67 |
+
query = takeCommand().lower()
|
| 68 |
+
|
| 69 |
+
# Logic for executing tasks based on query
|
| 70 |
+
if 'wikipedia' in query:
|
| 71 |
+
speak('Searching Wikipedia...')
|
| 72 |
+
query = query.replace("wikipedia", "")
|
| 73 |
+
results = wikipedia.summary(query, sentences=2)
|
| 74 |
+
speak("According to Wikipedia")
|
| 75 |
+
print(results)
|
| 76 |
+
speak(results)
|
| 77 |
+
|
| 78 |
+
elif 'open youtube' in query:
|
| 79 |
+
webbrowser.open("youtube.com")
|
| 80 |
+
|
| 81 |
+
elif 'open google' in query:
|
| 82 |
+
webbrowser.open("google.com")
|
| 83 |
+
|
| 84 |
+
elif 'open stackoverflow' in query:
|
| 85 |
+
webbrowser.open("stackoverflow.com")
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
elif 'play music' in query:
|
| 89 |
+
music_dir = 'D:\\Non Critical\\songs\\Favorite Songs2'
|
| 90 |
+
songs = os.listdir(music_dir)
|
| 91 |
+
print(songs)
|
| 92 |
+
os.startfile(os.path.join(music_dir, songs[0]))
|
| 93 |
+
|
| 94 |
+
elif 'the time' in query:
|
| 95 |
+
strTime = datetime.datetime.now().strftime("%H:%M:%S")
|
| 96 |
+
speak(f"Sir, the time is {strTime}")
|
| 97 |
+
|
| 98 |
+
elif 'open code' in query:
|
| 99 |
+
codePath = "C:\\Users\\Haris\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
|
| 100 |
+
os.startfile(codePath)
|
| 101 |
+
|
| 102 |
+
elif 'email to harry' in query:
|
| 103 |
+
try:
|
| 104 |
+
speak("What should I say?")
|
| 105 |
+
content = takeCommand()
|
| 106 |
+
to = "harryyourEmail@gmail.com"
|
| 107 |
+
sendEmail(to, content)
|
| 108 |
+
speak("Email has been sent!")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(e)
|
| 111 |
+
speak("Sorry my friend harry bhai. I am not able to send this email")
|
| 112 |
+
else:
|
| 113 |
+
print("No query matched")
|