AI reads books: Page-by-Page PDF Knowledge Extractor & Summarizer. script performs an intelligent page-by-page analysis of PDF books, methodically extracting knowledge points and generating progressive summaries at specified intervals
See the codeThe read_books.py script performs an intelligent page-by-page analysis of PDF books, methodically extracting knowledge points and generating progressive summaries at specified intervals. It processes each page individually, allowing for detailed content understanding while maintaining the contextual flow of the book. Below is a detailed explanation of how the script works:
This repo is one small doorway into the larger AI-building practice I share with patrons.
Setup
# Clone the repository
git clone [repository-url]
cd [repository-name]
# Install requirements
pip install -r requirements.txt
Configure
read_books.py and update the PDF_NAME constant with your PDF filenameANALYSIS_INTERVAL or TEST_PAGESRun
python read_books.py
Output The script will generate:
book_analysis/knowledge_bases/: JSON files containing extracted knowledgebook_analysis/summaries/: Markdown files with interval and final summariesbook_analysis/pdfs/: Copy of your PDF fileCustomization Options
ANALYSIS_INTERVAL = None to skip interval summariesTEST_PAGES = None to process entire bookMODEL and ANALYSIS_MODEL for different AI modelsPDF_NAME: The name of the PDF file to be analyzed.BASE_DIR: The base directory for the analysis.PDF_DIR: Directory where the PDF file is stored.KNOWLEDGE_DIR: Directory where the knowledge base will be saved.SUMMARIES_DIR: Directory where the summaries will be saved.PDF_PATH: Full path to the PDF file.OUTPUT_PATH: Path to the knowledge base JSON file.ANALYSIS_INTERVAL: Number of pages after which an interval analysis is generated. Set to None to skip interval analyses.MODEL: The model used for processing pages.ANALYSIS_MODEL: The model used for generating analyses.TEST_PAGES: Number of pages to process for testing. Set to None to process the entire book.PageContent ClassA Pydantic model that represents the structure of the response from the OpenAI API for page content analysis. It has two fields:
has_content: A boolean indicating if the page has relevant content.knowledge: A list of knowledge points extracted from the page.load_or_create_knowledge_base() -> Dict[str, Any]Loads the existing knowledge base from the JSON file if it exists. If not, it returns an empty dictionary.
save_knowledge_base(knowledge_base: list[str])Saves the knowledge base to a JSON file. It prints a message indicating the number of items saved.
process_page(client: OpenAI, page_text: str, current_knowledge: list[str], page_num: int) -> list[str]Processes a single page of the PDF. It sends the page text to the OpenAI API for analysis and updates the knowledge base with the extracted knowledge points. It also saves the updated knowledge base to a JSON file.
load_existing_knowledge() -> list[str]Loads the existing knowledge base from the JSON file if it exists. If not, it returns an empty list.
analyze_knowledge_base(client: OpenAI, knowledge_base: list[str]) -> strGenerates a comprehensive summary of the entire knowledge base using the OpenAI API. It returns the summary in markdown format.
setup_directories()Sets up the necessary directories for the analysis. It clears any previously generated files and ensures the PDF file is in the correct location.
save_summary(summary: str, is_final: bool = False)Saves the generated summary to a markdown file. It creates a file with a proper naming convention based on whether it is a final or interval summary.
print_instructions()Prints instructions for using the script. It explains the configuration options and how to run the script.
main()The main function that orchestrates the entire process. It sets up directories, loads the knowledge base, processes each page of the PDF, generates interval and final summaries, and saves them.
ANALYSIS_INTERVAL and a final summary after processing all pages.PDF_NAME constant with your PDF filename.153 followers Β· starred Jan 2025
323 followers Β· starred Jan 2025
15 followers Β· starred May 2025
13 followers Β· starred Jan 2025
AI reads books: Page-by-Page PDF Knowledge Extractor & Summarizer. script performs an intelligent page-by-page analysis of PDF books, methodically extracting knowledge points and generating progressive summaries at specified intervals
See the codeThe read_books.py script performs an intelligent page-by-page analysis of PDF books, methodically extracting knowledge points and generating progressive summaries at specified intervals. It processes each page individually, allowing for detailed content understanding while maintaining the contextual flow of the book. Below is a detailed explanation of how the script works:
This repo is one small doorway into the larger AI-building practice I share with patrons.
Setup
# Clone the repository
git clone [repository-url]
cd [repository-name]
# Install requirements
pip install -r requirements.txt
Configure
read_books.py and update the PDF_NAME constant with your PDF filenameANALYSIS_INTERVAL or TEST_PAGESRun
python read_books.py
Output The script will generate:
book_analysis/knowledge_bases/: JSON files containing extracted knowledgebook_analysis/summaries/: Markdown files with interval and final summariesbook_analysis/pdfs/: Copy of your PDF fileCustomization Options
ANALYSIS_INTERVAL = None to skip interval summariesTEST_PAGES = None to process entire bookMODEL and ANALYSIS_MODEL for different AI modelsPDF_NAME: The name of the PDF file to be analyzed.BASE_DIR: The base directory for the analysis.PDF_DIR: Directory where the PDF file is stored.KNOWLEDGE_DIR: Directory where the knowledge base will be saved.SUMMARIES_DIR: Directory where the summaries will be saved.PDF_PATH: Full path to the PDF file.OUTPUT_PATH: Path to the knowledge base JSON file.ANALYSIS_INTERVAL: Number of pages after which an interval analysis is generated. Set to None to skip interval analyses.MODEL: The model used for processing pages.ANALYSIS_MODEL: The model used for generating analyses.TEST_PAGES: Number of pages to process for testing. Set to None to process the entire book.PageContent ClassA Pydantic model that represents the structure of the response from the OpenAI API for page content analysis. It has two fields:
has_content: A boolean indicating if the page has relevant content.knowledge: A list of knowledge points extracted from the page.load_or_create_knowledge_base() -> Dict[str, Any]Loads the existing knowledge base from the JSON file if it exists. If not, it returns an empty dictionary.
save_knowledge_base(knowledge_base: list[str])Saves the knowledge base to a JSON file. It prints a message indicating the number of items saved.
process_page(client: OpenAI, page_text: str, current_knowledge: list[str], page_num: int) -> list[str]Processes a single page of the PDF. It sends the page text to the OpenAI API for analysis and updates the knowledge base with the extracted knowledge points. It also saves the updated knowledge base to a JSON file.
load_existing_knowledge() -> list[str]Loads the existing knowledge base from the JSON file if it exists. If not, it returns an empty list.
analyze_knowledge_base(client: OpenAI, knowledge_base: list[str]) -> strGenerates a comprehensive summary of the entire knowledge base using the OpenAI API. It returns the summary in markdown format.
setup_directories()Sets up the necessary directories for the analysis. It clears any previously generated files and ensures the PDF file is in the correct location.
save_summary(summary: str, is_final: bool = False)Saves the generated summary to a markdown file. It creates a file with a proper naming convention based on whether it is a final or interval summary.
print_instructions()Prints instructions for using the script. It explains the configuration options and how to run the script.
main()The main function that orchestrates the entire process. It sets up directories, loads the knowledge base, processes each page of the PDF, generates interval and final summaries, and saves them.
ANALYSIS_INTERVAL and a final summary after processing all pages.PDF_NAME constant with your PDF filename.153 followers Β· starred Jan 2025
323 followers Β· starred Jan 2025
15 followers Β· starred May 2025
13 followers Β· starred Jan 2025