2022-02-04 15:45:09 +01:00
|
|
|
<a id="base"></a>
|
|
|
|
|
2021-04-22 16:45:29 +02:00
|
|
|
# Module base
|
|
|
|
|
2022-02-04 15:45:09 +01:00
|
|
|
<a id="base.BaseSummarizer"></a>
|
|
|
|
|
2021-11-11 12:44:29 +01:00
|
|
|
## BaseSummarizer
|
2021-04-22 16:45:29 +02:00
|
|
|
|
|
|
|
```python
|
|
|
|
class BaseSummarizer(BaseComponent)
|
|
|
|
```
|
|
|
|
|
|
|
|
Abstract class for Summarizer
|
|
|
|
|
2022-02-04 15:45:09 +01:00
|
|
|
<a id="base.BaseSummarizer.predict"></a>
|
|
|
|
|
2022-05-06 16:00:08 +02:00
|
|
|
#### BaseSummarizer.predict
|
2021-04-22 16:45:29 +02:00
|
|
|
|
|
|
|
```python
|
2022-02-04 15:45:09 +01:00
|
|
|
@abstractmethod
|
|
|
|
def predict(documents: List[Document], generate_single_summary: Optional[bool] = None) -> List[Document]
|
2021-04-22 16:45:29 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Abstract method for creating a summary.
|
|
|
|
|
|
|
|
**Arguments**:
|
|
|
|
|
|
|
|
- `documents`: Related documents (e.g. coming from a retriever) that the answer shall be conditioned on.
|
|
|
|
- `generate_single_summary`: Whether to generate a single summary for all documents or one summary per document.
|
2022-02-04 15:45:09 +01:00
|
|
|
If set to "True", all docs will be joined to a single string that will then
|
|
|
|
be summarized.
|
|
|
|
Important: The summary will depend on the order of the supplied documents!
|
2021-04-22 16:45:29 +02:00
|
|
|
|
|
|
|
**Returns**:
|
|
|
|
|
2022-08-15 12:35:41 +02:00
|
|
|
List of Documents, where Document.content contains the summarization and Document.meta["context"]
|
2022-02-04 15:45:09 +01:00
|
|
|
the original, not summarized text
|
|
|
|
|
|
|
|
<a id="transformers"></a>
|
2021-04-22 16:45:29 +02:00
|
|
|
|
|
|
|
# Module transformers
|
|
|
|
|
2022-02-04 15:45:09 +01:00
|
|
|
<a id="transformers.TransformersSummarizer"></a>
|
|
|
|
|
2021-11-11 12:44:29 +01:00
|
|
|
## TransformersSummarizer
|
2021-04-22 16:45:29 +02:00
|
|
|
|
|
|
|
```python
|
|
|
|
class TransformersSummarizer(BaseSummarizer)
|
|
|
|
```
|
|
|
|
|
|
|
|
Transformer based model to summarize the documents using the HuggingFace's transformers framework
|
|
|
|
|
|
|
|
You can use any model that has been fine-tuned on a summarization task. For example:
|
|
|
|
'`bart-large-cnn`', '`t5-small`', '`t5-base`', '`t5-large`', '`t5-3b`', '`t5-11b`'.
|
|
|
|
See the up-to-date list of available models on
|
|
|
|
`huggingface.co/models <https://huggingface.co/models?filter=summarization>`__
|
|
|
|
|
|
|
|
**Example**
|
|
|
|
|
|
|
|
```python
|
|
|
|
| docs = [Document(text="PG&E stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions."
|
|
|
|
| "The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were scheduled to be affected by"
|
|
|
|
| "the shutoffs which were expected to last through at least midday tomorrow.")]
|
|
|
|
|
|
|
|
|
| # Summarize
|
|
|
|
| summary = summarizer.predict(
|
|
|
|
| documents=docs,
|
|
|
|
| generate_single_summary=True
|
|
|
|
| )
|
|
|
|
|
|
|
|
|
| # Show results (List of Documents, containing summary and original text)
|
|
|
|
| print(summary)
|
|
|
|
|
|
|
|
|
| [
|
|
|
|
| {
|
|
|
|
| "text": "California's largest electricity provider has turned off power to hundreds of thousands of customers.",
|
|
|
|
| ...
|
|
|
|
| "meta": {
|
|
|
|
| "context": "PGE stated it scheduled the blackouts in response to forecasts for high winds amid dry conditions. ..."
|
|
|
|
| },
|
|
|
|
| ...
|
|
|
|
| },
|
|
|
|
```
|
|
|
|
|
2022-03-10 15:01:05 +01:00
|
|
|
<a id="transformers.TransformersSummarizer.__init__"></a>
|
|
|
|
|
2022-05-06 16:00:08 +02:00
|
|
|
#### TransformersSummarizer.\_\_init\_\_
|
2022-03-10 15:01:05 +01:00
|
|
|
|
|
|
|
```python
|
2022-08-25 10:30:03 +02:00
|
|
|
def __init__(model_name_or_path: str = "google/pegasus-xsum", model_version: Optional[str] = None, tokenizer: Optional[str] = None, max_length: int = 200, min_length: int = 5, use_gpu: bool = True, clean_up_tokenization_spaces: bool = True, separator_for_single_summary: str = " ", generate_single_summary: bool = False, batch_size: int = 16, progress_bar: bool = True, use_auth_token: Optional[Union[str, bool]] = None)
|
2022-03-10 15:01:05 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
Load a Summarization model from Transformers.
|
|
|
|
|
|
|
|
See the up-to-date list of available models at
|
|
|
|
https://huggingface.co/models?filter=summarization
|
|
|
|
|
|
|
|
**Arguments**:
|
|
|
|
|
|
|
|
- `model_name_or_path`: Directory of a saved model or the name of a public model e.g.
|
|
|
|
'facebook/rag-token-nq', 'facebook/rag-sequence-nq'.
|
|
|
|
See https://huggingface.co/models?filter=summarization for full list of available models.
|
|
|
|
- `model_version`: The version of model to use from the HuggingFace model hub. Can be tag name, branch name, or commit hash.
|
|
|
|
- `tokenizer`: Name of the tokenizer (usually the same as model)
|
|
|
|
- `max_length`: Maximum length of summarized text
|
|
|
|
- `min_length`: Minimum length of summarized text
|
|
|
|
- `use_gpu`: Whether to use GPU (if available).
|
|
|
|
- `clean_up_tokenization_spaces`: Whether or not to clean up the potential extra spaces in the text output
|
|
|
|
- `separator_for_single_summary`: If `generate_single_summary=True` in `predict()`, we need to join all docs
|
|
|
|
into a single text. This separator appears between those subsequent docs.
|
|
|
|
- `generate_single_summary`: Whether to generate a single summary for all documents or one summary per document.
|
|
|
|
If set to "True", all docs will be joined to a single string that will then
|
|
|
|
be summarized.
|
|
|
|
Important: The summary will depend on the order of the supplied documents!
|
2022-05-11 11:11:00 +02:00
|
|
|
- `batch_size`: Number of documents to process at a time.
|
2022-08-08 15:32:44 +02:00
|
|
|
- `progress_bar`: Whether to show a progress bar.
|
2022-08-25 10:30:03 +02:00
|
|
|
- `use_auth_token`: The API token used to download private models from Huggingface.
|
|
|
|
If this parameter is set to `True`, then the token generated when running
|
|
|
|
`transformers-cli login` (stored in ~/.huggingface) will be used.
|
|
|
|
Additional information can be found here
|
|
|
|
https://huggingface.co/transformers/main_classes/model.html#transformers.PreTrainedModel.from_pretrained
|
2022-03-10 15:01:05 +01:00
|
|
|
|
2022-02-04 15:45:09 +01:00
|
|
|
<a id="transformers.TransformersSummarizer.predict"></a>
|
2021-04-22 16:45:29 +02:00
|
|
|
|
2022-05-06 16:00:08 +02:00
|
|
|
#### TransformersSummarizer.predict
|
2021-04-22 16:45:29 +02:00
|
|
|
|
|
|
|
```python
|
2022-05-11 11:11:00 +02:00
|
|
|
def predict(documents: List[Document], generate_single_summary: Optional[bool] = None) -> List[Document]
|
2021-04-22 16:45:29 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Produce the summarization from the supplied documents.
|
2022-02-04 15:45:09 +01:00
|
|
|
|
2021-04-22 16:45:29 +02:00
|
|
|
These document can for example be retrieved via the Retriever.
|
|
|
|
|
|
|
|
**Arguments**:
|
|
|
|
|
|
|
|
- `documents`: Related documents (e.g. coming from a retriever) that the answer shall be conditioned on.
|
|
|
|
- `generate_single_summary`: Whether to generate a single summary for all documents or one summary per document.
|
2022-02-04 15:45:09 +01:00
|
|
|
If set to "True", all docs will be joined to a single string that will then
|
|
|
|
be summarized.
|
|
|
|
Important: The summary will depend on the order of the supplied documents!
|
2021-04-22 16:45:29 +02:00
|
|
|
|
|
|
|
**Returns**:
|
|
|
|
|
|
|
|
List of Documents, where Document.text contains the summarization and Document.meta["context"]
|
2022-02-04 15:45:09 +01:00
|
|
|
the original, not summarized text
|
2021-04-22 16:45:29 +02:00
|
|
|
|
2022-05-11 11:11:00 +02:00
|
|
|
<a id="transformers.TransformersSummarizer.predict_batch"></a>
|
|
|
|
|
|
|
|
#### TransformersSummarizer.predict\_batch
|
|
|
|
|
|
|
|
```python
|
|
|
|
def predict_batch(documents: Union[List[Document], List[List[Document]]], generate_single_summary: Optional[bool] = None, batch_size: Optional[int] = None) -> Union[List[Document], List[List[Document]]]
|
|
|
|
```
|
|
|
|
|
|
|
|
Produce the summarization from the supplied documents.
|
|
|
|
|
|
|
|
These documents can for example be retrieved via the Retriever.
|
|
|
|
|
|
|
|
**Arguments**:
|
|
|
|
|
|
|
|
- `documents`: Single list of related documents or list of lists of related documents
|
|
|
|
(e.g. coming from a retriever) that the answer shall be conditioned on.
|
|
|
|
- `generate_single_summary`: Whether to generate a single summary for each provided document list or
|
|
|
|
one summary per document.
|
|
|
|
If set to "True", all docs of a document list will be joined to a single string
|
|
|
|
that will then be summarized.
|
|
|
|
Important: The summary will depend on the order of the supplied documents!
|
|
|
|
- `batch_size`: Number of Documents to process at a time.
|
|
|
|
|