Voice Assistant Development

Voice Assistant using Python

  • I am Saheer Ahmed from Web development team, OWASP Student Chapter.
  • I have written the program inspired by my favourite character Iron man's Artificial Intelligence "Jarvis".
  • I first started writing code for the information of colleges and their NIRF Rankings for students.
  • Later on I also started making it for the information of devices which are helpful for students and information on special student discounts like Apple Student Program, Samsung Student Program.

Let's first start with the installation of modules for the required program: (In terminal type the following things)

  • pip install pyttsx3

In case you receive such errors: 

  • No module named win32com.client
  • No module named win32
  • No module named win32api
Then, install pypiwin32 by typing the below command in the terminal :
  • pip install pypiwin32
Install speechRecognition:
  • pip install speechRecognition
After successfully installing this module, import this module into the program by writing an import statement.
  • import speechRecognition as sr
Defining Task 1: To search something on Wikipedia 

 To do Wikipedia searches, we need to install and import the Wikipedia module into our program. Type the below command to install the Wikipedia module :

  • pip install wikipedia
To know date and time with the help of voice assistant:

  • import datetime
We are gonna use 'sapi5' in our code.

What is sapi5?
  • Microsoft developed speech API.
  • Helps in synthesis and recognition of voice.
What Is VoiceId?
  • Voice id helps us to select different voices.
  • voice[0].id = Male voice 
  • voice[1].id = Female voice

Writing Our speak() Function :

We made a function called speak() at the starting of this tutorial. Now, we will write our speak() function to convert our text to speech.

def speak(audio):


engine.runAndWait() #Without this command, speech will not be audible to us.

Let's now jump to the code:

import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')


def speak(audio):

def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning!")

    elif hour>=12 and hour<18:
        speak("Good Afternoon!")

        speak("Good Evening!")

    speak("Jarvis for your help Sir")

def takeCommand():
    #It takes microphone input from the user and returns string output

    r =  sr.Recognizer()
    with sr.Microphone() as source:
        r.pause_threshold = 1
        audio = r.listen(source)

        query = r.recognize_google(audiolanguage='en-in')
        print(f"User said: {query}\n")  

    except Exception as E:
        # print(E)
        speak("I can't hear it sir")
        print("Say that again please...")
        return "None"
    return query

if __name__ == "__main__":
    # while True:
    if 1:

        query = takeCommand().lower()

    # Logic for executing tasks based on query
    if 'wikipedia' in query:
        speak('Searching Wikipedia...')
        query = query.replace("wikipedia""")
        results = wikipedia.summary(querysentences=2)
        speak("According to Wikipedia")

    elif 'open youtube' in query:

    elif 'open google' in query:

    elif 'open stackoverflow' in query:

    elif 'play music' in query:
          music_dir = 'D:\\Non Critical\\songs\\My playlist'
          songs = os.listdir(music_dir)

    elif 'the time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S"
            speak(f"Sir, the time is {strTime}"

    elif 'open code' in query:
        codePath = "C:\\Users\\SHAIK SAHEER AHMED\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"

    elif 'open facebook' in query:

    elif 'open twitter' in query:

    elif 'open whatsapp' in query:

    elif 'open instagram' in query:

    elif 'open wikipedia' in query:

    elif 'open google map' in query:

    elif 'open car dekho' in query:

    elif 'open cars 24' in query:

    elif 'open dude perfect' in query:

    elif 'open etv' in query:

    elif 'open amazon' in query:

    elif 'open zoom' in query:

    elif 'open flipkart' in query:
  • This is the code and path is added on my device for the command "open code".
  • You can add you own path by copying and pasting.
  • To open links you can run the program and can search just by speaking.
  • I have added English(Indian) for easy recognition and used google as browser to search.
  • You can add much more to this like I have added the things as mentioned in the introduction.