haystack/test/nodes/test_summarizer.py

86 lines
2.9 KiB
Python
Raw Normal View History

import pytest
import haystack
from haystack.utils.torch_utils import ListDataset
Refactoring of the `haystack` package (#1624) * Files moved, imports all broken * Fix most imports and docstrings into * Fix the paths to the modules in the API docs * Add latest docstring and tutorial changes * Add a few pipelines that were lost in the inports * Fix a bunch of mypy warnings * Add latest docstring and tutorial changes * Create a file_classifier module * Add docs for file_classifier * Fixed most circular imports, now the REST API can start * Add latest docstring and tutorial changes * Tackling more mypy issues * Reintroduce from FARM and fix last mypy issues hopefully * Re-enable old-style imports * Fix some more import from the top-level package in an attempt to sort out circular imports * Fix some imports in tests to new-style to prevent failed class equalities from breaking tests * Change document_store into document_stores * Update imports in tutorials * Add latest docstring and tutorial changes * Probably fixes summarizer tests * Improve the old-style import allowing module imports (should work) * Try to fix the docs * Remove dedicated KnowledgeGraph page from autodocs * Remove dedicated GraphRetriever page from autodocs * Fix generate_docstrings.sh with an updated list of yaml files to look for * Fix some more modules in the docs * Fix the document stores docs too * Fix a small issue on Tutorial14 * Add latest docstring and tutorial changes * Add deprecation warning to old-style imports * Remove stray folder and import Dict into dense.py * Change import path for MLFlowLogger * Add old loggers path to the import path aliases * Fix debug output of convert_ipynb.py * Fix circular import on BaseRetriever * Missed one merge block * re-run tutorial 5 * Fix imports in tutorial 5 * Re-enable squad_to_dpr CLI from the root package and move get_batches_from_generator into document_stores.base * Add latest docstring and tutorial changes * Fix typo in utils __init__ * Fix a few more imports * Fix benchmarks too * New-style imports in test_knowledge_graph * Rollback setup.py * Rollback squad_to_dpr too Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2021-10-25 15:50:23 +02:00
from haystack.schema import Document
from haystack.nodes import TransformersSummarizer
DOCS = [Document(content=doc) for doc in ["First test doc", "Second test doc"]]
EXPECTED_SUMMARIES = ["First summary", "Second summary"]
SUMMARIZED_DOCS = [
Document(content=doc.content, meta={"summary": summary}) for doc, summary in zip(DOCS, EXPECTED_SUMMARIES)
]
class MockHFPipeline:
def __init__(self, *a, **k):
pass
def __call__(self, docs, *a, **k):
summaries = [{"summary_text": summary} for summary in EXPECTED_SUMMARIES]
if isinstance(docs, ListDataset):
return [summaries for _ in docs]
return summaries
def tokenizer(self, *a, **k):
return {"input_ids": []}
@pytest.fixture
def mock_models(monkeypatch):
monkeypatch.setattr(haystack.nodes.summarizer.transformers, "pipeline", MockHFPipeline)
@pytest.fixture
def summarizer(mock_models) -> TransformersSummarizer:
return TransformersSummarizer(model_name_or_path="irrelevant/anyway", use_gpu=False)
@pytest.mark.unit
def test_summarization_no_docs(summarizer):
with pytest.raises(ValueError, match="at least one document"):
summarizer.predict(documents=[])
with pytest.raises(ValueError, match="at least one document"):
summarizer.predict_batch(documents=[])
@pytest.mark.unit
def test_summarization_no_docs(summarizer):
summarizer.min_length = 10
summarizer.max_length = 1
with pytest.raises(ValueError, match="min_length cannot be greater than max_length"):
summarizer.predict(documents=DOCS)
@pytest.mark.unit
def test_summarization_one_doc(summarizer):
summarized_docs = summarizer.predict(documents=[DOCS[0]])
assert len(summarized_docs) == 1
assert EXPECTED_SUMMARIES[0] == summarized_docs[0].meta["summary"]
@pytest.mark.unit
def test_summarization_more_docs(summarizer):
summarized_docs = summarizer.predict(documents=DOCS)
assert len(summarized_docs) == len(DOCS)
for expected_summary, summary in zip(EXPECTED_SUMMARIES, summarized_docs):
assert expected_summary == summary.meta["summary"]
@pytest.mark.unit
def test_summarization_batch_single_doc_list(summarizer):
summarized_docs = summarizer.predict_batch(documents=DOCS)
assert len(summarized_docs) == len(DOCS)
for expected_summary, summary in zip(EXPECTED_SUMMARIES, summarized_docs):
assert expected_summary == summary.meta["summary"]
@pytest.mark.unit
def test_summarization_batch_multiple_doc_lists(summarizer):
summarized_docs = summarizer.predict_batch(documents=[DOCS, DOCS])
assert len(summarized_docs) == 2 # Number of document lists
assert len(summarized_docs[0]) == len(DOCS)
for expected_summary, summary in zip(EXPECTED_SUMMARIES, summarized_docs[0]):
assert expected_summary == summary.meta["summary"]