建立文字生成 pipeline
Hugging Face 的 pipeline 讓你能輕鬆以各種任務來使用機器學習模型。在這個練習中,你將使用 gpt2 模型建立文字生成的 pipeline,並透過調整參數來自訂輸出。
你可以大膽嘗試在 pipeline 中使用不同的提示語,例如:「What if …?」、「How to …?」,或任何你想探索的創意點子。
本練習屬於課程
善用 Hugging Face
練習說明
- 補上缺漏的程式碼,使用
"gpt2"模型建立一個文字生成的 pipeline。 - 輸入一個你自訂的短句作為提示語;請保持精簡以避免逾時。
- 將 pipeline 設定為產生最多 10 個 token,並輸出2 個結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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'])