DEV Community

Cover image for Create Your Own Simple Virtual Assistant
Vinay Veerappaji
Vinay Veerappaji

Posted on

Create Your Own Simple Virtual Assistant

What is Virtual Assistant?

Definition: An intelligent virtual assistant (IVA) or intelligent personal assistant (IPA) is a software agent that can perform tasks or services for an individual based on commands or questions.

In simple words, it's a set of programs that will do tasks on its own which decreases users work on a machine over a set of commands.

There are different kinds of Virtual System in the industry for general-purpose, social media, marketing etc.

Difference between a Chatbot and Virtual Assistant:

Generally, a chatbot provides service through text message, they possess limited functionality and have no need for high-level algorithms.
On the other hand, Virtual assistants provide a more interactive platform. Virtual assistants provide a more interactive platform. Advanced NLP techniques and other complex algorithms are used.

Now what are we building??

Popular Virtual assistants like Alexa from Amazon, Siri from Apple, Cortana from Microsoft make use of highly sophisticated algorithms.

But I'm going to create a simple virtual assistant for our local machine which can perform simple tasks on my voice command built using basic concepts of Python and using its libraries.

Lets get started...

Firstly you need to install python on your machine.
Click here to download.
I recommend python3.6 or above

Packages need to installed:

Open the command prompt and install these packages using the below commands

pip install SpeechRecognition
pip install gtts
pip install playsound
Enter fullscreen mode Exit fullscreen mode

After all the installation we need to download pyaudio. Open the link and download the file based on your machine.
Set the downloaded file location with pip install

pip install 'file name with its absolute path'
Enter fullscreen mode Exit fullscreen mode

So now we are good to go...
Before that, let me explain two major packages that we are using,

Our Implementation Idea:

Using Speech Recognition, we going to convert user voice commands into words and machine perform tasks based on keyword from those converted texts. After performing the task the result is converted into speech using gtts(Google Text-to-Speech) library.

Lets code

Create a new python file with .py extension.

First import all the required package

import speech_recognition as sr
from gtts import gTTS
import playsound
import os
Enter fullscreen mode Exit fullscreen mode

Now will create a listen() function which converts the voice received from microphone into text.

def listen():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("I am listening..")
        audio = r.listen(source,phrase_time_limit = 10)
    data=""
    try:
        data = r.recognize_google(audio,language='en-US')
        print("You said:"+data)
    except sr.UnknownValueError:
        print("I cannot hear you")
    except sr.RequestError as e:
        print("Request Failed")
    return data
Enter fullscreen mode Exit fullscreen mode

After listening, there should be a response, so will create a respond() function.

def listen():
    print(String)
    tts = gTTS(text=String,lang="en")
    tts.save("Speech.mp3")
    playsound.playsound("Speech.mp3")
    os.remove("Speech.mp3")
Enter fullscreen mode Exit fullscreen mode

Then we need to create our Virtual Assistant. It checks the pattern using if conditions and performs the set of instructions inside the if body.

def voice_assistant(data):
    if "Who are you" in data:
        listening = True
        respond("I am your assistant")
    if "how are you" in data:
        listening = True
        respond("I am well")
try:
        return listening
    except UnboundLocalError:
        print("TimedOut-->Re-Launch")
Enter fullscreen mode Exit fullscreen mode

After creating all the required functions, we need to call those functions.

time.sleep(2)
respond("Hello, What can I do for you?")
listening = True
while listening == True:
    data = listen() #calling the listen()
    listening = voice_assistant(data)
Enter fullscreen mode Exit fullscreen mode

Now you can modify the responses and also can build more interactions in voice_assistant() function.

After this run file by opening command prompt

python filename.py
Enter fullscreen mode Exit fullscreen mode

The overall working, data flow all explained in the below image,
Alt Text

Now let us make our virtual assistant to tell the current date and time So for that, we are importing time module

import time
from time import ctime
Enter fullscreen mode Exit fullscreen mode

Now add the below if condition in voice_assistant() function

if "time" in data:
        listening  = True
        respond(crime())
Enter fullscreen mode Exit fullscreen mode

Get the full code here
Try to increase the lively interactions with virtual assistant using more if conditions.
If any doubts feel free to comment down.
Stay tuned to know more what else our virtual assistant can do.

Thank you

Top comments (4)

Collapse
 
98lenvi profile image
Lenvin Gonsalves

Awesome article 😁 Finally I have someone to talk to during the quarantine 😂

Collapse
 
vinayveerappaji profile image
Vinay Veerappaji

Lol.... Good luck with that.....

Collapse
 
karthikrajduraisamy profile image
Karthikraj Duraisamy

Thanks man! I never thought, creating a virtual assistant is such easy...

Collapse
 
vinayveerappaji profile image
Vinay Veerappaji

you're welcome!!!!