[Python] Using flan-t5-base for article summary

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm just starting ML journey. I picked Google/flan-t5-base for article summarization but I'm not getting a quality summary. It's just chunks picked from the input article text. I'm not sure what's playing wrong.

    from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM

    # Load FLAN-T5 base model and tokenizer
    model_name = r"C:\Users\Rishi_Niranjan\flan-t5-base"

    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

    # Build summarization pipeline
    summarizer = pipeline(
    "summarization",
    model=model,
    tokenizer=tokenizer,
    device=-1 # Set to 0 for GPU
    )

    # Your input article
    ARTICLE = """
    summarize: No text evidence that our universe might exist inside a black hole what's up with that well let's back up for a moment okay and put some base information on the table a black hole is a region of space and time where the strength of gravity is so high that light has no path to get out of the object all paths through spaceime curve back in on itself normally we think of a hole as a hole in the floor and you fall through come out the other side except it's a three-dimensional hole you can fall in in any direction that edge beyond which there's no return we call the event horizon it's a horizon h the universe has a horizon too it is a a distance beyond which no information can reach us because the universe isn't old enough for light from anything beyond that horizon to have gotten here yet by the way the more mass is in a black hole the bigger the black hole becomes in fact if you run the math on it it's it's linear so a black hole that's twice as massive as another one will be twice the diameter 10 times as massive the event horizon which is our practical definition of the size of the black hole will be 10 times as large there's a cool No text exercise we do in graduate school you can say given the mass of the universe how big would the black hole be that contain this much mass it's the size of our universe to our horizon and it's like wait a minute could we be one giant black hole the average density of matter contained within the size of our cosmic horizon matches right on with what you'd get for a black hole this size that's intriguing maybe it's just a coincidence
    """

    # Truncate input if needed
    inputs = tokenizer(ARTICLE, return_tensors="pt", max_length=1024, truncation=True)
    truncated_text = tokenizer.decode(inputs["input_ids"][0], skip_special_tokens=True)

    # Run summarization
    summary = summarizer(
    truncated_text,
    max_length=200,
    min_length=60,
    do_sample=False,
    num_beams=4
    )

    # Output result
    print("\nGenerated Summary:\n", summary[0]["summary_text"])


    Output:


    Generated Summary: a black hole is a region of space and time where the strength of gravity is so high that light >has no path to get out of the object all paths through spaceime curve back in on itself h the >universe has a horizon too he's a cool No text exercise we do in graduate
    ​

    Continue reading...

Compartilhe esta Página