Skip to content

Commit 2e202b1

Browse files
feat: Extend MarkDownTextEvaluator to support external_predictions_path. Add unit test
Signed-off-by: Nikos Livathinos <nli@zurich.ibm.com>
1 parent 5f9a279 commit 2e202b1

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

docling_eval/evaluators/markdown_text_evaluator.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
UnitEvaluation,
2727
)
2828
from docling_eval.evaluators.stats import DatasetStatistics, compute_stats
29+
from docling_eval.utils.external_docling_doc_loader import ExternalDoclingDocLoader
2930

3031
_log = logging.getLogger(__name__)
3132

@@ -116,6 +117,11 @@ def __call__(
116117
ds_path: Path to load the parquet files of the dataset
117118
split: Split of the dataset to load
118119
"""
120+
if external_predictions_path is not None:
121+
external_docling_doc_loader = ExternalDoclingDocLoader(
122+
external_predictions_path
123+
)
124+
119125
parquet_files = str(ds_path / split / "*.parquet")
120126
ds = load_dataset("parquet", data_files={split: parquet_files})
121127
_log.info(f"Overview of the dataset: {ds}")
@@ -146,16 +152,28 @@ def __call__(
146152
):
147153
data_record = DatasetRecordWithPrediction.model_validate(data)
148154
doc_id = data_record.doc_id
149-
if data_record.status not in self._accepted_status:
150-
_log.error(
151-
"Skipping record without successfull conversion status: %s", doc_id
152-
)
153-
rejected_samples[EvaluationRejectionType.INVALID_CONVERSION_STATUS] += 1
154-
continue
155-
156155
true_doc = data_record.ground_truth_doc
157156
true_md = self._docling_document_to_md(true_doc)
158-
pred_md = self._get_pred_md(data_record)
157+
158+
# Get the predicted markdown from the external predictions path
159+
if external_predictions_path is not None:
160+
pred_doc = external_docling_doc_loader(doc_id)
161+
if pred_doc is None:
162+
_log.error("No external prediction found for doc_id=%s", doc_id)
163+
rejected_samples[EvaluationRejectionType.MISSING_PREDICTION] += 1
164+
continue
165+
pred_md = self._docling_document_to_md(pred_doc)
166+
else:
167+
if data_record.status not in self._accepted_status:
168+
_log.error(
169+
"Skipping record without successfull conversion status: %s",
170+
doc_id,
171+
)
172+
rejected_samples[
173+
EvaluationRejectionType.INVALID_CONVERSION_STATUS
174+
] += 1
175+
continue
176+
pred_md = self._get_pred_md(data_record) # type: ignore
159177

160178
if not pred_md:
161179
_log.error("There is no markdown prediction for doc_id=%s", doc_id)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from pathlib import Path
2+
from typing import Optional
3+
4+
from docling_core.types.doc.document import DoclingDocument
5+
6+
7+
class ExternalDoclingDocLoader:
8+
def __init__(self, external_predictions_dir: Path):
9+
self._external_predictions_dir = external_predictions_dir
10+
11+
def __call__(self, doc_id: str) -> Optional[DoclingDocument]:
12+
r"""
13+
Load the DoclingDocument from the external predictions path
14+
"""
15+
json_path = self._external_predictions_dir / f"{doc_id}.json"
16+
dt_path = self._external_predictions_dir / f"{doc_id}.dt"
17+
yaml_path = self._external_predictions_dir / f"{doc_id}.yaml"
18+
yml_path = self._external_predictions_dir / f"{doc_id}.yml"
19+
20+
if json_path.is_file():
21+
return DoclingDocument.load_from_json(json_path)
22+
if dt_path.is_file():
23+
return DoclingDocument.load_from_doctags(dt_path)
24+
if yaml_path.is_file():
25+
return DoclingDocument.load_from_yaml(yaml_path)
26+
if yml_path.is_file():
27+
return DoclingDocument.load_from_yaml(yml_path)
28+
return None

tests/test_markdown_text_evaluator.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,16 @@ def test_markdown_text_evaluator():
3434
assert is_exception
3535

3636

37-
# if __name__ == "__main__":
38-
# test_markdown_text_evaluator()
37+
def test_markdown_text_evaluator_external_predictions():
38+
r"""Testing the evaluator with external predictions"""
39+
eval4 = MarkdownTextEvaluator()
40+
gt_path = Path("scratch/DPBench/gt_dataset")
41+
preds_path = Path("scratch/DPBench/predicted_documents/json")
42+
43+
v4 = eval4(gt_path, external_predictions_path=preds_path)
44+
assert v4 is not None
45+
46+
47+
if __name__ == "__main__":
48+
# test_markdown_text_evaluator()
49+
test_markdown_text_evaluator_external_predictions()

0 commit comments

Comments
 (0)