python-3.x - 在 python 中应用预训练的 facebook/bart-large-cnn 进行文本摘要

标签 python-3.x nlp huggingface-transformers summarization huggingface-tokenizers

我现在正在使用huggingface 变形金刚,并且对此有了一些见解。我正在使用 facebook/bart-large-cnn 模型来为我的项目执行文本摘要,并且我现在正在使用以下代码来进行一些测试:

text = """
Justin Timberlake and Jessica Biel, welcome to parenthood. 
The celebrity couple announced the arrival of their son, Silas Randall Timberlake, in 
statements to People."""

from transformers import pipeline
smr_bart = pipeline(task="summarization", model="facebook/bart-large-cnn")
smbart = smr_bart(text, max_length=150)
print(smbart[0]['summary_text'])

这段小代码实际上给了我一个很好的文本总结。但我的问题是,如何在数据框列之上应用相同的预训练模型。我的数据框如下所示:

ID        Lang          Text
1         EN            some long text here...
2         EN            some long text here...
3         EN            some long text here...

.... 50K 行依此类推

现在我想将预训练的模型应用于 col Text 以从中生成一个新列 df['summary'] ,生成的数据框应如下所示:

ID        Lang         Text                              Summary
1         EN            some long text here...           Text summary goes here...
2         EN            some long text here...           Text summary goes here...
3         EN            some long text here...           Text summary goes here...

我怎样才能实现这个目标?任何帮助将不胜感激。

最佳答案

您始终可以做的就是利用数据框 apply功能:

df = pd.DataFrame([('EN',text)]*10, columns=['Lang','Text'])

df['summary'] = df.apply(lambda x: smr_bart(x['Text'], max_length=150)[0]['summary_text'] , axis=1)

df.head(3)

输出:

    Lang    Text                                                summary
0   EN      \nJustin Timberlake and Jessica Biel, welcome ...   The celebrity couple announced the arrival of ...
1   EN      \nJustin Timberlake and Jessica Biel, welcome ...   The celebrity couple announced the arrival of ...
2   EN      \nJustin Timberlake and Jessica Biel, welcome ...   The celebrity couple announced the arrival of ...

这有点低效,因为每行都会调用管道(执行时间为 2 分 16 秒)。因此,我建议将 Text 列转换为列表并将其直接传递到管道(执行时间 41 秒):

df = pd.DataFrame([('EN',text)]*10, columns=['Lang','Text'])

df['summary'] = [x['summary_text'] for x in smr_bart(df['Text'].tolist(), max_length=150)]

df.head(3)

输出:

    Lang    Text                                                summary
0   EN      \nJustin Timberlake and Jessica Biel, welcome ...   The celebrity couple announced the arrival of ...
1   EN      \nJustin Timberlake and Jessica Biel, welcome ...   The celebrity couple announced the arrival of ...
2   EN      \nJustin Timberlake and Jessica Biel, welcome ...   The celebrity couple announced the arrival of ...

关于python-3.x - 在 python 中应用预训练的 facebook/bart-large-cnn 进行文本摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66372741/

相关文章:

python - 与 python3 asyncio 建立连接

python - 定义一个函数,该函数接受项目列表并返回以逗号分隔的项目名称列表

database - 文本情感检测数据集

python - 一次使用拥抱脸面膜填充超过 1 个蒙面 token 的最佳方式

nlp - 如何在batch_encode_plus之后获得一批句子的翻译?

python - 在 Python 3 中导入 Rosbag

python - 更正历史拼写

nlp - 识别句子中单词的上下文

python - 是否可以编辑 NLTK 的 vader 情感词典?

pytorch - 抱脸变形金刚 : truncation strategy in encode_plus