According to the Rasa website, it is a conversational AI platform. In our smart home project, we will use it for intent recognition and entity extraction. Although the current version of Rasa is 2.7.x, unfortunately, it is rather challenging to install Rasa 2.x to a Raspberry Pi device, see the discussions in Rasa community and in GitHub. I myself also tried many ways to build Rasa 2 on Raspberry without luck. Therefore, we will use the 1.10.x version below.
Train a Rasa Model
Training a model, especially a large model, on a Raspberry Pi device is slow. Instead, we shall train a model in a powerful server/laptop/desktop and then transfer the model to Raspberry Pi device. Below we assume
- Ubuntu 20.04 running in a server/laptop/desktop
- Docker, python, and git are installed in the Ubuntu
# get the script and yml files from https://github.com/SmartopiaHub/rasa
git clone https://github.com/SmartopiaHub/rasa
# to prevent permission issue
sudo chmod -R 777 rasa
cd rasa
# generate some training data
# if ruamel.yaml not found, the install it by pip3 install ruamel.yaml
python3 gen-training-sample.py
cp nlu.md rasa/data/
mkdir rasa/models && cd rasa
# train a Rasa model using version 1.10.20
# this version is picked to match the Rasa server running in Raspberry Pi
docker run -v $(pwd):/app rasa/rasa:1.10.20-full train
# rename the model
move models/* models/smartopia.tar.gz
Now the trained model is stored in ~/rasa/rasa/models/smartopia.tar.gz. Transfer the model to the Raspberry Pi device, for example, to the folder /rasa/smartopia.tar.gz
Install Rasa Server in Raspberry Pi
To run the trained model, we assume the following Raspberry Pi device
- Raspberry Pi 4B + Raspberry Pi OS (Buster, 32 bits, released on May 7, 2021).
The easiest way to install Rasa 1.x in Raspberry Pi is to use the docker image maintained by koenvervloesem.
The first step is to install docker if not onboard yet.
sudo apt update && sudo apt upgrade
# install docker
sudo apt-get install -y curl
curl -sSL https://get.docker.com | sh
# add pi to the docker group; to take effect, logout and login again
sudo usermod -aG docker pi
logout
# install docker-compose
# however, I found some issue of using docker-compose for the Rasa API
sudo apt install -y docker-compose
Now we pull the image from docker hub. At the time of writing, the latest version is 1.10.25. We also prepare some folders to host Rasa projects.
# pull docker image (version: 1.10.25)
sudo docker pull koenvervloesem/rasa
# prepare folders for rasa
sudo mkdir /rasa && sudo chmod -R pi /rasa
Now we transfer the trained model smartopia.tar.gz to the folder /rasa, either via a USB drive or SFTP. To start the service, we use the following command, where 5015 can be replaced with any other available port number.
# get into the /rasa folder and make sure that smartopia.tar.gz is there
cd /rasa
# now start the Rasa server
docker run -d --name=rasa -v $(pwd):/app -p 5005:5005 koenvervloesem/rasa run --enable-api -m /app/smartopia.tar.gz
# to stop the server, use the following command
docker container stop rasa
# to restart the server
docker container start rasa
Some Other Commands
# generate an initial project in the current folder
# make sure that the current folder is empty
# add --no-prompt option to avoid a permission error
docker run -v $(pwd):/app koenvervloesem/rasa init --no-promp
# use the following command to interact with the bot
docker run -v $(pwd):/app koenvervloesem/rasa shell
# enable API with the default model
docker run -v $(pwd):/app -p 5005:5005 koenvervloesem/rasa run --enable-api
# if API is enabled, we can test it
# change localhost below to the IP address when needed
# More about API: https://legacy-docs-v1.rasa.com/api/http-api
# get status
curl -GET localhost:5005/status
# predict an action
curl -POST localhost:5005/model/predict -d '{"q":"hello there"}'
# recognize intent and entities
curl -POST localhost:5005/model/parse -d '{"text":"turn off the light"}'
# download a docker image to the folder ~/rasa-v1.10.25
# useful if you need to transfer an image to a machine without Internet
curl -Lo download-frozen-image-v2.sh https://raw.githubusercontent.com/moby/moby/master/contrib/download-frozen-image-v2.sh
sudo chmod +x download-frozen-image-v2.sh
./download-frozen-image-v2.sh rasa-v1.10.25 koenvervloesem/rasa
tar -C 'rasa-v1.10.25' -cf 'rasa-v1.10.25.tar' .