Create Your Own Voice Assistant in Python – Step-by-Step Tutorial

Avani YadavAvani Yadav
3 min read

Imagine telling your computer what to do — and it listens. Meet Maya, your personal desktop voice assistant powered by Python. Whether you’re a curious beginner or a Python hobbyist, this project will teach you to build something magical from scratch.

In this tutorial, you’ll learn how to:

  • Use speech recognition and text-to-speech (TTS) in Python

  • Handle basic voice commands

  • Build a Python project that feels like real AI

We’ll use:

  • Python 3.x

  • speech_recognition

  • pyttsx3 for TTS

  • pyaudio

  • Built-in modules like datetime, os, webbrowser

Why build Maya?

  • Practice working with real-time audio input

  • Learn to handle APIs and Python modules

  • Great portfolio project for job seekers or students

Table of Contents

1. Prerequisites

Make sure you have:

  • Python 3.6 or later installed

  • A code editor (like VS Code)

  • A working microphone

  • Basic understanding of Python (functions, imports)

Install the required libraries:

pip install speechrecognition pyttsx3 pyaudio

2. Setting Up the Project

  • Create a file named maya.py

  • We’ll define three main functions:

    • speak(text) – speaks the response

    • listen() – listens to your voice

    • respond(command) – handles the logic

3. Writing the Code

Text-to-Speech with pyttsx3

import pyttsx3

engine = pyttsx3.init()
engine.setProperty('rate', 150)
engine.setProperty('voice', engine.getProperty('voices')[1].id)

def speak(text):
    print("Maya:", text)
    engine.say(text)
    engine.runAndWait()

Tip: You can list available voices using:

1

print(engine.getProperty('voices'))

Speech Recognition with speech_recognition

import speech_recognition as sr

def listen():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = r.listen(source)
        try:
            command = r.recognize_google(audio).lower()
            print("You said:", command)
        except sr.UnknownValueError:
            speak("Sorry, I didn’t catch that.")
            return ""
        return command

Responding to Commands

import datetime
import os
import webbrowser

def respond(command):
    if 'time' in command:
        current_time = datetime.datetime.now().strftime('%I:%M %p')
        speak(f"The time is {current_time}")
    elif 'open notepad' in command:
        os.system('notepad')
    elif 'open youtube' in command:
        webbrowser.open('https://www.youtube.com')
    elif 'exit' in command:
        speak("Goodbye!")
        exit()
    else:
        speak("Sorry, I can't do that yet.")

Putting It All Together

if __name__ == "__main__":
    speak("Hello, I am Maya. How can I help you?")
    while True:
        user_input = listen()
        if user_input:
            respond(user_input)

4. Testing the Assistant

To run Maya:

python maya.py

Try saying:

  • “What time is it?”

  • “Open Notepad”

  • “Open YouTube”

  • “Exit”

Tip: Make sure your microphone is working and that Python has access to it.

5. Conclusion

Congratulations! You’ve built a basic desktop voice assistant in Python. You’ve learned how to:

  • Use TTS and speech recognition

  • Handle commands

  • Combine Python modules into a voice-powered app

6. Next Steps

Enhance Maya with:

  • Wake-word detection (like “Hey Maya”)

  • Integration with GPT for smart responses

  • A GUI interface using Tkinter or PyQt

  • Smart home or browser automation

Got curious about these upgrades?
Let’s geek out together — feel free to reach out to me anytime!

[contact me here] https://forms.gle/2NJdonoGysJKGbJj7

0
Subscribe to my newsletter

Read articles from Avani Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Avani Yadav
Avani Yadav