Skip to main content Link Menu Expand (external link) Document Search Copy Copied

AI Powered Document Summarization - Part 1

The Information Overload Dilemma

These days, we’re always getting lots and lots of information thrown at us. It doesn’t matter if you’re a teacher getting ready for class, a business person looking at reports, or a researcher trying to keep up with new studies. Everyone has the same problem: How do you quickly find the important stuff in all that reading?

Imagine this scenario:

Rahul is a high school student who takes business classes. He has to read a really long case study for class. It’s important for him to understand it so he can follow the teacher’s lessons and join in class discussions. But he doesn’t have much time.

Now, Rahul has a new trick. He can use a smart computer tool that uses AI. He just uploads his long case study to this tool. In just a few minutes, the tool gives him a short, easy-to-read summary.

Before, Rahul would have spent hours reading every page carefully. Now, he gets the main ideas quickly. This saves him a lot of time. With all this extra time, Rahul can think about what he learned and be ready to talk about it in class.

This isn’t just a student’s fantasy – it’s the reality that cutting-edge AI technology is bringing to fruition. Let’s dive deep into the inner workings of this revolutionary tool and explore how it’s transforming the way we process and understand information.

Summarization tool

Here is a simple summarization tool that an help quickly summarize the documents, given the topics of summarization. It automatically creates the sections and summarizes informatio in each section and also provide extended information for each summary.

summary-tool

The Magic Behind the Curtain: How AI Summarization Works

At its core, the document summarization system we’re exploring leverages the power of Large Language Models (LLMs) like OpenAI’s GPT-4 or Anthropic’s Claude. These sophisticated AI models have been trained on vast amounts of text data, enabling them to understand and generate human-like text with remarkable accuracy. But how does our specific implementation harness this power? Let’s break it down step by step.

1. Setting the Stage: Environment and Dependencies

The foundation of our summarization tool is built on a robust set of Python libraries and frameworks. Here’s a closer look at the key components:

import io
import os
import traceback
from pydantic_core import from_json
from fasthtml.common import * 
from PyPDF2 import PdfReader
from langchain.chains.summarize import load_summarize_chain
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from pydantic import BaseModel, Field, ValidationError
from langchain.output_parsers import PydanticOutputParser

This setup combines several powerful tools:

  • fasthtml for creating a responsive web interface
  • PyPDF2 for handling PDF file inputs
  • langchain for orchestrating the AI models and summarization process
  • pydantic for defining structured data models and validating outputs

2. Structuring the Summary: Pydantic Models

One of the key innovations in this implementation is the use of Pydantic models to define a clear structure for the AI-generated summaries. This ensures consistency and allows for easy parsing and display of the results.

class SummaryLine(BaseModel):
    summary_item: str = Field(description="Actual summary sentence that contains highlighting key data points or information.", 
                              max_length=200)
    keywords: List[str] = Field(description="A list of exact words or phrases in the summary item that highlights most important data points or key ideas.")
    brief_descripton_of_summary: str = Field(description="This is elaborate description to provide context or background to the summary item.",
                                             min_length=200,                                             
                                             max_length=500)

class TopicSummaries(BaseModel):
    topic: str = Field(description="Topics of summary as mentioned in the instructions.")
    summaries: List[SummaryLine] = Field(description="This a list summary for a topic with each one having it's own keywords and context.",
                                         min_items=3, 
                                         max_items=5)

class CompleteSummary(BaseModel):
    summaries_list: List[TopicSummaries]

This structured approach allows us to:

  1. Clearly define what each part of the summary should contain
  2. Set constraints on length and content
  3. Ensure that the AI model’s output adheres to a consistent format

3. Guiding the AI: Crafting the Perfect Prompt

The heart of effective AI summarization lies in providing clear, detailed instructions to the model. Our implementation uses a carefully crafted template:

summarize_template = """
Write a concise summary of the case study given in the context. The summary should be based on the following topics.
"""

summary_sections = """
- Factual: Facts or information that contains numbers, dates, events etc. that are mostly quantitative or qualitative data
- SWOT: Key Strength, weakness, opportunities or threats that are mentioned in the case study
- Decisions and Outcomes: Key decisions taken and it's successful or failed outcomes and reasons
- Ethical and Governance: Key considerations from ethical and governance perspective
"""

context_str = """
<context>
{context_content}
</context>

The response must follow the following schema strictly. There will be penalty for not following the schema.
"""

This template does several crucial things:

  1. It specifies the overall task (summarizing a case study)
  2. It outlines specific topics to be covered, ensuring a comprehensive summary
  3. It provides a structure for the AI to follow, improving consistency and readability

This is how the actually format instruction looks like


{
  "$defs": {
    "SummaryLine": {
      "properties": {
        "summary_item": {
          "description": "Actual summary sentence that contains highlighting key data points or information.",
          "maxLength": 200,
          "title": "Summary Item",
          "type": "string"
        },
        "keywords": {
          "description": "A list of exact words or phrases in the summary item that highlights most important data points or key ideas.",
          "items": {
            "type": "string"
          },
          "title": "Keywords",
          "type": "array"
        },
        "brief_descripton_of_summary": {
          "description": "This is elaborate description to provide context or background to the summary item.",
          "maxLength": 500,
          "minLength": 200,
          "title": "Brief Descripton Of Summary",
          "type": "string"
        }
      },
      "required": [
        "summary_item",
        "keywords",
        "brief_descripton_of_summary"
      ],
      "title": "SummaryLine",
      "type": "object"
    },
    "TopicSummaries": {
      "properties": {
        "topic": {
          "description": "Topics of summary as mentioned in the instructions.",
          "title": "Topic",
          "type": "string"
        },
        "summaries": {
          "description": "This a list summary for a topic with each one having it's own keywords and context.",
          "items": {
            "$ref": "#/$defs/SummaryLine"
          },
          "maxItems": 5,
          "minItems": 3,
          "title": "Summaries",
          "type": "array"
        }
      },
      "required": [
        "topic",
        "summaries"
      ],
      "title": "TopicSummaries",
      "type": "object"
    }
  },
  "properties": {
    "summaries_list": {
      "items": {
        "$ref": "#/$defs/TopicSummaries"
      },
      "title": "Summaries List",
      "type": "array"
    }
  },
  "required": [
    "summaries_list"
  ]
}

4. The Summarization Engine: Bringing It All Together

The onepass_summarize function is where all these components come together to produce the final summary:

def onepass_summarize(pages, summary_sections, model):
    # This function performs a one-pass summarization of the given pages
    # It takes three parameters:
    # - pages: The text content to be summarized
    # - summary_sections: Instructions on what sections to include in the summary
    # - model: The AI model to use for summarization (e.g., GPT-4, Claude)

    # Combine different parts of the template to create a comprehensive instruction set for the AI
    onepass_summary_template = summarize_template + summary_sections + context_str + "{format_instructions}"
    # summarize_template: General instructions for summarization
    # summary_sections: Specific sections to include in the summary
    # context_str: Placeholder for the actual content to be summarized
    # {format_instructions}: Will be replaced with instructions on how to format the output

    # Create a parser that will ensure the AI's output matches our predefined structure
    output_parser = PydanticOutputParser(pydantic_object=CompleteSummary)
    # CompleteSummary is a Pydantic model that defines the expected structure of the summary

    # Get formatting instructions from the parser
    # These instructions tell the AI how to structure its output
    format_instructions = output_parser.get_format_instructions()

    # Create a prompt template using our combined instruction set
    prompt = PromptTemplate.from_template(onepass_summary_template)
    # This template will be filled with the actual content to summarize

    # Create a "chain" that processes the input through several steps:
    # 1. Fill the prompt template with content
    # 2. Send to the AI model for processing
    # 3. Parse the output to ensure it matches our expected structure
    summary_chain = prompt | model | output_parser

    # Invoke the chain with our content and format instructions
    summaries = summary_chain.invoke({
        "context_content": pages,  # The actual text to summarize
        "format_instructions": format_instructions  # How to format the output
    })

    # Return the structured summaries
    return summaries

This function:

  1. Combines the various template components into a single, comprehensive instruction set
  2. Sets up a parser to ensure the output matches our predefined structure
  3. Creates a “chain” that processes the input through the AI model and formats the output
  4. Invokes this chain with the document content and awaits the structured summary

5. User Interface using fastHtml

The summary tool is created using a new framework called fastHtml developed by [Jeremy Howard] (https://www.linkedin.com/in/howardjeremy/) and his [team] (https://www.answer.ai/). I loved his deep learning course on fast.ai and a big fan of his teachigs.

I wanted to give the web framework a try as it empowers you to create web applications completely Python without any knowldge of css and javascript or any other web framework.

I will write a blog about the creating the user interface separately.

Here is also the video on how to use the tool, which also takes you through the code implementation.

Beyond the Classroom: Real-World Applications

The applications of this AI-powered summarization tool extend far beyond of education, for example:

  1. Business Intelligence: Executives can quickly digest lengthy market reports, competitor analyses, and financial statements, allowing for faster, more informed decision-making.

  2. Legal Research: Lawyers can efficiently review case law and legal documents, extracting key precedents and arguments in a fraction of the time.

  3. Academic Research: Scholars can stay up-to-date with the latest publications in their field, quickly grasping the main contributions of new papers.

  4. Journalism: Reporters can rapidly process large volumes of information from various sources, helping them to identify key story angles and fact-check efficiently.

The Future of Information Processing and Cosumption

We’re entering a new time where there’s more information than ever before. Tools like this AI helper that can quickly sum up long documents aren’t just nice to have - we really need them now. There’s so much to read and learn these days, and being able to understand it all quickly can make a big difference in how well we do at work and in life.

So the next time you have too many things to read and feel overwhelmed, remember this: AI can be your helper. With just one click, it can make things clearer and easier to understand. This new way of dealing with lots of information is here now, and it’s going to keep getting better and better.