Get startedGet started for free

Building a text generation pipeline

Hugging Face pipelines make it simple to use machine learning models for a variety of tasks. In this exercise, you'll build a text generation pipeline using the gpt2 model and customize the output by adjusting its parameters.

Feel free to experiment with different prompts in the pipeline, such as "What if …?", "How to …?", or any other creative idea you’d like to explore.

This exercise is part of the course

Working with Hugging Face

View Course

Exercise instructions

  • Complete the missing code to build a text generation pipeline using the "gpt2" model.
  • Provide a custom sentence of your choice as the input prompt.
  • Configure the pipeline to generate up to 10 tokens and produce 3 outputs.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

from transformers import ____ 

my_pipeline = ____(task="text-generation", model="____")

# Generate three text outputs with a maximum length of 10 tokens
results = my_pipeline("____", max_length=____, num_return_sequences=____)

# Display each result
for result in results:
    print(result['generated_text'])
Edit and Run Code