Hugging Face LLM Text Summarization Tutorial

Hugging Face LLM Text Summarization Tutorial

Hugging Face LLM Text Summarization Tutorial

This step-by-step tutorial will guide you through creating a text summarization project using Hugging Face's Transformers library and a large language model (LLM). We'll use Python and a pre-trained model like BART to summarize text effectively. This guide is perfect for your Google Blog article!

Prerequisites

  • Basic Python knowledge
  • Jupyter Notebook or a Python IDE
  • Internet connection for downloading models
  • A Hugging Face account (optional, for certain models)

Step 1: Set Up Your Environment

Install the required libraries in your Python environment.

pip install transformers torch datasets
  • transformers: Hugging Face's library for pre-trained models.
  • torch: PyTorch, needed for model inference.
  • datasets: For loading sample datasets (optional).

Verify the installation:

import transformers
print(transformers.__version__)

Step 2: Choose a Pre-trained Model

Hugging Face offers models optimized for summarization, such as:

  • facebook/bart-large-cnn: BART model fine-tuned for summarization.
  • t5-small or t5-base: T5 models for text-to-text tasks.

We'll use facebook/bart-large-cnn for its performance and ease of use.

Step 3: Load the Model and Tokenizer

Load the model and tokenizer using the transformers library.

from transformers import BartTokenizer, BartForConditionalGeneration

# Load tokenizer and model
model_name = "facebook/bart-large-cnn"
tokenizer = BartTokenizer.from_pretrained(model_name)
model = BartForConditionalGeneration.from_pretrained(model_name)

print("Model and tokenizer loaded successfully!")

The tokenizer converts text into a model-compatible format, and the model generates summaries.

Step 4: Prepare Input Text

You'll need text to summarize. Here's an example:

input_text = """
The rapid advancement of artificial intelligence (AI) is transforming industries worldwide. From healthcare to finance, AI is being used to analyze vast amounts of data, make predictions, and automate tasks. In healthcare, AI-powered tools assist doctors in diagnosing diseases with higher accuracy. In finance, algorithms predict market trends and manage investments. However, the rise of AI also raises ethical concerns, including job displacement and privacy issues. Governments and organizations are now focusing on creating regulations to ensure AI is used responsibly.
"""

Replace input_text with your own article or document.

Step 5: Tokenize and Summarize

Convert the text into tokens and generate a summary.

# Tokenize input text
inputs = tokenizer(input_text, max_length=1024, return_tensors="pt", truncation=True)

# Generate summary
summary_ids = model.generate(
    inputs["input_ids"],
    max_length=150,  # Max length of summary
    min_length=40,   # Min length of summary
    length_penalty=2.0,  # Encourages concise output
    num_beams=4,     # Beam search for better results
    early_stopping=True
)

# Decode summary
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
print("Summary:", summary)

Parameters:

  • max_length and min_length: Control summary length.
  • num_beams: Improves quality with beam search.
  • length_penalty: Adjusts for shorter or longer outputs.

Example Output:

Summary: Artificial intelligence (AI) is revolutionizing industries like healthcare and finance by analyzing data, predicting trends, and automating tasks. AI aids in accurate disease diagnosis and investment management. However, it raises ethical concerns like job displacement and privacy, prompting regulatory efforts.

Step 6: Test with a Dataset (Optional)

Experiment with a dataset like CNN/DailyMail using Hugging Face's datasets library.

from datasets import load_dataset

# Load dataset
dataset = load_dataset("cnn_dailymail", "3.0.0", split="test[:1%]")

# Summarize first article
article = dataset[0]["article"]
inputs = tokenizer(article, max_length=1024, return_tensors="pt", truncation=True)
summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=40, length_penalty=2.0, num_beams=4)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
print("Article Summary:", summary)

This tests the model on real-world data.

Step 7: Fine-Tune Parameters

Adjust generation parameters for better results:

  • Increase max_length for longer summaries.
  • Reduce num_beams for faster inference.
  • Tweak length_penalty (e.g., 1.0 for shorter summaries).

Example tweak:

summary_ids = model.generate(
    inputs["input_ids"],
    max_length=100,
    min_length=30,
    length_penalty=1.0,
    num_beams=6
)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
print("Tuned Summary:", summary)

Step 8: Save and Deploy (Optional)

Save the model for reuse or deploy it in an application.

# Save model and tokenizer
model.save_pretrained("./bart_summarizer")
tokenizer.save_pretrained("./bart_summarizer")

# Load saved model
model = BartForConditionalGeneration.from_pretrained("./bart_summarizer")
tokenizer = BartTokenizer.from_pretrained("./bart_summarizer")

For deployment, integrate with a web app using Flask, FastAPI, or Hugging Face's Inference API.

Step 9: Explore Advanced Features

  • Fine-Tuning: Fine-tune on a custom dataset for domain-specific summarization (requires GPU).
  • Other Models: Try t5-base or pegasus-xsum.
  • Evaluation: Use ROUGE metrics (pip install rouge-score).

Example with T5:

from transformers import T5Tokenizer, T5ForConditionalGeneration

model_name = "t5-small"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)

# T5 requires "summarize:" prefix
input_text = "summarize: " + input_text
inputs = tokenizer(input_text, max_length=1024, return_tensors="pt", truncation=True)
summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=40, num_beams=4)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
print("T5 Summary:", summary)

Troubleshooting

  • Memory Issues: Use a smaller model like t5-small or reduce max_length.
  • Long Inference Time: Lower num_beams or use a smaller model.
  • Poor Summaries: Adjust length_penalty or try pegasus-xsum.

Conclusion

You've built a text summarization system using Hugging Face's Transformers! Extend it by fine-tuning, integrating into apps, or exploring Hugging Face's Inference API.

For more details, visit:

Comments

Popular posts from this blog

Building and Deploying a Recommender System on Kubeflow with KServe

CrewAI vs LangGraph: A Simple Guide to Multi-Agent Frameworks

Tutorial: Building Login and Sign-Up Pages with React, FastAPI, and XAMPP (MySQL)