mirror of
https://github.com/docling-project/docling.git
synced 2026-05-17 13:10:38 +00:00
24f2d148d9
* feat(table-structure): swap VLM model to granite-vision-4.1-4b Updates GraniteVisionTableStructureModel to use the 4.1 model. The 4.1 weights are pre-merged, so merge_lora_adapters() is now hasattr-guarded. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Eli Schwartz <eliyahu.schwartz@ibm.com> * feat(chart-extraction): swap V4 VLM model to granite-vision-4.1-4b Updates ChartExtractionModelGraniteVisionV4 to use the 4.1 model. hasattr-guards the merge_lora_adapters() call since 4.1 weights are pre-merged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Eli Schwartz <eliyahu.schwartz@ibm.com> * docs(example): mention granite-vision-4.1-4b in table-structure example Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Eli Schwartz <eliyahu.schwartz@ibm.com> * docs(catalog): update Granite Vision entry to 4.1-4b Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Eli Schwartz <eliyahu.schwartz@ibm.com> * feat(chart-extraction): honor cuda_use_flash_attention2 in V4 loader Mirrors the table-structure loader so ChartExtractionModelGraniteVisionV4 also passes _attn_implementation based on AcceleratorOptions. Without this the chart model falls back to the transformers SDPA default, which can hit cuDNN backend failures on some torch/cuDNN stacks while the table model (which already passed the flag) runs cleanly. Stores accelerator_options on the base class so subclasses can read it. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Eli Schwartz <eliyahu.schwartz@ibm.com> * fix(model-downloader): update Granite Vision log message to 4.1 The log message in download_models still mentioned "Granite Vision 4.0" after the model swap. Correct it to match the current model version. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Eli Schwartz <eliyahu.schwartz@ibm.com> * fix(chart-extraction): fall back to bare CSV when V4 model omits ```csv``` fence granite-vision-4.1-4b sometimes emits raw CSV without a ```csv``` code fence for the <chart2csv> prompt, which caused _extract_csv_to_dataframe to raise ValueError and drop the chart's tabular_chart metadata. Mirror the tolerant parsing already used by the v3 class: prefer a fenced block, otherwise strip any stray backtick prefix/suffix and parse the text as-is. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Eli Schwartz <eliyahu.schwartz@ibm.com> --------- Signed-off-by: Eli Schwartz <eliyahu.schwartz@ibm.com> Co-authored-by: Eli Schwartz <eliyahu.schwartz@ibm.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
80 lines
2.5 KiB
Python
Vendored
80 lines
2.5 KiB
Python
Vendored
# %% [markdown]
|
|
# Extract tables from a PDF using Granite Vision for table structure recognition.
|
|
#
|
|
# What this example does
|
|
# - Converts a PDF using the Granite Vision VLM for table structure extraction
|
|
# instead of the default TableFormer model.
|
|
# - Prints each detected table as Markdown to stdout.
|
|
#
|
|
# Prerequisites
|
|
# - Install Docling with VLM support: `pip install docling[vlm]`
|
|
# - A CUDA GPU is recommended; CPU works but is significantly slower.
|
|
#
|
|
# How to run
|
|
# - From the repo root: `python docs/examples/granite_vision_table_structure.py`
|
|
#
|
|
# Input document
|
|
# - Defaults to `tests/data/pdf/2206.01062.pdf`. Change `input_doc_path` as needed.
|
|
#
|
|
# Notes
|
|
# - The Granite Vision model (`ibm-granite/granite-vision-4.1-4b`) is downloaded
|
|
# automatically from HuggingFace on first run.
|
|
# - The model outputs table structure in OTSL (Open Table Structure Language) format,
|
|
# which Docling parses into structured table cells.
|
|
|
|
# %%
|
|
|
|
import logging
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from docling.datamodel.accelerator_options import AcceleratorDevice, AcceleratorOptions
|
|
from docling.datamodel.base_models import InputFormat
|
|
from docling.datamodel.pipeline_options import (
|
|
GraniteVisionTableStructureOptions,
|
|
PdfPipelineOptions,
|
|
)
|
|
from docling.document_converter import DocumentConverter, PdfFormatOption
|
|
|
|
_log = logging.getLogger(__name__)
|
|
|
|
|
|
def main():
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
data_folder = Path(__file__).parent / "../../tests/data"
|
|
input_doc_path = data_folder / "pdf/2206.01062.pdf"
|
|
|
|
# Configure pipeline to use Granite Vision for table structure
|
|
pipeline_options = PdfPipelineOptions()
|
|
pipeline_options.do_table_structure = True
|
|
pipeline_options.table_structure_options = GraniteVisionTableStructureOptions()
|
|
pipeline_options.accelerator_options = AcceleratorOptions(
|
|
device=AcceleratorDevice.AUTO,
|
|
)
|
|
|
|
doc_converter = DocumentConverter(
|
|
format_options={
|
|
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options),
|
|
}
|
|
)
|
|
|
|
start_time = time.time()
|
|
conv_res = doc_converter.convert(input_doc_path)
|
|
elapsed = time.time() - start_time
|
|
|
|
for table_ix, table in enumerate(conv_res.document.tables):
|
|
table_df = table.export_to_dataframe(doc=conv_res.document)
|
|
print(f"## Table {table_ix}")
|
|
print(table_df.to_markdown())
|
|
print()
|
|
|
|
_log.info(
|
|
f"Document converted in {elapsed:.2f} seconds "
|
|
f"({len(conv_res.document.tables)} tables found)."
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|