Jarvis AI Assistant Tutorial - CodeQuest

 Jarvis AI Assistant Tutorial - CodeQuest

Preview




This Voice Assistant will respond to your commands. It is able to listen and speak to you. It can open applications, play songs, YouTube videos, tell the time and search for people on wikipedia. It is a cool project to add to your CV and resume. You are free to add features as per your own wish and publish it on git or any other platform. It is pretty self-explanatory as ell as intuitive. Lets get right into it. 

Step 1: Import the libraries/modules

The libraries we will need for this project are:
  • pyttsx3
  • datetime
  • speech_recognition
  • os
  • wikipedia
  • pywhatkit
  • pyjokes
 Now lets import them



Step 2: Make it speak

First, we will initiate the engine with the voices
Then, we would make the function to speak something, along with with declaring a master.

Step 3: Make it greet

An important function for an assistant like this is to freet the user, when it starts, or boots. Here, it will greet us, according to the time.

Step 4: Enable it to listen

In this step, we will be making the function to listen to our voice and convert it to a string to use it to do tasks.

Step 5: Make it listen continuously 

For now, if we run the code, it would listen to us, but a voice assistant is always listening. So we would be colling the takeCommand function continuously
.



Now that we can get our Jarvis to listen, greet and speak, lets code some tasks for it.

Task 1: Open websites

First, we would be declaring a function to check for specific keywords in the command and act accordingly. The first task would be to open websites using the os module. It is pretty intuitive and straightforward. All the code for doing tasks would be in the work() fuunction.


Task 2: Playing songs and YouTube videos

We will be using the pywhatkit module for this, this would search the words on youtube.


Task 3: Searching Wikipedia

Here, it is a very useful function. Although it is necessary to include "Wikipedia" while saying the command.


Task 4: Telling jokes

In this one, I have include a function to say welcome, when said thank you from my side. We will be using the pyjokes module to get it done.



Final Code:

#Project Name: Jarvis
import pyttsx3
import datetime as dt
import speech_recognition as sr
import os
import wikipedia
import webbrowser
import pyjokes
import pywhatkit



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

MASTER = "Andey"
print("Initializing Jarvis. . . .")

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

def greet():
    hour = dt.datetime.now().hour
    # print(hour)
    if hour >=0 and hour < 12 :
        speak("Good Morning"+  "," +MASTER)
    elif hour >=12 and hour < 16 :
        speak("Good Afternoon"+ "," +MASTER)
    else :
        speak("Good Evening"+  "," +MASTER)
    speak("I am Jarvis, at your service")

speak("Initializing Jarvis")
greet()


def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = r.listen(source)

    try:
        print("Recognising...")
        query = r.recognize_google(audio, language='en-in')
        print(f"User said, {query}\n")
        query = query.lower()
    except Exception as e:
        query = None

    return query

def work(query):

    if 'open code' in query:
        speak("opening code")
        os.startfile('C:\\Users\\AndeyGamer\\AppData\\Local\Programs\\Microsoft VS Code\\Code.exe')

    elif 'open youtube' in query:
        webbrowser.open_new("youtube.com")

    elif 'play dola' in query:
        os.startfile('C:\\Users\\AndeyGamer\\Desktop\\dola.mp4')

    elif 'open google' in query:
        webbrowser.open_new("google.com")

    elif 'open reddit' in query:
        webbrowser.open_new("reddit.com")

    elif 'open chrome' in query:
        os.startfile('C:\Program Files\Google\Chrome\Application\chrome.exe')

    elif 'joke' in query:
        joke = pyjokes.get_joke()
        print(joke)
        speak(joke)

    elif 'play' in query:
        query = query.replace('play', '')
        if 'jarvis' in query:
            query = query.replace('jarvis', '')
        pywhatkit.playonyt(query)

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

    if 'thank you' in query:
        speak("Awww, You're welcome")

while True:
    query = takeCommand()
    if query == None:
        pass
    else:
        query = query.lower()
        if "jarvis" in query:
            work(query)
        if "quit" in query:
            exit()



Thus, the Jarvis Voice Assistant is ready. Feel free to add more functions from your side. Enroll for CodeQuest here. 

Thank You

- Team CodeQuest

Comments

  1. Thank you, a very nice project with a cool way of representation

    ReplyDelete

Post a Comment

Popular posts from this blog

Guess The Number Game Tutorial - Codequest