Python
    About Lesson

    Overview

    In this tutorial, you will learn, How to build a virtual assistant for your desktop using Python.

    The steps are required to build a virtual assistant is given below:

    1)Import all modules

    2)Speak function

    3)Start Virtual assistant

    4)Get Query function

    5)Recognize user’s voice

    Import all modules

    Make sure to install all the required libraries and dependencies before running the code.

    # import libraries
    import pyttsx3
    import speech_recognition as sr
    import webbrowser
    import wikipedia

    Speak function

    This function will make the virtual assistant speak.

    def speak(audio):
        engine  = pyttsx3.init()
        voices  = engine.getProperty('voices')
        engine.setProperty('voice', voices[0].id)
        engine.say(audio)
        engine.runAndWait()
        
    def hello():
        speak("""Hello there I am your assistant. Tell me how may I help you""")

    Start Virtual assistant

    if __name__ == '__main__':
        # main method for executing the functions
        take_query()

    Get Query function

    You can add more queries such as tell me a date or time etc.

    def take_query():
        hello()
        while(True):
            query   = take_command().lower()
            if "Hello How are you" in query:
                speak("I am fine")
            elif "open google" in query:
                speak("Opening Google")
                webbrowser.open("https://www.google.com")
            elif "bye" in query:
                speak("OK Bye")
                exit()
            elif "from wikipedia" in query:
                speak("Checking in Wikipedia")
                query   = query.replace("wikipedia", "")
                result  = wikipedia.summary(query, sentences=2)
                speak("According to Wikipedia")
                speak(result)

    Recognize user’s voice

    This function will recognize the what is your speaking.

    def take_command():
        r   = sr.Recognizer()
        with sr.Microphone() as source:
            print("Listening")
            r.pause_threshold   = 0.7
            audio   = r.listen(source)
            try:
                print("Recognizing ...")
                query = r.recognize_google(audio, language='en-in')
                print("The command is printed = ", query)
            except Exception as e:
                print(e)
                print("Please say it again!")
                return "None" 
            return query

    Complete Source Code

    # import libraries
    import pyttsx3
    import speech_recognition as sr
    import webbrowser
    import wikipedia
    
    def speak(audio):
        engine  = pyttsx3.init()
        voices  = engine.getProperty('voices')
        engine.setProperty('voice', voices[0].id)
        engine.say(audio)
        engine.runAndWait()
    
    def hello():
        speak("""Hello there I am your assistant. Tell me how may I help you""")
    
    def take_command():
        r   = sr.Recognizer()
        with sr.Microphone() as source:
            print("Listening")
            r.pause_threshold   = 0.7
            audio   = r.listen(source)
            try:
                print("Recognizing ...")
                query = r.recognize_google(audio, language='en-in')
                print("The command is printed = ", query)
            except Exception as e:
                print(e)
                print("Please say it again!")
                return "None" 
            return query
    
    def take_query():
        hello()
        while(True):
            query   = take_command().lower()
            if "Hello How are you" in query:
                speak("I am fine")
            elif "open google" in query:
                speak("Opening Google")
                webbrowser.open("https://www.google.com")
            elif "bye" in query:
                speak("OK Bye")
                exit()
            elif "from wikipedia" in query:
                speak("Checking in Wikipedia")
                query   = query.replace("wikipedia", "")
                result  = wikipedia.summary(query, sentences=2)
                speak("According to Wikipedia")
                speak(result)
    
    if __name__ == '__main__':
        # main method for executing the functions
        take_query()