IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Working with Hugging Face

Visualizza il corso

Istruzioni dell'esercizio

  • 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.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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'])
Modifica ed esegui il codice