How To Build Your Own Virtual Assistant Using Python

·

4 min read

Virtual Assistant is an awesome program that can help automate the work starting from playing a music for us, sending our emails, reading emails, speaking you a joke and anything that a normal person can do. It can be a good friend for you. We can ask our virtual assistant to do anything for us just by speaking to them.

To start with this project, we need to install some packages. If you want to install the packages in separate folder, create a virtual environment and install in it:

$ py -3 -m venv env
$ .\env\Scripts\activate

1/ Install 'pyttsx3': It is a cross-platform python text to speech library which is platform independent. It allows offline text to speech conversion and is a good library that can be used for any applicaton for text to speech conversions.

$ pip install pyttsx3

2/ Install 'SpeeechRecognition': It allows us to convert audio to text.

$ pip install SpeeechRecognition

3/ Install 'Wikipedia': We will use wikipedia to fetch our information from web.

$ pip install wikipedia

4/ Install 'pywhatkit': PyWhatKit is a Simple and Powerful WhatsApp Automation Library with many useful Features. We will use this library to search and play a music from youtube.

$ pip install pywhatkit

5/ Install 'pyjokes': One line jokes for programmers (jokes as a service)

pip install pyjokes

6/ Now that we have installed the required packages, we need to import it to use in our program

import speech_recognition as sr  # SpeechRecognition package to understand the speech
import pyttsx3  # To speak the text
import pywhatkit  # pywhatkit allows us to do searching in youtube and other fun
import datetime  # To use date and time
import wikipedia  # To use for searching online
import pyjokes  # To use jokes by in the program

7/ Create an instance of SpeechRecognition and pyttsx3

listener = sr.Recognizer()  # create a recognizer which will understand the voice
engine = pyttsx3.init()  # Python text to speech initialize
voices = engine.getProperty('voices')  # Get all the voices
engine.setProperty('voice', voices[1].id)  # set it to second voice

8/ Now we will create 'speak(text)' method that takes in the text and to make our machine talk.

def speak(text):
    engine.say(text)  # ask it to say the command
    engine.runAndWait()

9/ Create 'get_command()' to take in the query from the user in the form of voice command and return their command in text form.

def get_command():
    command = ''
    try:
        with sr.Microphone() as source:  # Use microphone
            print('Listening...')
            voice = listener.listen(source)  # lister to listen to the source and store as voice
            command = listener.recognize_google(voice)  # Converting the voice to text using google api
            command = command.lower()  # convert to lower case
            if 'alexa' in command:  # check if name 'alexa' is in command
                command = command.replace('alexa', '')  # Remove 'alexa' from command by replacing it with empty string
                print(command)

    except:
        pass

    return command  # return command that we are saying

10/ Define the method 'run_alexa()' (decide on any name) to decide what to do my our virtual assistant using the command of user

def run_alexa():  # To start alexa
    command = get_command()  # Get command
    #
    # if 'alexa' in command:  # check if name 'alexa' is in command
    #     command = command.replace('alexa', '')  # Remove 'alexa' from command by replacing it with empty string
    #     print(command)

    if 'play' in command:  # To play music
        song = command.replace('play', '')  # remove play from the command
        speak('playing' + song)
        pywhatkit.playonyt(song)  # play it in youtube

    elif 'time' in command:  # To get the time
        time = datetime.datetime.now().strftime(
            '%I:%M %p')  # %H:%M for 24 hr format # Get the hours and mins from datetime and format it
        speak('Current time is ' + time)
        print(time)

    elif 'who is' in command:  # To use word wikipedia in command
        person = command.replace('who the hell is', '')  # replace who is with empty str in command
        try:
            info = wikipedia.summary(person, 1)  # search person and mention how many lines we want
            speak(info)  # speak the information
            print(info)

        except:
            speak('Couldn\'t find ' + person)

    elif 'drink' in command:
        speak('sorry, I have a headache ')

    elif 'joke' in command:
        joke = pyjokes.get_joke()
        speak(joke)
        print(joke)

    elif 'what is your name' in command:
        speak('My name is alexa')

11/ Now we will run the program from main method.

if __name__=='__main__':
    speak('Hello, How can I help you? ')
    while True:
        try:
            run_alexa()  # Run it continuously

        except sr.UnknownValueError():
            listener = sr.Recognizer()
            continue

Try to run it in the command line and ask it to tell a joke...

Code link is in Github.