Design Jarvis Algorithm using Python

In this tutorial, we will learn how to Design Jarvis’s Algorithm using machine learning in Python. It is nothing like Iron man Jarvis coz he does way more things, but our Jarvis is kind of a personal assistant that helps you in doing some basic tasks. let’s get started!

Design Jarvis Algorithm using Python

Let’s Import some Libraries that we have to use here

import pyttsx3 #pip install pyttsx3
import speech_recognition as sr #pip install speechRecognition
import datetime
import wikipedia # pip install wikipedia
import webbrowser
import os

All the above-mentioned libraries will work fine but you may find an error about PyAudio. In that case just follow this link and you will get all your answers.

Let’s define Engine so that our Speak function will use the voice we have in our system

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

The above function speaks a given string

def wishMe():
    hour = int(
    if hour>=0 and hour<12:
        speak("Good Morning Sir")

    elif hour>=12 and hour<18:
        speak("Good Afternoon Sir")

        speak("Good Evening Sir")

    speak("I am Jarvis Sir. I am here to help you")

The above function wish us according to the time

def takeCommand():

    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.pause_threshold = 1
        audio = r.listen(source)
# it converts the audio input into string

        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

The above function takes microphone input from the user and returns string output.

Now let’s move further and do the remaining part

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

        #logic for executing tasks based on query
        if "how are you" in query:
            speak("I'm fine sir, how can i help you ?")

        elif "who are you" in query:
            speak("Sir I am Jarvis personal assistant ")

        elif 'wikipedia' in query:
            speak('Searching Wikipedia...please wait')
            query = query.replace("wikipedia", "")
            results =  wikipedia.summary(query, sentences = 2)
            speak("wikipedia says")

        elif'open youtube' in query:

        elif 'open google' in query:

        elif 'open stackoverflow' in query:

        elif 'play music'in query:
            music_dir = "C:\\Users\\Baali\\Music"
            songs = os.listdir(music_dir)
            os.startfile(os.path.join(music_dir, songs[0]))

        elif 'the time' in query:
            strTime ="%H:%M:%S")
            speak(f"Sir, the time is {strTime}")

        elif 'open code' in query:
            codePath = "C:\\Users\\Baali\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"

        elif 'jarvis quit' in query or 'exit' in query or 'close' in query:
            speak("Thanks you for using Jarvis Sir")

In the above-mentioned code line whenever our Jarvis hear anything Like “what is the time” it recognizes that we have used “the time” in the query so it will answer accordingly the same followed by other queries and at last if we want to quit the Jarvis just say”Jarvis quit or Exit” our program will end.


Thanks this is all for a basic Jarvis you can put many more things in this. I hope you like it.

Check out my other work here

Leave a Reply