Setting up JARVIS

Before we start defining a few important functions, let's create a speech engine first.

import pyttsx3
from decouple import config

USERNAME = config('USER')
BOTNAME = config('BOTNAME')


engine = pyttsx3.init('sapi5')

# Set Rate
engine.setProperty('rate', 190)

# Set Volume
engine.setProperty('volume', 1.0)

# Set Voice (Female)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

Let's analyze the above script. First of all, we have initialized an engine using the pyttsx3 module. sapi5 is a Microsoft Speech API that helps us use the voices. Learn more about it here. Next, we are setting the rate and volume properties of the speech engine using setProperty method. Now, we can get the voices from the engine using the getProperty method. voices will be a list of voices available in our system. If we print it, we can see as below:

[<pyttsx3.voice.Voice object at 0x000001AB9FB834F0>, <pyttsx3.voice.Voice object at 0x000001AB9FB83490>]

The first one is a male voice and the other one is a female voice. Since JARVIS was a female assistant, let's set the voice property to the female voice using the setProperty method. 

Note: If you get an error related to PyAudio, download PyAudio wheel from here and install it within the virtual environment.

Also, using the config method from decouple, we are getting the value of USER and BOTNAME from the environment variables.


1. Speak Function

Speak function will be responsible to speak whatever text is passed to it. Let's see the code:

# Text to Speech Conversion
def speak(text):
    """Used to speak whatever text is passed to it"""
    
    engine.say(text)
    engine.runAndWait()
In the speak() method, the engine speaks whatever text is passed to it using the say() method. Using the runAndWait() method, it blocks during the event loop and returns when the commands queue is cleared.

 

2. Greet Function

This function will be used to greet the user whenever the program is run. According to the current time, it greets Good MorningGood Afternoon, or Good Evening to the user.

from datetime import datetime


# Greet the user
def greet_user():
    """Greets the user according to the time"""
    
    hour = datetime.now().hour
    if (hour >= 6) and (hour < 12):
        speak(f"Good Morning {USERNAME}")
    elif (hour >= 12) and (hour < 16):
        speak(f"Good afternoon {USERNAME}")
    elif (hour >= 16) and (hour < 19):
        speak(f"Good Evening {USERNAME}")
    speak(f"I am {BOTNAME}. How may I assist you?")
 

First, we get the current hour, i.e., if the current time is 11:15 AM, the hour will be 11. If the value of hour is between 6 and 12, wish Good Morning to the user. If the value is between 12 and 16, wish Good Afternoon and similarly, if the value is between 16 and 19, wish Good Evening. We are using the speak method to wish the user.
 

3. Take User Input

This function is for taking the commands from the user and recognizing the command using the speech_recognition module.

import speech_recognition as sr
from random import choice
from utils import opening_text


# Takes Input from User
def take_user_input():
    """Takes user input, recognizes it using Speech Recognition module and converts it into text"""

    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')
        if not 'exit' in query or 'stop' in query:
            speak(choice(opening_text))
        else:
            hour = datetime.now().hour
            if hour >= 21 and hour < 6:
                speak("Good night sir, take care!")
            else:
                speak('Have a good day sir!')
            exit()
    except Exception:
        speak('Sorry, I could not understand. Could you please say that again?')
        query = 'None'
    return query
We have imported speech_recognition module as sr. The Recognizer class within the speech_recognition module helps us recognize the audio. The same module has a Microphone class that gives us access to the microphone of the device. So with the microphone as the source, we try to listen to the audio using the listen() method in the Recognizer class. We have also set the pause_threshold to 1, i.e., it will not complain even if we pause for one second during we speak.

Next, using the recognize_google() method from the Recognizer class, we try to recognize the audio. The recognize_google() method performs speech recognition on the audio passed to it, using the Google Speech Recognition API. We have set the language to en-in, i.e. English India. It returns the transcript of the audio which is nothing but a string. We've stored it in a variable called query.

If the query has exit or stop words in it, it means we're asking the assistant to stop immediately. So, before stopping, we greet the user again as per the current hour. If the hour is between 21 and 6, wish Good Night to the user, else, some other message. We create a utils.py file which has just one list containing a few statements as:

opening_text = [
    "Cool, I'm on it sir.",
    "Okay sir, I'm working on it.",
    "Just a second sir.",
]
 

If the query doesn't have those two words(exit or stop), we speak something to tell the user that we have heard you. For that, we will use the choice method from the random module to randomly select any statement from the opening_text list. After speaking, we exit from the program.

During this entire process, if we encounter an exception, we apologize to the user and set the query to None. In the end, we return the query.

Discussion
i am finding error in this line from functions.online_ops import find_my_ip which package i need to install to get rid of it

functions.online_ops could not be resolved error please give solution🙏

7

2