Random Text Summary Generator with Huggingface Transformers

  • Share this:

Code introduction


This function uses the summarization model from the Huggingface Transformers library to generate a random summary of the given text.


Technology Stack : Huggingface Transformers, pipeline

Code Type : Function

Code Difficulty : Intermediate


                
                    
import torch
from transformers import pipeline

def generate_random_summary(text):
    """
    Generate a random summary of the given text using the Huggingface Transformers library.
    """
    # Load the summary model
    summary_model = pipeline('summarization')
    
    # Generate a random summary
    summary = summary_model(text, max_length=150, min_length=30)
    
    return summary[0]['summary_text']