Build a Powerful Python Voice Assistant with GPT‑4: Step‑by‑Step Guide

This tutorial walks you through creating a Python voice assistant powered by GPT‑4, covering project setup, virtual environment creation, required package installation, core code for speech recognition, text‑to‑speech, command handling, and optional speech‑rate adjustment.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Build a Powerful Python Voice Assistant with GPT‑4: Step‑by‑Step Guide

Introduction

Voice assistants are becoming ubiquitous, and GPT‑4 can generate the Python code needed to build one that can set reminders, manage to‑do lists, perform web searches and provide help.

Prompt for GPT‑4

Provide the following prompt to ChatGPT‑4 to obtain the required code:

Set reminders

Create to‑do lists

Search the web

Provide an overview of available commands

Project Setup

Create a new project directory and a virtual environment:

$ mkdir voice_assist
$ cd voice_assist
$ python3 -m venv env
$ source env/bin/activate

Install the necessary packages:

$ pip install SpeechRecognition pyttsx3
$ brew install portaudio   # macOS only
$ pip install pyaudio

Core Python Code

Create voice_assistant.py and add the following imports:

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

Define the helper functions:

def initialize_engine():
    engine = pyttsx3.init()
    return engine

def speak(engine, text):
    engine.say(text)
    engine.runAndWait()

def listen():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = r.listen(source)
    try:
        command = r.recognize_google(audio)
        print(f"User said: {command}
")
    except Exception:
        print("Sorry, I didn't catch that. Could you please repeat?")
        return "None"
    return command.lower()

Implement the functional blocks for each voice‑assistant feature:

def set_reminder(engine, command):
    speak(engine, "What should I remind you about?")
    reminder = listen()
    speak(engine, "When do you want to be reminded? Please say the time in hours and minutes.")
    reminder_time = listen()
    try:
        hour, minute = map(int, reminder_time.split())
        now = datetime.datetime.now()
        reminder_dt = now.replace(hour=hour, minute=minute)
        if now > reminder_dt:
            reminder_dt += datetime.timedelta(days=1)
        speak(engine, f"Alright, I will remind you about '{reminder}' at {hour:02d}:{minute:02d}.")
        while True:
            if datetime.datetime.now() >= reminder_dt:
                speak(engine, f"Reminder: {reminder}")
                break
    except ValueError:
        speak(engine, "Sorry, I couldn't understand the time you provided. Please try again.")

def create_todo_list(engine, command):
    todo_list = []
    speak(engine, "Let's create a to‑do list. Please say the tasks one by one. Say 'done' when you're finished.")
    while True:
        task = listen()
        if task == "done":
            break
        todo_list.append(task)
        speak(engine, f"Added: {task}")
    speak(engine, "Here's your to‑do list:")
    for task in todo_list:
        speak(engine, task)

def search_web(engine, command):
    terms = command.replace("search", "").strip()
    if terms:
        url = f"https://www.google.com/search?q={terms}"
        speak(engine, f"Searching for '{terms}'")
        webbrowser.open(url)
    else:
        speak(engine, "Please provide a search term.")

def show_help(engine):
    help_text = """
I can help you with the following tasks:
1. Set reminders
2. Create to‑do lists
3. Search the web
4. Show available commands
5. Exit
"""
    print(help_text)
    speak(engine, help_text)

Finally, the main loop ties everything together:

def main():
    engine = initialize_engine()
    speak(engine, "Hello, I am your voice assistant. How can I help you today?")
    while True:
        command = listen()
        if "reminder" in command:
            set_reminder(engine, command)
        elif "to-do" in command or "todo" in command:
            create_todo_list(engine, command)
        elif "search" in command:
            search_web(engine, command)
        elif "help" in command:
            show_help(engine)
        elif "exit" in command or "quit" in command:
            speak(engine, "Goodbye!")
            break

if __name__ == "__main__":
    main()

Adjusting Speech Rate (Optional)

To slow down the voice output, modify initialize_engine as follows:

def initialize_engine():
    engine = pyttsx3.init()
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate - 50)
    return engine

Conclusion

Using GPT‑4 to generate the Python code dramatically speeds up the development of a functional voice assistant, providing a solid foundation for further customization and integration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GPT-4speech recognitionVoice Assistanttext-to-speech
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.