Create Your Own Simple Virtual Assistant
Tags: python, beginners, tutorial
Published: 2020-07-17 20:24:06

#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](https://www.python.org/downloads/) 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
```
After all the installation we need to download [pyaudio](https://www.lfd.uci.edu/~gohlke/pythonlibs/#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'
```

So now we are good to go... 
Before that, let me explain two major packages that we are using,
* [Speech Recognition](https://pythonprogramminglanguage.com/speech-recognition/): This python library converts the spoken words to text. 
* [Google Text-to-Speech](https://pypi.org/project/gTTS): This python library converts text to speech.

## 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
```python
import speech_recognition as sr
from gtts import gTTS
import playsound
import os
```

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

```python
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
```

After listening, there should be a response, so will create a respond() function. 
```python
def listen():
    print(String)
    tts = gTTS(text=String,lang="en")
    tts.save("Speech.mp3")
    playsound.playsound("Speech.mp3")
    os.remove("Speech.mp3")
```

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.
```python
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")
```

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

```python
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)
```
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
```
The overall working, data flow all explained in the below image,
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/opc9qa19egp99f1hpeki.jpg)

Now let us make our virtual assistant to tell the current date and time So for that, we are importing time module
```python
import time
from time import ctime
```
Now add the below if condition in voice_assistant() function
```python
if "time" in data:
        listening  = True
        respond(crime())
```

Get the full code [here](https://github.com/VinayVeerappaji/My-Virtual-Assitance)
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