JARVIS Assistant

VERSION1.0

A powerful Python-based virtual assistant inspired by Iron Man's JARVIS

GitHubWatch Demo

Overview

JARVIS (Just A Rather Very Intelligent System) is a Python-based voice-controlled AI assistant inspired by the AI system from the Iron Man movies. This project aims to create a personal assistant that can perform various tasks through voice commands, including:

  • Answering questions using Wolfram Alpha and Wikipedia
  • Performing system operations (shutdown, restart, etc.)
  • Retrieving weather information
  • Opening applications and websites
  • Providing system information
  • Controlling system settings (brightness, Bluetooth, etc.)
  • Performing calculations
  • Reading news headlines

Installation

Prerequisites

Make sure you have Python 3.6+ installed on your system.

Step 1: Clone the Repository

git clone https://github.com/Adnan-The-Coder/jarvis-build-v1

Step 2: Install Dependencies

The script will attempt to install required packages automatically, but you can also install them manually:

pip install -r requirements.txt

Step 3: API Keys

For full functionality, you'll need to obtain the following API keys:

  • Wolfram Alpha API key (currently set to "4Y594Q-5TAGRTVW86" in the code)

Step 4: Run the Assistant

python main.py

Features

Voice Recognition

Uses Google's Speech Recognition API to convert speech to text for processing commands.

Knowledge Base

Integrates with Wolfram Alpha and Wikipedia to answer questions and provide information.

System Control

Perform system operations like shutdown, restart, lock screen, and control system settings.

Web Integration

Open websites, search Google, and fetch weather information and news headlines.

Application Control

Launch applications like Chrome, PowerPoint, and Blender with voice commands.

System Information

Retrieve detailed information about CPU, memory, network, and other system components.

Code Structure

Imports and Dependencies

The script begins with a comprehensive try-except block to import all necessary libraries. If imports fail, it attempts to install the required packages:

try:
    import os 
    import subprocess
    import wolframalpha
    import pyttsx3
    # ... more imports
except:
    import os
    try:
        os.system('pip install -r requirements.txt')
    except:
        pass
    # ... retry imports

Voice Engine Setup

The script initializes the text-to-speech engine:

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

owner = "ADNAN"
my_current_location = "Hyderabad"

Core Functions

Several key functions power the assistant:

speak(audio)

Converts text to speech using the pyttsx3 engine.

play_audio_background(file_path, volume)

Plays audio files in the background using pygame.

wishMe()

Greets the user based on the time of day.

takeCommand()

Listens for voice input and converts it to text using speech recognition.

wolfram(query)

Sends queries to Wolfram Alpha API and returns the results.

Main Execution Flow

The main execution flow includes:

  1. Initialization and greeting
  2. Internet connection check
  3. Continuous listening for commands in a while loop
  4. Command processing and execution

Command Processing

The script uses a series of conditional statements to process different commands:

if 'wikipedia' in query:
    # Wikipedia search logic
elif 'open youtube' in query:
    # YouTube opening logic
elif 'the time' in query:
    # Time telling logic
# ... many more command handlers

API Reference

speak(audio)

Converts text to speech using the pyttsx3 engine.

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

Parameters:

  • audio (str): The text to be spoken

play_audio_background(file_path, volume)

Plays audio files in the background using pygame.

def play_audio_background(file_path: str, volume: float = 0.1):
    def _play():
        pygame.init()
        pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=4096)
        try:
            pygame.mixer.music.load(file_path)
            pygame.mixer.music.set_volume(volume)
            pygame.mixer.music.play()
        except pygame.error as e:
            print(f"Error loading or playing sound: {e}")

    threading.Thread(target=_play, daemon=True).start()

Parameters:

  • file_path (str): Path to the audio file
  • volume (float, optional): Volume level from 0.0 to 1.0. Default is 0.1

takeCommand()

Listens for voice input and converts it to text using speech recognition.

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)
    try:
        print("Recognizing...")   
        query = r.recognize_google(audio, language ='en-in')
        print(f"User said: {query}\n")
    except Exception as e:
        print(e)   
        print("Unable to Recognize your voice.") 
        return "None"
    return query

Returns:

  • str: The recognized text from speech, or "None" if recognition fails

wolfram(query)

Sends queries to Wolfram Alpha API and returns the results.

def wolfram(query):
    api_key = "4Y594Q-5TAGRTVW86"
    requester = wolframalpha.Client(api_key)
    requested = requester.query(query)
    try:
        answer = next(requested.results).text
        return answer
    except:
        speak("no data found")

Parameters:

  • query (str): The query to send to Wolfram Alpha

Returns:

  • str: The answer from Wolfram Alpha, or None if no data is found

Examples

Basic Commands

"Hello"

Greets you and asks how it can help.

"What's your name?"

Responds with its name (JARVIS).

"What time is it?"

Tells you the current time.

"What's today's date?"

Tells you the current date.

Web and Search

"Open YouTube"

Opens YouTube in your default browser.

"Search for Python tutorials"

Searches Google for Python tutorials.

"Wikipedia artificial intelligence"

Searches Wikipedia for information about artificial intelligence.

"What's the weather?"

Provides current weather information.

System Commands

"System information"

Provides detailed information about your system.

"CPU information"

Provides information about your CPU.

"Launch Chrome"

Opens Google Chrome browser.

"Lock window"

Locks your computer.

Knowledge and Calculations

"Calculate 25 plus 17"

Performs the calculation and returns the result (42).

"What is the capital of France?"

Uses Wolfram Alpha to answer knowledge questions.

"Tell me a joke"

Tells you a random joke.

"Temperature in New York"

Provides the current temperature in New York.

Get in Touch

Have questions, suggestions, or want to contribute to the project? Reach out through any of these channels:

GitHub RepositoryConnect on LinkedIn
Email: syedadnanali0106@gmail.com

Join the Community

The JARVIS project is always looking for contributors and testers. Whether you're a Python expert or just getting started, there are many ways to get involved:

  • Submit bug reports and feature requests on GitHub
  • Contribute code improvements and new features
  • Help improve documentation and examples
  • Share your experience and help others in discussions