MulaiMulai sekarang secara 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.

Latihan ini adalah bagian dari kursus

Working with Hugging Face

Lihat Kursus

Petunjuk latihan

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

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

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'])
Edit dan Jalankan Kode