百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

bert-extractive-summarizer

> 编程语言
开源

使用 BERT 进行易于使用的提取式文本摘要

1.5K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

使用 BERT 进行易于使用的提取式文本摘要

Bert Extractive Summarizer

This repo is the generalization of the lecture-summarizer repo. This tool utilizes the HuggingFace Pytorch transformers library to run extractive summarizations. This works by first embedding the sentences, then running a clustering algorithm, finding the sentences that are closest to the cluster's centroids. This library also uses coreference techniques, utilizing the https://github.com/huggingface/neuralcoref library to resolve words in summaries that need more context. The greedyness of the neuralcoref library can be tweaked in the CoreferenceHandler class.

As of the most recent version of bert-extractive-summarizer, by default, CUDA is used if a gpu is available.

Paper: https://arxiv.org/abs/1906.04165

Try the Online Demo:

Distill Bert Summarization Demo

Table of Contents

  1. Install
  2. Examples
    1. Simple Example
    2. SBert
    3. Retrieve Embeddings
    4. Use Coreference
    5. Custom Model Example
    6. Large Example
  3. Calculating Elbow
  4. Running the Service

Install

pip install bert-extractive-summarizer

Examples

Simple Example

from summarizer import Summarizer

body = 'Text body that you want to summarize with BERT'
body2 = 'Something else you want to summarize with BERT'
model = Summarizer()
model(body)
model(body2)

Specifying number of sentences

Number of sentences can be supplied as a ratio or an integer. Examples are provided below.

from summarizer import Summarizer
body = 'Text body that you want to summarize with BERT'
model = Summarizer()
result = model(body, ratio=0.2)  # Specified with ratio
result = model(body, num_sentences=3)  # Will return 3 sentences 

Using multiple hidden layers as the embedding output

You can also concat the summarizer embeddings for clustering. A simple example is below.

from summarizer import Summarizer
body = 'Text body that you want to summarize with BERT'
model = Summarizer('distilbert-base-uncased', hidden=[-1,-2], hidden_concat=True)
result = model(body, num_sentences=3)

Use SBert

One can use Sentence Bert with bert-extractive-summarizer with the newest version. It is based off the paper here: https://arxiv.org/abs/1908.10084, and the library here: https://www.sbert.net/. To get started, first install SBERT:

pip install -U sentence-transformers

Then a simple example is the following:

from summarizer.sbert import SBertSummarizer

body = 'Text body that you want to summarize with BERT'
model = SBertSummarizer('paraphrase-MiniLM-L6-v2')
result = model(body, num_sentences=3)

It is worth noting that all the features that you can do with the main Summarizer class, you can also do with SBert.

Retrieve Embeddings

You can also retrieve the embeddings of the summarization. Examples are below:

from summarizer import Summarizer
body = 'Text body that you want to summarize with BERT'
model = Summarizer()
result = model.run_embeddings(body, ratio=0.2)  # Specified with ratio. 
result = model.run_embeddings(body, num_sentences=3)  # Will return (3, N) embedding numpy matrix.
result = model.run_embeddings(body, num_sentences=3, aggregate='mean')  # Will return Mean aggregate over embeddings. 

Use Coreference

First ensure you have installed neuralcoref and spacy. It is worth noting that neuralcoref does not work with spacy > 0.2.1.

pip install spacy
pip install transformers # > 4.0.0
pip install neuralcoref

python -m spacy download en_core_web_md

Then to to use coreference, run the following:

from summarizer import Summarizer
from summarizer.text_processors.coreference_handler import CoreferenceHandler

handler = CoreferenceHandler(greedyness=.4)
# How coreference works:
# >>>handler.process('''My sister has a dog. She loves him.''', min_length=2)
# ['My sister has a dog.', 'My sister loves a dog.']

body = 'Text body that you want to summarize with BERT'
body2 = 'Something else you want to summarize with BERT'
model = Summarizer(sentence_handler=handler)
model(body)
model(body2)

Custom Model Example

…

Large Example

…

Calculating Elbow

As of bert-extractive-summarizer version 0.7.1, you can also calculate ELBOW to determine the optimal cluster. Below shows a sample example in how to retrieve the list of inertias.

from summarizer import Summarizer

body = 'Your Text here.'
model = Summarizer()
res = model.calculate_elbow(body, k_max=10)
print(res)

You can also find the optimal number of sentences with elbow using the following algorithm.

from summarizer import Summarizer

body = 'Your Text here.'
model = Summarizer()
res = model.calculate_optimal_k(body, k_max=10)
print(res)

Summarizer Options

…

Running the Service

There is a provided flask service and corresponding Dockerfile. Running the service is simple, and can be done though the Makefile with the two commands:

make docker-service-build
make docker-service-run

This will use the Bert-base-uncased model, which has a small representation. The docker run also accepts a variety of arguments for custom and different models. This can be done through a command such as:

docker build -t summary-service -f Dockerfile.service ./
docker run --rm -it -p 5000:5000 summary-service:latest -model bert-large-uncased

Other arguments can also be passed to the server. Below includes the list of available arguments.

  • -greediness: Float parameter that determines how greedy nueralcoref should be
  • -reduce: Determines the reduction statistic of the encoding layer (mean, median, max).
  • -hidden: Determines the hidden layer to use for embeddings (default is -2)
  • -port: Determines the port to use.
  • -host: Determines the host to use.

Once the service is running, you can make a summarization command at the http://localhost:5000/summarize endpoint. This endpoint accepts a text/plain input which represents the text that you want to summarize. Parameters can also be passed as request arguments. The accepted arguments are:

  • ratio: Ratio of sentences to summarize to from the original body. (default to 0.2)
  • min_length: The minimum length to accept as a sentence. (default to 25)
  • max_length: The maximum length to accept as a sentence. (default to 500)

An example of a request is the following:

…

GitHub Issues· 53 开放

在 GitHub 查看全部
  • #150

    [News API] Summarization returns empty string

    更新于 2025年6月22日
  • #35

    How to modify the code for domain specific summarization?

    更新于 2024年4月9日
  • #151

    cannot import name summarizer

    更新于 2023年9月26日
  • #41

    cannot import name 'summarize'

    更新于 2023年9月5日
  • #48

    'Summarizer' object is not callable

    更新于 2023年7月4日
  • #152

    Need a way to force load on CPU when an unsupported GPU throws a pytorch error.

    更新于 2023年3月27日
  • #126

    Using T5

    更新于 2023年2月24日
  • #149

    Trying to mimic the API's result

    更新于 2023年2月1日
  • #148

    Run Summarizer model on array of strings

    更新于 2023年1月4日
  • #146

    TypeError: 'Summarizer' object is not callable

    更新于 2022年12月11日

核心特点

  • •-greediness: Float parameter that determines how greedy nueralcoref should be
  • •-reduce: Determines the reduction statistic of the encoding layer (mean, median, max).
  • •-hidden: Determines the hidden layer to use for embeddings (default is -2)
  • •-port: Determines the port to use.
  • •-host: Determines the host to use.
  • •ratio: Ratio of sentences to summarize to from the original body. (default to 0.2)
  • •min_length: The minimum length to accept as a sentence. (default to 25)
  • •max_length: The maximum length to accept as a sentence. (default to 500)

> 标签

Pythonbertcoreferenceextractive-summarizationpytorch

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言