Random Summary Generation with Huggingface Transformers

  • Share this:

Code introduction


This custom function uses the pipeline feature from the Huggingface Transformers library, randomly selects a pre-trained model, and generates a summary for the input text.


Technology Stack : Huggingface Transformers

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
import random
from transformers import pipeline

def generate_random_summary(text):
    model_names = ["t5-base", "facebook/bart-large", "google/mt5-small"]
    model_name = random.choice(model_names)
    summary_model = pipeline("summarization", model=model_name)
    summary = summary_model(text)[0]['summary_text']
    return summary