Develop Desktop Voice Assistant using Python

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Nowadays, with the development of technology, we are preferring to speak rather than type. We all would be using Siri, Google Assistant, Alexa, etc. All these assistants are built using complex algorithms and steady hard work. These not only make our task easy but also save time. It would be wonderful to build a personal voice assistant right? In this article, we will be guiding you to build such a Voice Assistant Project using Python. So, let’s get started.

What is a Voice Assistant?

A Voice assistant is a user interface that takes the voice commands from the user and does the corresponding task. The task might be responding to the queries, connecting to the device, and doing the work specified by the user like opening google, etc. Here, we will be mimicking such a system using Python.

Voice Assistant project in Python

We will be using multiple Python modules to build this voice assistant for doing different tasks like accessing files from the device, opening a browser, finding time, weather, etc. One of the main modules is the speech_recognition which is the heart of the whole project. Using this, we will be listening to the voice messages, decode them and also give vocal outputs.

Other modules can be used based on the commands we are adding to our assistant. The ones we will be using in this article are:

1.  Subprocess: This module can be used to get information from the device to do the tasks like sleep, shutdown, etc.

2. Wolframalpha: This module helps us in answering some questions with the help of Wolfram’s algorithms, knowledgebase and AI technology.

3. Pyttsx3: Using this module helps in converting the text to speech form.

4. Datetime: This module is used to get the current time

5. Wikipedia: This module helps in opening wikipedia and searching for what the user asks.

6. Webbrowser: This module also helps in opening the browser and performing search.

7. Winshell and Os: This module helps in accessing the files and folders in the device

8. Pyjokes: Pyjokes module contains different jokes which can be made listened to by the user.

9. Requests: This helps in accessing the websites using url.

10. Shutil: This allows us to modify and operate on the files in the device.

11. Smtplib: This module helps in sending emails.

12. Bs4: This module helps in extracting information from the request made to the website.

13. Tkinter: We use this module to build the GUI

Download Python Voice Assistant Project

Please download the source code for the voice assistant in Python using the link: Python Voice Assistant Project

Prerequisites

It is advised that the user has prior knowledge of python and some command on basic Python modules. Some of the module we use are standard ones, while the ones that we have to import are as follows:

pip install wolframalpha
pip install pyttsx3
pip install wikipedia
pip install winshell
pip install pyjokes
pip install smtplib
pip install twilio
pip install beautifulsoup4
pip install tk

Steps to build the Voice Assistant Project using Python

We will be following the below steps to build the voice assistant:

1. First, we import the modules.

2. Followed by this, we will be building the voice assistant and create function to speak using the voice engine

3. Then we create the function to wish, get the name of the user and command

4. Next, we will be writing functions to do various tasks like getting a weather report, sending mail, etc.

5. Finally, we write the while loop to run the assistant continuously. In which we take the command and check the command to do the respective task using if else statements.

6. Note that all the tasks we include are our choice. Depending on your comfort, you can add required commands and remove the ones that you don’t need. And make the voice assistant based on your suitability.

1. Import the modules

The first step is to import all the required modules. We are importing the ones that we discussed above.

import subprocess
import wolframalpha
import pyttsx3
import random
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import winshell
import pyjokes
import json
import feedparser
import smtplib
import datetime 
import requests
from twilio.rest import Client
from bs4 import BeautifulSoup
import win32com.client as wincl
from urllib.request import urlopen

2. Building the Voice Assistant and write function to speak

Next, we will be getting the voice assistant using the module pyttsx3. We set the appropriate properties to the engine and write a function to speak the text sent as input.

voiceEngine = pyttsx3.init('sapi5')
voices = voiceEngine.getProperty('voices')
voiceEngine.setProperty('voice', voices[1].id)

def speak(text):
    voiceEngine.say(text)
    voiceEngine.runAndWait()

3. Writing functions to wish, get username, and get command

In this step, we will be creating the required function, the ones that we use to wish initially, get the name and take command after every completion of a task.

1. In the wish() function, we are calculating time and then based on that we are doing the appropriate greeting. Followed by, we are introducing the assistant.

2. In the getName() function, we are getting the name of the user as a command and then storing it in the global variable named ‘uname’.

3. And in the function getCommand(), we will be recognizing the microphone, listen to the user’s voice, and format it to a text form.

def wish():
    print("Wishing.")
    time = int(datetime.datetime.now().hour)
    global uname,asname
    if time>= 0 and time<12:
        speak("Good Morning sir or madam!")

    elif time<18:
        speak("Good Afternoon sir or madam!")

    else:
        speak("Good Evening sir or madam!")

    asname ="Jasper 1 point o"
    speak("I am your Voice Assistant from DataFlair,")
    speak(asname)
    print("I am your Voice Assistant,",asname)
def getName():
    global uname
    speak("Can I please know your name?")
    uname = takeCommand()
    print("Name:",uname)
    speak("I am glad to know you!")
    columns = shutil.get_terminal_size().columns
    speak("How can i Help you, ")
    speak(uname)

def takeCommand():
    recog = sr.Recognizer()
    
    with sr.Microphone() as source:
        print("Listening to the user")
        recog.pause_threshold = 1
        userInput = recog.listen(source)

    try:
        print("Recognizing the command")
        command = recog.recognize_google(userInput, language ='en-in')
        print(f"Command is: {command}\n")

    except Exception as e:
        print(e)
        print("Unable to Recognize the voice.")
        return "None"

    return command

4. Creating functions for some of the tasks

Here we will be creating functions to send mail, get the weather report, and get news.

1. In the sendEmail() function, we are connecting to the mail server, logging in using appropriate credentials, and sending the mail to the required receiver.

2. In the function getWeather(), we are getting the url for the required city. Then getting the request, converting it to json form and getting required details by indexing.

3. In the getNews() function, we are getting the request of the new website, then scraping the information using the bs4 module. Then, getting the main headlines.

def sendEmail(to, content):
    print("Sending mail to ", to)
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    #paste your email id and password in the respective places
    server.login('your email id', 'password') 
    server.sendmail('your email id', to, content)
    server.close()

def getWeather(city_name):
    cityName=place.get() #getting input of name of the place from user
    baseUrl = "http://api.openweathermap.org/data/2.5/weather?" #base url from where we extract weather report
    url = baseUrl + "appid=" + 'd850f7f52bf19300a9eb4b0aa6b80f0d' + "&q=" + cityName  
    response = requests.get(url)
    x = response.json()

    #If there is no error, getting all the weather conditions
    if x["cod"] != "404":
        y = x["main"]
        temp = y["temp"]
        temp-=273 
        pressure = y["pressure"]
        humidity = y["humidity"]
        desc = x["weather"]
        description = z[0]["description"]
        info=(" Temperature= " +str(temp)+"°C"+"\n atmospheric pressure (hPa) ="+str(pressure) +"\n humidity = " +str(humidity)+"%" +"\n description = " +str(description))
        print(info)
        speak("Here is the weather report at")
        speak(city_name)
        speak(info)
    else:
        speak(" City Not Found ")

def getNews():
    try:
        response = requests.get('https://www.bbc.com/news')
  
        b4soup = BeautifulSoup(response.text, 'html.parser')
        headLines = b4soup.find('body').find_all('h3')
        unwantedLines = ['BBC World News TV', 'BBC World Service Radio',
                    'News daily newsletter', 'Mobile app', 'Get in touch']

        for x in list(dict.fromkeys(headLines)):
            if x.text.strip() not in unwantedLines:
                print(x.text.strip())
    except Exception as e:
        print(str(e))

5. The main code to run the assistant

Finally, we are creating the code to run the voice assistant. First, we are creating the global names, wishing and getting the username. Next, in a while loop we continuously get the command, check the command using the if-else ladder and do corresponding operation.

if __name__ == '__main__':

    uname=''
    asname=''
    os.system('cls')
    wish()
    getName()
    print(uname)

    while True:

        command = takeCommand().lower()
        print(command)

        if "jarvis" in command:
            wish()
            
        elif 'how are you' in command:
            speak("I am fine, Thank you")
            speak("How are you, ")
            speak(uname)

        elif "good morning" in command or "good afternoon" in command or "good evening" in command:
            speak("A very" +command)
            speak("Thank you for wishing me! Hope you are doing well!")

        elif 'fine' in command or "good" in command:
            speak("It's good to know that your fine")
       
        elif "who are you" in command:
            speak("I am your virtual assistant.")

        elif "change my name to" in command:
            speak("What would you like me to call you, Sir or Madam ")
            uname = takeCommand()
            speak('Hello again,')
            speak(uname)
        
        elif "change name" in command:
            speak("What would you like to call me, Sir or Madam ")
            assname = takeCommand()
            speak("Thank you for naming me!")

        elif "what's your name" in command:
            speak("People call me")
            speak(assname)
        
        elif 'time' in command:
            strTime = datetime.datetime.now()
            curTime=str(strTime.hour)+"hours"+str(strTime.minute)+"minutes"+str(strTime.second)+"seconds"
            speak(uname)
            speak(f" the time is {curTime}")
            print(curTime)

        elif 'wikipedia' in command:
            speak('Searching Wikipedia')
            command = command.replace("wikipedia", "")
            results = wikipedia.summary(command, sentences = 3)
            speak("These are the results from Wikipedia")
            print(results)
            speak(results)

        elif 'open youtube' in command:
            speak("Here you go, the Youtube is opening\n")
            webbrowser.open("youtube.com")

        elif 'open google' in command:
            speak("Opening Google\n")
            webbrowser.open("google.com")

        elif 'play music' in command or "play song" in command:
            speak("Enjoy the music!")
            music_dir = "C:\\Users\\Gayathri\\Music"
            songs = os.listdir(music_dir)
            print(songs)
            random = os.startfile(os.path.join(music_dir, songs[1]))

        elif 'joke' in command:
            speak(pyjokes.get_joke())
            
        elif 'mail' in command:
            try:
                speak("Whom should I send the mail")
                to = input()
                speak("What is the body?")
                content = takeCommand()
                sendEmail(to, content)
                speak("Email has been sent successfully !")
            except Exception as e:
                print(e)
                speak("I am sorry, not able to send this email")

        elif 'exit' in command:
            speak("Thanks for giving me your time")
            exit()

        elif "will you be my gf" in command or "will you be my bf" in command:
            speak("I'm not sure about that, may be you should give me some time")

        elif "i love you" in command:
            speak("Thank you! But, It's a pleasure to hear it from you.")

        elif "weather" in command:
            speak(" Please tell your city name ")
            print("City name : ")
            cityName = takeCommand()
            getWeather(cityName)

        elif "what is" in command or "who is" in command:
            
            client = wolframalpha.Client("API_ID")
            res = client.query(command)

            try:
                print (next(res.results).text)
                speak (next(res.results).text)
            except StopIteration:
                print ("No results")

        elif 'search' in command:
            command = command.replace("search", "")
            webbrowser.open(command)

        elif 'news' in command:
            getNews()
        
        elif "don't listen" in command or "stop listening" in command:
            speak("for how much time you want to stop me from listening commands")
            a = int(takeCommand())
            time.sleep(a)
            print(a)

        elif "camera" in command or "take a photo" in command:
            ec.capture(0, "Jarvis Camera ", "img.jpg")
        
        elif 'shutdown system' in command:
                speak("Hold On a Sec ! Your system is on its way to shut down")
                subprocess.call('shutdown / p /f')

        elif "restart" in command:
            subprocess.call(["shutdown", "/r"])

        elif "sleep" in command:
            speak("Setting in sleep mode")
            subprocess.call("shutdown / h")

        elif "write a note" in command:
            speak("What should i write, sir")
            note = takeCommand()
            file = open('jarvis.txt', 'w')
            speak("Sir, Should i include date and time")
            snfm = takeCommand()
            if 'yes' in snfm or 'sure' in snfm:
                strTime = datetime.datetime.now().strftime("% H:% M:% S")
                file.write(strTime)
                file.write(" :- ")
                file.write(note)
            else:
                file.write(note)
        else:
            speak("Sorry, I am not able to understand you")

6. Code to create the GUI

In this code, we create a GUI with a button that starts the voice assistant by calling the function callVoiceAssistant(). And also shows the message of listening while the assistant is trying to get a command from the user.

#Creating the main window 
wn = tkinter.Tk() 
wn.title("DataFlair Voice Assistant")
wn.geometry('700x300')
wn.config(bg='LightBlue1')
  
Label(wn, text='Welcome to meet the Voice Assistant by DataFlair', bg='LightBlue1',
      fg='black', font=('Courier', 15)).place(x=50, y=10)

#Button to convert PDF to Audio form
Button(wn, text="Start", bg='gray',font=('Courier', 15),
       command=callVoiceAssistant).place(x=290, y=100)

showCommand=StringVar()
cmdLabel=Label(wn, textvariable=showCommand, bg='LightBlue1',
      fg='black', font=('Courier', 15))
cmdLabel.place(x=250, y=150)

#Runs the window till it is closed
wn.mainloop()

Output of Python Voice Assistant project

Here is the output on the console of the voice assistant.

Wishing.
I am your Voice Assistant from DataFlair, Jasper 1 point o
Listening to the user
Recognizing the command
Command is: Bob

Name: Bob
Bob
Listening to the user
Recognizing the command
Command is: how are you

how are you
Listening to the user
Recognizing the command
Command is: fine

fine
Listening to the user
Recognizing the command
Command is: can I know time

can i know time
22hours31minutes57seconds
Listening to the user
Recognizing the command

Unable to Recognize the voice.
none
Listening to the user
Recognizing the command
Command is: open Google

open google
Listening to the user
Recognizing the command

Unable to Recognize the voice.
none
Listening to the user
Recognizing the command
Command is: exit

exit

Output of Python Voice Assistant GUI

python voice assistant output

Summary

Hurray! You have successfully built your personal voice assistant. Feel free to add more commands based on your requirement. And get ready to be a bit lazy with the command!

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

3 Responses

  1. Jeff says:

    This is great but mine is not displaying the listening message. How do I go about it

  2. kamaljeet Singh Jadon says:

    Nice Way To Teaching About Technical Skills, Woh To make Our Future More Easier And Safe.

  3. madan says:

    news not working

Leave a Reply

Your email address will not be published. Required fields are marked *