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

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
Python中有可能理解嗎?如果是,為什麼以及如果不是為什麼?Python中有可能理解嗎?如果是,為什麼以及如果不是為什麼?Apr 28, 2025 pm 04:34 PM

文章討論了由於語法歧義而導致的Python中元組理解的不可能。建議使用tuple()與發電機表達式使用tuple()有效地創建元組。 (159個字符)

Python中的模塊和包裝是什麼?Python中的模塊和包裝是什麼?Apr 28, 2025 pm 04:33 PM

本文解釋了Python中的模塊和包裝,它們的差異和用法。模塊是單個文件,而軟件包是帶有__init__.py文件的目錄,在層次上組織相關模塊。

Python中的Docstring是什麼?Python中的Docstring是什麼?Apr 28, 2025 pm 04:30 PM

文章討論了Python中的Docstrings,其用法和收益。主要問題:Docstrings對於代碼文檔和可訪問性的重要性。

什麼是lambda功能?什麼是lambda功能?Apr 28, 2025 pm 04:28 PM

文章討論了Lambda功能,與常規功能的差異以及它們在編程方案中的效用。並非所有語言都支持他們。

什麼是休息時間,繼續並通過python?什麼是休息時間,繼續並通過python?Apr 28, 2025 pm 04:26 PM

文章討論了休息,繼續並傳遞Python,並解釋了它們在控制循環執行和程序流中的作用。

Python的通行證是什麼?Python的通行證是什麼?Apr 28, 2025 pm 04:25 PM

本文討論了Python中的“ Pass”語句,該語句是函數和類等代碼結構中用作佔位符的空操作,允許在沒有語法錯誤的情況下實現將來實現。

我們可以在Python中傳遞作為參數的函數嗎?我們可以在Python中傳遞作為參數的函數嗎?Apr 28, 2025 pm 04:23 PM

文章討論了將功能作為Python中的參數,突出了模塊化和用例(例如分類和裝飾器)等好處。

Python中的 /和//有什麼區別?Python中的 /和//有什麼區別?Apr 28, 2025 pm 04:21 PM

文章在Python中討論 /和//運營商: / for for True Division,//用於地板部門。主要問題是了解它們的差異和用例。 Character數量:158

See all articles

熱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

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

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

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

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!