DEV Community

parmarjatin4911@gmail.com
parmarjatin4911@gmail.com

Posted on

AI Speech Assistant

Introduction:

In this technical blog, we will explore how to build a simple Python speech assistant using the pyttsx3 library. A speech assistant is a program that can convert text into speech and perform certain tasks based on user commands. We will use pyttsx3, a powerful text-to-speech library, and the os module to interact with the operating system. This project will serve as a basic foundation to create your own speech-based applications, and it can be expanded with additional functionalities to suit your needs.

here's a basic example of a speech assistant using pyttsx3 that greets the user and responds to some basic shell commands:

Prerequisites:

Before we begin, make sure you have Python installed on your computer. If you haven't installed the pyttsx3 library, you can do so using pip:

pip install pyttsx3

Let's go through the code step by step to understand what each part does:

Step 1: Import Required Libraries

import pyttsx3
import os

The code starts by importing two libraries: pyttsx3 and os.

pyttsx3 is a text-to-speech conversion library that allows us to convert text into speech.
os is a module that provides a way to interact with the operating system, allowing us to run external commands.
Enter fullscreen mode Exit fullscreen mode

Step 2: Define the speak() Function

def speak(text): engine = pyttsx3.init() engine.say(text) engine.runAndWait()

The speak() function is defined to convert text into speech using the pyttsx3 library.

pyttsx3.init() initializes the text-to-speech engine.
engine.say(text) converts the input text into speech.
engine.runAndWait() plays the speech so that it can be heard.
Enter fullscreen mode Exit fullscreen mode

Step 3: Execute the Main Code Block

if name == "main": speak("Hello! I am your Python speech assistant.")

This block of code runs only when the script is executed directly (not imported as a module). When the script is executed, it greets the user with the message "Hello! I am your Python speech assistant."

Step 4: Create a Speech Assistant Loop

while True: command = input("Enter your command: ").lower()

A continuous loop is started using while True. This loop allows the user to input commands continuously. The input is converted to lowercase using .lower() so that the commands are case-insensitive for easier comparison.

Step 5: Process User Commands

if "stop" in command: speak("Goodbye!")
break
elif "hello" in command:
speak("Hello! How can I assist you?")
elif "notepad" in command:
speak("Opening Notepad.") os.system("notepad")
elif "calculator" in command:
speak("Opening Calculator.") os.system("calc")
else:
speak("Sorry, I couldn't understand that command.")

This part of the code processes the user's commands based on specific keywords.

If the user enters "stop" in their command, the assistant will say "Goodbye!" and break out of the loop, effectively terminating the program.
If the user enters "hello" in their command, the assistant will greet them with "Hello! How can I assist you?"
If the user enters "notepad" in their command, the assistant will say "Opening Notepad." and open the Notepad application using the os.system() function.
If the user enters "calculator" in their command, the assistant will say "Opening Calculator." and open the Calculator application using the os.system() function.
If the user enters any other command that doesn't match the above conditions, the assistant will say "Sorry, I couldn't understand that command."
Enter fullscreen mode Exit fullscreen mode

Top comments (0)