在當今快節奏的世界中,無論是快速瀏覽文章還是突出研究論文中的要點,將長篇內容壓縮為簡潔的摘要都是至關重要的。 Hugging Face 提供了一個強大的文字摘要工具:BART 模型。在本文中,我們將探討如何利用 Hugging Face 的預訓練模型,特別是 facebook/bart-large-cnn 模型來總結長篇文章和文字。
Hugging Face 為文字分類、翻譯和摘要等 NLP 任務提供了多種模型。最受歡迎的摘要模型之一是 BART(雙向和自回歸變壓器),它經過訓練可以從大型文件產生連貫的摘要。
要開始使用 Hugging Face 模型,您需要安裝 Transformer 函式庫。您可以使用 pip 來執行此操作:
pip install transformers
安裝庫後,您可以輕鬆載入預先訓練的模型進行摘要。 Hugging Face 的管道 API 提供了使用 facebook/bart-large-cnn 等模型的高級接口,該模型已針對摘要任務進行了微調。
from transformers import pipeline # Load the summarization model summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
現在您已準備好摘要產生器,您可以輸入任何長文字來產生摘要。以下是使用有關英國著名女演員瑪吉·史密斯夫人的範例文章的範例。
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中文網其他相關文章!