Welcome to the first project in the “Future Innovators: Cool STEM Projects to Change the World” series! Today, we’re going to build an AI-Powered Personal Assistant—your own version of Siri or Alexa, but customized to your liking and completely built by you. This project will introduce you to the exciting world of Artificial Intelligence (AI) and Natural Language Processing (NLP) using Python, giving you hands-on experience in building something fun and useful.
By the end of this post, you will have a fully functioning assistant that can respond to your voice commands, set reminders, play music, and more. The best part? It’s fully customizable—you can add any features you want, like checking the weather or controlling smart home devices.
What You Will Learn
- Basic concepts of AI and speech recognition.
- How to use Python to create a personal assistant.
- How to use the SpeechRecognition library to convert speech to text.
- How to use Pyttsx3 to convert text into speech (TTS).
- How to expand and customize your personal assistant with additional features.
STEM Learning Process
1. Science: Understand how sound waves are converted into data that a computer can process using speech recognition.
2. Technology: Learn how to code in Python and use machine learning-powered libraries for voice recognition and AI.
3. Engineering: Build and design a functional, real-world solution that can assist with daily tasks.
4. Math: Understand how algorithms analyze patterns in audio data to convert speech into text or responses.
What You Need
To get started, you will need:
- A computer with Python 3.x installed.
- A microphone (built-in for most laptops, external for desktops).
- Python Libraries:
- SpeechRecognition: Converts speech to text.
- Pyttsx3: Allows the assistant to speak back.
- Pyaudio: Captures audio from your microphone. You can install these libraries using the following command in your terminal or command prompt:
pip install SpeechRecognition pyttsx3 pyaudio
Setup and Installation Guide
1. Install Python
Download Python 3.x from here, and follow the installation instructions for your operating system.
- Windows/Mac/Linux: During installation, make sure to check the box that says “Add Python to PATH”.
- Verify Installation: After installation, open a terminal or command prompt and type:
python --version
You should see the Python version printed (e.g., Python 3.10
).
2. Install Required Python Libraries
Once Python is installed, open your terminal and install the necessary libraries:
pip install SpeechRecognition pyttsx3 pyaudio
If you encounter issues with Pyaudio, particularly on Windows, follow these steps:
- Download the appropriate Pyaudio wheel from here.
- Install the downloaded wheel using:
pip install path_to_downloaded_pyaudio_wheel
For Mac users, you may need to install portaudio:
brew install portaudio
Then install pyaudio again:
pip install pyaudio
3. Set Up Your Microphone
Make sure your microphone is correctly set up to interact with your assistant.
- Windows:
- Right-click the speaker icon in the system tray and select Sounds.
- Go to the Recording tab, ensure your microphone is enabled, and set it as the Default Device.
- Mac:
- Go to System Preferences > Sound.
- Under the Input tab, select your microphone and adjust the input volume.
- Linux:
- Go to Settings > Sound, and make sure your microphone is selected as the input device.
4. Test Your Microphone
To make sure your microphone is working, run this Python script:
import speech_recognition as sr
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Say something:")
audio = recognizer.listen(source)
try:
print("You said: " + recognizer.recognize_google(audio))
except sr.UnknownValueError:
print("Sorry, I could not understand.")
except sr.RequestError:
print("Sorry, there was an issue with the recognition service.")
If the script prints what you said, your microphone is working properly!
Building the AI-Powered Personal Assistant
Step 1: Import Required Libraries
First, import the necessary Python libraries for speech recognition and text-to-speech.
import speech_recognition as sr # For speech input
import pyttsx3 # For text-to-speech output
Step 2: Initialize the Assistant
Next, initialize the text-to-speech engine so your assistant can talk back to you.
# Initialize the pyttsx3 engine
engine = pyttsx3.init()
# Function to make the assistant speak
def speak(text):
engine.say(text)
engine.runAndWait()
Step 3: Listen for Voice Commands
Now, let’s enable your assistant to listen for your voice commands and convert them into text.
# Initialize the recognizer
recognizer = sr.Recognizer()
# Function to listen for voice input
def listen():
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio)
print(f"You said: {command}")
return command.lower()
except sr.UnknownValueError:
speak("Sorry, I didn't catch that. Could you repeat?")
return "None"
except sr.RequestError:
speak("Sorry, I'm having trouble connecting to the recognition service.")
return "None"
Step 4: Add Commands
Let’s define some basic tasks your assistant can perform, such as playing music or setting reminders.
def process_command(command):
if "play music" in command:
speak("Playing your favorite music!")
elif "reminder" in command:
speak("What would you like to be reminded about?")
reminder = listen()
speak(f"Reminder set: {reminder}")
elif "joke" in command:
speak("Why don't scientists trust atoms? Because they make up everything!")
elif "stop" in command:
speak("Goodbye!")
exit()
else:
speak("I'm not sure how to do that yet, but I'm learning!")
Step 5: Run the Assistant
Finally, we create a loop that keeps the assistant running and listening for new commands.
if __name__ == "__main__":
speak("Hello! How can I assist you today?")
while True:
command = listen()
if command != "None":
process_command(command)
Custom Features
Here are some ways you can customize and expand your assistant:
Custom Feature 1: Weather Checker
- Install the Requests Library:
pip install requests
- Add the Weather Function using an API like OpenWeatherMap:
import requests
def get_weather(city):
api_key = "your_openweathermap_api_key"
base_url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(base_url)
weather_data = response.json()
if weather_data["cod"] != "404":
main = weather_data["main"]
temperature = main["temp"]
speak(f"The temperature in {city} is {temperature} degrees Celsius.")
else:
speak("City not found.")
- Modify the
process_command()
function to include weather functionality:
elif "weather" in command:
speak("Which city's weather would you like to know?")
city = listen()
get_weather(city)
Custom Feature 2: Time Telling
Add the ability to tell the current time:
from datetime import datetime
def tell_time():
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
speak(f"The current time is {current_time}.")
Then modify the process_command()
function to respond to time queries:
elif "time" in command:
tell_time()
Testing Your Assistant
Run the Program:
To test your assistant, open your terminal, navigate to the directory where your code is saved, and run:
python assistant.py
Try commands like:
- “Play music” (your assistant should respond with a message pretending to play music).
- “Tell me a joke” (your assistant should respond with a joke).
- “Set a reminder” (
you can practice setting custom reminders).
Test Custom Features:
- Weather Checker: After integrating the weather feature, say “What’s the weather in [city]?” and see if it responds with the current temperature.
- Time Telling: Say “What’s the time?”, and your assistant should tell you the current time.
What’s Next?
You’ve now built a basic AI-powered personal assistant! You can keep adding more features—like controlling smart home devices, integrating with Google Calendar, or even playing actual music by connecting to Spotify. The possibilities are endless, and the assistant is entirely customizable to suit your needs.
In next week’s post, we’ll be creating a Smart Mirror—a futuristic mirror that displays the weather, calendar, and news as you go about your day. Stay tuned for more cool STEM projects, released weekly!
Resources and Additional Help
- Python Official Documentation: Python Docs
- SpeechRecognition Documentation: SpeechRecognition Docs
- Pyttsx3 Documentation: Pyttsx3 Docs
- Google Cloud Speech-to-Text API: Google Cloud Speech-to-Text
- Stack Overflow: Stack Overflow
If you run into issues or need more help, feel free to explore these resources or post questions on forums like Stack Overflow or post your comments here!
That’s your AI-Powered Personal Assistant project—keep innovating and have fun!
What’s Next?
In the next post, we’ll build a Smart Mirror—a futuristic device that shows the weather, calendar, news, and even gives you a compliment while you get ready in the morning. Stay tuned for more exciting STEM projects every week!
Subscribe to our email newsletter to get the latest posts delivered right to your email.
Comments