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.
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/activateInstall the necessary packages:
$ pip install SpeechRecognition pyttsx3
$ brew install portaudio # macOS only
$ pip install pyaudioCore Python Code
Create voice_assistant.py and add the following imports:
import os
import datetime
import webbrowser
import speech_recognition as sr
import pyttsx3Define 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 engineConclusion
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
