How to build a Virtual Assistant Using Python
What is a virtual assistant?
A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or Friday” created by Tony Stark in the Avengers series. Normally it feels impossible for any such thing to exist, but today’s technology has made it possible. Now, even you can command your device verbally and get things done without manually doing that task.
So, if you want to create one using Python, you can easily do so, as a major library is offered by Python that can help us in building a virtual assistant. Sapi5 and Espeak are the AI present in Windows and Linux respectively. These help us in having the voice from our device.
Advantages of the Virtual Assistant
- General-Purpose Conversations: These assistants provide a more personalized way of conversation between a user and the service provider. The user can easily share what is required and get the desired result.
- Eliminates Language Barriers: The users can use these assistants in their language, making it easier to understand and use.
- Helps in Hands-Free operation: These kinds of assistants let the users access many functions or make the work done without using their hands. They only need to command the assistant and the work will be done swiftly and error-less.
Disadvantages of the Virtual Assistant
- Expensive: These virtual assistant devices are not very cheap; therefore, everyone cannot afford them.
- Security issue: The owners of those devices do not do anything with your interactions with the device, but the device itself stores those things in the cloud, which is not considered hack-proof. It may result in the leak of your personal information.
- Lack of voice recognition: Technology today has evolved more than enough, but then also sometimes the assistants fail to recognize our command and we have to repeat ourselves.
Required Modules to create a Virtual Assistant
- pyttsx3: It is a cross-platform library that converts text to speech and is platform-independent. The best thing and advantage of using this library for converting text to speech is that it can be used without any active internet connection. Below given command has to be executed in the terminal to install this module:
pip install pyttsx3
- SpeechRecognition: This module is used to convert audio into text for the machine to process further steps. Below given command has to be executed in the terminal to install this module:
pip install SpeechRecognition
- Webbrowser: This module is used to display web-based records for the user, and all this is possible due to the high-level interface provided by this module.Below given command has to be executed in the terminal to install this module:
pip install webbrowser
- Wikipedia: It is used to get all kinds of required information from the Wikipedia website. Below given command has to be executed in the terminal to install this module:
pip install Wikipedia
Multiple methods used for the Virtual Assistant
1. Speak Method:
The speak method uses the pyttsx3 module totake the voice from our device. Below given code will help you understand Speak Method properly:
defspeak(aud):
engine =pyttsx3.init()
# The get methodis used to get the current value
# of engine property
voices =engine.getProperty('voices')
# The set method,.[0]=male voice and
# [1]=female voice in set property.
engine.setProperty('voice', voices[0].id)
# Creating a method for the assistant to speak.
engine.say(aud)
# Gaps, during the processing of the commands
# that are queued currently.
engine.runAndWait()
2. Take Query Method:
This method checks the conditions, output will be returned only when the condition is true. Multiple conditions can be added and if the condition is satisfied then we get the desired output. Below given code will help you understand the Take Query Method properly:
# Code to display Take_Querrymethod
defTake_query():
# Calling the Hello function to
# make the assistant more interactive
Hello()
# This is an infinite loop, i.e., it will continuously
# take queries till it meets the end condition and then
# only the loop will be terminated.
while(True):
# We take the query and convert it into the lowercase
# so that the input query matches the condition
# and the desired output can be provided.
qry=takeCommand().lower()
if"take me tojava t point"inqry:
speak("Opening java T point ")
# In the open method the link is provided to this
# method and it will take you to that website in your default
# browser
webbrowser.open("www.javatpoint.com")
continue
elif"open google"inqry:
speak("Opening Google ")
webbrowser.open("www.google.com")
continue
elif"which day it is"inqry:
tellDay()
continue
elif"what is the time"inqry:
tellTime()
continue
# This condition is used to stop the program and exit from the
# loop
elif"Thank You for your help"inqry:
speak("Bye. For more exciting things you can check GFG")
exit()
elif"from wikipedia"inqry:
# If any user wants to have a piece of information
# from wikipedia
speak("Inspecting the wikipedia ")
qry=qry.replace("wikipedia", "")
# The summary of five lines will be showed from the
# Wikipedia, the number of lines can be increased and
# decreased also.
result =wikipedia.summary(qry, sentences=5)
speak("As perWikipedia")
speak(result)
elif"What is your name"inqry:
speak("Hello sir, my name is Friday. Your desktop Assistant")
3. takeCommand Method:
This method is helps us to take commands from the user and recognize those commands using the SpeechRecognition module. Below given code will help you understand the takeCommand Method properly:
# We create this method to take commands from the user
# and recognizing the command through the speech Recognition module.
# The recognizer method is used to recognize the command
deftakeCommand():
r =sr.Recognizer()
# The microphone module is used from the speech recognition module
# to listen the command given by the user
with sr.Microphone() as source:
print('Listening')
# Time difference between the completion of the phrase and of
# non-speaking audio
r.pause_threshold=0.7
audio =r.listen(source)
# After this, we will use the try and catch method to see if
# sound is recognized or not. If the sound is recognized, it is
# well and good, else we will handle this exception.
print("Recognizing")
# This part is used to listen command in Indian english.
# ‘hi-In’ can also be used if we want to make the device
# listen command in hindi.
Command=r.recognize_google(audio, language='en-in')
print("You entered the command =", Command)
exceptException as e:
print(e)
print("Could not recognize!! Would you please repeat…")
return"None"
returnCommand
4. tellTime Method:
The tellTime method is used to command the virtual assistant to tell time. This method takes time, slices it, and then the called speak function speaks the time. Below given code will help you understand the tellTime Method properly:
# Code to display the tellTime method
deftellTime(self):
# This method is used to tell the time.
time =str(datetime.datetime.now())
# Following format will be used to display time:
# "2020-06-05 17:50:14.582630"
# and we will get the time after slicing
print(time)
hour =time[11:13]
min=time[14:16]
self.Speak(self, "Sir the time is"+hour +"Hours and"+min+"Minutes")
5. Hello Method:
This method can also be called as “the greeting method”. Below given code will help you understand the Hello Method properly:
defHello():
defHello():
# This function is made for greeting purpose.
# This function will be called when the assistant
# is called.
speak("""Hello sir I am Friday, your desktop assistant.
How may I help you""")
6. Main Method:
The main method is the portion of code where all the methods created will be executed after calling the Take_query method, and after recognizing the entered query, it returns (or speak) the desired output. Below given code will help you understand the Main Method properly:
if__name__ =='__main__':
# The main method tocall Take_query() method and execute
# the methods created before
Take_query()
After creating all these modules, they need to be embedded into a single code and successfully create the virtual assistant.
The Complete Example:
importpyttsx3asptx
importspeech_recognitionassr
importwebbrowser as wbb
importdatetime as dt
importwikipediaas wkp
# To take commands from the user, we use takeCommand method, this method
# recognises the command using the recognizer from
# the speech recognizition module
deftakeCommand():
r =sr.Recognizer()
# The microphone module is used from the speech recognition module
# to listen the command given by the user
with sr.Microphone() as source:
print('Listening')
# Time difference between the completion of the phrase and of
# non-speaking audio
r.pause_threshold=0.7
audio =r.listen(source)
# After this, we will use the try and catch method to see if
# sound is recognized or not. If the sound is recognized, it is
# well and good, else we will handle this exception.
print("Recognizing")
# This part is used to listen command in Indian english.
# ‘hi-In’ can also be used if we want to make the device
# listen command in hindi.
Qry=r.recognize_google(audio, language='en-in')
print("You entered the command = ", Qry)
exceptException as e:
print(e)
print("Could not recognize!! Would you please repeat…")
return"None"
returnQry
defspeak(aud):
engine =ptx.init()
# The get method is used to get the current value
# of engine property
voices =engine.getProperty('voices')
# The set method, .[0]=male voice and
# [1]=female voice in set property.
engine.setProperty('voice', voices[0].id)
# Creating a method for the assistant to speak.
engine.say(aud)
# Gaps, during the processing of the commands
# that are queued currently.
engine.runAndWait()
deftellDay():
# This function is used to tell that which day is it
day =dt.dt.today().weekday() +1
# This line lets us know the number
# that the function will use to tell the day
dict={1: 'Monday', 2: 'Tuesday',
3: 'Wednesday', 4: 'Thursday',
5: 'Friday', 6: 'Saturday',
7: 'Sunday'}
ifday indict.keys():
week_day=dict[day]
print(week_day)
speak("The day is "+week_day)
deftellTime(self):
# This method is used to tell the time.
time =str(dt.dt.now())
# Following format will be used to display the time:
# "2020-06-05 17:50:14.582630"
# and we will get the time after slicing
print(time)
hour =time[11:13]
min=time[14:16]
self.Speak(self, "Sir the time is"+hour +"Hours and"+min+"Minutes")
if__name__ =='__main__':
# The main method tocall Take_query() method and execute
# the methods created before
Take_query()
defTake_query():
# Calling the Hello function to
# make the assistant more interactive
Hello()
# This is an infinite loop, i.e., it will continuously
# take queries till it meets the end condition and then
# only the loop will be terminated.
while(True):
# We take the query and convert it into the lower case
# so that the inputed query matches with the condition
# and the desired output can be provided.
qry=takeCommand().lower()
if"take me tojava t point"inqry:
speak("Opening java T point ")
# In the open method the link is provided to this
# method and it will take you to that website in your default
# browser
wbb.open("www.javatpoint.com")
continue
elif"take me to google"inqry:
speak("Opening Google ")
wbb.open("www.google.com")
continue
elif"which day it is"inqry:
tellDay()
continue
elif"what is the time"inqry:
tellTime()
continue
# This condition is used to stop the program and exit from the
# loop
elif"Thank You for your help"inqry:
speak("Bye. For more exciting things you can check GFG")
exit()
elif"from wikipedia"inqry:
# If any user wants to have a piece of information
# from wikipedia
speak("Inspecting the wikipedia ")
qry=qry.replace("wikipedia", "")
# The summary of five lines will be showed from the
# Wikipedia, the number of lines can be increased and
# decreased also.
result =wikipedia.summary(qry, sentences=5)
speak("As perWikipedia")
speak(result)
elif"What is your name"inqry:
speak("Hello sir, my name is Friday. Your desktop Assistant")