×

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 while loop

Loops are essential in Python or any other programming language, as they help to execute a block of code repetitively. Sometimes, situations arise where you would need to use a piece of code...

2 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

GUI to extract lyrics from a song Using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

4 minutes read.

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

Append key Value to Dictionary in Python

The Python dictionary is one of the built-in data types. Elements of dictionaries are key-value pairs. In Python, there are numerous ways to add dictionaries. Let's examine some of the...

9 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.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

os.stat() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

How to append a string in python

Adding or appending a string to another string is easy in Python. It is called concatenation and is a basic concept of strings in almost all programming languages. Ways to Append...

4 minutes read.

Python vs PHP

Python Python is a high-level, object-oriented, interpreted language that is used to create independent program and algorithms for a variety of applications. It has a large library support base. In 1990,...

3 minutes read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.