You can download this code by clicking the button below.
This code is now available for download.
This function uses the text-generation pipeline from the Huggingface Transformers library to generate random text of a specified length. By specifying the model name, different pre-trained models can be loaded for text generation.
Technology Stack : Huggingface Transformers
Code Type : Text generation
Code Difficulty : Intermediate
def generate_random_text(text_length, model_name="gpt2"):
from transformers import pipeline
# Load the text generation pipeline with the specified model
generator = pipeline("text-generation", model=model_name)
# Generate random text of the specified length
generated_text = generator("The quick brown fox", max_length=text_length)[0]['generated_text']
return generated_text