Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Working with Hugging Face

Cursus bekijken

Oefeninstructies

  • 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; keep it short to prevent timeouts.
  • Configure the pipeline to generate up to 10 tokens and produce 2 outputs.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

from transformers import ____ 

gpt2_pipeline = ____(task="____", model="openai-community/gpt2")

# Generate three text outputs with a maximum length of 10 tokens
results = gpt2_pipeline("What if AI", max_new_tokens=____, num_return_sequences=____)

for result in results:
    print(result['generated_text'])
Code bewerken en uitvoeren