Table of Contents
- Introduction: Becoming Tony Stark
- The Magic Libraries You Need
- Step 1: Make Python Speak
- Step 2: Make Python Listen
- Step 3: The “Brain” (Commands)
- Full Source Code
Introduction: Becoming Tony Stark
We all remember that scene in Iron Man where Tony Stark wakes up and talks to JARVIS. It seemed like pure sci-fi magic. But today, with just a few lines of Python, you can build your own version.
In this tutorial, we aren’t just writing code; we are building a companion. By the end of this post, you will have a script that listens to your voice, talks back to you, and can even play YouTube videos on command.
The Magic Libraries You Need
We don’t need to invent complex AI from scratch. Python has amazing community libraries that do the heavy lifting for us.
Open your terminal (Command Prompt) and install these three packages:
Bash
pip install pyttsx3 speechrecognition pywhatkit wikipedia
pyttsx3: This allows Python to “talk” (Text-to-Speech).speechrecognition: This allows Python to “hear” (Speech-to-Text).pywhatkit: A fun library to send WhatsApp messages or play YouTube videos instantly.
Step 1: Make Python Speak
First, let’s give our assistant a voice. We use the pyttsx3 engine. You can even change the voice from male to female depending on your system settings!
Python
import pyttsx3
engine = pyttsx3.init()
def speak(text):
engine.say(text)
engine.runAndWait()
speak("Hello sir, I am ready for your command.")
Step 2: Make Python Listen
Now, let’s teach it to understand us. This function uses your laptop’s microphone to listen for commands and converts them into text strings.
Python
import speech_recognition as sr
def take_command():
listener = sr.Recognizer()
try:
with sr.Microphone() as source:
print("Listening...")
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'jarvis' in command:
# Remove the name "Jarvis" so we just get the command
command = command.replace('jarvis', '')
print(f"User said: {command}")
except:
pass
return command
Step 3: The “Brain” (Connecting it All)
Now for the fun part. We will create a loop that constantly listens for specific keywords like “Play,” “Time,” or “Who is.”
If you say “Play Believer by Imagine Dragons,” the script will automatically open YouTube and start the video.
<a id=”full-code”></a>
Full Source Code
Here is the complete script. Copy this into a file named jarvis.py and run it!
Python
import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
# Set voice to index 1 for female, 0 for male (depending on OS)
engine.setProperty('voice', voices[0].id)
def speak(text):
engine.say(text)
engine.runAndWait()
def run_jarvis():
try:
with sr.Microphone() as source:
print("Listening...")
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'play' in command:
song = command.replace('play', '')
speak('playing ' + song)
pywhatkit.playonyt(song)
elif 'time' in command:
time = datetime.datetime.now().strftime('%I:%M %p')
speak('Current time is ' + time)
elif 'who is' in command:
person = command.replace('who is', '')
info = wikipedia.summary(person, 1)
print(info)
speak(info)
else:
speak("I did not understand that, sir.")
except Exception as e:
print(e)
speak("My microphone is not working properly.")
# Run the assistant
while True:
run_jarvis()
Conclusion
You now have a personal assistant running on your laptop. Is it as smart as Iron Man’s? Not yet. But you can expand this code to open apps, send emails, or even control your smart home lights.
Challenge: Can you modify the code to make Jarvis tell you a joke? (Hint: Look up the python pyjokes library!)