NLP-文本摘要:利用预训练模型进行文本摘要任务【transformers:pipeline、T5、BART、Pegasus】

本文主要是介绍NLP-文本摘要:利用预训练模型进行文本摘要任务【transformers:pipeline、T5、BART、Pegasus】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、pipeline

可以使用pipeline快速实现文本摘要

from transformers import pipelinesummarizer = pipeline(task="summarization", model='t5-small')text = """summarize: (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar."This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."But for the second time since Francis' election, no Americans made the list."Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem."On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacyresult = summarizer(text, max_length=130, min_length=30, do_sample=False)print('result = ', result)

打印结果:

Token indices sequence length is longer than the specified maximum sequence length for this model (593 > 512). Running this sequence through the model will result in indexing errors
result =  [{'summary_text': 'a new group of bishops and archbishops will become cardinals . they come from 13 countries from every continent . the new cards are often referred to as the princes of the Catholic Church .'}]Process finished with exit code 0

二、T5

方式01、from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/t5/modeling_t5.py
from transformers import AutoTokenizer, AutoModelForSeq2SeqLMtokenizer = AutoTokenizer.from_pretrained(r'D:\Pretrained_Model\t5-base')
model = AutoModelForSeq2SeqLM.from_pretrained(r'D:\Pretrained_Model\t5-base')# 用T5做文本摘要任务,前面加 "summarize:"识别符
text = """summarize: (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar."This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."But for the second time since Francis' election, no Americans made the list."Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem."On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacyinputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')print('inputs = ', inputs)summary_ids = model.generate(inputs['input_ids'])print('\nsummary_ids = ', summary_ids)print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))

打印结果:

Ignored unknown kwarg option direction
inputs =  {'input_ids': tensor([[21603,    10,    41,   254, 17235,    61,  3809,     8,   511,    97,383,   112,     3, 16281,  4710,     6, 17384, 11065,    65,  2162,3,     9,   126,   563,    13, 25214,     7,    11, 11508, 11514,10776,     7,   356,    12,   582,   895, 10270,     7,  1636,    11,79,   369,    45,    66,   147,     8,   296,     5, 17384, 11065,243,  1771,    24,     3,    88,   133,  1520,     3,     9,  1338,13,   895, 10270,     7,    30,  2083,   968,    96,    26,  7920,84,    27,    56,   564,   627,   126, 21967,     7,   113,     6,1107,    45,  1179,  1440,    45,   334, 10829,     6,  6571,     8,3,  8482,     7, 26175,  2416,   344,     8,  2345,    13,  7332,11,     8,  1090,  2345,    15,     7,   915,    16,     8,   296,976,  1315,    12, 25770,  5061,     5,   368,   895, 10270,     7,33,   373,   359,   250,    79,   356,     8,  5739,    16,     8,2078,    11,    92, 11924,     8,   416,  2783,    15,     6, 19602,5523, 25770, 25224,  1079,   301,     5, 10618,   243,     5,   328,33,  1664,     3,  4822,    12,    38,     8, 22277,     7,    13,8,  6502,  2345,     5,    37,   126,   895, 10270,     7,   369,45,  1440,   224,    38, 22138,     6,   368,  5725,    11, 27274,5,    96,  3713,    19,     3,     9,  2783,    15,   113,   182,231,  2746,    12,  1535,    91,    12,   151,    30,     8,  6346,7,     6,    11,    25,  3133,   217,    24,    16,    48,   356,976, 10618,   243,     5,    96,  3774,    31,    60,  2508,    81,895, 10270,     7,    45,  3115, 20633,  1747,     6,   114,  9702,781,   221,     6,     8,  5824,  3368,    13,   304,  1725,     9,6, 21099,     6, 10508,     6, 30758,   535,   299,    21,     8,511,    97,   437, 11065,    31,  4356,     6,   150,  5452,   263,8,   570,     5,    96,   371,    52, 11389,     7,    31,  3275,19,   182,   964,    10,   216,  2746,    12,   281,    12,     8,20187,   158,  5082,    88,  2593,  1066,   145,  1747,    24,    33,641,   420,    18,    88, 19649,    28,   895, 10270,     7,   976,10618,   243,     5, 14702,  5377,   155,   235,     6,     3,     9,5812,    13,  2078,   892,    44,  2566,   152,   636,    16,   368,5092,     6,  4466,    24, 11065,  2162,   112,   126, 21079,    13,895, 10270,     7,    30,     8,  6502,   377, 11535,    13,     8,12741,  8237,    63,     6,    84, 18681,    15,     7,     8,   719,13,     8, 22673,    12,  1850,    31,  3879,  4687,    16, 15659,109,  6015,     5,    96,  7638, 18886,    13,   386,  7624,  1076,45,   623,   550,     6,     8, 17384,    31,     7,  3703,    21,895, 10270,   497,    24,   334,   415,  2078, 15314,     3,     9,286,    44,     8,   600,   953,   535,    86,   119,  1234,     6,11065,  2746,     3,     9,    72,    20, 21411,  2078,    11,  2746,12,  1616,  5139,   912,    45,   422,  2597,    24,  2561,   623,45,  6502,   159,    51,    31,     7,   579,  6881,     6,  5377,155,   235,   243,     5,   466,   744,    31,    17,  1243, 11065,19,     8,   166, 19068,  5982,    12,     3,     9,   102,  2700,895, 10270,     7,    45,     8,  2421,   296,     6,   713,     5,22738,    16,     8, 13978,     7,     6,    46,  3094,   381,    13,6271,   797,  2078,   904,   130,  2650,   895, 10270,     7,     6,11,    16,     8,  8754,     7,     6,   472,     5,  1079,     3,4,     4, 13671,     6,  4068, 11065,    54,   106,  1601,   336,215,     6,  7817,     8,   166,   895, 10270,     7,    45,  3411,6,     8, 12729,    11,  2648,     5,    86,   811,    12,     8,627,   126,   895, 10270,     7, 11065,  2650,    30,  1771,     6,874, 10611, 11508, 11514, 10776,     7,    11, 25214,     7,    56,92,    36, 13242,    38,   895, 10270,     7,     5,  2506,   215,6, 17384, 11065,  7817,   957,   126,   895, 10270,     7,     6,379, 25214,     7,    45, 22179,    11,  4152,  2917,     9,  1699,7,    32,     5, 19602,    31,     7,  4173, 27575,    11,  2144,10333,   109,   377,  8038,    76,  9859,    12,    48,   934,     5,1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1,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 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}summary_ids =  tensor([[    0,   126,   895, 10270,     7,   369,    45,  1179,  1440,    45,334, 10829,     3,     5,    79,    33,  1664,     3,  4822,    12]])['new cardinals come from 13 countries from every continent . they are sometimes referred to']
['new cardinals come from 13 countries from every continent . they are sometimes referred to']Process finished with exit code 0

方式02、from transformers import T5Tokenizer, T5ForConditionalGeneration

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/t5/modeling_t5.py
from transformers import T5Tokenizer, T5ForConditionalGenerationtokenizer = T5Tokenizer.from_pretrained(r'D:\Pretrained_Model\t5-base')
model = T5ForConditionalGeneration.from_pretrained(r'D:\Pretrained_Model\t5-base')text = """(CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar."This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."But for the second time since Francis' election, no Americans made the list."Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem."On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacyinputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')print('inputs = ', inputs)summary_ids = model.generate(inputs['input_ids'])print('\nsummary_ids = ', summary_ids)print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))

打印结果:

inputs =  {'input_ids': tensor([[   41,   254, 17235,    61,  3809,     8,   511,    97,   383,   112,3, 16281,  4710,     6, 17384, 11065,    65,  2162,     3,     9,126,   563,    13, 25214,     7,    11, 11508, 11514, 10776,     7,356,    12,   582,   895, 10270,     7,  1636,    11,    79,   369,45,    66,   147,     8,   296,     5, 17384, 11065,   243,  1771,24,     3,    88,   133,  1520,     3,     9,  1338,    13,   895,10270,     7,    30,  2083,   968,    96,    26,  7920,    84,    27,56,   564,   627,   126, 21967,     7,   113,     6,  1107,    45,1179,  1440,    45,   334, 10829,     6,  6571,     8,     3,  8482,7, 26175,  2416,   344,     8,  2345,    13,  7332,    11,     8,1090,  2345,    15,     7,   915,    16,     8,   296,   976,  1315,12, 25770,  5061,     5,   368,   895, 10270,     7,    33,   373,359,   250,    79,   356,     8,  5739,    16,     8,  2078,    11,92, 11924,     8,   416,  2783,    15,     6, 19602,  5523, 25770,25224,  1079,   301,     5, 10618,   243,     5,   328,    33,  1664,3,  4822,    12,    38,     8, 22277,     7,    13,     8,  6502,2345,     5,    37,   126,   895, 10270,     7,   369,    45,  1440,224,    38, 22138,     6,   368,  5725,    11, 27274,     5,    96,3713,    19,     3,     9,  2783,    15,   113,   182,   231,  2746,12,  1535,    91,    12,   151,    30,     8,  6346,     7,     6,11,    25,  3133,   217,    24,    16,    48,   356,   976, 10618,243,     5,    96,  3774,    31,    60,  2508,    81,   895, 10270,7,    45,  3115, 20633,  1747,     6,   114,  9702,   781,   221,6,     8,  5824,  3368,    13,   304,  1725,     9,     6, 21099,6, 10508,     6, 30758,   535,   299,    21,     8,   511,    97,437, 11065,    31,  4356,     6,   150,  5452,   263,     8,   570,5,    96,   371,    52, 11389,     7,    31,  3275,    19,   182,964,    10,   216,  2746,    12,   281,    12,     8, 20187,   158,5082,    88,  2593,  1066,   145,  1747,    24,    33,   641,   420,18,    88, 19649,    28,   895, 10270,     7,   976, 10618,   243,5, 14702,  5377,   155,   235,     6,     3,     9,  5812,    13,2078,   892,    44,  2566,   152,   636,    16,   368,  5092,     6,4466,    24, 11065,  2162,   112,   126, 21079,    13,   895, 10270,7,    30,     8,  6502,   377, 11535,    13,     8, 12741,  8237,63,     6,    84, 18681,    15,     7,     8,   719,    13,     8,22673,    12,  1850,    31,  3879,  4687,    16, 15659,   109,  6015,5,    96,  7638, 18886,    13,   386,  7624,  1076,    45,   623,550,     6,     8, 17384,    31,     7,  3703,    21,   895, 10270,497,    24,   334,   415,  2078, 15314,     3,     9,   286,    44,8,   600,   953,   535,    86,   119,  1234,     6, 11065,  2746,3,     9,    72,    20, 21411,  2078,    11,  2746,    12,  1616,5139,   912,    45,   422,  2597,    24,  2561,   623,    45,  6502,159,    51,    31,     7,   579,  6881,     6,  5377,   155,   235,243,     5,   466,   744,    31,    17,  1243, 11065,    19,     8,166, 19068,  5982,    12,     3,     9,   102,  2700,   895, 10270,7,    45,     8,  2421,   296,     6,   713,     5, 22738,    16,8, 13978,     7,     6,    46,  3094,   381,    13,  6271,   797,2078,   904,   130,  2650,   895, 10270,     7,     6,    11,    16,8,  8754,     7,     6,   472,     5,  1079,     3,     4,     4,13671,     6,  4068, 11065,    54,   106,  1601,   336,   215,     6,7817,     8,   166,   895, 10270,     7,    45,  3411,     6,     8,12729,    11,  2648,     5,    86,   811,    12,     8,   627,   126,895, 10270,     7, 11065,  2650,    30,  1771,     6,   874, 10611,11508, 11514, 10776,     7,    11, 25214,     7,    56,    92,    36,13242,    38,   895, 10270,     7,     5,  2506,   215,     6, 17384,11065,  7817,   957,   126,   895, 10270,     7,     6,   379, 25214,7,    45, 22179,    11,  4152,  2917,     9,  1699,     7,    32,5, 19602,    31,     7,  4173, 27575,    11,  2144, 10333,   109,377,  8038,    76,  9859,    12,    48,   934,     5,     1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1,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 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}summary_ids =  tensor([[    0,   126,   895, 10270,     7,   369,    45,  1179,  1440,    45,334, 10829,     3,     5,    79,    33,   557,     3,  4822,    12]])['new cardinals come from 13 countries from every continent . they are often referred to']
['new cardinals come from 13 countries from every continent . they are often referred to']Process finished with exit code 0

三、BART

方式01、from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/t5/modeling_t5.py
from transformers import AutoTokenizer, AutoModelForSeq2SeqLMtokenizer = AutoTokenizer.from_pretrained(r'D:\Pretrained_Model\bart-base')
model = AutoModelForSeq2SeqLM.from_pretrained(r'D:\Pretrained_Model\bart-base')# 用T5做文本摘要任务,前面加 "summarize:"识别符
text = """summarize: (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar."This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."But for the second time since Francis' election, no Americans made the list."Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem."On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacyinputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')print('inputs = ', inputs)summary_ids = model.generate(inputs['input_ids'])print('\nsummary_ids = ', summary_ids)print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))

打印结果:

Ignored unknown kwarg option direction
inputs =  {'input_ids': tensor([[    0, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437, 40402,35,    36, 16256,    43,  2709,     5,   200,    86,   148,    39,13102,  5073,     6,  8509,  5075,    34,   585,    10,    92,   333,9, 19929,     8,  9599,   428, 32084,   278,     7,   555,  1886,17552,   480,     8,    51,   283,    31,    70,    81,     5,   232,4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,  8509,5075,    26,   395,    14,    37,    74,   946,    10,   529,     9,1886, 17552,    15,   902,   501,    22, 37460,    61,    38,    40,766,   379,    92,  6293,    54,     6,   567,    31,   508,   749,31,   358,  9183,     6, 19318,     5,  9473,  3006,  1168, 26633,5678,   227,     5,  2197,     9,  8947,     8,     5,  1989, 37391,1455,    11,     5,   232,    60,   309,     7, 11484,  4611,     4,50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,   188,  1886,17552,    32,   460,   505,   142,    51,   278,     5,  6328,    11,5,  2352,     8,    67, 10371,     5,   220, 16627,     6,  3480,3596, 11484,  9821,   610,   226,     4,  3823,    26,     4,   252,32,  2128,  4997,     7,    25,     5, 39978,     9,     5,  4019,2197,     4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,20,    92,  1886, 17552,   283,    31,   749,   215,    25, 13934,6,   188,  3324,     8,  7095,     4, 50118,  1437,  1437,  1437,1437,  1437,  1437,  1437,    22,   713,    16,    10, 16627,    54,182,   203,  1072,     7,  1338,    66,     7,    82,    15,     5,5510,     6,     8,    47,  2563,   192,    14,    11,    42,   278,60,  3823,    26,     4,    22,  1185,   214,  1686,    59,  1886,17552,    31,  3700, 16042,  2127,     6,   101,  6268,  3060,  2794,6,     5,  3073,  2946,     9, 17922,   102,     6, 12276,     6,6547,     6, 17609,    72, 50118,  1437,  1437,  1437,  1437,  1437,1437,  1437,   125,    13,     5,   200,    86,   187,  5075,   108,729,     6,   117,  1791,   156,     5,   889,     4, 50118,  1437,1437,  1437,  1437,  1437,  1437,  1437,    22, 38461,   354,   108,6184,    16,   182,   699,    35,    91,  1072,     7,   213,     7,5, 20456, 41464,   918,  1195,    87,  2127,    14,    32,   416,299,    12, 18888,    19,  1886, 17552,    60,  3823,    26,     4,50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,  5469,  3043,29765,     6,    10,  3097,     9,  2352,   750,    23,  3350,   260,589,    11,   188,  3123,     6,  1581,    14,  5075,   585,    39,92, 15777,     9,  1886, 17552,    15,     5,  4019, 37072,     9,5, 14230, 39880,     6,    61, 16293,  1626,     5,   825,     9,5,  3771,   118,     7,  5772,   108, 32357,    11, 26557,     4,50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,    22,  4148,23220,     9,   130, 11036,   604,    31,   444,   409,     6,     5,8509,    18,  5717,    13, 32533,   224,    14,   358,   400,  2352,8613,    10,   317,    23,     5,   380,  2103,    72, 50118,  1437,1437,  1437,  1437,  1437,  1437,  1437,    96,    97,  1617,     6,5075,  1072,    10,    55, 34930,  2352,     8,  1072,     7,  1798,3114,  2956,    31,   650,  1822,    14,  2662,   444,    31, 42580,18,   476,  5228,     6,  3043, 29765,    26,     4, 50118,  1437,1437,  1437,  1437,  1437,  1437,  1437,   280,   630,    75,  1266,5075,    16,     5,    78, 29476,  4822,     7,  9653,  1886, 17552,31,     5,  2623,   232,     6,   600,     4, 22856,    11,     5,18283,    29,     6,    41,  2284,   346,     9,  5862,   470,  2352,2262,    58,  1440,  1886, 17552,     6,     8,    11,     5,  7571,29,     6,   312,     4,   610, 26166, 24457,     6,  2661,  5075,32839,  1538,    94,    76,     6,  3873,     5,    78,  1886, 17552,31,  1429,     6,     5,  5639,     8,  1327,     4, 50118,  1437,1437,  1437,  1437,  1437,  1437,  1437,    96,  1285,     7,     5,379,    92,  1886, 17552,  5075,  1440,    15,   395,     6,   292,3562,  9599,   428, 32084,     8, 19929,    40,    67,    28,  7809,25,  1886, 17552,     4, 50118,  1437,  1437,  1437,  1437,  1437,1437,  1437,  1426,    76,     6,  8509,  5075,  3873,   753,    92,1886, 17552,     6,   217, 19929,    31, 17009,     8, 18294,  1243,21433,   139,     4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,1437,  3480,    18,  3028, 12032,     8,  4845, 14286,   459,   274,5223,   257,  3162,     7,    42,   266,     4, 50118,     2]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}summary_ids =  tensor([[    2,     0,  1640, 16256,    43,  1437,  1437,  1437,  2537,  1437,1437,    36, 16256,    43,  2709,     5,   200,    86,   148,     2]])['(CNN)   Â   (CNN)For the second time during']
['(CNN)   Â   (CNN)For the second time during']Process finished with exit code 0

方式02、from transformers import BartTokenizer, BartForConditionalGeneration

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/t5/modeling_t5.py
from transformers import BartTokenizer, BartForConditionalGenerationtokenizer = BartTokenizer.from_pretrained(r'D:\Pretrained_Model\bart-base')
model = BartForConditionalGeneration.from_pretrained(r'D:\Pretrained_Model\bart-base')text = """(CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar."This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."But for the second time since Francis' election, no Americans made the list."Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem."On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacyinputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')print('inputs = ', inputs)summary_ids = model.generate(inputs['input_ids'])print('\nsummary_ids = ', summary_ids)print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))

打印结果:

inputs =  {'input_ids': tensor([[    0, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,  1437,36, 16256,    43,  2709,     5,   200,    86,   148,    39, 13102,5073,     6,  8509,  5075,    34,   585,    10,    92,   333,     9,19929,     8,  9599,   428, 32084,   278,     7,   555,  1886, 17552,480,     8,    51,   283,    31,    70,    81,     5,   232,     4,50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,  8509,  5075,26,   395,    14,    37,    74,   946,    10,   529,     9,  1886,17552,    15,   902,   501,    22, 37460,    61,    38,    40,   766,379,    92,  6293,    54,     6,   567,    31,   508,   749,    31,358,  9183,     6, 19318,     5,  9473,  3006,  1168, 26633,  5678,227,     5,  2197,     9,  8947,     8,     5,  1989, 37391,  1455,11,     5,   232,    60,   309,     7, 11484,  4611,     4, 50118,1437,  1437,  1437,  1437,  1437,  1437,  1437,   188,  1886, 17552,32,   460,   505,   142,    51,   278,     5,  6328,    11,     5,2352,     8,    67, 10371,     5,   220, 16627,     6,  3480,  3596,11484,  9821,   610,   226,     4,  3823,    26,     4,   252,    32,2128,  4997,     7,    25,     5, 39978,     9,     5,  4019,  2197,4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,    20,92,  1886, 17552,   283,    31,   749,   215,    25, 13934,     6,188,  3324,     8,  7095,     4, 50118,  1437,  1437,  1437,  1437,1437,  1437,  1437,    22,   713,    16,    10, 16627,    54,   182,203,  1072,     7,  1338,    66,     7,    82,    15,     5,  5510,6,     8,    47,  2563,   192,    14,    11,    42,   278,    60,3823,    26,     4,    22,  1185,   214,  1686,    59,  1886, 17552,31,  3700, 16042,  2127,     6,   101,  6268,  3060,  2794,     6,5,  3073,  2946,     9, 17922,   102,     6, 12276,     6,  6547,6, 17609,    72, 50118,  1437,  1437,  1437,  1437,  1437,  1437,1437,   125,    13,     5,   200,    86,   187,  5075,   108,   729,6,   117,  1791,   156,     5,   889,     4, 50118,  1437,  1437,1437,  1437,  1437,  1437,  1437,    22, 38461,   354,   108,  6184,16,   182,   699,    35,    91,  1072,     7,   213,     7,     5,20456, 41464,   918,  1195,    87,  2127,    14,    32,   416,   299,12, 18888,    19,  1886, 17552,    60,  3823,    26,     4, 50118,1437,  1437,  1437,  1437,  1437,  1437,  1437,  5469,  3043, 29765,6,    10,  3097,     9,  2352,   750,    23,  3350,   260,   589,11,   188,  3123,     6,  1581,    14,  5075,   585,    39,    92,15777,     9,  1886, 17552,    15,     5,  4019, 37072,     9,     5,14230, 39880,     6,    61, 16293,  1626,     5,   825,     9,     5,3771,   118,     7,  5772,   108, 32357,    11, 26557,     4, 50118,1437,  1437,  1437,  1437,  1437,  1437,  1437,    22,  4148, 23220,9,   130, 11036,   604,    31,   444,   409,     6,     5,  8509,18,  5717,    13, 32533,   224,    14,   358,   400,  2352,  8613,10,   317,    23,     5,   380,  2103,    72, 50118,  1437,  1437,1437,  1437,  1437,  1437,  1437,    96,    97,  1617,     6,  5075,1072,    10,    55, 34930,  2352,     8,  1072,     7,  1798,  3114,2956,    31,   650,  1822,    14,  2662,   444,    31, 42580,    18,476,  5228,     6,  3043, 29765,    26,     4, 50118,  1437,  1437,1437,  1437,  1437,  1437,  1437,   280,   630,    75,  1266,  5075,16,     5,    78, 29476,  4822,     7,  9653,  1886, 17552,    31,5,  2623,   232,     6,   600,     4, 22856,    11,     5, 18283,29,     6,    41,  2284,   346,     9,  5862,   470,  2352,  2262,58,  1440,  1886, 17552,     6,     8,    11,     5,  7571,    29,6,   312,     4,   610, 26166, 24457,     6,  2661,  5075, 32839,1538,    94,    76,     6,  3873,     5,    78,  1886, 17552,    31,1429,     6,     5,  5639,     8,  1327,     4, 50118,  1437,  1437,1437,  1437,  1437,  1437,  1437,    96,  1285,     7,     5,   379,92,  1886, 17552,  5075,  1440,    15,   395,     6,   292,  3562,9599,   428, 32084,     8, 19929,    40,    67,    28,  7809,    25,1886, 17552,     4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,1437,  1426,    76,     6,  8509,  5075,  3873,   753,    92,  1886,17552,     6,   217, 19929,    31, 17009,     8, 18294,  1243, 21433,139,     4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,3480,    18,  3028, 12032,     8,  4845, 14286,   459,   274,  5223,257,  3162,     7,    42,   266,     4, 50118,     2]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}summary_ids =  tensor([[    2,     0,  1640, 16256,    43,   480,  1437,  1437,  2537,  1437,1437,  1437,    36, 16256,    43,  2709,     5,   200,    86,     2]])['(CNN) --  Â    (CNN)For the second time']
['(CNN) --  Â    (CNN)For the second time']Process finished with exit code 0

四、Pegasus

方式01、from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/pegasus/modeling_pegasus.py
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLMtokenizer = AutoTokenizer.from_pretrained(r'D:\Pretrained_model\pegasus-cnn_dailymail')
model = AutoModelForSeq2SeqLM.from_pretrained(r'D:\Pretrained_model\pegasus-cnn_dailymail')text = """(CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar."This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."But for the second time since Francis' election, no Americans made the list."Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem."On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacyinputs = tokenizer.encode(text)
inputs = torch.tensor([inputs])print('inputs = ', inputs)summary_ids = model.generate(inputs)print('\nsummary_ids = ', summary_ids)print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))

打印结果:

inputs =  tensor([[  143, 40155,   158,   581,   109,   453,   166,   333,   169, 95987,108, 11481,  7756,   148,  1487,   114,   177,   456,   113, 35712,111, 66941,   116,   323,   112,   460, 30726,   116,  1315,   111,157,   331,   135,   149,   204,   109,   278,   107, 11481,  7756,243,  1342,   120,   178,   192,  1137,   114,   988,   113, 30726,116,   124,  1538,  1265,   198, 35871,   162,   125,   138,   442,738,   177, 18345,   170,   108,   792,   135,  1428,  1105,   135,290, 10156,   108, 14451,   109,   115,  8597, 32478,  1784,   317,109,  1887,   113,  6807,   111,   109,   970, 24353,   799,   115,109,   278,   745,   992,   112, 20525,  4474,   107,   351, 30726,116,   127,   329,   356,   262,   157,   323,   109,  4104,   115,109,  1588,   111,   163, 14094,   109,   352, 32577,   108, 11869,4244, 20525, 18672,  1084,  1054,   107,  6611,   243,   107,   322,127,  1254,  3795,   112,   130,   109, 54407,   113,   109,  4569,1887,   107,   139,   177, 30726,   116,   331,   135,  1105,   253,130, 16958,   108,   351,  3571,   111, 14838,   107,   198,   287,117,   114, 32577,   170,   221,   249,  1728,   112,  1111,   165,112,   200,   124,   109, 11691,   108,   111,   119,  2312,   236,120,   115,   136,   323,   745,  6611,   243,   107,   198,   417,131,   216,  1767,   160, 30726,   116,   135,  2222, 10912,  1262,108,   172,  5365, 23288,   108,   109,  3755,  2273,   113, 43439,108, 14668,   108,  6398,   108, 32671,   496,   343,   118,   109,453,   166,   381,  7756,   131,  2974,   108,   220,  3361,   266,109,   467,   107,   198, 59883,   131,  2293,   117,   221,   786,151,   285,  1728,   112,   275,   112,   109, 12483, 26941, 30713,3317,   880,   197,  1262,   120,   127,   506,   349,   121, 22564,122, 30726,   116,   745,  6611,   243,   107,  8751,  5706,  1418,497,   108,   114,  4609,   113,  1588,   689,   134, 69328,   502,115,   351,  3477,   108,  3151,   120,  7756,  1487,   169,   177,11598,   113, 30726,   116,   124,   109,  4569, 26717,   113,   109,60574,   108,   162, 56784,   109,   558,   113,   109, 33806,   112,1694,   131, 25910,   115, 26163,   107,   198,  1189, 11733,   113,339,  5509,  1024,   135,   571,   429,   108,   109, 11481,   131,116,  2257,   118, 30726,   416,   120,   290,   391,  1588,  8068,114,   295,   134,   109,   461,   826,   496,   222,   176,   989,108,  7756,  1728,   114,   154, 24500,  1588,   111,  1728,   112,1232,  6243,   675,   135,   360,  1724,   120,  2051,   571,   135,52403,   131,   116,   484,  3853,   108,  5706,  1418,   497,   243,107,   485,   591,   131,   144,  1021,  7756,   117,   109,   211,110, 39619, 18827,   112, 17717, 30726,   116,   135,   109,  1690,278,   108,   577,   107, 16591,   115,   109,  8821,   116,   108,142,  2186,   344,   113,  5249,   655,  1588,  3635,   195,  1729,30726,   116,   108,   111,   115,   109,  6939,   116,   108,   873,107,  1084, 61939, 12964,   108,  2901,  7756, 24828,  3792,   289,232,   108,  4486,   109,   211, 30726,   116,   135,  2466,   108,109,  6802,   111,  1922,   107,   222,   663,   112,   109,   738,177, 30726,   116,  7756,  1729,   124,  1342,   108,   668,  5774,66941,   116,   111, 35712,   138,   163,   129,  7051,   130, 30726,116,   107,  2882,   232,   108, 11481,  7756,  4486,  1925,   177,30726,   116,   108,   330, 35712,   135, 17256,   111, 58499, 55600,107, 11869,   131,   116,  4767, 18834,   111,  2333, 65534, 15391,28929,  5674,   112,   136,   731,   107,     1]])summary_ids =  tensor([[    0,   139,   177, 30726,   116,   331,   135,  1105,   253,   130,16958,   108,   351,  3571,   111, 14838,   110,   107,   106,  1667,3361,   266,   109,   467,   118,   109,   453,   166,   381,  7756,131,  2974,   110,   107,     1]])["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]
["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]Process finished with exit code 0

方式02、from transformers import PegasusTokenizer, PegasusForConditionalGeneration

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/pegasus/modeling_pegasus.py
from transformers import PegasusTokenizer, PegasusForConditionalGenerationtokenizer = PegasusTokenizer.from_pretrained(r'D:\Pretrained_Model\pegasus-cnn_dailymail')
model = PegasusForConditionalGeneration.from_pretrained(r'D:\Pretrained_Model\pegasus-cnn_dailymail')text = """(CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar."This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."But for the second time since Francis' election, no Americans made the list."Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem."On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacyinputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')print('inputs = ', inputs)summary_ids = model.generate(inputs['input_ids'])print('\nsummary_ids = ', summary_ids)print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))

打印结果:

inputs =  {'input_ids': tensor([[  143, 40155,   158,   581,   109,   453,   166,   333,   169, 95987,108, 11481,  7756,   148,  1487,   114,   177,   456,   113, 35712,111, 66941,   116,   323,   112,   460, 30726,   116,  1315,   111,157,   331,   135,   149,   204,   109,   278,   107, 11481,  7756,243,  1342,   120,   178,   192,  1137,   114,   988,   113, 30726,116,   124,  1538,  1265,   198, 35871,   162,   125,   138,   442,738,   177, 18345,   170,   108,   792,   135,  1428,  1105,   135,290, 10156,   108, 14451,   109,   115,  8597, 32478,  1784,   317,109,  1887,   113,  6807,   111,   109,   970, 24353,   799,   115,109,   278,   745,   992,   112, 20525,  4474,   107,   351, 30726,116,   127,   329,   356,   262,   157,   323,   109,  4104,   115,109,  1588,   111,   163, 14094,   109,   352, 32577,   108, 11869,4244, 20525, 18672,  1084,  1054,   107,  6611,   243,   107,   322,127,  1254,  3795,   112,   130,   109, 54407,   113,   109,  4569,1887,   107,   139,   177, 30726,   116,   331,   135,  1105,   253,130, 16958,   108,   351,  3571,   111, 14838,   107,   198,   287,117,   114, 32577,   170,   221,   249,  1728,   112,  1111,   165,112,   200,   124,   109, 11691,   108,   111,   119,  2312,   236,120,   115,   136,   323,   745,  6611,   243,   107,   198,   417,131,   216,  1767,   160, 30726,   116,   135,  2222, 10912,  1262,108,   172,  5365, 23288,   108,   109,  3755,  2273,   113, 43439,108, 14668,   108,  6398,   108, 32671,   496,   343,   118,   109,453,   166,   381,  7756,   131,  2974,   108,   220,  3361,   266,109,   467,   107,   198, 59883,   131,  2293,   117,   221,   786,151,   285,  1728,   112,   275,   112,   109, 12483, 26941, 30713,3317,   880,   197,  1262,   120,   127,   506,   349,   121, 22564,122, 30726,   116,   745,  6611,   243,   107,  8751,  5706,  1418,497,   108,   114,  4609,   113,  1588,   689,   134, 69328,   502,115,   351,  3477,   108,  3151,   120,  7756,  1487,   169,   177,11598,   113, 30726,   116,   124,   109,  4569, 26717,   113,   109,60574,   108,   162, 56784,   109,   558,   113,   109, 33806,   112,1694,   131, 25910,   115, 26163,   107,   198,  1189, 11733,   113,339,  5509,  1024,   135,   571,   429,   108,   109, 11481,   131,116,  2257,   118, 30726,   416,   120,   290,   391,  1588,  8068,114,   295,   134,   109,   461,   826,   496,   222,   176,   989,108,  7756,  1728,   114,   154, 24500,  1588,   111,  1728,   112,1232,  6243,   675,   135,   360,  1724,   120,  2051,   571,   135,52403,   131,   116,   484,  3853,   108,  5706,  1418,   497,   243,107,   485,   591,   131,   144,  1021,  7756,   117,   109,   211,110, 39619, 18827,   112, 17717, 30726,   116,   135,   109,  1690,278,   108,   577,   107, 16591,   115,   109,  8821,   116,   108,142,  2186,   344,   113,  5249,   655,  1588,  3635,   195,  1729,30726,   116,   108,   111,   115,   109,  6939,   116,   108,   873,107,  1084, 61939, 12964,   108,  2901,  7756, 24828,  3792,   289,232,   108,  4486,   109,   211, 30726,   116,   135,  2466,   108,109,  6802,   111,  1922,   107,   222,   663,   112,   109,   738,177, 30726,   116,  7756,  1729,   124,  1342,   108,   668,  5774,66941,   116,   111, 35712,   138,   163,   129,  7051,   130, 30726,116,   107,  2882,   232,   108, 11481,  7756,  4486,  1925,   177,30726,   116,   108,   330, 35712,   135, 17256,   111, 58499, 55600,107, 11869,   131,   116,  4767, 18834,   111,  2333, 65534, 15391,28929,  5674,   112,   136,   731,   107,     1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}summary_ids =  tensor([[    0,   139,   177, 30726,   116,   331,   135,  1105,   253,   130,16958,   108,   351,  3571,   111, 14838,   110,   107,   106,  1667,3361,   266,   109,   467,   118,   109,   453,   166,   381,  7756,131,  2974,   110,   107,     1]])["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]
["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]Process finished with exit code 0



参考资料:
Transformers预训练模型使用:文本摘要 Summarization

这篇关于NLP-文本摘要:利用预训练模型进行文本摘要任务【transformers:pipeline、T5、BART、Pegasus】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1128742

相关文章

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系

springboot如何通过http动态操作xxl-job任务

《springboot如何通过http动态操作xxl-job任务》:本文主要介绍springboot如何通过http动态操作xxl-job任务的问题,具有很好的参考价值,希望对大家有所帮助,如有错... 目录springboot通过http动态操作xxl-job任务一、maven依赖二、配置文件三、xxl-

使用Python进行GRPC和Dubbo协议的高级测试

《使用Python进行GRPC和Dubbo协议的高级测试》GRPC(GoogleRemoteProcedureCall)是一种高性能、开源的远程过程调用(RPC)框架,Dubbo是一种高性能的分布式服... 目录01 GRPC测试安装gRPC编写.proto文件实现服务02 Dubbo测试1. 安装Dubb

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Linux使用scp进行远程目录文件复制的详细步骤和示例

《Linux使用scp进行远程目录文件复制的详细步骤和示例》在Linux系统中,scp(安全复制协议)是一个使用SSH(安全外壳协议)进行文件和目录安全传输的命令,它允许在远程主机之间复制文件和目录,... 目录1. 什么是scp?2. 语法3. 示例示例 1: 复制本地目录到远程主机示例 2: 复制远程主

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可