Here comes new Siri again
Back to Tutorials
aiTutorialbeginner

Here comes new Siri again

June 6, 202615 views4 min read

Learn to build a basic voice assistant similar to Siri using Python's speech recognition and text-to-speech libraries. This beginner-friendly tutorial teaches you how to create a command-based assistant that listens, understands, and responds to voice commands.

Introduction

In this tutorial, you'll learn how to create a basic voice assistant similar to Siri using Python and speech recognition technology. We'll build a simple command-based assistant that can listen to your voice, understand basic commands, and respond accordingly. This project will teach you fundamental concepts of voice recognition, natural language processing, and how AI assistants like Siri work under the hood.

Prerequisites

  • Basic understanding of Python programming
  • Python 3.6 or higher installed on your computer
  • Internet connection for downloading required packages
  • Microphone access on your device

Step-by-step instructions

Step 1: Set Up Your Python Environment

Install Required Packages

First, we need to install the necessary Python packages for speech recognition and text-to-speech functionality. Open your terminal or command prompt and run:

pip install SpeechRecognition pyttsx3

Why we do this: The SpeechRecognition package allows us to convert spoken words into text, while pyttsx3 handles text-to-speech conversion so our assistant can talk back to us.

Step 2: Create Your Basic Voice Assistant

Write the Main Code

Create a new Python file called voice_assistant.py and add the following code:

import speech_recognition as sr
import pyttsx3

# Initialize the speech recognizer and text-to-speech engine
recognizer = sr.Recognizer()
engine = pyttsx3.init()

# Function to speak text
def speak(text):
    engine.say(text)
    engine.runAndWait()

# Function to listen for commands
def listen():
    with sr.Microphone() as source:
        print("Listening...")
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)
        
    try:
        # Convert speech to text
        command = recognizer.recognize_google(audio)
        print(f"You said: {command}")
        return command.lower()
    except sr.UnknownValueError:
        print("Sorry, I didn't understand that.")
        return ""
    except sr.RequestError:
        print("Could not request results; check your internet connection.")
        return ""

# Main loop
if __name__ == "__main__":
    speak("Hello! I am your voice assistant. How can I help you?")
    
    while True:
        command = listen()
        
        if 'hello' in command:
            speak("Hello there! How can I assist you?")
        elif 'what is your name' in command:
            speak("I am your voice assistant.")
        elif 'time' in command:
            from datetime import datetime
            current_time = datetime.now().strftime("%H:%M")
            speak(f"The current time is {current_time}")
        elif 'exit' in command or 'quit' in command:
            speak("Goodbye!")
            break
        else:
            speak("I'm sorry, I don't understand that command.")

Why we do this: This code sets up the basic framework for our assistant, including initializing the speech recognition and text-to-speech components, and creating functions to handle listening and speaking.

Step 3: Test Your Voice Assistant

Run the Program

Save your file and run it using:

python voice_assistant.py

Why we do this: Running the program will start your assistant and allow you to test its basic functionality.

Step 4: Interact with Your Assistant

Give Voice Commands

When you see "Listening..." on the screen, speak clearly and try these commands:

  • "Hello"
  • "What is your name"
  • "What time is it"
  • "Exit" or "Quit"

Why we do this: Testing with different commands helps you understand how speech recognition works and how your assistant processes different inputs.

Step 5: Enhance Your Assistant

Add More Features

Let's expand our assistant by adding more commands. Modify the main loop in your code to include:

elif 'weather' in command:
    speak("I can't check the weather right now, but you can ask me to search for it online.")
elif 'search' in command:
    speak("What would you like me to search for?")
    search_term = listen()
    if search_term:
        speak(f"Searching for {search_term} on the internet.")

Why we do this: Adding more features makes your assistant more useful and demonstrates how real voice assistants like Siri can handle various tasks.

Step 6: Improve Recognition Accuracy

Adjust for Better Performance

For better speech recognition, you can improve the code by adding:

# Add this inside the listen() function
recognizer.energy_threshold = 300
recognizer.pause_threshold = 0.5

Why we do this: Adjusting these parameters helps the assistant better distinguish between speech and background noise, improving accuracy.

Step 7: Make It More User-Friendly

Enhance the Interface

Add visual feedback by modifying the speak function:

def speak(text):
    print(f"Assistant: {text}")
    engine.say(text)
    engine.runAndWait()

Why we do this: Visual feedback helps users understand what the assistant is saying, especially useful for debugging and user experience.

Summary

Congratulations! You've built a basic voice assistant similar to Siri using Python. This tutorial taught you how to:

  • Use speech recognition to convert voice to text
  • Use text-to-speech to generate voice responses
  • Process voice commands and respond appropriately
  • Improve recognition accuracy through parameter adjustments

This foundational knowledge is similar to what Apple's Siri uses in its backend systems. While our assistant is simple, it demonstrates the core concepts behind modern voice assistants. As you continue learning, you can expand this assistant with more complex features like web searching, calendar integration, and natural language understanding.

Source: The Verge AI

Related Articles