Build your own AI Virtual Assistant

Saishree Sriram
4 min readSep 16, 2020

--

A simple python code to build a Virtual Assistant to complete the tasks just by a command.

I always choose a lazy person to do a hard job because a lazy person will find an easy way to do it.” — Bill Gates.

Not sure whether Gates Said or not, but the quote is the most popular one- that’s because there’s some truth in it. The fact is, Lazy ones try to finish the work more efficiently. If someone rewards the laziest person ever, I’ll ask someone to bring that reward behalf of me. Anyone else is in the same way!?

Haha, I’m even lazy to ask YouTube to play my favourite video.

Why waste my precious energy and time in typing those long phrases? So, I ask Cortana to help me in Laptop and talk with Google on mobile. iPhone users have their best friend, Siri. Probably from now, I’ll use my VA to listen to my commands and help me in a better way. Do you want to do it for you!?

Why late? Let’s create!

Step 1:

Import required modules to build virtual assistant.

First, you need to import all these required modules.

You can download these modules through command prompt in windows or through the terminal in Ubuntu using pip operator.

pyttsx3 is to convert text-to-speech(no worries, work offline too) and speech recognition is to take input from the user in the form of speech

Hey programmers, refer to the python docs for full information. It helps you in future projects too!

Step 2:

We’ll work with Tony in this tutorial. We are using sapi5 as our Text-to-speech(TTS) engine. As soon as we get the details of the current voice, we can set the index of the voice, i.e., 0 for male and 1 for female.

The takeCommand() function receives input from us through the microphone as a source. Using Google API to recognise audio, Tony can understand. You can cross-check by adding speak() at the end.

It’s almost done. Our VA is able to get our words and talk to us. But how’s that going to help us!?

Step 3:

We need to assign tasks for Tony. If I command- “Hey Tony, Can you please open Wikipedia and tell me the Best Indian Cricketer”. Yes, It fetches me the result.

Oh. No! It’s using our favourite Internet explorer to do this task.

But, I want Tony to use google chrome or firefox. Wait… It can do that too!

Step 4:

Tony can open google, Play music and even open any App present in your PC. We are just informing Tony to go to the chrome path or location of the file/ App. To play music, tell the music directory and loop over it or use a random function. (Do it by yourself champs!)

That’s it? What else can Tony do for me?

Step 5:

No. Tony has various features. You can ask Tony to play videos, Open PDFs and even write emails.

What?

Yes. Tony can write emails too! You can try it. But it is never recommended to send emails, as the internet is not safe these days.

If you have any dummy email for study purposes or something else, then you can try sending an email by allowing non-secure apps in Google.

That’s all — Code Tony to wish you according to the time. Play with your Virtual Assistant by extending it to many more features.

Learning by Sharing…

Congratulations for making it to end. It’s awesome to see a lazy kiddo making it to end. I hope this article helped you; we can connect on GitHub or Linkedin. I would be happy to see your feedback, suggestions or queries.

Time is precious, don’t waste it. Drop some claps down here and go build your assistant now.

Remember to get back if struck; Will look forward for some fancy assistant names from you.

--

--