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:
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:
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 Morning, Good Afternoon, or Good Evening to the user.
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.
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:
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.
7
