Coders Packet

Personal Assistant using Python

By Dipak Ghosh

Personal Assistant using Python Speech Recognition and Voice Input from user with various features like opening several websites and applications etc.

import speech_recognition as sr     
from win32com.client import constants, Dispatch 
import datetime, wikipedia, webbrowser  

Modules used: speech_recognition, datetime, wikipedia, webbrowser, win32com

Know more about these and their installation here.

def asst_speaks(audio)
def greet()
def user_speaks()
def reg_browser()
def main()

Functions used: asst_speaks(audio), greet(), user_speaks(), reg_browser(), main()

Let us see about these functions in detail.

asst_speaks(audio) - This function is used whenever the assistant has to speak. The 'audio' parameter is the text that it will speak which are based on evaluation of user input passed.

def asst_speaks(audio):  
    print(name+": "+audio.upper())  
    speaker = Dispatch("SAPI.SpVoice")
    del speaker

greet() - This function is used to generate the greeting message by detecting the hour when asked to greet the user like "Good Morning!", "Good Afternoon", "Good Evening" etc. By default it greets on loading the system.

def greet(): 
    h = int(   
    if h>=0 and h<12:
        asst_speaks("Good morning!")
    elif h==12:
        asst_speaks("Good noon!")       
    elif h>12 and h<18:
        asst_speaks("good afternoon!") 
        asst_speaks("good evening")   
    asst_speaks("I am "+name+", how can I help you?")
user_speaks() - This is the audio input function from the user end. The audio that the user provides gets converted in the form of text in this function itself for processing.
def user_speaks(): 
    with sr.Microphone(device_index=1) as source:
        audio = r.listen(source, phrase_time_limit=5) 
    except :    #if poor audio quality or excessive background noise 
        asst_speaks("I didn't get you. Try saying it again!")
    return com

reg_browser() - This function is used to register the browser (Google Chrome) in this case with the program. It is executed at the beginning of the execution of the program for hassle free execution og thr program.

def reg_browser(): 
    chrome_path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
    webbrowser.register('google-chrome', None,webbrowser.BackgroundBrowser(chrome_path))

main() - This function is the driver code for the entire program. Here is where all the functions are brought to operation. Firstly, the greet() and reg_browser() functions are executed. Then user is asked for input of command and as per the command the task is executed and this process continues...

def main():     #driver code
    while(True): #infinite loop for seamless interaction
        com=user_speaks().lower()   #taking user command
        if "wikipedia" in com: #if users says to wiki something
            asst_speaks("Searching wikipedia")
            res=wikipedia.summary(com,sentences=2)  #extracting summary of the wikipedia search
            asst_speaks("According to wikipedia:"+res)  #audio output of the summary
            asst_speaks("Want to know more! Shall I open wikipedia?")
            if "yes" or "yeah" in dec:
                webbrowser.get("google-chrome").open("")    #opens wikipedia home page
                asst_speaks("As you wish sir!")
        elif "open youtube" in com: #if users says to open youtube
        elif "open google" in com:  #if user says to open google
        elif "open stackoverflow" in com:   #if user says to open stack overflow
        elif "open geeks for geeks" in com: #if user says to open Geeeks for Geeks
        elif "open codechef" in com:    #if user says to open Codechef
        elif "open github" in com:  #if user says to open Github
        elif "play music" in com or "open spotify" in com:  #if user says to open Spotify
        elif "open hotstar" in com: #if user says to open Hotstar
        elif "time" in com: #if user asks the time
            asst_speaks("Sir the time is "+timestr)
        elif "your name" in com:    #if user asks the assistant his name
            asst_speaks("Hi, my name is"+name)
        elif "about creator" in com:    #if user wants to know about the creator
            asst_speaks("I am one of the many creations of sir Dipak Ghosh. Want to know more?")
            if "yes" or "yeah" in dec:
                webbrowser.get("google-chrome").open("")  #more about the creator
                asst_speaks("As you wish sir!")
        elif "exit" or "shutdown" in com:   #if user says to exit
            asst_speaks("Are you sure you want to exit?")
            if "yes" or "yeah" in dec:  #confirmation

Features of the Voice Assistant:-

1. Tell about itself

2. Tell about its creator

3. Wikipedia something.

4. Open Websites like Google, GeeksforGeeks, StackOverflow, Spotify, Hotstar etc. 

5. Tell date and time.

and much more...


Download Complete Code


No comments yet