mirror of
https://github.com/docling-project/docling.git
synced 2025-06-27 05:20:05 +00:00

fix: Support for RTL programmatic documents fix(parser): detect and handle rotated pages fix(parser): fix bug causing duplicated text fix(formula): improve stopping criteria chore: update lock file fix: temporary constrain beautifulsoup * switch to code formula model v1.0.1 and new test pdf Signed-off-by: Matteo-Omenetti <Matteo.Omenetti1@ibm.com> * switch to code formula model v1.0.1 and new test pdf Signed-off-by: Matteo-Omenetti <Matteo.Omenetti1@ibm.com> * cleaned up the data folder in the tests Signed-off-by: Peter Staar <taa@zurich.ibm.com> * switch to code formula model v1.0.1 and new test pdf Signed-off-by: Matteo-Omenetti <Matteo.Omenetti1@ibm.com> * added three test-files for right-to-left Signed-off-by: Peter Staar <taa@zurich.ibm.com> * fix black Signed-off-by: Matteo-Omenetti <Matteo.Omenetti1@ibm.com> * added new gt for test_e2e_conversion Signed-off-by: Matteo-Omenetti <Matteo.Omenetti1@ibm.com> * added new gt for test_e2e_conversion Signed-off-by: Matteo-Omenetti <Matteo.Omenetti1@ibm.com> * Add code to expose text direction of cell Signed-off-by: Christoph Auer <cau@zurich.ibm.com> * new test file Signed-off-by: Matteo-Omenetti <Matteo.Omenetti1@ibm.com> * update lock Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * fix mypy reports Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * fix example filepaths Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * add test data results Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * pin wheel of latest docling-parse release Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * use latest docling-core Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * remove debugging code Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * fix path to files in example Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> * Revert unwanted RTL additions Signed-off-by: Christoph Auer <cau@zurich.ibm.com> * Fix test data paths in examples Signed-off-by: Christoph Auer <cau@zurich.ibm.com> --------- Signed-off-by: Matteo-Omenetti <Matteo.Omenetti1@ibm.com> Signed-off-by: Peter Staar <taa@zurich.ibm.com> Signed-off-by: Christoph Auer <cau@zurich.ibm.com> Signed-off-by: Michele Dolfi <dol@zurich.ibm.com> Co-authored-by: Matteo-Omenetti <Matteo.Omenetti1@ibm.com> Co-authored-by: Peter Staar <taa@zurich.ibm.com> Co-authored-by: Christoph Auer <cau@zurich.ibm.com>
146 lines
5.1 KiB
Python
146 lines
5.1 KiB
Python
import json
|
|
import logging
|
|
import time
|
|
from pathlib import Path
|
|
from typing import Iterable
|
|
|
|
import yaml
|
|
|
|
from docling.datamodel.base_models import ConversionStatus
|
|
from docling.datamodel.document import ConversionResult
|
|
from docling.datamodel.settings import settings
|
|
from docling.document_converter import DocumentConverter
|
|
|
|
_log = logging.getLogger(__name__)
|
|
|
|
USE_V2 = True
|
|
USE_LEGACY = True
|
|
|
|
|
|
def export_documents(
|
|
conv_results: Iterable[ConversionResult],
|
|
output_dir: Path,
|
|
):
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
success_count = 0
|
|
failure_count = 0
|
|
partial_success_count = 0
|
|
|
|
for conv_res in conv_results:
|
|
if conv_res.status == ConversionStatus.SUCCESS:
|
|
success_count += 1
|
|
doc_filename = conv_res.input.file.stem
|
|
|
|
if USE_V2:
|
|
# Export Docling document format to JSON:
|
|
with (output_dir / f"{doc_filename}.json").open("w") as fp:
|
|
fp.write(json.dumps(conv_res.document.export_to_dict()))
|
|
|
|
# Export Docling document format to YAML:
|
|
with (output_dir / f"{doc_filename}.yaml").open("w") as fp:
|
|
fp.write(yaml.safe_dump(conv_res.document.export_to_dict()))
|
|
|
|
# Export Docling document format to doctags:
|
|
with (output_dir / f"{doc_filename}.doctags.txt").open("w") as fp:
|
|
fp.write(conv_res.document.export_to_document_tokens())
|
|
|
|
# Export Docling document format to markdown:
|
|
with (output_dir / f"{doc_filename}.md").open("w") as fp:
|
|
fp.write(conv_res.document.export_to_markdown())
|
|
|
|
# Export Docling document format to text:
|
|
with (output_dir / f"{doc_filename}.txt").open("w") as fp:
|
|
fp.write(conv_res.document.export_to_markdown(strict_text=True))
|
|
|
|
if USE_LEGACY:
|
|
# Export Deep Search document JSON format:
|
|
with (output_dir / f"{doc_filename}.legacy.json").open(
|
|
"w", encoding="utf-8"
|
|
) as fp:
|
|
fp.write(json.dumps(conv_res.legacy_document.export_to_dict()))
|
|
|
|
# Export Text format:
|
|
with (output_dir / f"{doc_filename}.legacy.txt").open(
|
|
"w", encoding="utf-8"
|
|
) as fp:
|
|
fp.write(
|
|
conv_res.legacy_document.export_to_markdown(strict_text=True)
|
|
)
|
|
|
|
# Export Markdown format:
|
|
with (output_dir / f"{doc_filename}.legacy.md").open(
|
|
"w", encoding="utf-8"
|
|
) as fp:
|
|
fp.write(conv_res.legacy_document.export_to_markdown())
|
|
|
|
# Export Document Tags format:
|
|
with (output_dir / f"{doc_filename}.legacy.doctags.txt").open(
|
|
"w", encoding="utf-8"
|
|
) as fp:
|
|
fp.write(conv_res.legacy_document.export_to_document_tokens())
|
|
|
|
elif conv_res.status == ConversionStatus.PARTIAL_SUCCESS:
|
|
_log.info(
|
|
f"Document {conv_res.input.file} was partially converted with the following errors:"
|
|
)
|
|
for item in conv_res.errors:
|
|
_log.info(f"\t{item.error_message}")
|
|
partial_success_count += 1
|
|
else:
|
|
_log.info(f"Document {conv_res.input.file} failed to convert.")
|
|
failure_count += 1
|
|
|
|
_log.info(
|
|
f"Processed {success_count + partial_success_count + failure_count} docs, "
|
|
f"of which {failure_count} failed "
|
|
f"and {partial_success_count} were partially converted."
|
|
)
|
|
return success_count, partial_success_count, failure_count
|
|
|
|
|
|
def main():
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
input_doc_paths = [
|
|
Path("./tests/data/pdf/2206.01062.pdf"),
|
|
Path("./tests/data/pdf/2203.01017v2.pdf"),
|
|
Path("./tests/data/pdf/2305.03393v1.pdf"),
|
|
Path("./tests/data/pdf/redp5110_sampled.pdf"),
|
|
]
|
|
|
|
# buf = BytesIO(Path("./test/data/2206.01062.pdf").open("rb").read())
|
|
# docs = [DocumentStream(name="my_doc.pdf", stream=buf)]
|
|
# input = DocumentConversionInput.from_streams(docs)
|
|
|
|
# # Turn on inline debug visualizations:
|
|
# settings.debug.visualize_layout = True
|
|
# settings.debug.visualize_ocr = True
|
|
# settings.debug.visualize_tables = True
|
|
# settings.debug.visualize_cells = True
|
|
|
|
doc_converter = DocumentConverter()
|
|
|
|
start_time = time.time()
|
|
|
|
conv_results = doc_converter.convert_all(
|
|
input_doc_paths,
|
|
raises_on_error=False, # to let conversion run through all and examine results at the end
|
|
)
|
|
success_count, partial_success_count, failure_count = export_documents(
|
|
conv_results, output_dir=Path("scratch")
|
|
)
|
|
|
|
end_time = time.time() - start_time
|
|
|
|
_log.info(f"Document conversion complete in {end_time:.2f} seconds.")
|
|
|
|
if failure_count > 0:
|
|
raise RuntimeError(
|
|
f"The example failed converting {failure_count} on {len(input_doc_paths)}."
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|