Hugging Faceで文章要約を試してみた

文章要約を簡単にしてみたいです。

Hugging faceでできるぞ

Hugging Faceはあいかわらずすごいですね。

下記の部分にその記述があります。

https://huggingface.co/transformers/task_summary.html#summarization

環境構築

Google Colabで環境構築を行います。

下記の記事に手順を記述しています。

ライブラリを導入します。

! pip install transformers 

モデルとTokenizerをまとめて扱えるpipelineを使用します。

このパイプラインはデータの前処理からモデルの推論までを簡単にできるものです。タスクを指定して簡単に動作できます。下記リンク先に詳細があります。

https://huggingface.co/transformers/main_classes/pipelines.html

from transformers import pipeline

summarizer = pipeline("summarization")

下記のCNNのニュースを要約してみます。

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.
"""

予め、作成したpipelineにCNNのニュースを入力して、最大長:130、最小:30の要約文を作成します。

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

出力結果は下記になります。

[{'summary_text': ' In total, Barrientos has been married to 10 men since 1999 . 
She faces two counts of "offering a false instrument for filing in the first degree" 
Prosecutors say the marriages were part of an immigration scam .'}]

パイプラインでは中のモデルが分からないのでトークナイザーとモデルを指定して動作させてみます。

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

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)
tokenizer.decode(outputs[0])

出力結果は異なりますが要約処理ができています。

<pad> prosecutors say the marriages were part of an immigration scam. 
if convicted, barrientos faces two criminal counts of "offering a false instrument for filing in the first degree" 
she has been married 10 times, nine of them between 1999 and 2002.</s>

以外に簡単にできましたね

Hugging faceのpipelineは簡単にいろんなタスクを試せるのでまた紹介するぞ

Hugging Faceを用いて日本語のデータのポジネガ分析を行い方は下記をご覧ください。

Close Bitnami banner
Bitnami