JARVIS Thumbnail

JARVIS In Python | Ironman’s JARVIS Using Python5 min read

Hey Python Programs, In this Post We Will Be Creating a Very Exiting Project Which is Ironman’s Jarvis And We will be Creating a AI Which Responds, And Perform Tasks As You Want. Jarvis is a Voice Assistant which Automates your Daily Tasks.

Video Tutorial Of JARVIS:

Read Also:

• Email Automation Using Python

• Keylogger Using Python

• QR Code Generator In Python

Requirements For Jarvis:

  • Python Text to Speech
pip install pyttsx3
  • Wikipedia
pip install wikipedia
  • Speech Recognition
pip install speech_recognition
  • PyAuido

pip install pyauido

Now After Installing the Required Modules Open Vs Code & Create a File Named “assitant.py” And Copy the Following Code:

Importing Required Modules:

from email.mime import audio
from unittest import result
import pyttsx3
import webbrowser
import datetime
import wikipedia
import os
import speech_recognition as sr

Setting Up Python Text to Speech:

engine = pyttsx3.init('sapi5')
voices  = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)

Adding Audio Function in Jarvis:

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

Jarvis Wishing Feature:

def wish():
    hour  = int(datetime.datetime.now().hour)
    if hour >=0 and hour<12:
        speak("Good Morning Sir!, i am jarvis, how may i help you?")

    elif hour >=12 and hour<18:
        speak("Good Afternoon Sir!, i am jarvis, how may i help you?")

    else:
        speak("Good Evening Sir!, i am jarvis, how may i help you?")

Jarvis Take Command Feature:

def takeCommand():
   r = sr.Recognizer()
   with sr.Microphone() as source:
    print("Listening...")
    r.pause_threshold = 1
    audio = r.listen(source)

    try:
        print("Recongizing...")
        query = r.recognize_google(audio, language='en-in')
        print(f"User Said: {query}\n")

    except Exception as e:
    #    print(e)

       print("Say that Again")
       return "None"
    return query

Adding Logics to Jarvis:

if __name__ == "__main__":
 wish()
 while True:
  query = takeCommand().lower()

 # Logic for Jarvis
  if 'wikipedia' in query:
    speak('Searching Wikipedia...')
    query = query.replace("wikipedia", "")
    results = wikipedia.summary(query, sentences=2)
    speak("According to Wikipedia")
    speak(results)
  
  elif 'open youtube' in query:
    speak('Opening YouTube')
    webbrowser.open("youtube.com")

  elif 'open google' in query:
    speak('Opening Google')
    webbrowser.open("google.com")

  elif 'open gmail' in query:
    speak('Opening Gmail')
    webbrowser.open("gmail.com")
    
  elif 'what is the time'  in query:
    strTime = datetime.datetime.now().strftime("%H:%M:%S")
    speak(f"Sir the Current time is {strTime}")

  elif 'open vs code' in query:
   VsCodePath = "C:\\Users\\Tanmay Sinha\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
   speak("Opening Visual Studio Code")
   os.startfile(VsCodePath)

  elif 'open spotify' in query:
   Spotify = "spotify.exe"
   speak("Opening Spotify Music")
   os.startfile(Spotify)
   
  elif 'open mister programmer website' in query:
    speak('Opening mister programmer website')
    webbrowser.open("mrprogrammer.in")

  elif 'open github' in query:
    speak('Opening github')
    webbrowser.open("github.com")

  elif 'play music' in query:
   music_directory = "C:\\Music for Jarvis Program"
   songs = os.listdir(music_directory)
   print(songs)
   os.startfile(os.path.join(music_directory, songs[3]))

  elif 'great work jarvis' in query:
     speak("Thank you Sir, I Love You")

Full Source Code:

from email.mime import audio
from unittest import result
import pyttsx3
import webbrowser
import datetime
import wikipedia
import os
import speech_recognition as sr

engine = pyttsx3.init('sapi5')
voices  = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def wish():
    hour  = int(datetime.datetime.now().hour)
    if hour >=0 and hour<12:
        speak("Good Morning Sir!, i am jarvis, how may i help you?")

    elif hour >=12 and hour<18:
        speak("Good Afternoon Sir!, i am jarvis, how may i help you?")

    else:
        speak("Good Evening Sir!, i am jarvis, how may i help you?")



def takeCommand():
   r = sr.Recognizer()
   with sr.Microphone() as source:
    print("Listening...")
    r.pause_threshold = 1
    audio = r.listen(source)

    try:
        print("Recongizing...")
        query = r.recognize_google(audio, language='en-in')
        print(f"User Said: {query}\n")

    except Exception as e:
    #    print(e)

       print("Say that Again")
       return "None"
    return query


if __name__ == "__main__":
 wish()
 while True:
  query = takeCommand().lower()

 # Logic for Jarvis
  if 'wikipedia' in query:
    speak('Searching Wikipedia...')
    query = query.replace("wikipedia", "")
    results = wikipedia.summary(query, sentences=2)
    speak("According to Wikipedia")
    speak(results)
  
  elif 'open youtube' in query:
    speak('Opening YouTube')
    webbrowser.open("youtube.com")

  elif 'open google' in query:
    speak('Opening Google')
    webbrowser.open("google.com")

  elif 'open gmail' in query:
    speak('Opening Gmail')
    webbrowser.open("gmail.com")
    
  elif 'what is the time'  in query:
    strTime = datetime.datetime.now().strftime("%H:%M:%S")
    speak(f"Sir the Current time is {strTime}")

  elif 'open vs code' in query:
   VsCodePath = "C:\\Users\\Tanmay Sinha\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
   speak("Opening Visual Studio Code")
   os.startfile(VsCodePath)

  elif 'open spotify' in query:
   Spotify = "spotify.exe"
   speak("Opening Spotify Music")
   os.startfile(Spotify)
   
  elif 'open mister programmer website' in query:
    speak('Opening mister programmer website')
    webbrowser.open("mrprogrammer.in")

  elif 'open github' in query:
    speak('Opening github')
    webbrowser.open("github.com")

  elif 'play music' in query:
   music_directory = "C:\\Music for Jarvis Program"
   songs = os.listdir(music_directory)
   print(songs)
   os.startfile(os.path.join(music_directory, songs[3]))

  elif 'great work jarvis' in query:
     speak("Thank you Sir, I Love You")

So this was it for This Blog See you in the Next One Till Then Keep Coding Keep Exploring!

JARVIS – GitHub

DOWNLOAD SOURCE CODE

Tanmay Sinha

Author

  • Tanmay Sinha

    Founder & CEO of MrProgrammer.in | I help Learning Freaks to Step into the World of Programming and Our Mission is to Provide High Quality and Valuable Content to Our Users! We believe that everyone has the potential to learn to code, and we are committed to making that happen. Our courses are designed to be engaging and interactive, and we offer a variety of learning paths to meet the needs of all learners. We are also committed to providing our learners with the support they need to succeed, and we offer a variety of resources to help learners learn at their own pace. If you are interested in learning to code, we encourage you to check out our courses. We are confident that you will find our content to be high-quality, valuable, and makes sense.