首页 > 解决方案 > 我正在使用 Huggingface 的预训练 Pegasus 模型获取 Inshorts 数据集的抽象摘要

问题描述

我正在使用 Huggingface 的预训练 Pegasus 模型获取 Inshorts 数据集的抽象摘要。

我正在尝试在“inshorts”数据集(https://www.kaggle.com/shashichander009/inshorts-news-data/metadata)上使用此模型,其中我基本上需要由预训练模型为每个 Inshorts 样本生成的摘要使用输入作为数据集中的“短裤”列来预测摘要。因此,我尝试遍历“短裤”列但抛出错误:

TypeError: 'NoneType' object is not callable

下面是后台代码:

from transformers import PegasusForConditionalGeneration, PegasusTokenizer

tokenizer = PegasusTokenizer.from_pretrained("google/pegasus-xsum")

model = PegasusForConditionalGeneration.from_pretrained("google/pegasus-xsum")

以下是与数据集相关的代码:

import io

df = pd.read_excel(io.BytesIO(uploaded['inshorts.xlsx']))

这是它引发错误的代码:

for a in range(0,3):
  
text = df.iloc[a,1]
  
updated_text= ' """ ' + text + ' """ ' 
  
print(updated_text)

错误在这一行:

tokens = tokenizer(updated_text , truncation=True, padding="longest", return_tensors="pt")

summary = model.generate(**tokens)
  
tokenizer.decode(summary[0])

标签: pythontensorflowdeep-learningnlpsummarization

解决方案


问题是因为运行后tokenizer仍然存在。有一个名为running的必备软件包,可能您已经安装了,但请记住,安装后您必须使用 session/kernel 才能使更改生效。完成上述步骤后,t​​okenizer 变量将保存对象并解决了问题。Nonetokenizer = PegasusTokenizer.from_pretrained("google/pegasus-xsum")SentencePiecepegasusrestarttransformers.models.pegasus.tokenization_pegasus.PegasusTokenizer


推荐阅读