How to make a Basic Jarvis Using Python

iron man, robotic, superhero, hero, toy, man, iron, design, futuristic, modern, light, character, standing, brown robot, brown hero, iron man, iron man, iron man, iron man, iron man

Jarvis is a fictional character shown in many marvel movies as virtual personal assistant of iron man, Who helps tony stark build his suits and armors and it is a very intelligent machine.

J.A.R.V.I.S. (Just a Rather Very Intelligent System) is a fictional character voiced by Paul Bettany in the Marvel Cinematic Universe (MCU) film franchise, based on the Marvel Comics characters Edwin Jarvis and H.O.M.E.R., respectively the household butler of the Stark family and another AI designed by Stark. J.A.R.V.I.S. is an artificial intelligence created by Tony Stark, who later controls his Iron Man and Hulkbuster armor for him. In Avengers: Age of Ultron, after being partially destroyed by Ultron, a common misconception is that J.A.R.V.I.S. is given physical form as the character Vision, physically portrayed by Bettany. Different versions of the character also appear in comics published by Marvel Comics, depicted as AI designed by Iron Man and Nadia van Dyne.

1. Install Necessary Modules/Libraries

These are some necessary modules which you will need to make your own jarvis assistant

  1. pip install pyttsx3 # Text-to-speech conversion
  2. pip install speech_recognition as sr # Using for taking input using Mic
  3. import datetime # Get time
  4. pip install webbrowser # Open websites
  5. os # System command (Not needed to install)

2. Speak Function

def speak(text): #Function that takes text as input and return audio output
    engine = pyttsx3.init() #initializing Pyttsx3 engine for speaking
    engine.say(text) #Engine will say the text provided by user
    engine.runAndWait() #runAndWait() a built-in function used to exit after speaking for another iteration

3. Listening Function

def listen():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source: #Taking input from microphone
        print("Listening...")
        recognizer.pause_threshold = 1 #It waits for 1 second before completing sentence
        try:
            audio = recognizer.listen(source)
            command = recognizer.recognize_google(audio, language='en-in') #Using Google recognizer
            print(f"You said: {command}")
            return command.lower()
        except sr.UnknownValueError:
            print("Sorry, I couldn't understand.")
            return ""
        except sr.RequestError:
            print("Network issue.")
            return ""

This function uses microphone as input instead of traditional keyboard input, it enables computer for taking input through mic and perform according to user.

4. Commands

def execute_command(command):
    if 'time' in command: #Ask time for getting time
        time = datetime.datetime.now().strftime('%H:%M:%S')
        speak(f"The time is {time}")
    elif 'open google' in command:
        speak("Opening Google")
        webbrowser.open("https://www.google.com") #It will open google in browser
    elif 'open youtube' in command:
        speak("Opening YouTube")
        webbrowser.open("https://www.youtube.com") #It will open Youtube in browser
    elif 'play music' in command:
        music_dir = "C:\\Users\\Public\\Music"  # Change path as needed
        songs = os.listdir(music_dir)
        if songs:
            os.startfile(os.path.join(music_dir, songs[0]))
            speak("Playing music")
        else:
            speak("No music found.")
    elif 'exit' in command:
        speak("Goodbye!")
        exit()
    else:
        speak("Sorry, I can't do that yet.")

5. Main Function Where code starts to execute

if __name__ == "__main__":
    speak("Hello, I am Jarvis. How can I assist you?")
    while True:
        user_command = listen()
        if user_command:
            execute_command(user_command)

Compiled Code in one file

import pyttsx3  # Text-to-speech conversion
import speech_recognition as sr  # Recognize speech
import datetime  # Get time
import webbrowser  # Open websites
import os  # System commands

def speak(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()

def listen():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        recognizer.pause_threshold = 1
        try:
            audio = recognizer.listen(source)
            command = recognizer.recognize_google(audio, language='en-in')
            print(f"You said: {command}")
            return command.lower()
        except sr.UnknownValueError:
            print("Sorry, I couldn't understand.")
            return ""
        except sr.RequestError:
            print("Network issue.")
            return ""

def execute_command(command):
    if 'time' in command:
        time = datetime.datetime.now().strftime('%H:%M:%S')
        speak(f"The time is {time}")
    elif 'open google' in command:
        speak("Opening Google")
        webbrowser.open("https://www.google.com")
    elif 'open youtube' in command:
        speak("Opening YouTube")
        webbrowser.open("https://www.youtube.com")
    elif 'play music' in command:
        music_dir = "C:\\Users\\Public\\Music"  # Change path as needed
        songs = os.listdir(music_dir)
        if songs:
            os.startfile(os.path.join(music_dir, songs[0]))
            speak("Playing music")
        else:
            speak("No music found.")
    elif 'exit' in command:
        speak("Goodbye!")
        exit()
    else:
        speak("Sorry, I can't do that yet.")

if __name__ == "__main__":
    speak("Hello, I am Jarvis. How can I assist you?")
    while True:
        user_command = listen()
        if user_command:
            execute_command(user_command)

Download File from here

Conclusion

Creating a basic Jarvis assistant using Python is an exciting and beginner-friendly project that introduces you to voice recognition, text-to-speech, and automation. With just a few lines of code, you can build a smart assistant capable of responding to commands, opening websites, playing music, and more.

However, this is just the beginning! You can take Jarvis to the next level by integrating machine learning techniques such as natural language processing (NLP), deep learning, and speech synthesis. By using models like OpenAI’s GPT, Google’s Dialogflow, or TensorFlow, you can make your assistant understand context, learn from interactions, and provide intelligent responses.

With continuous learning and experimentation, you can transform this simple assistant into a powerful AI-driven Jarvis, capable of handling complex tasks, automating workflows, and even assisting in smart home control.

Keep coding, keep innovating, and build the future of AI! Thanks for reading my blog, Share it and leave a beautiful comment if you liked the code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top