-
Notifications
You must be signed in to change notification settings - Fork 2
Generative AI Book Italian
This wiki page provides step-by-step instructions for setting up and running the Generative AI jupyter notebook code from this GitHub repository.
This script is designed for an experimental setup of "BookLed," where pages of a physical card board book are read, and AI-generated audio content in Italian is supplied automatically as the pages turn. Here’s a detailed explanation of how the script works:
- Open the GitHub repository link: GitHub Link
- Click on the
Rawbutton to view the raw file. - Right-click on the page and select
Save as...to download the file to your local machine.
- Open your terminal (or Git Bash if on Windows).
- Navigate to the directory where you want to clone the repository:
cd /path/to/your/directory - Clone the repository:
git clone https://github.com/robotoons/BookLed.git
- Navigate to the specific directory containing the Jupyter notebook:
cd BookLed/codes/jupyter notebooks
- Open your terminal.
- Navigate to the directory containing the Jupyter notebook if you haven't already:
cd /path/to/BookLed/codes/jupyter notebooks - Create a virtual environment (optional but recommended):
python -m venv myenv
- Activate the virtual environment:
- On Windows:
myenv\Scripts\activate
- On macOS/Linux:
source myenv/bin/activate
- On Windows:
- Install the required dependencies:
Manually install the necessary packages:
pip install pyserial jupyter
- Ensure your virtual environment is activated.
- Launch Jupyter Notebook:
jupyter notebook
- In the Jupyter Notebook interface, navigate to the directory containing your downloaded notebook file (
BookLed Experiment 3 (Generative AI Book Italian).ipynb) and open it.
-
Imports and Dependencies:
- Various libraries are imported, including
os,time,serial,threading,requests,pydub,wave,pyaudio,dotenv,json, andopenai. - Deprecation warnings are suppressed.
- Various libraries are imported, including
-
Create a Temporary Directory:
- A temporary directory named
temp_mp3is created to store downloaded MP3 files.
- A temporary directory named
-
Configure Serial Connection:
- A serial connection to the BookLed device is established via a specified COM port (
COM4).
- A serial connection to the BookLed device is established via a specified COM port (
-
Load API Key:
- The script loads the OpenAI API key from an environment variable using
dotenv.
- The script loads the OpenAI API key from an environment variable using
-
Set URLs for Prompts and Images:
- URLs for prompts and images stored on GitHub are specified. These are used later to retrieve content.
-
Function:
read_last_page_number:- Reads data from the serial port to extract the last page number detected by the BookLed device.
-
Function:
read_file_to_string:- Reads the content of a local file and returns it as a string.
-
Function:
download_file_from_github:- Downloads a file from a specified GitHub URL and returns its content as a string.
-
Function:
download_mp3:- Downloads an MP3 file from a URL and saves it to the temporary directory.
-
Function:
play_audio:- Converts an MP3 file to WAV format and plays it using
pyaudio.
- Converts an MP3 file to WAV format and plays it using
-
Function:
play_mp3_in_thread:- Runs
play_audioin a separate thread to allow concurrent playback.
- Runs
-
Function:
call_openai_api:- Sends a request to the OpenAI API to generate storytelling content based on the provided prompt and image URL.
-
Function:
text_to_speech:- Converts generated text content to speech using the OpenAI API and saves it as an MP3 file.
-
Prompt Source Selection:
- Determines whether to use a local prompt file or download it from GitHub.
-
BookLed and AI Play Loop:
- Initializes global variables (
current_page,stop_audio,audio_thread). - Enters a loop where it continuously reads the current page number from the BookLed device.
- When a new page is detected:
- Stops any currently playing audio.
- Calls the OpenAI API to convert the page image into storytelling content.
- Converts the generated content into speech.
- Starts playing the new audio in a separate thread.
- Handles interruptions and ensures proper cleanup on exit.
- Initializes global variables (
The script integrates hardware (BookLed) with AI services (OpenAI API) to create an interactive experience where turning the pages of a physical book triggers the generation and playback of audio content. This is achieved through serial communication with the BookLed device, API calls to OpenAI for content generation and text-to-speech conversion, and concurrent audio playback using threading.