Main navigation

Menu
Add To Bookmark

How To Create A Virtual Assistant Using Python?


By NIIT Editorial

Published on 27/08/2021

8 minutes

When it comes to AI assistants, Jarvis from Iron Man, Google Home, and Amazon Alexa are all that we know. However, have you ever thought how brilliant it would be if you could create your virtual assistant? Well, with speech recognition and python magic, you can! Creating a personal assistant enables you to introduce personalized features explicit to your requirements. It allows you to lead an efficient and advanced lifestyle!

This guide lists the process of creating a personal virtual assistant by using the Python library. If you wish to create the same, read on! But first, if you are unaware of Virtual Assistant, here's a quick briefing on it. Let's have a look! 

What is a Virtual Assistant?

Virtual Assistants are nothing but AI (Artificial Intelligence) based programs. These programmed applications perceive human commands and language that leads to program performing tasks. To be more precise, these smart computer programs understand human language through text or particular voice commands. Consequently, they perform tasks for the user. 

Ideally, Virtual Assistants execute simple human jobs, namely, adding tasks to the calendar, rendering information that is usually searched for in web browsers, and keeping track of smart devices at homes such as cameras, lights, or thermostats. 

Virtual Assistants are basically a part of the IoT (Internet of Things). IoT is usually a network/chain of "things" that collect and transmit data over a wireless network. The best part is, this process occurs without any human intervention. 

Coming back to virtual assistants, the core of these assistants comprises an audio engine and speech recognition programs. Once you obtain this core, you will come across endless possibilities along with multiple features to add! 

How to create a virtual assistant using Python

To begin with this tutorial, we will write the code on PyCharm IDE by Jetbrains. 

Built by JetBrains (a Czech company), PyCharm is a consolidated or integrated development platform ideal for computer programming. It focuses on the Python programming language. 

Once you download and install PyCharm, launch the application. Go to the File menu. Click on the 'new project.' Your screen will display a pop-up message asking you to enter your project's name. Enter the desired name. 

As per this guide, we will name our project with the title "virtualBot. 

The project gets loaded. Once the loading is complete, empty the automated content in the" main.py" file. Doing this will provide you with a clean screen for your code. 

What Does a Virtual Assistant do? 

Ideally, a Virtual Assistant will, 

  • Listen to your command. 
  • Respond to your command (the response can either be of success or failure).

In this guide, we will use 3 Python Libraries. These include, 

1)    Python Speech Recognition: Recognition speech translates the verbal words into written text. Python supports several speech recognition engines like Microsoft Bing Voice Recognition, Google Speech Recognition, etc. This library transforms the audio signals into a comprehensive computer-readable format. We can also refer to it as a spectrogram. It is a 3D graph with frequency on the Y-axis, time on the X-axis, and color denoting strength. 

To utilize the Python Speech Recognition library, tap on this link. As the page opens, you will see something like this:

Copy the pip installation command. 

Command to Install Library: 

 

pip install SpeechRecognition




 

Note : use pip instead of pip3 even for python3.

 

Paste the copied command in the terminal. Click enter. You will see the package gets installed. 

 

On successful Installation following message appears :


 

Once the first package gets installed, it's time to get the second one. 

  1. Python Texts To Speech v3(Pyttsx3): This conservation library in Python translates text to speech. Working offline, this library is compatible with other Python versions of 2 and 3.

 

You can download the package here. Similar to the first library, copy the command displayed on the website and paste it into the terminal. 



 

Command to Install Library: 

 

pip install pyttsx3

 

For linux user install below library additionally for pyttsx3 to work :

 

sudo apt install libespeak1



 

On successful Installation following message appears :

 

 

Right after installing the second package, we will work on installing the third package. 

 

  1. Python Audio (PyAudio): With Pyaudio from Python, you can play and record audio quickly on several devices. You can download the package here. Repeat the same process and install the package similar to the 1st step above.

 

Command to Install Library:

 

pip install PyAudio

   

On successful Installation following message appears :

   




 

NOTE 

If you are a Linux user, you may come across an error while installing the Pyaudio library. In such a case, below are a few steps you need to follow if you wish to fix the error. 

 
  • With this command, install portaudio modules
 

sudo apt-get install -y libasound2-dev

  
  • Using the below command, unzip the archive 
 

tar -zxvf file_name.tgz

 
  • Enter the directory, then run :
 

./configure && make

 
  • Once you see this successful, use the command below to install it.
 

sudo make install  

 
  • Install Pyaudio. 
 

pip install PyAudio


Here we go! We completed installing 3 Python Libraries successfully! 

Next, it's time to give life to our Virtual Assistant. Here's how:

Final Code :

Virtual Assistant with PythonPython

Wrap up

While Virtual Assistants have become quite basic now, these little demonstrations indicate the introduction of something huge! With the evolution, introduction, and advances in technology, we can transform the world safely, efficiently, and sustainably through assistants! On that note, we end our guide here. 

Although there are a plethora of things you can obtain with Python libraries, following this tutorial will help you delve more into turning your virtual assistant sharper and smarter.

To learn about Python libraries and AI enroll in the Advanced PGP in Data Science and Machine Learning at NIIT.



Advanced PGP in Data Science and Machine Learning (Full Time)

Become an industry-ready StackRoute Certified Data Science professional through immersive learning of Data Analysis and Visualization, ML models, Forecasting & Predicting Models, NLP, Deep Learning and more with this Job-Assured Program with a minimum CTC of ₹5LPA*.

Job Assured Program*

Practitioner Designed

Top