Build a Text Summarization App with LangChain and Streamlit

A Comprehensive Tutorial Using LangChain and OpenAI

Bhavik Jikadara
5 min readOct 4, 2024

--

Large Language Models (LLMs) are transforming the landscape of Natural Language Processing (NLP), making it possible for machines to perform tasks such as summarization, classification, generation, and more. These models, trained on massive amounts of text data, can now interpret, process, and generate human-like text, simplifying workflows in various industries.

In this tutorial, we’ll focus on text summarization, an essential NLP task that extracts or generates concise summaries from longer text bodies. Whether you’re dealing with news articles, research papers, or even meeting transcripts, text summarization can save hours by condensing vast information into digestible formats.

This post will guide you through the steps to build a Text Summarization App using LangChain and Streamlit. I will cover everything from obtaining the API key to deploying the app in just four simple steps.

What is Text Summarization?

Text summarization is the process of distilling the most important information from a text into a shorter version. In practical terms, this means creating a brief but informative summary of long-form content like research papers, podcasts, news articles, or even meeting notes.

There are two primary types of text summarization:

  1. Extractive Summarization — This approach selects and combines key sentences or phrases directly from the source text, leaving the wording unchanged.
  2. Abstractive Summarization — This method goes deeper. The system first understands the core ideas in the text and then paraphrases them, creating an entirely new summary. Abstractive summarization is more advanced because it requires not just selection but generation of content.

How the App Works

Our app will be built using Streamlit for the front-end interface and LangChain to handle the back-end text summarization process. The workflow is simple:

  1. The user submits a block of text.
  2. The text is pre-processed and split into chunks for easier processing.
  3. A summarization chain is applied using an LLM to create a concise summary.
  4. The summarized text is then displayed on the app’s front end.
Text summarization workflow

Step 1: Get an OpenAI API Key

Before building our app, we need access to OpenAI’s powerful language models. To do that, you’ll need an OpenAI API key. You can generate one by visiting OpenAI’s API page and signing up. Once you have the key, keep it secure, as it will be required to interact with OpenAI’s models in our app.

For more details on how to get the API key, you can refer to LangChain’s tutorial.

Step 2: Setting Up the Coding Environment

Once you have your API key, it’s time to set up your environment. You can build and run the app both locally and in the cloud. Let’s walk through the setup for both.

Local Development

Make sure your local environment has Python 3.7 or higher installed. Then, install the necessary libraries using the following pip command:

pip install streamlit langchain openai tiktoken

Cloud Deployment

If you prefer to develop in the cloud, Streamlit Community Cloud makes it easy. You can use their templates to get started quickly. Simply include the required libraries in a requirements.txt file as shown below:

streamlit
langchain
openai
tiktoken

Step 3: Building the App

Now, let’s get into the exciting part — building the actual app. In just 38 lines of code, we can create a working text summarization app that integrates with OpenAI’s LLM models.

Code Overview

We will be using LangChain to handle text processing and the LLM, while Streamlit will power the front-end interface for the app. Here’s a breakdown of what we’re about to build:

import streamlit as st
from langchain import OpenAI
from langchain.docstore.document import Document
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.summarize import load_summarize_chain

def generate_response(txt):
# Instantiate the LLM model
llm = OpenAI(temperature=0, openai_api_key=openai_api_key)
# Split text
text_splitter = CharacterTextSplitter()
texts = text_splitter.split_text(txt)
# Create multiple documents
docs = [Document(page_content=t) for t in texts]
# Text summarization
chain = load_summarize_chain(llm, chain_type='map_reduce')
return chain.run(docs)

Explaining the Code

  1. Importing Libraries — We import Streamlit for the UI, LangChain for interaction with OpenAI, and other necessary components like Document and CharacterTextSplitter.
  2. generate_response() Function — This function does the heavy lifting. It splits the input text into smaller, manageable chunks using CharacterTextSplitter. Then, we create documents from each chunk and feed them into the load_summarize_chain() function, which applies the summarization logic.
  3. UI Elements — We set up the front end using Streamlit. The app interface is clean and intuitive, with a text area for user input and a submit button. After submission, the app displays a loading spinner while processing the text and generates a summary using the OpenAI LLM model.

Here’s the full UI code:

# Page title
st.set_page_config(page_title='🦜🔗 Text Summarization App')
st.title('🦜🔗 Text Summarization App')

# Text input
txt_input = st.text_area('Enter your text', '', height=200)
# Form to accept user's text input for summarization
result = []
with st.form('summarize_form', clear_on_submit=True):
openai_api_key = st.text_input('OpenAI API Key', type='password', disabled=not txt_input)
submitted = st.form_submit_button('Submit')
if submitted and openai_api_key.startswith('sk-'):
with st.spinner('Calculating...'):
response = generate_response(txt_input)
result.append(response)
del openai_api_key
if len(result):
st.info(response)

Once the user enters their text and submits the form, the app processes it and returns a summarized version. For security, we clear the API key after each submission to ensure it’s not stored in memory.

Step 4: Deploying the App

After building the app, it’s time to deploy it so others can use it.

Deployment Steps:

  1. Create a GitHub repository for your app’s code.
  2. Visit Streamlit Community Cloud, and click “New app.”
  3. Select your GitHub repository, branch, and app file.
  4. Hit the “Deploy!” button, and your app will be live.

With these steps, you’ll have a fully deployed text summarization app!

Conclusion

Building a text summarization app using LangChain and Streamlit is a powerful way to harness LLMs for practical use cases. By breaking down the process into four manageable steps, you can create an efficient tool that leverages AI to condense large bodies of text into meaningful summaries.

I hope this tutorial helped you in your journey to mastering LLM-based app development. If you have any questions or suggestions, feel free to reach out via Twitter or LinkedIn.

Happy coding!

--

--

Bhavik Jikadara
Bhavik Jikadara

Written by Bhavik Jikadara

🚀 AI/ML & MLOps expert 🌟 Crafting advanced solutions to speed up data retrieval 📊 and enhance ML model lifecycles. buymeacoffee.com/bhavikjikadara

No responses yet