-
-
Notifications
You must be signed in to change notification settings - Fork 137
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I am back. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"?