Pythonと機械学習でテキストを要約する方法

要約は、多くの開発者が自動化したいと考える、非常に一般的なタスクです。例えば、あなたが書いているブログの各記事の要約を自動的に作成できたらいいと思いませんか?あるいは、従業員のためのドキュメントを自動的に要約できたらいいと思いませんか?たくさんの良いアプリケーションが存在する。

Bart Large CNNのような変換器ベースのモデルは、Pythonで簡単にテキストを要約することができます。これらの機械学習モデルは使いやすいのですが、スケールするのが難しいです。Bart Large CNNの使い方と、その性能を最適化する方法を見ていきましょう。

要約筆記

トランスフォーマーとバート大型CNN

Transformersは、最近、テキストの要約のような非常に高度な自然言語処理のユースケースを実現することを可能にした、高度なPythonフレームワークです。

トランスフォーマーやニューラルネットワークが登場する以前は、いくつかの選択肢がありましたが、どれもあまり満足できるものではありませんでした。

ここ数年、様々なユースケースに対応するため、Transformerをベースにした多くの優れた事前学習済み自然言語処理モデルが作成されています。Bart Large CNNはFacebookからリリースされ、テキストの要約に優れた結果を出しています。

ここでは、Bart Large CNNをPythonのコードで使用する方法を紹介します。

Pythonでテキストを要約する

Bart Large CNNを使う最も簡単な方法は、Hugging Faceリポジトリからダウンロードし、Transformersライブラリのテキスト要約パイプラインを使うことである。

from transformers import pipeline

summarizer = pipeline("summarization", model="facebook/bart-large-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."""

summary = summarizer(article, max_length=130, min_length=30))

出力します。

Liana Barrientos, 39, is charged with two counts of "offering a false instrument for filing in the first degree" In total, she 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.

このように、これはたった4行のPythonのコードで、要約の質も非常に良いのですしかし、モデルが大きいので初回のダウンロードに時間がかかることにお気づきかもしれません。

min_length と max_length パラメータは、要約の最小サイズと最大サイズを示します。これらは単語ではなく、トークンの数を表します。基本的に、トークンは単語だけでなく、句読点やサブワードであることもあります。一般に、100個のトークンは75個の単語にほぼ等しいと考えることができます。

重要:入力テキストは1024トークンより大きくできません(多かれ少なかれ800ワードに相当)。より大きなテキストを要約したい場合は、テキストのいくつかの部分を独立して要約し、その結果を再び組み立てるのが良い方法です。要約の要約を行うこともできます。

パフォーマンスに関する考察

しかし、このBart Large CNNモデルには2つの大きな問題がある。

まず、多くのディープラーニングモデルと同様に、重要なディスク容量とRAM(約1.5GB!)を必要とします。そしてこれは、GPT-3、GPT-J、T5 11Bなどのような巨大なものに比べれば、まだ小さなディープラーニングモデルとみなすことができます。

さらに言えば、かなり遅いです。このモデルは、実はボンネットの中でテキスト生成を行っているのですが、テキスト生成は本質的に遅いのです。800語で構成されるテキストを要約しようとすると、良いCPUで20秒程度かかります...。

その解決策は、Bart Large CNNをGPU上に展開することです。例えば、NVIDIA Tesla T4では、10倍のスピードアップが期待でき、800語のテキストを2秒程度で要約することができます。

GPUはもちろん非常に高価なので、計算をして投資に見合うかどうかを判断するのはあなた次第です

外部APIを本番に活用する

Bart Large CNNによるテキスト要約は、簡単なスクリプトで非常に使いやすいのですが、大量のリクエストに対して本番で使いたい場合はどうしたらいいでしょうか。

上記のように、最初の解決策は、GPUを備えた独自のハードウェアのプロビジョニングを引き受け、要約を高速化するためにいくつかのプロダクションの最適化に取り組むことでしょう。

2つ目の解決策は、このタスクをNLP Cloudのような専用サービスに委ねることです。NLP Cloudは、APIを通じてBart Large CNNモデルを提供してくれるでしょう。 要約APIエンドポイントのテストはこちら

結論

2022年、TransformersとBart Large CNNのおかげで、Pythonで高度なテキスト要約をわずかな労力で行うことができるようになった。

テキストの要約は非常に便利な作業であり、現在ではより多くの企業がアプリケーションの中で自動化しています。お分かりのように、複雑さは性能面から来るものです。Bart Large CNNを使ったテキスト要約を高速化するためのテクニックはいくつかありますが、これは別の記事で紹介します。

この記事が、あなたの次のプロジェクトの時間短縮に役立てば幸いです。 NLPクラウドでテキスト要約をお気軽にお試しください。

Julien Salinas
NLP CloudのCTO。