Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huggingface Summarization

I am practicing with Transformers to summarize text. Following the tutorial at : https://huggingface.co/transformers/usage.html#summarization

from transformers import pipeline

summarizer = pipeline("summarization")

ARTICLE = """ New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County, New York.
A year later, she got married again in Westchester County, but to a different man and without divorcing her first husband.
Only 18 days after that marriage, she got hitched yet again. Then, Barrientos declared "I do" five more times, sometimes only within two weeks of each other.
In 2010, she married once more, this time in the Bronx. In an application for a marriage license, she stated it was her "first and only" marriage.
Barrientos, now 39, is facing two criminal counts of "offering a false instrument for filing in the first degree," referring to her false statements on the
2010 marriage license application, according to court documents.
Prosecutors said the marriages were part of an immigration scam.
On Friday, she pleaded not guilty at State Supreme Court in the Bronx, according to her attorney, Christopher Wright, who declined to comment further.
After leaving court, Barrientos was arrested and charged with theft of service and criminal trespass for allegedly sneaking into the New York subway through an emergency exit, said Detective
Annette Markowski, a police spokeswoman. In total, Barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002.
All occurred either in Westchester County, Long Island, New Jersey or the Bronx. She is believed to still be married to four men, and at one time, she was married to eight men at once, prosecutors say.
Prosecutors said the immigration scam involved some of her husbands, who filed for permanent residence status shortly after the marriages.
Any divorces happened only after such filings were approved. It was unclear whether any of the men will be prosecuted.
The case was referred to the Bronx District Attorney\'s Office by Immigration and Customs Enforcement and the Department of Homeland Security\'s
Investigation Division. Seven of the men are from so-called "red-flagged" countries, including Egypt, Turkey, Georgia, Pakistan and Mali.
Her eighth husband, Rashid Rajput, was deported in 2006 to his native Pakistan after an investigation by the Joint Terrorism Task Force.
If convicted, Barrientos faces up to four years in prison.  Her next court appearance is scheduled for May 18.
"""

print(summarizer(ARTICLE, max_length=130, min_length=30))

I get the expected summarized text, but when I try another model (in the tutorial they used T5) :

from transformers import AutoModelWithLMHead, AutoTokenizer

model = AutoModelWithLMHead.from_pretrained("t5-base")
tokenizer = AutoTokenizer.from_pretrained("t5-base")

# T5 uses a max_length of 512 so we cut the article to 512 tokens.
inputs = tokenizer.encode("summarize: " + ARTICLE, return_tensors="pt", max_length=512)
outputs = model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
print(outputs)

I only get this kind of output :

tensor([[    0,    16,   792,     6,  1207,  3483,   235,     7,    65,   118,
          4464,   335,   648,     6,    28,  4169,    13,   160,  5281,     7,
         16198,   344,  5247,    11,  4407,     3,     5,   255,    19,  6141,
            12,   341,    36,  4464,    12,   662,  1076,     6,    11,    44,
            80,    97,     6,   255,  4464,  2641,  1076,    44,   728,     3,
             5]])

I'd like to get the summarized text with T5 in the end, just like the first tutorial. I am using Google Colab.

like image 817
xamlova Avatar asked Jun 29 '20 05:06

xamlova


People also ask

Can Bert do Abstractive summarization?

Abstractive BERT Summarization Performance Summarization aims to condense a document into a shorter version while preserving most of its meaning. Abstractive summarization task requires language generation capabilities to create summaries containing novel words and phrases not featured in the source document.

Which model is best for text summarization?

Neural sequence-to-sequence models have provided a viable new approach for abstractive text summarization (meaning they are not restricted to simply selecting and rearranging passages from the original text).

How does Abstractive summarization work?

Abstractive summarizers are so-called because they do not select sentences from the originally given text passage to create the summary. Instead, they produce a paraphrasing of the main contents of the given text, using a vocabulary set different from the original document.

What is summarization in information retrieval?

Text summarization is a way to condense the large amount of information into a concise form by the process of selection of important information and discarding unimportant and redundant information.


3 Answers

The pipeline class is hiding a lot of the steps you need to perform to use a model. In general the models are not aware of the actual words, they are aware of numbers. The tokenizer is the object which maps these number (called ids) to the actual words. When you print the variable inputs you will also see these numbers:

#print(inputs)
tensor([[21603,    10,   368,  1060,    41,   254, 17235,    61, 10555,   301,
         13662,  1386,  3483,   235,     7,    47,  1902,   203,   625,     6,
           255,   530,  4464,    16,  1244, 13263,  1334,     6,   368,  1060,
             5,    71,   215,   865,     6,   255,   530,  4464,   541,    16,
          1244, 13263,  1334,     6,    68,    12,     3,     9,   315,   388,
            11,   406,  1227,  1967,    75,    53,   160,   166,  2553,     5,
          3462,   507,   477,   227,    24,  5281,     6,   255,   530,  1560,
          4513,   780,   541,     5,    37,    29,     6,  1386,  3483,   235,
             7, 10126,    96,   196,   103,   121,   874,    72,   648,     6,
          1664,   163,   441,   192,  1274,    13,   284,   119,     5,    86,
          8693,   255,  4464,   728,    72,     6,    48,    97,    16,     8,
          4027,    29,   226,     5,    86,    46,   917,    21,     3,     9,
          5281,  3344,     6,   255,  4568,    34,    47,   160,    96, 14672,
            11,   163,   121,  5281,     5,  1386,  3483,   235,     7,     6,
           230,  6352,     6,    19,  5008,   192,  4336, 12052,    13,    96,
          1647,    49,    53,     3,     9,  6136,  5009,    21,  9479,    16,
             8,   166,  1952,   976,     3, 13215,    12,   160,  6136,  6643,
            30,     8,  2735,  5281,  3344,   917,     6,  1315,    12,  1614,
          2691,     5,   749,     7,    15,  1071,  5535,   243,     8,  5281,
             7,   130,   294,    13,    46, 10653, 13236,     5,   461,  1701,
             6,   255,     3, 30827,    26,    59, 10945,    44,  1015,  8531,
          2243,    16,     8,  4027,    29,   226,     6,  1315,    12,   160,
          4917,     6, 14702, 16634,     6,   113, 14833,    12,  1670,   856,
             5,   621,  3140,  1614,     6,  1386,  3483,   235,     7,    47,
         10195,    11,  4977,    28, 14806,    13,   313,    11,  4336,     3,
           929,     7,  3968,    21,     3, 18280, 14801,    53,   139,     8,
           368,  1060, 24435,   190,    46,  3583,  7189,     6,   243, 29271,
          6206,  1954,  2185, 21180,     6,     3,     9,  2095,  5468,     7,
         15893,     5,    86,   792,     6,  1386,  3483,   235,     7,    65,
           118,  4464,   335,   648,     6,    28,  4169,    13,   160,  5281,
             7, 16198,   344,  5247,    11,  4407,     5,   432,  6935,   893,
            16,  1244, 13263,  1334,     6,  3230,  2834,     6,   368,  5092,
            42,     8,  4027,    29,   226,     5,   451,    19,  6141,    12,
           341,    36,  4464,    12,   662,  1076,     6,    11,    44,    80,
            97,     6,   255,    47,  4464,    12,  2641,  1076,    44,   728,
             6,     3, 29905,   497,     5,   749,     7,    15,  1071,  5535,
           243,     8, 10653, 13236,  1381,   128,    13,   160,  2553,     7,
             6,   113,  5132,    21,  3574,  6198,  2637, 10545,   227,     8,
          5281,     7,     5,  2372,  7759,     7,  2817,   163,   227,   224,
          9479,     7,   130,  3754,     5,    94,    47, 19363,   823,   136,
            13,     8,  1076,    56,    36, 23489,    15,    26,     5,    37,
           495,    47,     3,  4822,    12,     8,  4027,    29,   226,  3570,
         10154,    31,     7,  2126,    57, 24123,    11,  6150,     7, 27075,
            11,     8,  1775,    13, 31032,  3684,    31,     7, 25093,  6022,
             5, 13065,    13,     8,  1076,    33,    45,    78,    18,  9341,
            96,  1271,    18,    89,   521, 11918,   121,  1440,     6,   379,
         10438,     6,  9299,     6,  5664,     6,  6697,    11,  2148,    23,
             5,  1347, 21227,  2553,     6,  9053, 11740, 13509,  2562,     6,
            47,    20, 16262,    16,  3581,    12,   112,  4262,  6697,   227,
            46,  4962,    57,     8, 16761,   332, 17262,   159,    51, 16107,
          5205,     5,   156,     3, 21217,     6,  1386,  3483,   235,     7,
          8519,    95]])

You can view this mapping from tokens to the ids as dictionary with:

tokenizer.get_vocab()

and see for example that 368 represents ▁New or 1060 ▁York (the first two words of your ARTICLE string). All you have to add to your code is the following line to convert your output back to the actual words:

tokenizer.decode(outputs[0])

Output:

in total, barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002. she is believed to still be married to four men, and at one time, she married eight men at once.
like image 138
cronoik Avatar answered Oct 08 '22 18:10

cronoik


You are almost there :)

You just need to add following to see a more readable output:

generated = tokenizer.decode(outputs[0])
print(generated)
like image 1
DesiKeki Avatar answered Oct 08 '22 18:10

DesiKeki


You did not mention the model in the pipeline you try below code

summarizer = pipeline("summarization", model="t5-base", tokenizer="t5-base", framework="tf")
atricle = "paste your text here"
print(summarizer(ARTICLE, max_length=130, min_length=30))
like image 1
Sumit Nikam Avatar answered Oct 08 '22 17:10

Sumit Nikam