AI -Assistent in Python
import pyttsx3 #pip install pyttsx3
import speech_recognition as sr #pip install speechRecognition
import datetime
import wikipedia #pip install wikipedia
import webbrowser
import os
import smtplib
from time import sleep
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)
voicespeed = 110
engine.setProperty('rate',voicespeed)
chrome_path = "C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Google Chrome.lnk"
def open_chrome():
url = "https://www.google.co.in/"
webbrowser.get(chrome_path).open(url)
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!")
elif hour>=12 and hour<18:
speak("Good Afternoon!")
else:
speak("Good Evening!")
speak("I am Jarvis Sir. Please tell me how may I help you")
def takeCommand():
#It takes microphone input from the user and returns string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
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(e)
print("Say that again please...")
return "None"
return query
def sendEmail(to, content):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('[email protected]', 'Hasya1512')
server.sendmail('[email protected]', to, content)
server.close()
if __name__ == "__main__":
wishMe()
while True:
# if 1:
query = takeCommand().lower()
# Logic for executing tasks based on query
if 'how are you' in query:
print("i am fine,how are you")
speak("i am fine,how are you")
elif 'wikipedia' in query:
speak('Searching Wikipedia...')
voicespeed = 30
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
# speak("According to Wikipedia")
speak(results)
print(results)
elif "shutdown" in query:
print("Turning off in 5 seconds")
speak("Turning off in 5 seconds")
sleep(5)
os.system("shutdown /s /t 1")
elif 'search' in query:
print("What should I search?")
speak("What should I search?")
search = takeCommand().lower()
webbrowser.get(chrome_path).open_new_tab(search + ".com")
elif 'what AI can do' in query:
print("AI could end Mankind")
speak("AI could end Mankind")
elif 'i am also fine' in query:
print("that's nice")
speak("that's nice")
elif 'prime minister of india'in query:
print("Narendra Modi is the prime minister of India")
speak("Narendra Modi is the prime minister of India")
elif 'open youtube' in query:
webbrowser.open("youtube.com")
elif 'open google' in query:
webbrowser.open("google.com")
elif 'open stackoverflow' in query:
webbrowser.open("stackoverflow.com")
elif 'play music' in query:
music_dir = 'D:\\Non Critical\\songs\\Favorite Songs2'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
print(f"Sir, the time is {strTime}")
speak(f"Sir, the time is {strTime}")
elif ' what is the date today' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
print(f"Sir, the time is {strTime}")
speak(f"Sir, the time is {strTime}")
# elif "addition" in query:
elif 'open chrome' in query:
codePath = "C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Google Chrome.lnk"
os.startfile(codePath)
# elif 'chrome' in query:
# open_chrome()
elif 'email ' in query:
try:
speak("What should I say?")
content = takeCommand()
to = "[email protected]"
sendEmail(to, content)
speak("Email has been sent!")
except Exception as e:
print(e)
speak("Sorry my friend Hasya. I am not able to send this email")
Coding boy Hasya