Live Example
Go to A.I Jarvis Exampleimport pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import pyjokes
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
#print(voices[1].id)
engine.setProperty('voice',voices[1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate',180)
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe():
hour = int (datetime.datetime.now().hour)
if hour >=0 and hour <12:
speak("Good morning!, Sir!")
elif hour >= 12 and hour < 18:
speak("Good Afternoon!, Sir!")
else:
speak("Good Evening!, Sir!")
speak("Jarvis is Online..")
speak("!!. how are you !")
def takecommand():
# it takes microphone input from the user and reutrns output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Rechognizing...")
query = r.recognize_google(audio, language='en=in')
print(f"User said:{query}\n")
except Exception as e:
print(e)
print("Say that again please...")
return "None"
return query
def sendEmail(to, content):
server = smtplib.SHTP('smtp.gmail.com', 587)
server.echo()
server.starttls()
server.login('youremail@gmial.com','your=password-here')
server.sendmail('youremail@gmail.com',to,content)
server.close()
if __name__ =="__main__":
wishMe()
while True:
query = takecommand().lower()
#Logic for executing tasks bassed on query
if 'wikipedia' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia","")
results = wikipedia.summary(query, sentences = 2)
speak("According to Wikipedia")
print(results)
speak(results)
elif 'open youtube' in query:
webbrowser.open("youtube.com")
elif 'open googel' in query:
webbrowser.open("google.com")
elif 'open stackoverflow' in query:
webbrowser.open("stackoverflow.com")
elif 'open whatsapp' in query:
speak("opening whatsapp")
loc = "C:\\Users\\princ\\Downloads\\WhatsApp.exe"
os.startfile(loc)
elif 'play music' in query:
music_dir = 'C:\\Users\\princ\\OneDrive\\Desktop\\New folder (3)\\download'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[5]))
elif 'the time'in query:
strTime = datetime.datetime.now().strftime("%I:%M:%p")
speak(f"sir, the time is {strTime}")
print(strTime)
elif 'open pycharm' in query:
codePy = "C:\\Program Files\\JetBrains\\PyCharm Community Edition 2023.1.2\\bin\\pycharm64.exe"
os.startfile(codePy)
elif 'open code' in query:
codeVs = "C:\\Users\\princ\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
os.startfile(codeVs)
elif 'email to prince' in query:
try:
speak("what should i sey?")
content = takeCommand()
to = "princeyourEmail.com"
sendEmail(to,content)
speak("Email has been sent!")
except Exception as e:
print (a)
speak("Sorry my frind prince. I am not able to send this email")
elif 'joke' in query:
joke1=pyjokes.get_jokes(language='en',category='neutral')
print(joke1)
speak(joke1)
elif 'offline' in query:
speak("jarvis is off line sir! hava a nice day sir!")
exit()
elif 'leave me alone' in query:
speak("Sorry Prince!, I will be quit it")
exit()
.png)