Reading long articles and documents can be time-consuming, especially when only the key information is needed. An AI text summarizer can solve this problem by automatically generating a shorter version of the given text while retaining its important information.
This project is a web-based AI Text Summarizer built using Python, Streamlit, PyTorch, and Hugging Face Transformers. Users can paste an article or document into the application and generate a concise summary with customizable minimum and maximum lengths.
The application uses facebook/bart-large-cnn, a pretrained Transformer model specifically fine-tuned for text summarization. It also supports longer inputs by splitting the text into smaller chunks and summarizing each chunk separately.
Table of Contents
Imports and Technologies
import streamlit as st import torch from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
Streamlit is used to create the web interface, including the text box, buttons, sliders, and summary display.
PyTorch is responsible for running the deep-learning model. The application automatically checks whether a CUDA-enabled GPU is available and uses it when possible.
device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu"
)
The Hugging Face Transformers library provides the tokenizer and BART model.The project uses the following dependencies:
streamlit>=1.40.0 transformers>=4.45.0,<5.0.0 torch>=2.2.0 sentencepiece>=0.2.0
Model Used: BART Large CNN
The summarization model is defined as:MODEL_NAME = "facebook/bart-large-cnn"
BART (Bidirectional and Auto-Regressive Transformers) is an encoder-decoder Transformer model. It is well suited for text generation tasks such as summarization.
The bart-large-cnn model has been fine-tuned on the CNN/Daily Mail dataset, making it suitable for generating summaries of news-style and general text.
The project performs abstractive summarization, meaning the model generates new sentences rather than simply selecting sentences directly from the original text.
Loading the Model
The tokenizer and model are loaded using:
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)The tokenizer converts the input text into tokens that the model can understand, while AutoModelForSeq2SeqLM loads the sequence-to-sequence BART model.
The application also uses:
@st.cache_resourceThis prevents Streamlit from repeatedly loading the large model whenever the application reruns, improving performance.
Important Part: Text Summarization
Before generating a summary, the input is converted into tensors:
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
max_length=1024
)
The tokenizer converts human-readable text into numerical tokens. truncation=True prevents the input from exceeding the specified token limit.
The summary is generated using:
output_ids = model.generate(
**inputs,
max_length=max_length,
min_length=min_length,
num_beams=4,
length_penalty=1.0,
early_stopping=True,
no_repeat_ngram_size=3
)
Some important parameters are:
- max_length controls the maximum summary length.
- min_length prevents the summary from becoming too short.
- num_beams=4 uses beam search to explore multiple possible output sequences.
- early_stopping=True allows generation to stop when an appropriate ending is reached.
- no_repeat_ngram_size=3 helps reduce repetitive phrases.
Finally, the generated token IDs are converted back into readable text:
tokenizer.decode(
output_ids[0],
skip_special_tokens=True
)
Handling Long Text
One of the important features of the application is its ability to handle longer documents.
The input is divided into chunks of 650 words:
words = text.split()
chunk_size = 650
chunks = [
" ".join(words[i:i + chunk_size])
for i in range(0, len(words), chunk_size)
]
Each chunk is summarized separately, and the summaries are then combined.
Long Document
↓
Split into Chunks
↓
Summarize Each Chunk
↓
Combine Summaries
↓
Final Summary
If the combined summary is still too long, the application sends it through the model one more time to create a more concise final result. This provides a simple multi-stage summarization approach.Streamlit Interface
The application provides a simple interface where users can enter text and control the summary length using sliders.
It also performs basic validation, such as checking whether the user has entered text and whether the minimum summary length is smaller than the maximum length.
After generating the summary, users can download it as a .txt file using Streamlit's download_button.
Conclusion
The AI Text Summarizer is built using Python, Streamlit, PyTorch, and Hugging Face Transformers. It uses the BART Large CNN model to generate concise and meaningful summaries from user-provided text. The application uses tokenization to convert text into a format the model can process and beam search to improve the quality of generated summaries. For longer documents, the text is divided into smaller chunks and summarized separately before combining the results. Streamlit provides the interactive web interface, while PyTorch manages model execution on the CPU or GPU. The application also allows users to control the minimum and maximum summary length and download the generated summary as a text file.
Frequently Asked Questions (FAQs)
1. What model is used in the AI Text Summarizer?
The application uses the BART Large CNN (facebook/bart-large-cnn) model, a pretrained Transformer model fine-tuned for text summarization.
3.How does the application handle long text?BART is an encoder-decoder Transformer model designed for sequence-to-sequence tasks. It is well suited for abstractive summarization, where the model generates a new summary instead of simply selecting sentences from the original text.
4.What is the role of PyTorch in the project?The application divides long input text into smaller chunks of approximately 650 words. Each chunk is summarized separately, and the individual summaries are combined to create the final summary. If the combined summary is still too long, it is summarized again.
5.What technologies are used to build the application?PyTorch is used to load and run the BART model. The application automatically checks for a CUDA-enabled GPU and uses it when available; otherwise, it runs the model on the CPU.
The project uses Python, Streamlit, PyTorch, Hugging Face Transformers, and SentencePiece. Streamlit provides the web interface, while Transformers and BART handle the summarization process.
0 Comments