AI Virtual Assistant Using Python

What is a Virtual Assistant?

An artificial intelligence-based program that recognizes human voice and commands, which, in response to the program performing those tasks, is called a Virtual Assistant. It can get commands using text or human voice. The main aim of virtual assistance is to simplify human tasks. When a user gives commands to their virtual assistant, it will do that task for the humans. For example, setting an alarm, setting a reminder, tracking the smart devices, etc.

Virtual Assistants are a sub-part of the Internet of Things. IoT is a branch consisting of physical devices and appliances connected with sensors that collect user data and transmit it over a wireless network. Some of the famous virtual assistants are Jarvis, Google Home, and Alexa.

Virtual Assistants consist of speech recognition programs and an audio engine that receives the users' voice and recognizes the voice to perform the tasks.

Virtual Assistants seem to be full of technical terminologies, but it is not so. We can make our Virtual Assistant using different libraries and modules provided by Python.

Virtual Assistant using Python

Python provides a number of libraries by which we can make virtual assistants easily. These include:

1) pyttsx3: The library pyttsx3 is used to convert text to speech. It can be used on any platform or IDE and doesn't require an internet connection. This can be installed using the pip command:

2) webbrowser: The webbrowser library in Python is used to display web-based documents to the user. It provides a high-level interface. This module can be downloaded using the pip command:

3) Wikipedia: The Wikipedia library in Python is used to get various information fetched from the Wikipedia website. This library can be installed with the pip command:

4) SpeechRecognition: The library SpeechRecognition is used to convert speech into text. It recognizes the human voice and converts it into text. It also responds to human commands. This library can be installed using the pip command.

5) PyAudio: This library allows you to play and record audio. This library can be installed using the pip command

How Does the Virtual Assistant Work?

The virtual assistants will get the human commands by their voice. It recognizes the voice and then responds to the command. It will write the command again or respond vocally. The response can be correct or not, as the virtual assistants are just a piece of the program and can be ineffective many times.

Implementation of Virtual Assistant in Python

Implementation of Virtual Assistant in Python consists of multiple steps:

1. Importing all the libraries

2. Functions of the Virtual Assistant

We will make different functions for each function and command in the virtual assistant. This includes:

  1. Speech method
  2. command method
  3. dayDate method
  4. time method
  5. welcome method
  6. ask method
  7. main method

Speech Method

This function will help the user to get voice from the system. It will give a response to the user's command. This can be completed with the pyttsx3 module.

In this, we made a function speak( ) and made an object method that takes the pyttsx. Using the setProperty( ) function, it takes the voice, and then, using the say( ) function, we can speak, and it will recognize our voice.

Command Method

This method will recognize the human voice and speak out the commands which the user will say. The user will speak through the microphone, and the virtual assistant will speak out the command and respond accordingly.

We have declared a function command( ), which uses the Recognizer( ) function to recognize the voice. Using the microphone, it takes the input from the user and then responds accordingly.

The dayDate method

This function will return the day and date of the present day using the datetime( ) function.

In this, we have made a function dayDate( ), which uses the datetime library to give the present day. We declared a dictionary of all the days and then printed today's day.

The time method

This function is used to return the present time. It uses the datetime library to read the current time.

We made the function time( ), which uses the datetime.now( ) function to read the time and date and convert it into a string to print it.

The welcome method

This method will welcome the user by introducing itself.

The ask method

This method checks different conditions and responds accordingly.

We made a function ask( ), which will check different commands and call the corresponding functions; for example, when will the user asks "How are you?", it will revert as "I am fine, thank you, how are you", and many more.

Main method

It will call the ask( ) and welcome( ) function.

Let's compile all these methods and make our virtual assistant, which will make our work easy and interact with us just like a friend.

Output:

Welcome! Hello, I am your Virtual assistant Sophia. How may I help you?
Speak...
Trying to get your voice...
You have just said:  what is the time
2023-11-03 14:25:36
Speak...
Trying to get your voice...
You have just said:  how are you
I am fine, thank you. how are you?
Speak...
Trying to get your voice...

Sorry, I didn't get you.. Say it again
Speak...