首先安装transformers
>pip install transformers
from transformers import pipeline
API例子1–情感分类
首先需要下载并缓存预训练模型
classifier = pipeline('sentiment-analysis')
尝试输入一段文本,预训练模型会给出相应的情感类别,和相应的置信度分数
classifier('这部电影真的很垃圾,浪费我的时间!!!')
classifier('this movie is really great! I love it')
API例子二–智能填词(Masked Language Modeling)
下载并缓存预训练模型
unmask = pipeline('fill-mask')
尝试输入一段有空缺的句子,观察被填补的空缺是否符合真实情况
from pprint import pprint
results1 = unmask(f'{unmask.tokenizer.mask_token} is the most beatiful woman in Harry Potter!')
results2 = unmask(f'{unmask.tokenizer.mask_token} is the best player in the NBA!')
pprint(results1)
pprint(results2)
API例子三–文本生成
下载并缓存预训练模型
from transformers import pipeline, set_seed
generator = pipeline('text-generation', model='gpt2')
set_seed(42)
generator("Xiao ming loves Xiao Hong secretly,",max_length=100, num_return_sequences=5)
更多的任务pipeline API可以在下面的网站找到:
https://huggingface.co/transformers/task_summary.html