AI Voice Assistant Using Python

What is a Voice Assistant?

A voice assistant is software based on artificial intelligence and machine learning, used to help humans in their regular tasks using voice commands. It recognises human voice and tries to get our commands and complete the tasks accordingly. Voice assistants are just AI tools that get and interpret commands from users and respond just like a human.

The most popular and widely used voice assistants are Alexa, Google Assistant, Siri, etc. We can get voice assistants mostly in smart devices, smart phones, smartwatches, tablets, etc. Nowadays, voice assistants can be used in home appliances like lights, fans, air conditioners, etc.

Voice assistants cannot be mixed up with together with virtual assistants. The virtual assistant is similar to the voice assistant, but not exactly the voice assistant. The voice assistant is a type of virtual assistant that works with the voice command.

Working of Voice Assistants

Voice Assistants is based on artificial intelligence and works on speech recognition that recognises the human voice and fulfil their tasks and respond to them just like a human. The voice assistants are trained on the human data by which it learns and responds accordingly to fulfil human needs and commands.

Let's take an instance to learn how the voice assistant works:

The user gives the command to set the alarm for 10 o'clock. The voice assistant will recognise the human voice and understands what to do. Then, it will go to the user's mobile phone's settings and will set the alarm clock. Then, it will tell the user that it has completed the task.

Humans can ask anything with the help of voice assistants. Also, they can talk to their voice assistants just like a friend. For example, asking about any query, asking about any general thing, asking to do something, and many more.

Here is the list of work we can ask our voice assistant to do for us:

  • Setting alarm
  • Playing music
  • Opening and searching on any website
  • Sending mails

Automatic Speech Recognition helps to detect the human voice and helps to translate the speech to text. A Voice assistant uses Natural Language Processing which helps to understand the meaning and purpose of the command the user gives to the voice assistant.

We train our machine in a manner that whenever a user gives a command, it will understand the command and respond accordingly and immediately. It recognises the voice and understands the meaning using NLP. Then, with the help of wonderful libraries in Python helps to search the query and send responses, which are then converted to speech using the text to speech method. It is just a task of a few moments. Different functions, libraries, and methods make it a quick process.

Voice Assistant in Python

Python offers different APIs and libraries which helps to recognise human speech and libraries to make voice assistant easily. These are:

1. Speech Recognition: The speech recognition library in Python is used to recognise human voices. This library can be installed using the pip command:

2. Pyttsx3: This library converts text into speech in Python. We can use this library without an internet connection. As we are creating a voice assistant, we need to convert text into speech. We can download this library using the pip command:

3. Requests: It helps to connect to the web pages by sending HTTP requests. We can install this library using the pip command:

4. BeautifulSoup: The Beautiful Soup library is used to scrape the data from other web pages. This can be downloaded using the pip command:

5. Pyjokes: This is used to get the collection of jokes available on the internet. This can be downloaded using the pip command:

6. Datetime: This library is useful for telling the date and time. It can give real-time and dates.

7. Twilio: This library is used to make calls and messages from the user's device. We can install the twilio module using the pip command:

8. Ecapture: We can use this module to capture images from the system's camera. For installing this module, we can use the pip command:

9. WolframAlpha: This library is very useful in searching and solving high level queries within a minimal period of time using Wolfram's algorithm. We can install this library using the pip command:

10. Subprocess: This module helps to get the subprocess of any system details that can be used for different commands like sleep, shutdown, etc.

11. Web Browser: The web browser library is used to complete web searches.

12. Tkinter: This library is used to create the interface for the voice assistant.

13. Wikipedia: This library helps to search query and get data from Wikipedia. We can install this library using the pip command:

Now, we will implement and make our voice assistant in Python.

Implementation of Voice Assistant in Python

The first step is to import all the necessary libraries in Python.

We imported all the necessary libraries which are useful for the AI voice assistant.

Now, we will make different functions for each command we want to ask by the voice assistant.

We have made our engine of pyttsx3 with Sapi5, which is used for converting text to speech. Sapi5 is a speech application platform interface of Microsoft. It is used to recognise the human voice. And we have defined the voice type by voice ID as 0. The voice ID 0 is of the male voice. We can change the voice to female by setting the voice Id as 1.

Now, we will work on the functions that will help our voice assistant to get the commands and respond accordingly.

The speak( ) function

This function will get the voice from the user and respond to the query. We will be able to listen to our voice assistant with the help of the runAndWait( ) function.

The wish( ) function

The wish( ) function is made in order to wish the user. It uses the datetime library, which reads the current time. Using different conditional statements, we checked the current time and then wished the user according to the time. For example, if the current time is 05:50 pm, the voice assistant will wish the user by speaking Good Evening! I am Jack, your voice assistant, How may I assist you?

The command( ) function:

The command( ) function is used to get the commands from the user and respond by speaking out the answers or solutions.

The name( ) function:

The name( ) function is created to get the name of the user for addressing the user.

The assistant_name( ) function

This function is used to give the name to the assistant.

Now, we will make different functions for the task we want our voice assistant to do.

The mail( ) function

We made a function mail( ), which is used to send mail from sender to receiver. It will take the mail IDs of the sender and receiver. It will log in with the email ID and password and then send it to the receiver. Using a try block, it will check if the mail has been sent to the receiver; otherwise, it will throw an exception.

The ask( ) method

The ask( ) function in used by the voice assistant to complete the task whenever the user gives any command. It will respond according to the command by analysing it.

Next, we will make the main function, where we will call the necessary functions to activate the voice assistant.

It's time to compile all the functions together and start our voice assistant, which will help you complete your tasks and talk with you just like a friend.

Output:

Good Afternoon !
I am Jack, your voice assistant, How may I assist you ?
Speak...
Trying to get your voice !

You Just Said :  rose
Welcome  rose
Speak...
Trying to get your voice !

You Just Said :  take my photo
Speak...
Trying to get your voice !

You Just Said :  how are you
Speak...
Trying to get your voice !

You Just Said :  what is your name
My name is Jack
Speak...
Trying to get your voice !

You Just Said :  are you free
Ofcourse, always free for you
Speak...
Trying to get your voice !

You Just Said :  Jack black
Good Afternoon !
I am Jack, your voice assistant, How may I assist you ?
Speak...
Trying to get your voice !

You Just Said :  make me laugh
An SEO expert walks into a bar, bars, pub, public house, Irish pub, tavern, bartender, beer, liquor, wine, alcohol, spirits...

Speak...
Trying to get your voice !

You Just Said :  tell me the time
It's  16: 55: 53