Skip to content
TheChatArchiveThe Chat Archive
GeminiGemini 3 Pro Preview· googleHugging Face Frontier Reasoning Traces

Implement a Python script to build a text summarizer using transformers.

  1. User

    Implement a Python script to build a text summarizer using transformers.

  2. Gemini Assistant

    Crafting a Python Solution

    I'm now thinking about the structure of the Python script. I'm focusing on incorporating the transformers library effectively, specifically for summarization tasks. The plan involves selecting a suitable pre-trained model and preprocessing text. I'm starting to sketch out the logical flow: input text, model loading, summarization, and output.

    Analyzing Model Choices

    I've been examining different summarization models, focusing on the differences between abstractive and extractive methods. Abstractive models like BART and T5 seem ideal, leveraging the power of transformers. I am also determining the best approach to use, either the pipeline abstraction, which is the easiest, or using model/tokenizer classes, which is more complex but more powerful. I've begun drafting code for the pipeline method, importing the class and running a first pass with a sample text.

    Defining Core Functionality

    I'm now diving into the finer details of the custom model approach, focusing on BART. I've defined the necessary imports and am specifying facebook/bart-large-cnn. My next step is to detail the preprocessing steps, the text encoding, and the crucial generation parameters (max_length, min_length, length_penalty, and num_beams). I'm also outlining the decoding stage to get the final summarized text.

    Incorporating User Constraints

    I'm now thinking about how to handle the inevitable user constraints. Text input length is limited, so I'm considering adding truncation or informing the user of this limitation. I am also planning on updating the output handling, and I'm updating my explanation's structure to focus on Hugging Face, installation instructions, and script examples, using both the pipeline and the manual method.

    Refining the Core Code

    I'm now implementing the core summarization logic using AutoModelForSeq2SeqLM. I've initiated the setup for tokenization, loading the model, and encoding the input text. Then, I will generate the summary and decode the output, followed by refining the overall code for readability and clarity with comprehensive comments and organized headers.

    Here is a guide and Python scripts to build a text summarizer using the Hugging Face transformers library.

    I will provide two approaches:

    1. The "Pipeline" Approach: The easiest method, perfect for quick implementation.
    2. The Fine-Grained Approach: Using the Tokenizer and Model classes manually, which gives you control over parameters (like length, creativity, etc.).

    Prerequisites

    You will need to install the transformers library and a deep learning backend (usually PyTorch).

    pip install transformers torch
    

    (Note: If you use a model like T5 or Pegasus, you might also need pip install sentencepiece)


    Approach 1: The Quickest Way (Using pipeline)

    The pipeline API abstracts away all the complex code. By default, it uses the sshleifer/distilbart-cnn-12-6 model, but we will specify facebook/bart-large-cnn for better quality.

    from transformers import pipeline
    
    def summarize_text_pipeline(text):
        # Initialize the HuggingFace summarization pipeline
        # You can define a specific model (e.g., "facebook/bart-large-cnn") 
        # or let it use the default.
        summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
        
        # Generate summary
        # max_length: The maximum number of tokens in the output
        # min_length: The minimum number of tokens in the output
        # do_sample: False uses greedy decoding (more deterministic), True adds variety
        summary = summarizer(text, max_length=150, min_length=30, do_sample=False)
        
        return summary[0]['summary_text']
    
    if __name__ == "__main__":
        # Example long text
        article = """
        Artificial intelligence (AI) is intelligence demonstrated by machines, as opposed to the natural intelligence displayed by humans or animals. Leading AI textbooks define the field as the study of "intelligent agents": any system that perceives its environment and takes actions that maximize its chance of achieving its goals. Some popular accounts use the term "artificial intelligence" to describe machines that mimic "cognitive" functions that humans associate with the human mind, such as "learning" and "problem solving".
        AI applications include advanced web search engines, recommendation systems (used by YouTube, Amazon and Netflix), understanding human speech (such as Siri and Alexa), self-driving cars (e.g., Tesla), automated decision-making and competing at the highest level in strategic game systems (such as chess and Go). As machines become increasingly capable, tasks considered to require "intelligence" are often removed from the definition of AI, a phenomenon known as the AI effect. For instance, optical character recognition is frequently excluded from things considered to be AI, having become a routine technology.
        """
        
        result = summarize_text_pipeline(article)
        print("Original Length:", len(article))
        print("Summary Length:", len(result))
        print("\nSUMMARY:\n", result)
    

    Approach 2: The Customizable Way (Using Model & Tokenizer)

    This approach loads the model and tokenizer separately. This is useful if you want to control the Beam Search, Length Penalty, or run the model on a GPU.

    import torch
    from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
    
    class TextSummarizer:
        def __init__(self, model_name="facebook/bart-large-cnn"):
            print(f"Loading model: {model_name}...")
            self.tokenizer = AutoTokenizer.from_pretrained(model_name)
            self.model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
            
            # Move to GPU if available
            self.device = "cuda" if torch.cuda.is_available() else "cpu"
            self.model.to(self.device)
            print(f"Model loaded on {self.device}")
    
        def summarize(self, text, max_length=150, min_length=40):
            # 1. Preprocess the text (Tokenize)
            # 'truncation=True' ensures the input fits the model's max input size
            inputs = self.tokenizer.encode(
                "summarize: " + text, 
                return_tensors="pt", 
                max_length=1024, 
                truncation=True
            ).to(self.device)
    
            # 2. Generate Summary IDs
            # num_beams=4: Uses beam search (checks multiple paths) for better quality
            # length_penalty=2.0: Encourages the model to generate longer sequences
            # early_stopping=True: Stops generation when all beams attain EOS token
            summary_ids = self.model.generate(
                inputs,
                max_length=max_length,
                min_length=min_length,
                length_penalty=2.0,
                num_beams=4,
                early_stopping=True
            )
    
            # 3. Decode the summary IDs back to text
            summary = self.tokenizer.decode(summary_ids[0], skip_special_tokens=True)
            return summary
    
    if __name__ == "__main__":
        input_text = """
         NASA's James Webb Space Telescope has produced the deepest and sharpest infrared image of the distant universe to date. Known as Webb's First Deep Field, this image of galaxy cluster SMACS 0723 is overflowing with detail. Thousands of galaxies – including the faintest objects ever observed in the infrared – have appeared in Webb’s view for the first time. This slice of the vast universe covers a patch of sky approximately the size of a grain of sand held at arm’s length by someone on the ground.
        The image shows the galaxy cluster SMACS 0723 as it appeared 4.6 billion years ago. The combined mass of this galaxy cluster acts as a gravitational lens, magnifying much more distant galaxies behind it. Webb’s NIRCam has brought those distant galaxies into sharp focus – they have tiny, faint structures that have never been seen before, including star clusters and diffuse features. Researchers will soon begin to learn more about the galaxies’ masses, ages, histories, and compositions, as Webb seeks the earliest galaxies in the universe.
        """
        
        ts = TextSummarizer()
        summary_result = ts.summarize(input_text)
        
        print("\n---- GENERATED SUMMARY ----\n")
        print(summary_result)
    

    Which Model should I use?

    While the scripts above use BART (facebook/bart-large-cnn), here are other popular models you can swap into the model_name string:

    1. facebook/bart-large-cnn: (Best All-rounder) Great for news and articles.
    2. t5-base or t5-large: (Google's T5) Very versatile. Note: T5 requires you to prepend "summarize: " to your input text.
    3. google/pegasus-xsum: (State of the Art) Excellent for very abstractive summarization (rephrasing dramatically rather than just copying sentences).
    4. philschmid/bart-large-cnn-samsum: Specifically fine-tuned for summarizing dialogue (chat logs, meeting transcripts).

    Critical Note on Input Length

    Standard Transformer models (like BERT, BART, T5) have a maximum input limit (usually 512 or 1024 tokens).

    • If your text is longer than a few paragraphs, the rest will be cut off.
    • Solution for long documents: You must split your text into chunks, summarize each chunk, and then (optionally) summarize the combined summaries.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.