首頁 後端開發 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

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

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
<🎜>掩蓋:探險33-如何獲得完美的色度催化劑
2 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++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教學
1677
14
CakePHP 教程
1430
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
Python與C:學習曲線和易用性 Python與C:學習曲線和易用性 Apr 19, 2025 am 12:20 AM

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

學習Python:2小時的每日學習是否足夠? 學習Python:2小時的每日學習是否足夠? Apr 18, 2025 am 12:22 AM

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python vs.C:探索性能和效率 Python vs.C:探索性能和效率 Apr 18, 2025 am 12:20 AM

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

Python vs. C:了解關鍵差異 Python vs. C:了解關鍵差異 Apr 21, 2025 am 12:18 AM

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python標準庫的哪一部分是:列表或數組? Python標準庫的哪一部分是:列表或數組? Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

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

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

科學計算的Python:詳細的外觀 科學計算的Python:詳細的外觀 Apr 19, 2025 am 12:15 AM

Python在科學計算中的應用包括數據分析、機器學習、數值模擬和可視化。 1.Numpy提供高效的多維數組和數學函數。 2.SciPy擴展Numpy功能,提供優化和線性代數工具。 3.Pandas用於數據處理和分析。 4.Matplotlib用於生成各種圖表和可視化結果。

Web開發的Python:關鍵應用程序 Web開發的Python:關鍵應用程序 Apr 18, 2025 am 12:20 AM

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優

See all articles