Why buy an Alexa if you can make your own?
Secure your privacy while reaping the benefits!
Personal assistants on the market like Google Home and Amazon Alexa offer a lot of functionality for their value, but work (quite literally) inside a black box. The uncertainty of not knowing how your data is being processed is a genuine concern among many others that can cause users like me to avoid buying such products.
In this post, I will share with you how I used Raspberry Pi and Python’s requests library to construct my own voice-activated personal assistant. Since you’ll know everything going on under the hood, privacy issues and the lack of customisability becomes less of a problem.
Configuring your assistant on your desktop/laptop
Before we configure the Raspberry Pi computer, we can start work on the assistant using our development environment. We’ll start by creating a Python script assistant.py for us to define a class describing our assistant and its functions. The first thing we have to do is to import the relevant packages to ensure the assistant can receive and parse audio commands, and reply using a text-to-speech (TTS) engine.
We can first configure the TTS engine by using the pyttx3 package.
After this, we need to define a method for the assistant to listen to its environment and parse any speech-like audio. If the assistant’s name is detected, the assistant will send the command after it to another method that carries out different assistant functions depending on the command. Python does not support multithreading due to the Global Interpreter Lock, but we can work around this using a callback function.
Now we can define the method take_command to detect key words or phrases from the command to determine the user’s intent. You could also train an NLP language model to do the same thing, but this project showcase is intended for people without machine learning experience. Feel free to do so though, as it would make the assistant a lot more robust.
We can now start to define methods for the different functions of the assistant. Since I use Wikipedia on a daily, my assistant to be able to do that for me hands-free. I did this by using the Wikipedia library for Python.
Besides this, I would also like my assistant to retrieve weather data from an API. We can do this using the Python’s requests library.
There are many APIs you can use to increase the functionality of your assistant. Here are some functions I’ve implemented that you can try out for yourself, but you shouldn’t limit yourself to this list:
- Solving math problems using Wolfram Alpha
- Telling the date and time
- Greeting the user depending on the time of day and giving a self-introduction
- Telling jokes
- Shutting down
- Reading the latest news
- Recommending recipes to try out
After defining your methods, your assistant is ready! Just create an object from the Assistant class and call its listen_background method.
How can we take this further?
Currently, the assistant is limited by the hardware your development environment offers. Using hardware like a Raspberry Pi computer (RPi) allows us to do more interesting things by using its General Purpose Input/Output (GPIO) pins to interact with sensors and actuators in the real-world. In this part of the article, I’ll go through the steps needed to run your assistant on an RPi computer.
What you’ll need
- A Raspberry Pi computer
- A USB microphone
- A speaker with a HDMI/USB input
Steps to take
- Connect the microphone and speaker to the RPi.
- Upload your script from your development environment onto the RPi. You can do this using a physical connection or a cloud service like Google Drive.
- Install the relevant packages. Since RPi comes with Raspberry Pi OS (Debian-based), a few more steps are needed to ensure pyttsx3 can function. Open a terminal and run this command:
sudo apt-get install python-espeak && sudo apt-get update && sudo apt-get install espeak
- Run the script on the RPi.
Congratulations! You’ve made your own voice-activated personal assistant! If you’re interested in other projects I’ve done or want to reach out to me for anything, here are my Github and LinkedIn accounts.