Random Model Based Text Summarization

  • Share this:

Code introduction


This function uses the pipeline function from the Huggingface Transformers library to create an automatic summarization model and uses a randomly selected model to summarize the input text.


Technology Stack : Huggingface Transformers, pipeline

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import random
from transformers import pipeline

def generate_random_summary(text):
    # Define a list of available models for summarization
    models = ["t5-small", "t5-base", "t5-large", "facebook/bart-large-cnn", "google/summarization-model"]

    # Randomly select a model from the list
    selected_model = random.choice(models)

    # Create a summarization pipeline using the selected model
    summarizer = pipeline("summarization", model=selected_model)

    # Generate a summary for the given text
    summary = summarizer(text, max_length=150, min_length=30, do_sample=False)

    return summary[0]['summary_text']