Build Your Own Mini Alexa with Python: A Step-by-Step Guide

Shubha AryaShubha Arya
3 min read

Build Your Own Mini Alexa with Python: A Step-by-Step Guide

Have you ever wanted to create your own voice assistant like Alexa or Google Assistant? In this post, I will show you how I built a Mini Alexa using Python!

This simple project can listen to your voice, understand basic commands, respond to you, and even open websites or gather information from Wikipedia.

Let’s get started!

Tech Stack

  • Python

  • pyttsx3 (text-to-speech)

  • SpeechRecognition (speech-to-text)

  • Wikipedia API

  • Webbrowser module

  • JSON (for command-response mapping)

How It Works Here’s a breakdown:

  • Listen: Uses your microphone to capture voice input.

  • Understand: Converts speech to text.

  • Respond: Matches commands with responses or actions.

  • Speak: Replies using text-to-speech.

The Code Below is the full working code for Mini Alexa:

import pyttsx3
import speech_recognition as sr
import json
import datetime
import webbrowser
import wikipedia

# Initialize the text-to-speech engine
engine = pyttsx3.init()
engine.setProperty('rate', 150)  # Speed of speech
engine.setProperty('volume', 1.0)  # Volume (0.0 to 1.0)

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

def listen():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = r.listen(source)
        try:
            command = r.recognize_google(audio)
            print("You said:", command)
            return command.lower()
        except sr.UnknownValueError:
            speak("Sorry, I did not understand.")
            return ""
        except sr.RequestError:
            speak("Sorry, there is a network issue.")
            return ""

def respond(command):
    if 'your name' in command:
        speak("I am Mini Alexa, your voice assistant.")
    elif 'time' in command:
        time = datetime.datetime.now().strftime('%I:%M %p')
        speak(f"The time is {time}")
    elif 'open google' in command:
        webbrowser.open('https://www.google.com')
        speak("Opening Google")
    elif 'who is' in command:
        person = command.replace('who is', '')
        info = wikipedia.summary(person, sentences=2)
        speak(info)
    elif 'stop' in command or 'bye' in command:
        speak("Goodbye! Have a nice day.")
        exit()
    else:
        speak("Sorry, I don't know that command yet.")

if __name__ == "__main__":
    speak("Hi, I am Mini Alexa. How can I help you?")
    while True:
        cmd = listen()
        respond(cmd)

Key Parts Explained

  • Text-to-Speech: pyttsx3 lets Python speak the text you provide.

  • Speech Recognition: SpeechRecognition listens to your microphone and turns your voice into text.

  • Wikipedia API: Provides quick summaries for “Who is…” questions.

  • Webbrowser Module: Can open Google or any other site you prefer.

Sample Commands Try saying:

  • “What is your name?”

  • “What’s the time?”

  • “Open Google”

  • “Who is Elon Musk?”

  • “Stop” or “Bye” to exit

How to Run Install dependencies:

pip install pyttsx3 SpeechRecognition wikipedia

Make sure you have a microphone.

Run the script:

python mini_alexa.py

Next Steps This is just the beginning! You can:

  • Add more custom commands.

  • Integrate smart home APIs.

  • Use AI models for smarter responses.

Conclusion Creating a Mini Alexa is a great way to learn about speech recognition, APIs, and Python automation. I hope this encourages you to build your own smart assistant!

Got questions? Want to add more features? Let’s discuss in the comments!

0
Subscribe to my newsletter

Read articles from Shubha Arya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shubha Arya
Shubha Arya

Data Scientist | Bestselling Author | Fitness Enthusiast | Reforming education through skills-based learning and lifelong curiosity.