# Magic Storybook with ChatGPT

## Overview

https://youtu.be/Jgv0AHgn_24

> ‘I wonder,’ he said to himself, ‘what’s in a book while it’s closed. Oh, I know it’s full of letters printed on paper, but all the same, something must be happening, because as soon as I open it, there’s a whole story with people I don’t know yet and all kinds of adventures, deeds and battles. And sometimes there are storms at sea, or it takes you to strange cities and countries. All those things are somehow shut in a book. Of course you have to read it to find out. But it’s already there, that’s the funny thing. I just wish I knew how it could be.’

> Bastian, the Neverending Story

We live in an age of wonder. Every day, new innovations and inventions change the way we interact with the world around us and with each other. Many of the marvels we read about in the science fiction stories of our youth are suddenly, magically possible with a little clever engineering and a healthy dose of imagination.&nbsp;

This guide will show you how to create your own never-ending story book using a Raspberry Pi and a touch screen display. Speak your request out loud, and the book will write you an original story based on your prompt. The software uses Open AI to leverage ChatGPT, a language-based AI, to fill an endless storybook with imaginative&nbsp;stories.

This code includes an editable prompt file called **bookprompt.txt** , so you can dictate the character names, story length, writing style, or any other parameters you'd like. Your daughter will love reading stories about herself and her cat as they adventure through an endless world of imagination, in the style of Dr. Seuss!

What will you imagine?

![](https://cdn-learn.adafruit.com/assets/assets/000/121/094/medium800/raspberry_pi_kids_storybook.jpeg?1684270885)

## Parts
### Pi Foundation Display - 7" Touchscreen Display for Raspberry Pi

[Pi Foundation Display - 7" Touchscreen Display for Raspberry Pi](https://www.adafruit.com/product/2718)
The **7” Touchscreen Display for Raspberry Pi** gives users the ability to create all-in-one, integrated projects such as tablets, infotainment systems and embedded projects!

The 800x480 display connects via an adapter board which handles power and signal conversion....

In Stock
[Buy Now](https://www.adafruit.com/product/2718)
[Related Guides to the Product](https://learn.adafruit.com/products/2718/guides)
![Top down view of a finger touching the screen of a Pi Foundation Display - 7" Touchscreen Display for Raspberry Pi. ](https://cdn-shop.adafruit.com/640x480/2718-07.jpg)

### Raspberry Pi 4 Model B - 8 GB RAM

[Raspberry Pi 4 Model B - 8 GB RAM](https://www.adafruit.com/product/4564)
The Raspberry Pi 4 Model B is the newest Raspberry Pi computer made, and the Pi Foundation knows you can always make a good thing _better_! And what could make the Pi 4 better than the 3? How about a&nbsp;_faster_ processor, USB 3.0 ports, and updated Gigabit Ethernet chip with...

In Stock
[Buy Now](https://www.adafruit.com/product/4564)
[Related Guides to the Product](https://learn.adafruit.com/products/4564/guides)
![Angled shot of Raspberry Pi 4](https://cdn-shop.adafruit.com/640x480/4564-00.jpg)

### Mini USB Microphone

[Mini USB Microphone](https://www.adafruit.com/product/3367)
Hey, listen up! It's the world's smallest USB microphone! OK maybe it's not in the Guinness Record Book, but it is _pretty darn small_! This plug-and-play **Mini USB Microphone** needs no driver, and its USB interface means you can use it with any computer,...

In Stock
[Buy Now](https://www.adafruit.com/product/3367)
[Related Guides to the Product](https://learn.adafruit.com/products/3367/guides)
![Mini USB Microphone dongle](https://cdn-shop.adafruit.com/640x480/3367-00.jpg)

### Magnetic contact switch (door sensor)

[Magnetic contact switch (door sensor)](https://www.adafruit.com/product/375)
This sensor is essentially a reed switch, encased in an ABS plastic shell. Normally the reed is 'open' (no connection between the two wires). The other half is a magnet. When the magnet is less than 13mm (0.5") away, the reed switch closes. They're often used to detect when a...

In Stock
[Buy Now](https://www.adafruit.com/product/375)
[Related Guides to the Product](https://learn.adafruit.com/products/375/guides)
![Top view of magnetic contact switch with three screws.](https://cdn-shop.adafruit.com/640x480/375-01.jpg)

### Part: Heat Shrink
quantity: 1
Heat Shrink Pack
[Heat Shrink](https://www.adafruit.com/product/344)

### Part: USB Panel Mount
quantity: 1
Panel Mount Extension USB Cable - Micro B Male to Micro B Female
[USB Panel Mount](https://www.adafruit.com/product/3258)

### Part: On/Off Switch
quantity: 1
USB Cable with Switch
[On/Off Switch](https://www.adafruit.com/product/1620)

### Part: SD Card
quantity: 1
SD/MicroSD Memory Card - 16GB Class 10 - Adapter Included
[SD Card](https://www.adafruit.com/product/2693)

### Part: Rare Earth Magnet
quantity: 1
High-strength 'rare earth' magnet
[Rare Earth Magnet](https://www.adafruit.com/product/9)

### Part: NeoPixel
quantity: 1
Flora RGB Smart NeoPixel version 2 - Pack of 4
[NeoPixel](https://www.adafruit.com/product/1260)

### Additional Parts & Materials

- A book that's at least 2 1/2" thick and larger than 8"x6"
- [A right-angle USB cable](https://a.co/d/9IXvTs0) - this makes an easier fit inside the book
- [A slim USB battery](https://a.co/d/ixEUGeD) - I tried a few and this one fit best
- 1/4" Craft foam to use as support inside the book
- Decor: [book corners](https://a.co/d/gHvH7QU), metallic vinyl sticker paper, uv resin, scrapbook supplies and findings to match your aesthetic

### Tools

- Jig saw & drill for hollowing out the book (or a very sharp knife and a lot of patience)
- [Very sharp knife](https://a.co/d/a4ITg4m) - I like this one from OLFA
- [USB Keyboard](https://www.adafruit.com/product/1736) for ease in programming the Pi
- E6000 glue, hot glue, white glue, mod podge. You're gonna need lots of glue.

# Magic Storybook with ChatGPT

## Wiring Diagram

![](https://cdn-learn.adafruit.com/assets/assets/000/121/057/medium800/raspberry_pi_magic_book_wiring5.jpg?1684186554)

There are three elements to solder to the Raspberry Pi: a touchscreen display, a NeoPixel, and a magnetic reed switch.&nbsp;

[https://pinout.xyz/](https://pinout.xyz/) is a great pinout diagram of the Raspberry Pi 4. If you're using a different model, be sure to check the correct pinout and adjust accordingly.

**Touchscreen connections**

- Pin 2 to Touchscreen 5V+
- Pin 6 to Touchscreen G

**NeoPixel connections**

- Pin 4 to NeoPixel +
- Pin 12 (GPIO 18) to NeoPixel IN
- Pin 14 to NeoPixel G

**Magnetic Switch Connections**

- Pin 9 to one switch wire
- Pin 11 (GPIO 17) to the other switch wire

**USB Microphone**

Plug the USB microphone into one of the USB ports to connect it.

&nbsp;

# Magic Storybook with ChatGPT

## Create an Account with OpenAI

Info: 

In your web browser, visit [https://platform.openai.com/](https://platform.openai.com/)

Click the "sign up" link. Then, you can use your e-mail to sign up, or an existing Google or Microsoft account.

OpenAI may require additional steps such as e-mail or phone verification before you can log in to your account.

![circuitpython_Screenshot_2023-03-08_08-43-40.png](https://cdn-learn.adafruit.com/assets/assets/000/119/295/medium640/circuitpython_Screenshot_2023-03-08_08-43-40.png?1678286661)

Once you have completed the verification process and logged in, you will next create an API key. Use the menu in the far upper right corner (probably labeled "Personal") and then select "View API Keys".

![circuitpython_ksnip_20230308-084810.png](https://cdn-learn.adafruit.com/assets/assets/000/119/296/medium640/circuitpython_ksnip_20230308-084810.png?1678286921)

Then, create a fresh API key by clicking "Create new secret key".

![circuitpython_ksnip_20230308-084917.png](https://cdn-learn.adafruit.com/assets/assets/000/119/298/medium640/circuitpython_ksnip_20230308-084917.png?1678287073)

Save this secret key to use later in the next step.

&nbsp;

![circuitpython_ksnip_20230308-084955.png](https://cdn-learn.adafruit.com/assets/assets/000/119/299/medium640/circuitpython_ksnip_20230308-084955.png?1678287111)

At the time of writing, OpenAI provides a free credit with new accounts. After the free credit is used or expires, you'll need to enter a credit card in your billing information to keep using the service.

Using the project tends to cost a few cents per session at most, and it's easy to limit your monthly bill to a pre-set amount such as $8.00.

To set a hard usage limit per month, visit the "Usage Limits" section of the OpenAI website.

![circuitpython_ksnip_20230310-085720.png](https://cdn-learn.adafruit.com/assets/assets/000/119/338/medium640/circuitpython_ksnip_20230310-085720.png?1678460269)

![circuitpython_ksnip_20230310-085702.png](https://cdn-learn.adafruit.com/assets/assets/000/119/339/medium640/circuitpython_ksnip_20230310-085702.png?1678460280)

# Magic Storybook with ChatGPT

## Software Setup

## 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](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux) guide for more help setting it up.

After installing, make sure everything is up to date:

```terminal
sudo apt update
sudo apt upgrade
sudo pip3 install --upgrade setuptools
```

 **Reboot** after it completes with:

```terminal
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&nbsp;[Python Virtual Environment Usage on Raspberry Pi](https://learn.adafruit.com/python-virtual-environment-usage-on-raspberry-pi)&nbsp;guide. To Install and activate the virtual environment, use the following commands:

```terminal
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:

```terminal
source story/bin/activate
```

To deactivate, you can use&nbsp;`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](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-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`:

```terminal
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.

![](https://cdn-learn.adafruit.com/assets/assets/000/121/111/medium800/raspberry_pi_Screen_Shot_2023-05-17_at_2.52.58_PM.png?1684360447)

## 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:

```terminal
pip3 install --upgrade openai
```

## Additional Required Dependencies

A few more additional libraries and other dependencies are required including the NeoPixel and LED Animation libraries:

```terminal
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:

```terminal
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:

```terminal
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:

```terminal
sudo mv bookprompt.txt /boot/ &gt; /dev/null 2&gt;&amp;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.

```terminal
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:

```terminal
nano ~/keys.txt
```

Change the value of the key so it reads something like:

```python
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:

```python
# 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:

```python
# 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:

```terminal
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.&nbsp;

Go ahead and **reboot**. The file system should now be read-only. Any new file changes will be discarded when the Pi is rebooted.

![](https://cdn-learn.adafruit.com/assets/assets/000/121/168/medium800/raspberry_pi_Screen_Shot_2023-05-22_at_10.16.59_AM.png?1684776136)

# Magic Storybook with ChatGPT

## Code Overview

This is intended to be a high level overview of the code, since it is relatively complex. There are two files that handle the storybook code. These are the **story.py** file, which handles the majority of the code and the **listener.py** file, which handles the listening and speech recognition tasks.

# Story Code
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Magic_AI_Storybook/story.py

## Configuration Options

This project has a lot of configuration options to get it to run exactly as you would like. In order to get through them, they will be discussed in groups.

### General Settings

- `STORY_WORD_LENGTH`: The approximate number of words used in the generated stories.
- `REED_SWITCH_PIN`: The pin that your reed switch is wired to in case your wiring varies from the wiring in this guide.
- `NEOPIXEL_PIN`: The pin your NeoPixels are wired to. There are very few PWM pins on the Pi, so this shouldn't change.
- `API_KEYS_FILE`: The location of your **keys.txt** file. By default, it points to your home directory.
- `PROMPT_FILE`: The location of your **bookprompt.txt** file. This was placed in the **/boot** folder for your convenience if you enabled the read-only file system.

### Other Setting Groups

- **Quit Settings** : Gesture settings related to quitting the app
- **NeoPixel Settings** : NeoPixel parameters and Colors
- **Image Settings** : The filenames of the images
- **Asset Paths** : The file locations of the images and fonts. By default they are relative to the main script.
- **Font Path & Size** : The font files and font sizes for the Title and Text
- **Delay Settings** : Used to control the animations of the text
- **Whitespace Settings** : Settings to control the amount of whitespace around the text.
- **ChatGPT Parameters** : The Basic ChatGPT settings
- **Speech Recognition Parameters** : Parameters to control the voice input values

## External Config Files

Next the script pulls in external configuration files including **keys.txt** and **bookprompt.txt**. The keys are read in as a config file, which is a built-in Python mechanism that makes it easy to parse and the prompt is read as a plain text file.

## UI Element Classes

The Button and Textarea classes are intended to make handling the buttons and text areas easier. The buttons are the clickable elements and the text area is the area on the page that displays text. They mostly store all the relevant information related to their particular special purpose, though the button also handles displaying and checking if you clicked within its boundaries.

## The Book Class

The book class is the main class that handles everything related to the book and handles the majority of the logic. Here are some of the notable functions:

- **Start** : Handles the starting and initialization of the code. This is what runs while it initially displays the welcome screen. It sets up Pygame. Then it loads the images, buttons, and fonts and starts some subthreads.
- **Handle Functions** : These run in the background and handle various tasks such as putting the book to sleep when the reed switch is closed, displaying the pulsing green NeoPixels while loading, and handling button presses.
- **Display Surface** : Handles displaying and rotating the image
- **Fade In Surface** : Handles the fade in animation of the title text
- **Display Current Page** : Displays the page of the story we are currently set to as well as the buttons.
- **Display Title Text** : Handles slicing up and centering the title text
- **Previous Page, Next Page, New Story** : Button handlers for navigation or making a new story.
- **Display Loading, Welcome, Message** : Display the appropriate special page
- **Load Story** : Parse the story output from ChatGPT into paragraphs and a title
- **Add Page** : Creates a new page in memory which includes a title and the height at which the text should display.
- **Generate New Story** : Prompts the user for a story. Display a message and use the listener class to listen and recognize the speech.
- **Sleep** and **Wake** : Put the book in sleep or wake modes. This turns off the backlight and coordinates with other threads to put the book into a sleep state or wake from it.
- **SendChat** : Formats the prompt into a data structure that is passed along to ChatGPT. Special characters that may be returned are stripped.

## Parse Arguments

The app only takes a single argument, which is rotation. By default it is set to 90 degrees, but can also be set to 270 if the display is installed upside down.

## Main

This is the entry point for the application and handles initializing the book and calling handle events in a loop while the book is running.

# Listener Code
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Magic_AI_Storybook/listener.py

The Listener Class is fairly simple. It is mostly a wrapper for the `SpeechRecognition` library that listens in the background when the listening has started and is able to be stopped early in case the book needs to go to sleep.

The recognize function simply makes use of some recently added functionality in `SpeechRecognition` that interfaces with OpenAI's whisper API. If there is any communication issue, it will make up to 3 attempts before failing.

# Magic Storybook with ChatGPT

## Electronics Assembly

Solder a power, ground, and data IN wire to your NeoPixel. Make the wires about 6-8" long.

Trim the female jumper connectors off the end of your magnetic switch and off one end of the black and red jumper cables that came with the touch screen. They're a little too tall for this build so we'll solder these wires to the Pi instead.

![raspberry_pi_01_trimmed.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/184/medium640/raspberry_pi_01_trimmed.jpg?1684863593)

Add a 1/4" piece of heat shrink to each of your wires.

![raspberry_pi_02_heatshrink.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/185/medium640/raspberry_pi_02_heatshrink.jpg?1684863620)

[Here's a great pinout diagram for the Raspberry Pi 4.](https://pinout.xyz/) Solder your switch wires to **G** and **GPIO 17** : the 5th and 6th pin on the inside row of pins.

![raspberry_pi_03_switch_wired.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/188/medium640/raspberry_pi_03_switch_wired.jpg?1684863870)

Solder your NeoPixel wires to the outside row of pins as shown:&nbsp;

- **5v** (red) to **5v** power (2nd pin on the Pi)
- **Data IN** (white) to **GPIO 18** (6th pin on the Pi)
- **G** (black) to **G** (7th pin on the Pi)

![raspberry_pi_04_pixel_wired.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/189/medium640/raspberry_pi_04_pixel_wired.jpg?1684863898)

Solder the red and black jumper cables for the touch screen to 5v Power (red), the first pin on the Pi on the outside row, and G (black) to the 3rd pin on the Pi also on the outside row.

![raspberry_pi_05_screen_wired.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/190/medium640/raspberry_pi_05_screen_wired.jpg?1684864049)

Cover each connection with heat shrink so there's no chance they'll short with each other.

![raspberry_pi_06_fully_wired.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/191/medium640/raspberry_pi_06_fully_wired.jpg?1684864142)

Plug your USB microphone into one of the USB ports on the Pi.

![raspberry_pi_07_microphone.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/192/medium640/raspberry_pi_07_microphone.jpg?1684864177)

Plug your USB microphone into one of the USB ports on the Pi. Plug the ribbon cable in as shown with the silver side facing in to the Pi.&nbsp;

This connector is tricky... pull upwards gently on the plastic corners to release and push them back toward the Pi to tighten.

![raspberry_pi_08_ribboncable1.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/193/medium640/raspberry_pi_08_ribboncable1.jpg?1684864218)

Plug the other end of the cable into your touch screen as shown. Plug the jumper cables into 5v (red) and G (black).

![raspberry_pi_10_screenplugged.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/195/medium640/raspberry_pi_10_screenplugged.jpg?1684864325)

Gently turn the Pi upside-down and screw it to the mounting holes on the back of the touch screen. This is not the way it's designed to be mounted, so you'll need to carefully manage the wires. I could only get three screws to bite, but that should be plenty for this project.

![raspberry_pi_11_assembled.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/198/medium640/raspberry_pi_11_assembled.jpg?1684864435)

![](https://cdn-learn.adafruit.com/assets/assets/000/121/199/medium800/raspberry_pi_12_powerwires.jpg?1684864496)

Plug your USB cable into the Pi's power port and plug the other end into your on/off switch. Then plug another cable from the switch to the battery's "out".&nbsp;

The panel-mount USB cable will plug into the battery's "in" so you can charge the book without disassembling it.

# Magic Storybook with ChatGPT

## Book Assembly

Give a little thought to the first and last pages that you cut. I left the portrait of Sir Thomas Moore intact, so that he can enjoy the stories along with the reader.&nbsp;

We will attach one page to the screen after cutting a window in it, so figure out which pages you want to keep and start cutting below that. I also didn't cut all the way to the back of the book - my book was thick enough that I could keep some pages.

I started by tracing the touch screen onto a sheet of paper, which I could then use as a template to line up my cutout. Give yourself a little extra room so you can get the electronics in and out easily.

&nbsp;

![raspberry_pi_00_template.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/202/medium640/raspberry_pi_00_template.jpg?1684868604)

To stabilize the pages, I built a box out of some wood scraps and screwed it all together around the pages I wanted to cut. I put a screw right through the center as well -- we're cutting that part out so it's okay to put a hole in it.

![raspberry_pi_01_pageblock.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/203/medium640/raspberry_pi_01_pageblock.jpg?1684868880)

Danger: 

Next I drilled a hole right in the corner so I could get my jig saw blade through. The corners are the hardest part with the saw, and I wanted them rounded, so using the drill made this easier. I could just cut straight from hole to hole and the drill holes made the corners rounded for me.

![raspberry_pi_02_drill_corner.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/204/medium640/raspberry_pi_02_drill_corner.jpg?1684868966)

Next I cut all around the edges with a jig saw. If you don't have access to a jig saw, it's perfectly possible to cut the pages out a few at a time with a sharp knife and a lot of patience. The inside of the box won't show in the finished project so it doesn't need to be perfect.

![raspberry_pi_03_bandsaw.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/205/medium640/raspberry_pi_03_bandsaw.jpg?1684869116)

To secure the pages together and keep them from tearing, I coated the outside and inside of the pages with mod podge. This dried perfectly clear and kept the book looking just like a book, but made the pages into a solid block.

![raspberry_pi_05_modpodge.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/208/medium640/raspberry_pi_05_modpodge.jpg?1684896094)

I used my dremel to hollow out a divot in the cover for my rare earth magnet, and glued it securely in place with E6000. Then I carved out a hollow for my NeoPixel and a hollow for the reed switch, which I made sure to line up with the magnet. The magnet will activate the switch when the book is closed and cause the project to sleep.

![raspberry_pi_07_magnet.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/210/medium640/raspberry_pi_07_magnet.jpg?1684896009)

![raspberry_pi_08_carve_pages.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/211/medium640/raspberry_pi_08_carve_pages.jpg?1684896417)

I used a sharp utility knife to carve a hole for the panel mount USB cable. We'll use this for charging the battery. I cut another hole on the side of the book that's big enough to poke my finger in to activate the USB on/off switch.&nbsp;

Your layout may vary.&nbsp;

![raspberry_pi_09_charging.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/212/medium640/raspberry_pi_09_charging.jpg?1684896277)

![raspberry_pi_10_onoff_hole.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/213/medium640/raspberry_pi_10_onoff_hole.jpg?1684886530)

I glued some thick craft foam supports inside, and used them to securely wedge the on/off switch and the battery in place behind the holes.&nbsp;

![raspberry_pi_11_cables.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/214/medium640/raspberry_pi_11_cables.jpg?1684886844)

I glued the NeoPixel and reed switch in place, making sure they were flush with the top. I spent some time getting the foam inserts just right so there is room inside for all the cables and the touch screen sits as securely and flush as possible with the top of the cut pages.

![raspberry_pi_22_set_in.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/216/medium640/raspberry_pi_22_set_in.jpg?1684886944)

# Magic Storybook with ChatGPT

## Decoration

I cleaned the outside of the book and then rubbed it with a black wash by diluting some black acrylic paint in water until it was pretty thin and runny, then painting it all over the book. Before it dried, I wiped the whole book with a paper towel, removing the black from the higher sections but leaving it in the low points and corners. This accents all the fabulous detail on the book cover and really makes it pop.

![raspberry_pi_14_blackwash21.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/217/medium640/raspberry_pi_14_blackwash21.jpg?1684887047)

![raspberry_pi_15_blackwash2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/218/medium640/raspberry_pi_15_blackwash2.jpg?1684887140)

![raspberry_pi_16_blackwash3.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/219/medium640/raspberry_pi_16_blackwash3.jpg?1684887210)

I covered up the old gold lettering on the front and the spine with a thicker layer of black acrylic paint.

![raspberry_pi_16_black_paint.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/221/medium640/raspberry_pi_16_black_paint.jpg?1684887294)

I added some book corners I found on [Amazon](https://www.amazon.com/dp/B08H82QSVW?_encoding=UTF8&psc=1&ref_=cm_sw_r_cp_ud_dp_KB4V926Q64A9HANQJ6VT). I used some strong pliers and also some E6000 to secure the corners.

![raspberry_pi_17_corners.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/222/medium640/raspberry_pi_17_corners.jpg?1684887472)

To finish off the cover, I glued on a [3D printed Auryn symbol](https://www.thingiverse.com/thing:17951), from the Neverending Story movie. This feels so appropriate. I also cut a vinyl auryn sticker on my Cricut vinyl cutter and fixed it to the spine, using some extra glue to be sure it stays stuck.

![raspberry_pi_book_details.jpeg](https://cdn-learn.adafruit.com/assets/assets/000/121/225/medium640/raspberry_pi_book_details.jpeg?1684887532)

I wanted to soften the transition between the book and the screen, so I decided to add a metallic leafy filigree sticker. This artwork was made in Midjourney and I converted it to a cuttable file in Photoshop, then uploaded it to the Cricut Design Space to cut it out on my vinyl cutter.&nbsp;

![raspberry_pi_18_sticker.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/226/medium640/raspberry_pi_18_sticker.jpg?1684887697)

[Here's a link to my project](https://design.cricut.com/landing/project-detail/645e87100a6d38a16bae8ecb) in case you want to use this artwork with your Cricut. The project contains the leaf border, a small Auryn for the spine, and a template of the screen size cutout.&nbsp;

I traced out the screen dimensions using a paper template, then carefully cut through the page so the screen shows through.&nbsp;

![raspberry_pi_19_template_trace.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/227/medium640/raspberry_pi_19_template_trace.jpg?1684887744)

![raspberry_pi_20_cut_page.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/228/medium640/raspberry_pi_20_cut_page.jpg?1684887854)

I made sure everything lined up to my satisfaction, then glued this title page to the edge of the touch screen using E6000 glue. This paper is a little bit brittle, so take some time and be sure to get this right the first time if you can.&nbsp;

The screen is held in place by this page. I didn't secure it down inside the book, because I will probably need to access the USB ports or SD card at some point, so I didn't want to make it permanent. If you're making this for a kid, it might be a good idea to glue this top page down to the book once you're sure you're happy with the setup.

![raspberry_pi_23_glue_page.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/229/medium640/raspberry_pi_23_glue_page.jpg?1684887837)

To highlight the NeoPixel indicator light, I used a small ring from my scrapbooking drawer filled with UV resin. The resin cures in minutes when you shine a blacklight on it and does a great job of pulling the light up into a dome so it diffuses and refracts in a magical way.

![raspberry_pi_24_uv_resin.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/230/medium640/raspberry_pi_24_uv_resin.jpg?1684888056)

![raspberry_pi_25_uv_resin_done.jpg](https://cdn-learn.adafruit.com/assets/assets/000/121/231/medium640/raspberry_pi_25_uv_resin_done.jpg?1684887960)

![](https://cdn-learn.adafruit.com/assets/assets/000/121/233/medium800/raspberry_pi_imagine.jpg?1684887818)

# Magic Storybook with ChatGPT

## Usage

## Running the Code

There are a few different ways to run the code. If you followed the setup instructions and created the icons, it should automatically run upon starting the Pi.

If you exited the code and you would like to run the code again, you can do so simply by double-tapping the icon on the desktop.

![](https://cdn-learn.adafruit.com/assets/assets/000/121/169/medium800/raspberry_pi_launch_icon.png?1684861995)

If it asks you what you want to do with the file, just choose **execute**.

![](https://cdn-learn.adafruit.com/assets/assets/000/121/170/medium800/raspberry_pi_execute.png?1684862090)

Another option is to use **ssh** to connect remotely to your Raspberry Pi from your desktop computer. You can then type commands into the Pi without having to disassemble your book and plug in a keyboard, or remove the SD card in order to enter commands. This is also a great way to troubleshoot your project: when you're running the script from an ssh terminal you can see any error codes to help figure out why your project isn't working.

#### [Adafruit's Raspberry Pi Lesson 6. Using SSH](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh/overview)

From either your ssh window or from a keyboard plugged into your Pi, you can run the code manually from the command line / terminal window using the following commands:

```terminal
source ~/story/bin/activate
cd ~/Magic_AI_Storybook
sudo -E PATH=$PATH python story.py
```

## Creating a New Story

When you first start the script, you will be prompted about what kind of story your would like.

![](https://cdn-learn.adafruit.com/assets/assets/000/121/172/medium800/raspberry_pi_story_prompt.png?1684862162)

Wait for the NeoPixels to turn yellow and speak into the microphone using a loud clear voice. If it doesn't hear you correctly, it may generate a story different than requested. Once it has detected speech, it will display a Dreaming screen (created with Midjourney AI) while it generates the story:

![](https://cdn-learn.adafruit.com/assets/assets/000/121/173/medium800/raspberry_pi_dreaming.png?1684862355)

Once the story has been generated, it will display the story for you to read:

![](https://cdn-learn.adafruit.com/assets/assets/000/121/174/medium800/raspberry_pi_New_Story.png?1684862420)

## Navigation

At the bottom of each page, there will be two or three buttons. **Back** and **Next** are used to navigate between pages and stories. If you are at the end of the last story, pressing the Next button will generate a new story.

If there are multiple stories, they will be displayed in the order they were generated and pressing Back at the beginning of a story will load the previous one and pressing Next at the end of a story which has another story after it will load that story.

Pressing the **New Story** button, which looks like a plus sign, at any time will prompt you for a new story and then display that story.

![](https://cdn-learn.adafruit.com/assets/assets/000/121/175/medium800/raspberry_pi_navigation.png?1684862552)

## Putting the Book in a Sleep State

Closing the reed switch by closing the cover should put the book in a sleep state. If it is in the middle of a more complex task such as creating a new story, it attempts to stop those tasks and then places the book in a sleep state.

## Exiting the Script

To exit the script, you can close the book three times within five seconds. If you have changed the Quit settings from the defaults, the behavior could be a bit different.

## Changing the Prompt: bookprompt.txt

To change the ChatGPT prompt, you will want edit the **/boot/bookprompt.txt** file. If you have changed the file system to read-only, you will need to shut down the Pi, remove the SD Card, place it in a computer and you can just edit the file from the boot partition. Place it back into the Pi when finished and start it back up again.

The file is at **/boot/bookprompt.txt**. By default, it reads:

```auto
Write a complete story. It must begin with a title and have a body of approximately
{STORY_WORD_LENGTH} words long and a happy ending. The specific
story request is "{STORY_REQUEST}". The title should each be
specified and the body should start on the next line.
```

To change the prompt, ssh into your Pi or plug in your keyboard and open a terminal window. Type these commands:

```terminal
cd /boot
sudo nano bookprompt.txt
```

This will open **bookprompt.txt** in the nano text editor.&nbsp;

This is a powerful bit of code that lets you set whatever parameters you'd like for your stories. Example text you could add:

"Make every story about a girl named Sarah and her teddy bear Sugarbear"  
"Write in the style of Neil Gaiman"  
"The story should be in rhyming couplets and iambic pentameter"  
"The stories should be magical and strange, with wild animal spirit magic based on ancient Norse and Finnish mythology and should end with a confusing moral"  
"Every story should contain a pun"

The possibilities are endless. What kind of stories will your book imagine?

The prompts in **bookprompt.txt** will affect every story you ask for, so if you want just one rhyming story, use the voice control feature to ask for it. If you want EVERY story to rhyme, change it here in **bookprompt.txt**.

The variables `STORY_WORD_LENGTH` and `STORY_REQUEST` can be found in **story.py**.&nbsp; By default the stories are 800 words long. Change `STORY_WORD_LENGTH` in **story.py** to edit this.

# Troubleshooting

This is a complicated project with a lot of steps. Here are a few things to try if you're running into trouble.

### Story.py Doesn't Run

1. Use ssh to connect to your Pi using your computer. This is a powerful little tool that will allow you to control your Pi without having to plug in a keyboard and monitor. Here is a full tutorial that will help you get started:

[Adafruit's Raspberry Pi Lesson 6. Using SSH](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh/overview)

Once you can ssh into your Pi, type `sudo -E PATH=$PATH ~/story/bin/python ~/Magic_AI_Storybook/story.py`&nbsp;into your terminal window to start the story script. The terminal window will give you a verbose output about what's going on with your project, which is VERY helpful in the debugging process.

2. Start Over. Head back to the Software Install page and run through every step again. It's easy to miss a command, or not to notice if you've mistyped something. This is where ssh comes in handy again: you can copy/paste the commands easily from your computer's web browser into your terminal window and that lessens the chance of a typo.

### The Pi Keeps Rebooting / Crashing

Is your battery sufficiently charged? And is it plugged in to the charger? Try unplugging it. I had trouble with the Pi rebooting over and over when I started it up while the charging cord was plugged in. Be sure the battery is charged and unplug before you start.

If the **story.py** script keeps quitting on you, perhaps you missed our little Easter Egg: if you open and close the book, activating the magnetic reed switch 3x within 5 seconds, then **story.py** will exit so you can get to the Pi desktop. This is a feature, not a bug! Relaunch the script by double-clicking the desktop icon.

### The App is Still Running in Fullscreen, but not Responding

If you set up the Pi with the Read-Only File system, you can simply restart it. You can also plug a keyboard in and hit Alt+F4. After about 5 seconds, a popup should appear asking if you want to **Wait** or **Force Quit**. Choose **Force Quit**.

### The Story pages through by itself

This can happen if you have something obscuring or stuck to part of the touch screen. I initially stuck a pretty vinyl sticker on the edges of the screen to cover the paper transition, but the vinyl activated the capacitive touch response on the screen and made it act like I was touching it. Be sure your screen is completely clear of glue or stickers or anything that will make it react.

### It Doesn't Listen / Can't Seem to Hear You

Try changing the sensitivity of the microphone by editing the `ENERGY_THRESHOLD` variable in **story.py**.&nbsp;

If that doesn't help, try running this script. Save it as **microphone\_recognition.py** &nbsp;in the same folder as your **listener.py** file. You'll have to paste your API key into it.

```auto
from listener import Listener

OPENAI_API_KEY = ""

listener = Listener(OPENAI_API_KEY)

print("Say Something")
listener.listen()

if listener.speech_waiting():
    print(f"Whisper API thinks you said {listener.recognize()}")
else:
    print("No speech detected")
```

### Authentication Errors

If you get stuck on the "Dreaming" page, you may have an authentication error. Did you remember to add your Open API key to **keys.txt**? Try editing **keys.txt** and make sure your key is in there and looks correct.

```terminal
cd ~
sudo nano keys.txt
```

If it still doesn't work, try going back to Open API and generating a new key, and use that one.


## Featured Products

### Pi Foundation Display - 7" Touchscreen Display for Raspberry Pi

[Pi Foundation Display - 7" Touchscreen Display for Raspberry Pi](https://www.adafruit.com/product/2718)
The **7” Touchscreen Display for Raspberry Pi** gives users the ability to create all-in-one, integrated projects such as tablets, infotainment systems and embedded projects!

The 800x480 display connects via an adapter board which handles power and signal conversion....

In Stock
[Buy Now](https://www.adafruit.com/product/2718)
[Related Guides to the Product](https://learn.adafruit.com/products/2718/guides)
### Raspberry Pi 4 Model B - 8 GB RAM

[Raspberry Pi 4 Model B - 8 GB RAM](https://www.adafruit.com/product/4564)
The Raspberry Pi 4 Model B is the newest Raspberry Pi computer made, and the Pi Foundation knows you can always make a good thing _better_! And what could make the Pi 4 better than the 3? How about a&nbsp;_faster_ processor, USB 3.0 ports, and updated Gigabit Ethernet chip with...

In Stock
[Buy Now](https://www.adafruit.com/product/4564)
[Related Guides to the Product](https://learn.adafruit.com/products/4564/guides)
### Magnetic contact switch (door sensor)

[Magnetic contact switch (door sensor)](https://www.adafruit.com/product/375)
This sensor is essentially a reed switch, encased in an ABS plastic shell. Normally the reed is 'open' (no connection between the two wires). The other half is a magnet. When the magnet is less than 13mm (0.5") away, the reed switch closes. They're often used to detect when a...

In Stock
[Buy Now](https://www.adafruit.com/product/375)
[Related Guides to the Product](https://learn.adafruit.com/products/375/guides)
### Flora RGB Smart NeoPixel version 3 - Pack of 4

[Flora RGB Smart NeoPixel version 3 - Pack of 4](https://www.adafruit.com/product/1260)
What's a wearable project without LEDs? Our favorite part of the Flora platform is these tiny smart pixels. Designed specifically for wearables, these updated Flora NeoPixels have ultra-cool technology: these ultra-bright LEDs have a constant-current driver cooked right into the LED...

In Stock
[Buy Now](https://www.adafruit.com/product/1260)
[Related Guides to the Product](https://learn.adafruit.com/products/1260/guides)
### USB Cable with Switch

[USB Cable with Switch](https://www.adafruit.com/product/1620)
Add a power switch to any USB-powered project simply by plugging this between the USB power port and the USB cable. This is the most useful thing you never knew you needed! You'll want to pick up a bunch for your electronic projects.  
  
Comes with a USB A socket on one end and a...

In Stock
[Buy Now](https://www.adafruit.com/product/1620)
[Related Guides to the Product](https://learn.adafruit.com/products/1620/guides)
### Panel Mount Extension USB Cable - Micro B Male to Micro B Female

[Panel Mount Extension USB Cable - Micro B Male to Micro B Female](https://www.adafruit.com/product/3258)
Check out this handy MicroUSB extension cable, which will make it easy for you to enclose a device that has a B&nbsp;type (micro USB host) port. Great if you need to extend the USB port for your Raspberry Pi, Arduino, Feather or really anything development board.  
  
This Micro-B...

In Stock
[Buy Now](https://www.adafruit.com/product/3258)
[Related Guides to the Product](https://learn.adafruit.com/products/3258/guides)
### Heat Shrink Pack

[Heat Shrink Pack](https://www.adafruit.com/product/344)
Heat shrink is the duct tape of electronics, it keeps your stuff all safe and kept together. Especially when wiring and soldering, use heat shrink to add mechanical strength to cables. We use this stuff all the time and having a zip-lock bag of all the possible sizes is super...

In Stock
[Buy Now](https://www.adafruit.com/product/344)
[Related Guides to the Product](https://learn.adafruit.com/products/344/guides)
### Mini USB Microphone

[Mini USB Microphone](https://www.adafruit.com/product/3367)
Hey, listen up! It's the world's smallest USB microphone! OK maybe it's not in the Guinness Record Book, but it is _pretty darn small_! This plug-and-play **Mini USB Microphone** needs no driver, and its USB interface means you can use it with any computer,...

In Stock
[Buy Now](https://www.adafruit.com/product/3367)
[Related Guides to the Product](https://learn.adafruit.com/products/3367/guides)

## Related Guides

- [Raspberry Pi Care and Troubleshooting](https://learn.adafruit.com/raspberry-pi-care-and-troubleshooting.md)
- [Light of Your Life Wedding Bouquet](https://learn.adafruit.com/light-of-your-life-wedding-bouquet.md)
- [Flora-Powered TARDIS Costume (for Dogs!)](https://learn.adafruit.com/wearable-flora-powered-tardis-costume-dogs.md)
- [Light-Activated Pixel Heart](https://learn.adafruit.com/light-activated-pixel-heart.md)
- [Adafruit NeoPixel Überguide](https://learn.adafruit.com/adafruit-neopixel-uberguide.md)
- [3d Printed Neopixel Tactile Switch Buttons](https://learn.adafruit.com/3d-printed-neopixel-tactile-switch-buttons.md)
- [NeoPixels on Raspberry Pi](https://learn.adafruit.com/neopixels-on-raspberry-pi.md)
- [Blahaj Alarm and Lamp](https://learn.adafruit.com/blahaj-alarm.md)
- [Trinket Sound-Reactive LED Color Organ](https://learn.adafruit.com/trinket-sound-reactive-led-color-organ.md)
- [FFT: Fun with Fourier Transforms](https://learn.adafruit.com/fft-fun-with-fourier-transforms.md)
- [Introducing Gemma](https://learn.adafruit.com/introducing-gemma.md)
- [3D Printed Wireless MIDI Controller Guitar](https://learn.adafruit.com/ez-key-wireless-midi-controller-guitar.md)
- [FLORA Pixel Brooch](https://learn.adafruit.com/flora-pixel-brooch.md)
- [PianoGlove](https://learn.adafruit.com/pianoglove.md)
- [NeoPixel Tiara](https://learn.adafruit.com/neopixel-tiara.md)
- [Light-Up Angler Fish Embroidery](https://learn.adafruit.com/light-up-angler-fish-embroidery.md)
