-
Notifications
You must be signed in to change notification settings - Fork 2
Generative AI Book Italian
This script is designed for an experimental setup called "BookLed," where pages of a physical 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:
-
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.