Coders Packet

Simple virtual assistant using Python

By Patnam Venkata Koushik

Building a simple virtual assistant that takes input from the user as an audio input and gives the output as a text and audio output in Python

This project can be used for personal purposes or can be used to develop it much further and make it more advanced. This project is built with the following packages: speech_recognition, pyttsx3, pywhatkit, datetime, Wikipedia, pyjokes.

The above-mentioned packages can be installed using the 'pip3 install' command and the following packages can be imported into the project using the 'import' command. 

The first thing to do after installing and importing all the dependencies/ packages is to build a code to take input from the user in audio format through a microphone

listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)



def talk(text):
    engine.say(text)
    engine.runAndWait()



def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'alexa' in command:
                command = command.replace('alexa', '')
                print(command)
    except:
        pass
    return command

 

 
Then we can define a function for running the commands that are given as an input by the user and return the output as an audio output
 
def run_alexa():
    command = take_command()
    print(command)
    if 'play' in command:
        song = command.replace('play', '')
        talk('playing ' + song)
        pywhatkit.playonyt(song)
    elif 'time' in command:
        time = datetime.datetime.now().strftime('%I:%M %p')
        talk('Current time is ' + time)
    elif 'who the heck is' in command:
        person = command.replace('who the heck is', '')
        info = wikipedia.summary(person, 1)
        print(info)
        talk(info)
    elif 'date' in command:
        talk('sorry, I have a headache')
    elif 'are you single' in command:
        talk('I am in a relationship with wifi')
    elif 'joke' in command:
        talk(pyjokes.get_joke())
    else:
        talk('Please say the command again.')

 

 

Download Complete Code

Comments

No comments yet