How to make a voice assistant on Python

Learn how to build your own voice assistant with Python, complete with an example.

Creating a Voice Assistant with Python

Creating a voice assistant with Python is a great way to make your applications more interactive and useful. With a few lines of code, you can create a voice assistant that can respond to commands, answer questions, and even interact with other programs. Here, we'll show you how to create a voice assistant with Python.

The first step is to install the necessary packages. We'll need pyaudio and speech_recognition for voice input and pyttsx3 for text-to-speech output. You can install them using pip.

pip install pyaudio
pip install speech_recognition
pip install pyttsx3

Now, we'll need to create a function that will handle voice input. This function will take the audio input and convert it to text using the speech_recognition library. We'll also define a list of commands our voice assistant should recognize.

import speech_recognition as sr

def recognize_speech_from_mic(recognizer, microphone):
    """Transcribe speech from recorded from `microphone`.
    Returns a dictionary with three keys:
    "success": a boolean indicating whether or not the API request was
               successful
    "error":   `None` if no error occured, otherwise a string containing
               an error message if the API could not be reached or
               speech was unrecognizable
    "transcription": `None` if speech could not be transcribed,
               otherwise a string containing the transcribed text
    """
    # check that recognizer and microphone arguments are appropriate type
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError("`recognizer` must be `Recognizer` instance")

    if not isinstance(microphone, sr.Microphone):
        raise TypeError("`microphone` must be `Microphone` instance")

    # adjust the recognizer sensitivity to ambient noise and record audio
    # from the microphone
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)

    # set up the response object
    response = {
        "success": True,
        "error": None,
        "transcription": None
    }

    # try recognizing the speech in the recording
    # if a RequestError or UnknownValueError exception is caught,
    #     update the response object accordingly
    try:
        response["transcription"] = recognizer.recognize_google(audio)
    except sr.RequestError:
        # API was unreachable or unresponsive
        response["success"] = False
        response["error"] = "API unavailable"
    except sr.UnknownValueError:
        # speech was unintelligible
        response["error"] = "Unable to recognize speech"

    return response

commands = ["turn on the lights", "turn off the lights",
            "open the door", "close the door"]

Now, we need to create a function to handle the command recognition. This function will take the text input and compare it to the list of commands we defined earlier. If the text matches any of the commands, the function will return True and call the appropriate function.

def recognize_command(text):
    for phrase in commands:
        if phrase in text:
            return True
    return False

def execute_command(text):
    if recognize_command(text):
        # execute command here
        pass

Finally, we need to create a loop that will continuously listen for audio input and call the appropriate functions. We'll set up the microphone and recognizer, then start the loop.

# set up the microphone and recognizer
mic = sr.Microphone()
recognizer = sr.Recognizer()

# start the loop
while True:
    # listen for audio input
    response = recognize_speech_from_mic(recognizer, mic)

    # check if audio was successfully transcribed
    if response["success"]:
        # call the appropriate function
        execute_command(response["transcription"])
    else:
        print("I didn't catch that. What did you say?n")

And that's it! With just a few lines of code, you can create a voice assistant that can respond to commands and interact with other programs.

Answers (0)