Building a PDF Summarizer with LangChain
Tags: programming, tutorial, python, opensource
Published: 2023-12-11 23:59:43

We are in an era inundated with information, and the ability to distill complex documents into concise, digestible summaries is a skill in high demand. Imagine a world where lengthy PDFs, often dense with data, could be swiftly summarized with precision. 

There are many paid and free tools that can help summarize documents such as PDFs out there, but you can build your custom PDF summarizer tailored to your taste using tools powered by LLMs.

In this article, you will learn how to build a PDF summarizer using LangChain, Gradio and you will be able to see your project live, so you if are looking to get started with LangChain or build an LLM-powered application for your portfolio, this tutorial is for you.

## Prerequisites
- **LangChain**: This is one of the most useful libraries to help developers build apps powered by LLMs.
LangChain enables LLM models to generate responses based on the most up-to-date information available online and also simplifies the process of organizing large volumes of data so that it can be easily accessed by LLMs.

Install using `pip`.

```python
pip install langchain
```

- **Gradio**: Gradio is the fastest way to demo your machine learning model with a friendly web interface so that anyone can use it, anywhere!

Installation using `pip`.

```python
pip install gradio
```

To build this LLM-powered application, we will break it down into three simple and easy steps.

## Step 1:
Import the important libraries, for this tutorial we will import LangChain and Gradio. 
```python
from langchain.document_loaders import PyPDFLoader
import gradio as gr

loader = PyPDFLoader("whitepaper.pdf") 
documents = loader.load()
```
From the code above: 
- `from langchain.document_loaders import PyPDFLoader`: Imports the PyPDFLoader module from LangChain, enabling PDF document loading ("whitepaper.pdf") which is in the same directory as our Python script.
- `import gradio as gr`: Imports Gradio, a Python library for creating customizable UI components for machine learning models and functions.

## Step 2: 
Create a summarize function to make the summarization. This function will define the PDF file path and an optional custom prompt as input.
```python
def summarize_pdf (pdf_file_path, custom_prompt=""):
    loader = PyPDFLoader(pdf_file_path)
    docs = loader.load_and_split()
    chain = load_summarize_chain(llm, chain_type="map_reduce")
    summary = chain.run(docs)
    
    return summary
```
- We define a function named `summarize_pdf` that takes a PDF file path and an optional custom prompt.
- Then we use the PyPDFLoader to load and split the PDF document into separate sections.
- Utilizing the LangChain's summarization capabilities through the `load_summarize_chain` function to generate a summary based on the loaded document.
- Return the generated summary.

## Step 3: 
This is the step where we need to set the Gradio Interface. 
```python
def main():
    input_pdf_path = gr.inputs.Textbox(label="Enter the PDF file path")
    output_summary = gr.outputs.Textbox(label="Summary")
    
    interface = gr.Interface(
        fn = summarize_pdf,
        inputs = input_pdf_path,
        outputs = output_summary,
        title = "PDF Summarizer",
        description = "This app allows you to summarize your PDF files.",
    ).launch(share=True)
```
- Sets up Gradio UI components for user interaction.
- We define an input field (`input_pdf_path`) for users to enter the PDF file path.
- Then we specify an output field (`output_summary`) to display the summarized text.
- We create a Gradio interface (`interface`) that utilizes the summarize_pdf function as the core functionality.
- Finally, we configure the interface with a title, description, input/output components, and launch the UI for user interaction.

## Launch/ Testing
We can now call the `main()` function to run the application. 

```python
main()
```
![Demo - One](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mw745tb6dg48amfnhtkg.png)

From the screenshot, you will see that Gradio launched the application on a public and private URL. 

Here is what the finished project looks like:

![PDF Summarizer image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icfrqfie1o6u82p334yn.png)

## Conclusion

By harnessing LangChain’s capabilities alongside Gradio’s intuitive interface, we’ve demystified the process of converting lengthy PDF documents into concise, informative summaries.

This isn’t just about building a tool; it’s about embracing the potential of technology to enhance information accessibility. Through this article, we’ve bridged the gap between complex data and user-friendly insights, empowering individuals to navigate through vast volumes of information effortlessly.

Happy summarizing! 😎 

### Resources

Full GitHub code: https://github.com/zenUnicorn/PDF-Summarizer-Using-LangChain


LangChain documentation: https://python.langchain.com/docs/get_started/introduction