首頁 後端開發 Python教學 使用 Hugging Face 的 BART 模型總結文本

使用 Hugging Face 的 BART 模型總結文本

Jan 07, 2025 am 07:28 AM

Summarizing Text Using Hugging Face

在當今快節奏的世界中,無論是快速瀏覽文章還是突出研究論文中的要點,將長篇內容壓縮為簡潔的摘要都是至關重要的。 Hugging Face 提供了一個強大的文字摘要工具:BART 模型。在本文中,我們將探討如何利用 Hugging Face 的預訓練模型,特別是 facebook/bart-large-cnn 模型來總結長篇文章和文字。

開始使用 Hugging Face 的 BART 模型

Hugging Face 為文字分類、翻譯和摘要等 NLP 任務提供了多種模型。最受歡迎的摘要模型之一是 BART(雙向和自回歸變壓器),它經過訓練可以從大型文件產生連貫的摘要。

第 1 步:安裝 Hugging Face Transformers 庫

要開始使用 Hugging Face 模型,您需要安裝 Transformer 函式庫。您可以使用 pip 來執行此操作:

pip install transformers
登入後複製

步驟 2:導入摘要管道

安裝庫後,您可以輕鬆載入預先訓練的模型進行摘要。 Hugging Face 的管道 API 提供了使用 facebook/bart-large-cnn 等模型的高級接口,該模型已針對摘要任務進行了微調。

from transformers import pipeline

# Load the summarization model
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
登入後複製

第 3 步:運行摘要器

現在您已準備好摘要產生器,您可以輸入任何長文字來產生摘要。以下是使用有關英國著名女演員瑪吉·史密斯夫人的範例文章的範例。

ARTICLE = """ Dame Margaret Natalie Smith (28 December 1934 – 27 September 2024) was a British actress. Known for her wit in both comedic and dramatic roles, she had an extensive career on stage and screen for over seven decades and was one of Britain's most recognisable and prolific actresses. She received numerous accolades, including two Academy Awards, five BAFTA Awards, four Emmy Awards, three Golden Globe Awards and a Tony Award, as well as nominations for six Olivier Awards. Smith is one of the few performers to earn the Triple Crown of Acting.
Smith began her stage career as a student, performing at the Oxford Playhouse in 1952, and made her professional debut on Broadway in New Faces of '56. Over the following decades Smith established herself alongside Judi Dench as one of the most significant British theatre performers, working for the National Theatre and the Royal Shakespeare Company. On Broadway, she received the Tony Award for Best Actress in a Play for Lettice and Lovage (1990). She was Tony-nominated for Noël Coward's Private Lives (1975) and Tom Stoppard's Night and Day (1979).
Smith won Academy Awards for Best Actress for The Prime of Miss Jean Brodie (1969) and Best Supporting Actress for California Suite (1978). She was Oscar-nominated for Othello (1965), Travels with My Aunt (1972), A Room with a View (1985) and Gosford Park (2001). She portrayed Professor Minerva McGonagall in the Harry Potter film series (2001–2011). She also acted in Death on the Nile (1978), Hook (1991), Sister Act (1992), The Secret Garden (1993), The Best Exotic Marigold Hotel (2012), Quartet (2012) and The Lady in the Van (2015).
Smith received newfound attention and international fame for her role as Violet Crawley in the British period drama Downton Abbey (2010–2015). The role earned her three Primetime Emmy Awards; she had previously won one for the HBO film My House in Umbria (2003). Over the course of her career she was the recipient of numerous honorary awards, including the British Film Institute Fellowship in 1993, the BAFTA Fellowship in 1996 and the Society of London Theatre Special Award in 2010. Smith was made a dame by Queen Elizabeth II in 1990.
"""

# Generate the summary
summary = summarizer(ARTICLE, max_length=130, min_length=30, do_sample=False)

# Print the summary
print(summary)
登入後複製

輸出:

[{'summary_text': 'Dame Margaret Natalie Smith (28 December 1934 – 27 September 2024) was a British actress. Known for her wit in both comedic and dramatic roles, she had an extensive career on stage and screen for over seven decades. She received numerous accolades, including two Academy Awards, five BAFTA Awards, four Emmy Awards, three Golden Globe Awards and a Tony Award.'}]
登入後複製

正如您從輸出中看到的,摘要器將文章的要點濃縮為簡短、可讀的格式,突出顯示了關鍵事實,例如她的職業生涯壽命和榮譽。

另一種方法:總結文件中的文本

在某些用例中,您可能希望從檔案而不是硬編碼字串中讀取文字。下面是一個更新的 Python 腳本,它從文字檔案中讀取文章並產生摘要。

from transformers import pipeline

# Load the summarizer pipeline
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

# Function to read the article from a text file
def read_article_from_file(file_path):
    with open(file_path, 'r') as file:
        return file.read()

# Path to the text file containing the article
file_path = 'article.txt'  # Change this to your file path

# Read the article from the file
ARTICLE = read_article_from_file(file_path)

# Get the summary
summary = summarizer(ARTICLE, max_length=130, min_length=30, do_sample=False)

# Print the summary
print(summary)
登入後複製

文件輸入:

在這種情況下,您需要將文章儲存到文字檔案(範例中為article.txt),腳本將讀取內容並對其進行總結。

結論

Hugging Face 的 BART 模型是自動文字摘要的絕佳工具。無論您是在處理長文章、研究論文或任何大量文本,該模型都可以幫助您將資訊提煉成簡潔的摘要。

本文示範如何將 Hugging Face 的預訓練摘要模型整合到您的專案中,包括硬編碼文字和檔案輸入。只需幾行程式碼,您就可以在 Python 專案中啟動並運行高效的摘要管道。

以上是使用 Hugging Face 的 BART 模型總結文本的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1662
14
CakePHP 教程
1419
52
Laravel 教程
1312
25
PHP教程
1262
29
C# 教程
1235
24
Python vs.C:申請和用例 Python vs.C:申請和用例 Apr 12, 2025 am 12:01 AM

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時的Python計劃:一種現實的方法 2小時的Python計劃:一種現實的方法 Apr 11, 2025 am 12:04 AM

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python:遊戲,Guis等 Python:遊戲,Guis等 Apr 13, 2025 am 12:14 AM

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

您可以在2小時內學到多少python? 您可以在2小時內學到多少python? Apr 09, 2025 pm 04:33 PM

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

Python與C:學習曲線和易用性 Python與C:學習曲線和易用性 Apr 19, 2025 am 12:20 AM

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

Python和時間:充分利用您的學習時間 Python和時間:充分利用您的學習時間 Apr 14, 2025 am 12:02 AM

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python:探索其主要應用程序 Python:探索其主要應用程序 Apr 10, 2025 am 09:41 AM

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

Python:自動化,腳本和任務管理 Python:自動化,腳本和任務管理 Apr 16, 2025 am 12:14 AM

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

See all articles