Generating Random Text with Huggingface Transformers

  • Share this:

Code introduction


This function uses a pre-trained language model from the Huggingface Transformers library to generate random text based on the provided prompt.


Technology Stack : Huggingface Transformers

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from transformers import pipeline

def generate_random_text(prompt, max_length=50):
    # This function generates random text using a pre-trained language model from Huggingface Transformers.
    
    # Randomly select a pre-trained model from the Huggingface model hub.
    model_name = random.choice([
        "gpt2",
        "distilgpt2",
        "t5-small",
        "t5-base"
    ])
    
    # Load the model and tokenizer.
    generator = pipeline("text-generation", model=model_name)
    
    # Generate random text based on the provided prompt.
    generated_text = generator(prompt, max_length=max_length)[0]['generated_text']
    
    return generated_text

# Code Information