×

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

  1. 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.
  2. Eliminates Language Barriers: The users can use these assistants in their language, making it easier to understand and use.
  3. 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

  1. Expensive: These virtual assistant devices are not very cheap; therefore, everyone cannot afford them.
  2. 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.
  3. 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")

Related Topics

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

Python Seaborn

Seaborn is an open-source library in Python that is used for data visualization and plotting graphs. The plots are used for the Visualization of data. It is built on top...

10 minutes read.

Add Element to Tuple in Python

Python: Popular high-level, all-purpose programming language Python. The new version of the Python programming language, Python 3, is used for all software applications, including web development. Python is the best programming...

4 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

Decision Tree Classification in Python

In this article, we will learn the implementation of the decision tree in Sklearn, which is nothing but the Scikit Learn library of python. First, we should learn what classification...

12 minutes read.

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.

How to implement classifiers in Python

What is classification? Classification is a type of supervised machine learning problem with a categorical target (response) variable. Given the known label in the training data, the classifier approximates a mapping...

4 minutes read.

Python Program to Check Leap Year

Python Program to Check Leap Year Leap year: We all know that each year has 365 or 366 days. But whenever a year has 366 days, then the year is said...

2 minutes read.

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

Speech Recognition in Python

What is Speech Recognition? Speech Recognition is a term defined for automatic recognition of human speech. Speech recognition is the most significant activity in the domain of the interaction between the...

8 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

PyShark in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Python Selectors

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.