ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

使用huggingface的pipeline进行文本生成Text Generation

2026/8/4 12:43:14 拓冰建站 浏览量
使用huggingface的pipeline进行文本生成Text Generation

默认使用的模型是gpt2,会下载到本地的~/.cache/huggingface/目录下,
第一次运行会自动下载,第二次运行直接从本地加载,
可以尝试断网之后运行,也一样的会出结果.

先安装以下:

pip install huggingface_hub

pip install transformers

pip install --upgrade pip

pip install tensorflow

from transformers import pipeline
from huggingface_hub.hf_api import HfFolder
HfFolder.save_token('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')
pipe = pipeline("text-generation", num_return_sequences=2)
print(pipe("In this course, we will teach you how to"))