-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Docling JSON ingestion (#783)
* feat: add Docling JSON ingestion Signed-off-by: Panos Vagenas <[email protected]> * update conversion as per review comments, add tests, revert Docling JSON disambiguation, document intricacies Signed-off-by: Panos Vagenas <[email protected]> * Update docling/backend/json/docling_json_backend.py Co-authored-by: Cesar Berrospi Ramis <[email protected]> Signed-off-by: Panos Vagenas <[email protected]> --------- Signed-off-by: Panos Vagenas <[email protected]> Co-authored-by: Cesar Berrospi Ramis <[email protected]>
- Loading branch information
Showing
8 changed files
with
144 additions
and
1 deletion.
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
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,58 @@ | ||
from io import BytesIO | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
from docling_core.types.doc import DoclingDocument | ||
from typing_extensions import override | ||
|
||
from docling.backend.abstract_backend import DeclarativeDocumentBackend | ||
from docling.datamodel.base_models import InputFormat | ||
from docling.datamodel.document import InputDocument | ||
|
||
|
||
class DoclingJSONBackend(DeclarativeDocumentBackend): | ||
@override | ||
def __init__( | ||
self, in_doc: InputDocument, path_or_stream: Union[BytesIO, Path] | ||
) -> None: | ||
super().__init__(in_doc, path_or_stream) | ||
|
||
# given we need to store any actual conversion exception for raising it from | ||
# convert(), this captures the successful result or the actual error in a | ||
# mutually exclusive way: | ||
self._doc_or_err = self._get_doc_or_err() | ||
|
||
@override | ||
def is_valid(self) -> bool: | ||
return isinstance(self._doc_or_err, DoclingDocument) | ||
|
||
@classmethod | ||
@override | ||
def supports_pagination(cls) -> bool: | ||
return False | ||
|
||
@classmethod | ||
@override | ||
def supported_formats(cls) -> set[InputFormat]: | ||
return {InputFormat.JSON_DOCLING} | ||
|
||
def _get_doc_or_err(self) -> Union[DoclingDocument, Exception]: | ||
try: | ||
json_data: Union[str, bytes] | ||
if isinstance(self.path_or_stream, Path): | ||
with open(self.path_or_stream, encoding="utf-8") as f: | ||
json_data = f.read() | ||
elif isinstance(self.path_or_stream, BytesIO): | ||
json_data = self.path_or_stream.getvalue() | ||
else: | ||
raise RuntimeError(f"Unexpected: {type(self.path_or_stream)=}") | ||
return DoclingDocument.model_validate_json(json_data=json_data) | ||
except Exception as e: | ||
return e | ||
|
||
@override | ||
def convert(self) -> DoclingDocument: | ||
if isinstance(self._doc_or_err, DoclingDocument): | ||
return self._doc_or_err | ||
else: | ||
raise self._doc_or_err |
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
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,58 @@ | ||
"""Test methods in module docling.backend.json.docling_json_backend.py.""" | ||
|
||
from io import BytesIO | ||
from pathlib import Path | ||
|
||
import pytest | ||
from pydantic import ValidationError | ||
|
||
from docling.backend.json.docling_json_backend import DoclingJSONBackend | ||
from docling.datamodel.base_models import InputFormat | ||
from docling.datamodel.document import DoclingDocument, InputDocument | ||
|
||
GT_PATH: Path = Path("./tests/data/groundtruth/docling_v2/2206.01062.json") | ||
|
||
|
||
def test_convert_valid_docling_json(): | ||
"""Test ingestion of valid Docling JSON.""" | ||
cls = DoclingJSONBackend | ||
path_or_stream = GT_PATH | ||
in_doc = InputDocument( | ||
path_or_stream=path_or_stream, | ||
format=InputFormat.JSON_DOCLING, | ||
backend=cls, | ||
) | ||
backend = cls( | ||
in_doc=in_doc, | ||
path_or_stream=path_or_stream, | ||
) | ||
assert backend.is_valid() | ||
|
||
act_doc = backend.convert() | ||
act_data = act_doc.export_to_dict() | ||
|
||
exp_doc = DoclingDocument.load_from_json(GT_PATH) | ||
exp_data = exp_doc.export_to_dict() | ||
|
||
assert act_data == exp_data | ||
|
||
|
||
def test_invalid_docling_json(): | ||
"""Test ingestion of invalid Docling JSON.""" | ||
cls = DoclingJSONBackend | ||
path_or_stream = BytesIO(b"{}") | ||
in_doc = InputDocument( | ||
path_or_stream=path_or_stream, | ||
format=InputFormat.JSON_DOCLING, | ||
backend=cls, | ||
filename="foo", | ||
) | ||
backend = cls( | ||
in_doc=in_doc, | ||
path_or_stream=path_or_stream, | ||
) | ||
|
||
assert not backend.is_valid() | ||
|
||
with pytest.raises(ValidationError): | ||
backend.convert() |
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