Overview

In this tutorial, we’ll create a voice-controlled AI assistant using Python. This assistant will respond to voice commands, perform web searches, tell the weather, read emails, and even manage your daily tasks. It’s a great project to explore natural language processing, speech recognition, and automation with Python.


Why Build a Voice-Controlled AI Assistant?

With AI assistants like Siri, Alexa, and Google Assistant becoming common, building your own lets you understand the underlying technologies while tailoring it to your specific needs.


Step 1: Setting Up the Environment

Install Required Libraries

Install the necessary libraries for speech recognition, text-to-speech, and automation:


pip install SpeechRecognition pyttsx3 pywhatkit pyaudio

Step 2: Listening to Voice Commands

Set up speech recognition to capture voice commands using your microphone.


import speech_recognition as sr

def listen_command():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        try:
            audio = recognizer.listen(source)
            command = recognizer.recognize_google(audio)
            print(f"You said: {command}")
            return command.lower()
        except sr.UnknownValueError:
            print("Sorry, I didn't catch that.")
            return None

Step 3: Responding with Text-to-Speech

Use pyttsx3 to make your assistant speak responses.


import pyttsx3

engine = pyttsx3.init()

def speak(text):
    engine.say(text)
    engine.runAndWait()

speak("Hello! How can I assist you today?")

Step 4: Adding Basic Commands

Let’s add functionality to respond to basic tasks like saying the time, performing web searches, or opening apps.


import datetime
import pywhatkit

def process_command(command):
    if "time" in command:
        now = datetime.datetime.now().strftime("%H:%M")
        speak(f"The time is {now}")
    elif "search for" in command:
        query = command.replace("search for", "").strip()
        speak(f"Searching for {query}")
        pywhatkit.search(query)
    elif "play" in command:
        song = command.replace("play", "").strip()
        speak(f"Playing {song}")
        pywhatkit.playonyt(song)
    else:
        speak("I'm sorry, I can't perform that task yet.")

Step 5: Adding Weather Reports

Integrate a weather API (e.g., OpenWeatherMap) to fetch current weather information.

  1. Sign Up for an API Key on OpenWeatherMap.
  2. Install the requests library:

   pip install requests
  1. Fetch weather data:

import requests

API_KEY = "your_openweathermap_api_key"
BASE_URL = "http://api.openweathermap.org/data/2.5/weather"

def get_weather(city):
    params = {"q": city, "appid": API_KEY, "units": "metric"}
    response = requests.get(BASE_URL, params=params)
    data = response.json()

    if data.get("cod") == 200:
        weather = data["weather"][0]["description"]
        temperature = data["main"]["temp"]
        speak(f"The weather in {city} is {weather} with a temperature of {temperature}°C.")
    else:
        speak("Sorry, I couldn't find the weather for that location.")

Step 6: Combining It All

Here’s the full workflow:

  1. Listen to commands.
  2. Process the command and take action.
  3. Speak back the results.

if __name__ == "__main__":
    speak("Initializing your assistant...")
    while True:
        command = listen_command()
        if command:
            if "exit" in command or "stop" in command:
                speak("Goodbye!")
                break
            elif "weather in" in command:
                city = command.replace("weather in", "").strip()
                get_weather(city)
            else:
                process_command(command)

Step 7: Advanced Features (Optional)

  1. Email Reading: Use imaplib to fetch and read emails aloud.
  2. Task Management: Integrate with Google Calendar for scheduling.
  3. AI Chat: Use OpenAI’s GPT API to make your assistant conversational.
  4. Voice Activation: Add a wake word (e.g., “Hey Assistant”) for hands-free activation.