Voice assistant on Raspberry Pi
- Audio Processing: pyaudio
- Voice AI: vapi.ai
- Docker installed on your system
- Make (optional, for using Makefile commands)
-
Build the Docker image
make build # or without make: # docker build -t assistant.py .
-
Run the application
make run # or without make: # docker run --rm assistant.py
-
Run the application locally
sudo apt install portaudio19-dev python3 -m venv .venv --system-site-packages source .venv/bin/activate pip install -r requirements.txt python3 main.py -
Control the volume
amixer get Master # amixer -c 0 sset Master 100% amixer set Master 10%+ # increase 10% volume amixer set Master 10%- # decrease 10% volume amixer set Master unmute
-
Control the volume
| Command | Description |
|---|---|
make build |
Build the Docker image |
make run |
Run the container (executes main.py) |
make run-env |
Run with environment variables from .env file |
make shell |
Open an interactive bash shell in the container |
make dev |
Run with volume mount for development (live code reload) |
make python |
Open a Python REPL in the container |
make logs |
Show container logs |
make stop |
Stop the running container |
make clean |
Remove container and image |
Create a .env file in the project root (copy from .env.example):
VAPI_API_KEY=your_vapi_api_key_here
VAPI_ASSISTANT_ID=your_vapi_assistant_id_hereUse the development mode to mount your local code into the container:
make devThis allows you to edit code locally and run it immediately in the container without rebuilding.
assistant.py/
├── Dockerfile # Docker configuration
├── Makefile # Build and run commands
├── requirements.txt # Python dependencies
├── main.py # Main application script
├── .env # Environment variables (create from .env.example)
└── README.md # This file
- Add the package to
requirements.txt - Rebuild the Docker image:
make build
-
Create your Python script in the project directory
-
Run it using:
docker run --rm -v $(PWD):/app assistant.py python3 your_script.py
import sounddevice as sd
import numpy as np
# Record 5 seconds of audio
duration = 5 # seconds
fs = 44100 # Sample rate
recording = sd.rec(int(duration * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finishedimport pyaudio
import numpy as np
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=44100,
output=True)
# Generate and play a tone
t = np.linspace(0, 1, 44100)
tone = np.sin(2 * np.pi * 440 * t) # 440 Hz
stream.write(tone.astype(np.float32).tobytes())
stream.close()
p.terminate()If you encounter audio device issues on Raspberry Pi:
-
Ensure audio devices are properly connected
-
Check device permissions
-
You might need to run Docker with additional privileges:
docker run --rm --privileged assistant.py
- Ensure
.envfile exists in the project root - Use
make run-envinstead ofmake run - Check file permissions on
.env
Feel free to submit issues and enhancement requests!