Skip to content

How to build a voice-activated assistant with python

Image of the author

David Cojocaru @cojocaru-david

How to Build a Voice-Activated Assistant with Python visual cover image

Build Your Own Voice-Activated Assistant with Python

Voice-activated assistants like Siri, Alexa, and Google Assistant have transformed how we interact with technology. Imagine having your own personalized assistant, tailored to your specific needs! This guide will walk you through building a voice-activated assistant with Python, leveraging powerful libraries and APIs. Whether you’re interested in home automation, boosting productivity, or simply having fun, this project provides a fantastic introduction to AI and voice interaction.

Why Python for Voice Assistants?

Python has emerged as the go-to language for developing voice assistants, thanks to its ease of use and extensive ecosystem of libraries. Here’s why Python excels:

Prerequisites

Before we begin, ensure you have the following:

Setting Up Your Development Environment

Let’s install the necessary Python libraries using pip, the package installer for Python:

pip install SpeechRecognition pyttsx3 pyaudio

Here’s a breakdown of each library:

Building the Core Functionality

Let’s start with the fundamental building blocks of your voice assistant.

1. Speech Recognition: Listening to Your Voice

The speech_recognition library allows your assistant to understand your commands.

import speech_recognition as sr

recognizer = sr.Recognizer()
with sr.Microphone() as source:
    print("Listening...")
    recognizer.adjust_for_ambient_noise(source) # Optional: Reduces noise
    audio = recognizer.listen(source)

    try:
        text = recognizer.recognize_google(audio)
        print(f"You said: {text}")
    except sr.UnknownValueError:
        print("Sorry, I couldn't understand that.")
    except sr.RequestError as e:
        print(f"Could not request results from Google Speech Recognition service; {e}")

This code snippet captures audio from your microphone, converts it to text using Google’s Speech Recognition API, and prints the recognized text. The adjust_for_ambient_noise function helps improve accuracy by reducing background noise.

2. Text-to-Speech: Giving Your Assistant a Voice

The pyttsx3 library enables your assistant to respond verbally.

import pyttsx3

engine = pyttsx3.init()
engine.say("Hello! How can I help you today?")
engine.runAndWait()

This code initializes the pyttsx3 engine, speaks the specified text, and waits for the speech to finish.

3. Adding Basic Commands: Making Your Assistant Interactive

Now, let’s add some simple commands to make your assistant more interactive.

import datetime

# (Previous speech recognition code here - the 'text' variable is used below)

if "hello" in text.lower():
    engine.say("Hi there!")
elif "what time is it" in text.lower() or "what's the time" in text.lower():
    current_time = datetime.datetime.now().strftime("%H:%M")
    engine.say(f"The current time is {current_time}")
else:
    engine.say("Sorry, I didn't understand that command.")

engine.runAndWait()

This code checks the recognized text for specific keywords (“hello,” “what time is it,” etc.) and executes corresponding actions.

Enhancing Your Assistant: Taking it to the Next Level

Integrating AI with OpenAI

For more intelligent and context-aware responses, consider integrating OpenAI’s GPT models.

Important: This requires an OpenAI API key. Treat your API key with utmost security and do not expose it in your code.

import openai

openai.api_key = "YOUR_OPENAI_API_KEY" # Replace with your actual API key

# (Previous speech recognition code here - the 'text' variable is used below)

try:
    response = openai.Completion.create(
        engine="text-davinci-003", # Or a more current model
        prompt=text,
        max_tokens=50,
        n = 1,
        stop=None,
        temperature = 0.5 # Adjust for more or less randomness
    )
    answer = response.choices[0].text.strip()
    engine.say(answer)
    engine.runAndWait()

except Exception as e:
    print(f"Error communicating with OpenAI: {e}")
    engine.say("I'm having trouble connecting to the internet.")
    engine.runAndWait()

This code sends the recognized text to OpenAI’s API, receives a generated response, and speaks the response. Remember to replace "YOUR_OPENAI_API_KEY" with your actual API key.

Adding Wake Word Detection

To make your assistant more convenient to use, implement wake word detection. This allows your assistant to listen only when a specific phrase (e.g., “Hey Assistant”) is spoken.

Note: pocketsphinx can be tricky to set up and requires additional installation steps specific to your operating system. Consider using other wake word detection libraries like snowboy (deprecated, but examples exist) or Porcupine for simpler setup. This example assumes you have pocketsphinx set up correctly.

from pocketsphinx import LiveSpeech

engine.say("Ready to listen for 'Hey Assistant'.")
engine.runAndWait()

for phrase in LiveSpeech():
    if "hey assistant" in str(phrase).lower():
        engine.say("How can I help?")
        engine.runAndWait()
        # Your Speech Recognition and Command Processing Logic Here
        break # Exit the wake word loop and process the command

This code continuously listens for the wake word (“hey assistant”). Once detected, it initiates the speech recognition process and command execution.

Testing and Debugging

Conclusion

Building a voice-activated assistant with Python is a rewarding and educational experience. By combining speech recognition, text-to-speech, and AI, you can create a powerful and personalized tool that enhances your productivity and simplifies your life. Remember to explore the vast possibilities of these technologies and continue to experiment and refine your assistant’s capabilities. With creativity and dedication, you can unlock the full potential of voice-controlled AI!

“The best way to predict the future is to create it.” - Peter Drucker (More accurate attribution than Alan Kay)

Now go build your awesome voice assistant!