Jarvis Voice Assistant (Latest and Very USEFUL!)

voiceassistant

Introduction to Voice Assistants

A voice assistant is a software agent that can interpret human speech and respond via synthesized voices. These assistants can perform tasks or services based on voice commands. In this guide, we will create a simple voice assistant using Python.

Benefits of Voice Assistants

Getting Started with Your Voice Assistant

To build a voice assistant, you need to install the following Python libraries:

Voice Assistant Code Example

Here is a Python script to create a voice assistant. You can use this script to perform various tasks based on voice commands.

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

print(pyfiglet.figlet_format("VOICE ASSISTANT"))

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

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

def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour < 12:
        speak("Good Morning!")
    elif hour >= 12 and hour < 18:
        speak("Good Afternoon!")
    else:
        speak("Good Evening!")
    speak("How can I help you today?")

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("Say that again please...")
        return "None"
    return query

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

        if 'wikipedia' in query:
            speak('Searching Wikipedia...')
            query = query.replace("wikipedia", "")
            results = wikipedia.summary(query, sentences=2)
            speak("According to Wikipedia")
            print(results)
            speak(results)

        elif 'open youtube' in query:
            speak("Opening YouTube")
            webbrowser.open("https://youtube.com")

        elif 'open google' in query:
            speak("Opening Google")
            webbrowser.open("https://google.com")

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

        elif 'time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")
            speak(f"The time is {strTime}")

        elif 'open code' in query:
            codePath = "C:\\path\\to\\your\\code\\editor.exe"
            os.startfile(codePath)

        elif 'open notepad' in query:
            speak("Opening Notepad")
            os.startfile("notepad.exe")

        elif 'shutdown' in query:
            speak("Shutting down the computer")
            os.system("shutdown /s /t 1")

        elif 'open cmd' in query:
            speak("Opening Command Prompt")
            os.startfile("cmd.exe")