Install Raspberry Pi OS 64-bit Desktop
For the storybook, you will need the 64-bit version of the latest Raspberry Pi OS because the OpenAI libraries will only install on that version. The Desktop version makes displaying graphics much simpler. You can refer to the CircuitPython Libraries on Linux and Raspberry Pi guide for more help setting it up.
After installing, make sure everything is up to date:
sudo apt update sudo apt upgrade sudo pip3 install --upgrade setuptools
Reboot after it completes with:
sudo reboot
Setup Virtual Environment
Starting with the Bookworm version of Raspberry Pi OS, you will need to install your python modules in a virtual environment. You can find more information in the Python Virtual Environment Usage on Raspberry Pi guide. To Install and activate the virtual environment, use the following commands:
sudo apt install python3-venv python3 -m venv story --system-site-packages
You will need to activate the virtual environment every time the Pi is rebooted. To activate it:
source story/bin/activate
To deactivate, you can use deactivate
, but leave it active for now.
Blinka Installation
Once you have everything set up, you will need to open a terminal and install Blinka. Refer to the Installing CircuitPython Libraries on Raspberry Pi page to quickly get up and running.
Run the raspi-blinka
script, which will install Blinka and update some additional environment settings. You will be installing the NeoPixel library further down, which requires root access, so you will need to install everything with sudo
:
cd ~ pip3 install --upgrade adafruit-python-shell wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py sudo -E env PATH=$PATH python3 raspi-blinka.py
Reboot after it completes.
Install OpenAI Library
The main library that you will need for this project is the OpenAI library, which allows you to interact with the OpenAI:
pip3 install --upgrade openai
Additional Required Dependencies
A few more additional libraries and other dependencies are required including the NeoPixel and LED Animation libraries:
sudo apt install libpulse-dev pulseaudio apulse python3-pyaudio pip3 install --upgrade SpeechRecognition pygame adafruit-circuitpython-neopixel adafruit-circuitpython-led-animation rpi-backlight whisper-mic
Enable Backlight Control
The easiest way to enable backlight control is through the rpi-backlight
library, which was installed in the previous section. In order for the library to work properly, you will need to add a rules file, which gives it access to the lower level hardware. You can create the rules file by running the following command:
echo 'SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"' | sudo tee -a /etc/udev/rules.d/backlight-permissions.rules SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"
Download the Files
This project uses quite a few files. The easiest way to add the files to your Pi with the folders intact and without downloading the entirety of the Learn code is to use git with the sparse-checkout option. You can copy the relevant folder with the following commands:
cd ~ git clone -n --depth=1 --filter=tree:0 https://github.com/adafruit/Adafruit_Learning_System_Guides.git cd Adafruit_Learning_System_Guides git sparse-checkout set --no-cone Magic_AI_Storybook git checkout mv Magic_AI_Storybook .. cd ../Magic_AI_Storybook rm -Rf ../Adafruit_Learning_System_Guides
Move Prompt and Key Files
There are a couple of files that will need to be moved outside of this folder. The first file is bookprompt.txt. You will be setting the file system to be read-only soon and moving this file to the /boot folder will allow you to easily change it on the SD Card plugged into a computer:
sudo mv bookprompt.txt /boot/ > /dev/null 2>&1
The second file keys.txt is where you will provide your API keys. It is moved outside of the folder in order to prevent accidentally committing the code to the repository. It is placed in a text file instead of using environment variables in case you want to have the script start up automatically where you can't depend on the contents of the environment variables.
mv keys.txt ..
Add Your OpenAI Key
Add the secret key you created when you set up your OpenAI account to the keys.txt file in your home directory. From the command line, you can open it in your favorite editor such as nano:
nano ~/keys.txt
Change the value of the key so it reads something like:
OPENAI_API_KEY = sk-b6j4FFt78209dkifJhld783GtkP5
Update ALSA Config
With a default installation of the SpeechRecognition library and the ALSA config, a large number of warnings will appear whenever voice input is initiated. While the warnings are being printed to the console, there is a noticeable lag of a couple seconds before it actually starts listening. You can reduce this by making the following changes.
Open /usr/share/alsa/alsa.conf in a text editor. About halfway down you will find these lines:
# redirect to load-on-demand extended pcm definitions pcm.cards cards.pcm pcm.default cards.pcm.default pcm.sysdefault cards.pcm.default pcm.front cards.pcm.front pcm.rear cards.pcm.rear pcm.center_lfe cards.pcm.center_lfe pcm.side cards.pcm.side pcm.surround21 cards.pcm.surround21 pcm.surround40 cards.pcm.surround40 pcm.surround41 cards.pcm.surround41 pcm.surround50 cards.pcm.surround50 pcm.surround51 cards.pcm.surround51 pcm.surround71 cards.pcm.surround71 pcm.iec958 cards.pcm.iec958 pcm.spdif iec958 pcm.hdmi cards.pcm.hdmi pcm.dmix cards.pcm.dmix pcm.dsnoop cards.pcm.dsnoop pcm.modem cards.pcm.modem pcm.phoneline cards.pcm.phoneline
Comment out everything from pcm.front cards.pcm.front
on down with a #
at the beginning of the line. The result should look like this:
# redirect to load-on-demand extended pcm definitions pcm.cards cards.pcm pcm.default cards.pcm.default pcm.sysdefault cards.pcm.default #pcm.front cards.pcm.front #pcm.rear cards.pcm.rear #pcm.center_lfe cards.pcm.center_lfe #pcm.side cards.pcm.side #pcm.surround21 cards.pcm.surround21 #pcm.surround40 cards.pcm.surround40 #pcm.surround41 cards.pcm.surround41 #pcm.surround50 cards.pcm.surround50 #pcm.surround51 cards.pcm.surround51 #pcm.surround71 cards.pcm.surround71 #pcm.iec958 cards.pcm.iec958 #pcm.spdif iec958 #pcm.hdmi cards.pcm.hdmi #pcm.dmix cards.pcm.dmix #pcm.dsnoop cards.pcm.dsnoop #pcm.modem cards.pcm.modem #pcm.phoneline cards.pcm.phoneline
Create Desktop and Autostart Shortcuts
To create a desktop shortcut icon, there is a small script included that uses your current username to write the correct paths. While the script can correctly run using sudo, just running it without sudo is preferable, so that you can easily modify or delete the shortcuts in the future. To run the script, just use the following commands:
cd ~/Magic_AI_Storybook python make_shortcut.py
Now the script should automatically run upon startup. If you don't want it to automatically run, you can simply delete the file at ~/.config/autostart/storybook.desktop
.
Optional: Making the File System Read-only
In order to turn the Raspberry Pi off without risking damaging the file system, you may wish to put it in read-only mode, which is called the "overlay". You can still make changes directly to files on the SD Card while it is plugged into another computer if needed or you can disable the Overlay File System to allow writing again.
If you would like to customize your code, you may want to wait on this step and come back after making changes.
To enable it, run sudo raspi-config
, then go to Performance Options → Overlay File System. Choose Yes to enable the Overlay File System. This step may take a moment to finish. Then choose Yes again to write-protect the boot partition.
Go ahead and reboot. The file system should now be read-only. Any new file changes will be discarded when the Pi is rebooted.
Text editor powered by tinymce.