Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(deepdoc): preserve textless PDF figures for VLM extraction (#19679) (#19688)

Ritik Yadavc6ed1d525bba
diff --git a/deepdoc/parser/pdf_parser.py b/deepdoc/parser/pdf_parser.py
index ae565352cd..03a7be5835 100644
--- a/deepdoc/parser/pdf_parser.py
+++ b/deepdoc/parser/pdf_parser.py
@@ -1456,9 +1456,9 @@ class RAGFlowPdfParser:
figure_positions = []
# crop figure out and add caption
for k, bxs in figures.items():
- txt = "\n".join([b["text"] for b in bxs])
- if not txt:
+ if not bxs:
continue
+ txt = "\n".join([b["text"] for b in bxs if b.get("text")])
poss = []
@@ -1466,13 +1466,13 @@ class RAGFlowPdfParser:
img = cropout(bxs, "figure", poss)
if img is None:
continue
- figure_results.append((img, [txt]))
+ figure_results.append((img, [txt] if txt else [""]))
figure_positions.append(poss)
else:
img = cropout(bxs, "figure", poss)
if img is None:
continue
- res.append((img, [txt]))
+ res.append((img, [txt] if txt else [""]))
positions.append(poss)
for k, bxs in tables.items():
diff --git a/test/unit_test/deepdoc/parser/test_figure_parser.py b/test/unit_test/deepdoc/parser/test_figure_parser.py
index b11dadf401..948511bb13 100644
--- a/test/unit_test/deepdoc/parser/test_figure_parser.py
+++ b/test/unit_test/deepdoc/parser/test_figure_parser.py
@@ -81,6 +81,7 @@ def _load_figure_parser(monkeypatch):
def timeout(*_args, **_kwargs):
return lambda function: function
+ _module(monkeypatch, "common.exceptions", TaskCanceledException=type("TaskCanceledException", (Exception,), {}))
_module(monkeypatch, "common.connection_utils", timeout=timeout)
_module(
monkeypatch,
@@ -297,3 +298,37 @@ def test_figure_wrappers_pass_dataset_language_to_model_and_parser(
)
assert module.VisionFigureParser.call_args.kwargs["lang"] == expected_language
parser_instance.assert_called_once()
+
+
+def test_vision_figure_parser_pdf_wrapper_handles_textless_figure_items(monkeypatch):
+ module, FakeImage = _load_figure_parser(monkeypatch)
+ model_config = {"llm_name": "vision-model"}
+ vision_model = object()
+ fake_img = FakeImage()
+ boosted_figures = [((fake_img, ["Enhanced VLM visual description"]), [(0, 10, 100, 20, 200)])]
+ parser_instance = Mock(return_value=boosted_figures)
+
+ module.get_tenant_default_model_by_type = Mock(return_value=model_config)
+ module.LLMBundle = Mock(return_value=vision_model)
+ module.VisionFigureParser = Mock(return_value=parser_instance)
+
+ # tbls containing a figure with empty initial OCR text (e.g. textless image/diagram)
+ tbls = [
+ (
+ (fake_img, [""]),
+ [(0, 10, 100, 20, 200)],
+ )
+ ]
+
+ result = module.vision_figure_parser_pdf_wrapper(
+ tbls=tbls,
+ sections=[],
+ callback=lambda *_args, **_kwargs: None,
+ tenant_id="tenant-id",
+ lang="English",
+ )
+
+ assert result == boosted_figures
+ module.VisionFigureParser.assert_called_once()
+ assert module.VisionFigureParser.call_args.kwargs["figures_data"] == [((fake_img, [""]), [(0, 10, 100, 20, 200)])]
diff --git a/test/unit_test/deepdoc/parser/test_pdf_parser_table_coordinates.py b/test/unit_test/deepdoc/parser/test_pdf_parser_table_coordinates.py
index 7b0edf1552..6d38f2b5e4 100644
--- a/test/unit_test/deepdoc/parser/test_pdf_parser_table_coordinates.py
+++ b/test/unit_test/deepdoc/parser/test_pdf_parser_table_coordinates.py
@@ -36,7 +36,7 @@ def _load_pdf_parser(monkeypatch):
AscendLayoutRecognizer=object,
LayoutRecognizer=object,
Recognizer=_FakeRecognizer,
- TableStructureRecognizer=object,
+ TableStructureRecognizer=SimpleNamespace(is_caption=lambda _box: False),
)
rag_mod = _stub_module(monkeypatch, "rag")
@@ -100,6 +100,13 @@ class _FakeRecognizer:
best_reverse = reverse
return best_i
+ @staticmethod
+ def find_overlapped(box, boxes, naive=False):
+ for i, candidate in enumerate(boxes):
+ if _FakeRecognizer.overlapped_area(box, candidate) > 0.1:
+ return i
+ return 0 if boxes else None
+
@staticmethod
def find_horizontally_tightest_fit(box, boxes):
min_distance = 1000000
@@ -345,3 +352,37 @@ def test_table_transformer_keeps_rotated_ocr_and_tsr_coordinates_aligned(monkeyp
assert [box["R_top"] for box in parser.boxes] == [210, 240]
assert [box["C"] for box in parser.boxes] == [0, 1]
assert [box["C_left"] for box in parser.boxes] == [105, 170]
+
+
+def test_extract_table_figure_preserves_textless_figures(monkeypatch):
+ module = _load_pdf_parser(monkeypatch)
+ parser = module.RAGFlowPdfParser.__new__(module.RAGFlowPdfParser)
+ parser.page_from = 0
+ parser.page_cum_height = [0]
+ parser.page_images = [_FakeImage()]
+ parser.page_layout = [[{"type": "figure", "x0": 50, "top": 50, "x1": 200, "bottom": 200}]]
+ parser.tbl_det = SimpleNamespace(construct_table=lambda *args, **kwargs: "")
+ parser.is_english = False
+
+ # Figure box with empty OCR text (e.g. textless diagram, chart, or photo)
+ parser.boxes = [
+ {
+ "text": "",
+ "layout_type": "figure",
+ "layoutno": "figure-0",
+ "page_number": 0,
+ "x0": 50,
+ "x1": 200,
+ "top": 50,
+ "bottom": 200,
+ }
+ ]
+
+ items = parser._extract_table_figure(need_image=True, ZM=1, return_html=False, need_position=True)
+ assert len(items) == 1
+ (img, descriptions), poss = items[0]
+ assert img is not None
+ assert isinstance(descriptions, list)
+ assert descriptions == [""]
+ assert poss == [(0, 50, 200, 50, 200)]