Jarvis AI Assistant in Python - Full Code and Setup Guide
This document contains the full source code and explanation for a Jarvis-like voice assistant written
in Python. It supports:
- Voice commands
- Web and Wikipedia search
- Playing YouTube songs
- Reading files
- Opening folders and apps
- System control (shutdown, restart, lock)
Tested for both PC and Android (via Pydroid3).
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import pyjokes
import pywhatkit
import webbrowser
import os
import subprocess
engine = pyttsx3.init()
def speak(text):
print("Jarvis:", text)
engine.say(text)
engine.runAndWait()
def wish():
hour = datetime.datetime.now().hour
if hour < 12:
speak("Good morning!")
elif hour < 18:
speak("Good afternoon!")
else:
speak("Good evening!")
speak("I am Jarvis. How may I help you?")
def take_command():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source)
try:
print("Recognizing...")
query = recognizer.recognize_google(audio, language="en-in")
print("You said:", query)
except Exception:
speak("Sorry, I didn't catch that. Say it again.")
return "None"
return query.lower()
def open_app(app_name):
paths = {
"chrome": "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
"notepad": "notepad.exe",
"word": "C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE"
}
if app_name in paths:
subprocess.Popen(paths[app_name])
speak(f"Opening {app_name}")
else:
speak(f"I don't know how to open {app_name}")
def open_folder(folder_name):
folders = {
"downloads": "C:\\Users\\%USERNAME%\\Downloads",
"documents": "C:\\Users\\%USERNAME%\\Documents",
"desktop": "C:\\Users\\%USERNAME%\\Desktop"
}
if folder_name in folders:
os.startfile(os.path.expandvars(folders[folder_name]))
speak(f"Opening {folder_name} folder")
else:
speak(f"Folder {folder_name} not configured")
def read_file():
file_path = "C:\\Users\\%USERNAME%\\Documents\\notes.txt"
try:
with open(os.path.expandvars(file_path), 'r') as f:
content = f.read()
speak("Reading your notes:")
speak(content)
except FileNotFoundError:
speak("Sorry, the notes file is missing.")
def system_control(command):
if command == "shutdown":
speak("Shutting down the system.")
os.system("shutdown /s /t 1")
elif command == "restart":
speak("Restarting the system.")
os.system("shutdown /r /t 1")
elif command == "lock":
speak("Locking the system.")
os.system("rundll32.exe user32.dll,LockWorkStation")
def run_jarvis():
wish()
while True:
query = take_command()
if 'time' in query:
time = datetime.datetime.now().strftime("%I:%M %p")
speak(f"The time is {time}")
elif 'who is' in query:
person = query.replace("who is", "")
info = wikipedia.summary(person, sentences=2)
speak(info)
elif 'joke' in query:
speak(pyjokes.get_joke())
elif 'play' in query:
song = query.replace("play", "")
speak(f"Playing {song}")
pywhatkit.playonyt(song)
elif 'open youtube' in query:
webbrowser.open("https://youtube.com")
speak("Opening YouTube")
elif 'open google' in query:
webbrowser.open("https://google.com")
speak("Opening Google")
elif 'open' in query and 'folder' in query:
for key in ['downloads', 'documents', 'desktop']:
if key in query:
open_folder(key)
elif 'open' in query and 'app' in query:
for key in ['chrome', 'notepad', 'word']:
if key in query:
open_app(key)
elif 'read file' in query or 'read notes' in query:
read_file()
elif 'shutdown' in query:
system_control("shutdown")
elif 'restart' in query:
system_control("restart")
elif 'lock' in query:
system_control("lock")
elif 'search for' in query or 'look up' in query:
search_term = query.replace("search for", "").replace("look up", "").strip()
speak(f"Searching Google for {search_term}")
pywhatkit.search(search_term)
elif 'exit' in query or 'bye' in query:
speak("Goodbye!")
break
else:
speak("I didn't understand that command.")
run_jarvis()