Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long text support #33

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,6 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
.vs/
jupyternotebook/output
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ summary = summarizer.summarize(transcript)
print(summary[0]["summary_text"])
```

If the transcript is longer than the specified maximum sequence length, the summarizer will split the transcript
into chunks and summarize each chunk individually. The chunks are then concatenated to form the final summary.

The improved summarization pipeline shows below how to use the summarizer with a long length text:

```python
from whisperplus.pipelines.long_text_support_summarization import (
LongTextSupportSummarizationPipeline,
)

summarizer = LongTextSupportSummarizationPipeline(model_id="facebook/bart-large-cnn")
summary_text = summarizer.summarize(transcript)
print(summary_text)
```

### Speaker Diarization

```python
Expand Down
1,279 changes: 1,279 additions & 0 deletions jupyternotebook/whisperplusnotebook.ipynb
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the folder name to "notebook"?

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions requirements.txt
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the test-related libraries to the dev-req.txt file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I am back.

Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ Requests==2.31.0
torch==2.1.0
torchaudio==2.1.0
transformers==4.35.2
pytest==7.4.0
pytest-cov==4.1.0
pytest-mock==3.10.0
Empty file added tests/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions tests/test_long_text_support_summarization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from unittest.mock import MagicMock, patch

import pytest

from whisperplus import LongTextSupportSummarizationPipeline


class TestLongTextSupportSummarizationPipeline:

@pytest.fixture
def summarizer(self):
return LongTextSupportSummarizationPipeline()

@pytest.fixture
def summarizer_mocker(self):
with patch(
'whisperplus.pipelines.long_text_support_summarization.LongTextSupportSummarizationPipeline'
'.load_model', new=lambda x: None):
summarizer = LongTextSupportSummarizationPipeline()
summarizer.load_model = MagicMock(side_effect=Exception("Model loading failed"))
return summarizer

def test_load_model(self, summarizer):
# Test whether the model is loaded successfully
assert summarizer.model is not None

def test_split_text_into_chunks(self, summarizer):
# Test chunking of splitting text
text = "This is a test text. " * 50
chunks = summarizer.split_text_into_chunks(text, 100)
assert type(chunks) is list
assert len(chunks) > 1 # 应该被分割成多个块

def test_summarize_long_text_chunking(self, summarizer):
# Test chunking of long text summaries
long_text = "This is a long text. " * 1000
summary = summarizer.summarize_long_text(long_text, 130, 30)
assert type(summary) is str
assert len(summary) > 0

def test_summarize_short_text(self, summarizer):
# Test summarization of short text
short_text = "This is a short text."
summary = summarizer.summarize(short_text)
assert type(summary) is list
assert len(summary) > 0

def test_summarize_long_text(self, summarizer):
# Test summarization of long text
long_text = "This is a long text. " * 1000 # 创建一个足够长的文本
summary = summarizer.summarize(long_text)
assert type(summary) is str
assert len(summary) > 0

def test_model_loading_exception(self, summarizer_mocker):
# Check if the model attribute is still None after an exception is raised
assert summarizer_mocker.model is None

with pytest.raises(Exception) as exc_info:
summarizer_mocker.load_model()
assert "Model loading failed" in str(exc_info.value)
3 changes: 2 additions & 1 deletion whisperplus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from whisperplus.pipelines.long_text_support_summarization import LongTextSupportSummarizationPipeline
from whisperplus.pipelines.summarization import TextSummarizationPipeline
from whisperplus.pipelines.whisper import SpeechToTextPipeline
from whisperplus.pipelines.whisper_diarize import ASRDiarizationPipeline
Expand All @@ -7,4 +8,4 @@
__version__ = '0.0.6'
__author__ = 'kadirnar'
__license__ = 'Apache License 2.0'
__all__ = ['']
__all__ = ['LongTextSupportSummarizationPipeline']
60 changes: 60 additions & 0 deletions whisperplus/pipelines/long_text_support_summarization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import logging

import torch
from transformers import AutoTokenizer, pipeline

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


class LongTextSupportSummarizationPipeline:

def __init__(self, model_id: str = "facebook/bart-large-cnn"):
logging.info("Initializing Text Summarization Pipeline")
self.model_id = model_id
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
self.model_max_length = self.tokenizer.model_max_length
self.model = None
self.load_model()

def load_model(self):
try:
logging.info("Loading model...")
self.model = pipeline(
"summarization", model=self.model_id, device=0 if self.device == "cuda" else -1)
logging.info("Model loaded successfully.")
except Exception as e:
logging.error(f"Error loading model: {e}")

def summarize(self, text: str, max_length: int = 130, min_length: int = 30):
# Check if text needs to be split into smaller chunks
tokens = self.tokenizer.encode(text, truncation=False, return_tensors='pt')
if tokens.size(1) > self.model_max_length:
# Split the text
return self.summarize_long_text(text, max_length, min_length)
else:
return self.model(text, max_length=max_length, min_length=min_length, do_sample=False)

def summarize_long_text(self, text, max_length, min_length):
# Split the text into chunks
chunk_size = self.model_max_length - 50 # To account for [CLS], [SEP], etc.
text_chunks = self.split_text_into_chunks(text, chunk_size)

summaries = []
for chunk in text_chunks:
summary = self.model(chunk, max_length=max_length, min_length=min_length, do_sample=False)
summaries.append(summary[0]['summary_text'])

return ' '.join(summaries)

def split_text_into_chunks(self, text, chunk_size):
tokens = self.tokenizer.encode(text)
chunk_start = 0
chunks = []
while chunk_start < len(tokens):
chunk_end = min(chunk_start + chunk_size, len(tokens))
chunk = self.tokenizer.decode(
tokens[chunk_start:chunk_end], skip_special_tokens=True, clean_up_tokenization_spaces=True)
chunks.append(chunk)
chunk_start += chunk_size
return chunks
1 change: 1 addition & 0 deletions whisperplus/pipelines/summarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TextSummarizationPipeline:

def __init__(self, model_id: str = "facebook/bart-large-cnn"):
logging.info("Initializing Text Summarization Pipeline")
self.device = None
self.model_id = model_id
self.model = None
self.set_device()
Expand Down
Loading