Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: update PDF path for analysis and enhance model loading with cached properties
myhloli86b6e18ab33e
diff --git a/mineru/backend/hybrid/analyze.py b/mineru/backend/hybrid/analyze.pyindex 76cbb583..045b8aa5 100644--- a/mineru/backend/hybrid/analyze.py+++ b/mineru/backend/hybrid/analyze.py@@ -2076,8 +2076,8 @@ def doc_analyze(if __name__ == "__main__":# pdf_path = "/Users/myhloli/pdf/截断合并/demo1-3.pdf"- # pdf_path = "/Users/myhloli/pdf/png/20250911-100102.png" # shubiao.png- pdf_path = "/Users/myhloli/pdf/纵向表格.pdf"+ # pdf_path = "/Users/myhloli/pdf/png/shubiao.png" # shubiao.png+ pdf_path = "/Users/myhloli/pdf/demo1.pdf"pdf_bytes = read_fn(pdf_path)middle_json, model_list = doc_analyze(pdf_bytes, effort="low", image_analysis=True)logger.info(f"middle_json: {middle_json}")diff --git a/mineru/backend/local_model_runtime.py b/mineru/backend/local_model_runtime.pyindex cd3fdf9f..804bd60f 100644--- a/mineru/backend/local_model_runtime.py+++ b/mineru/backend/local_model_runtime.py@@ -6,6 +6,7 @@ from __future__ import annotationsimport osimport threadingfrom collections.abc import Callable+from functools import cached_propertyfrom typing import Anyimport torch@@ -264,6 +265,7 @@ class HybridLocalModelContextSingleton:def get_model(self,) -> HybridLocalModelContext:+ """获取并复用唯一的 Hybrid 本地模型上下文。"""with self._lock:if self._model is None:self._model = HybridLocalModelContext()@@ -290,6 +292,7 @@ class HybridLocalModelContext:self,device: str | None = None,) -> None:+ """初始化 Hybrid 基础运行时,其他模型在首次访问对应属性时加载。"""if device is not None:self.device: str = deviceelse:@@ -317,23 +320,35 @@ class HybridLocalModelContext:# 初始化layout模型,用于提供行内公式检测框和Hybrid标题拆分self.layout_model = self.get_layout_model()- # 初始化公式解析模型- self.mfr_model = self.get_mfr_model()-- # 初始化无线表格识别模型- self.wireless_table_model = self.get_wireless_table_model()-- # 初始化有线表格识别模型- self.wired_table_model = self.get_wired_table_model()-- # 初始化表格类型分类模型- self.table_cls_model = self.get_table_cls_model()-- # 初始化表格方向分类模型- self.table_orientation_cls_model = self.get_table_orientation_cls_model()-- # 初始化印章识别模型- self.seal_model = self.get_seal_ocr_model()+ @cached_property+ def mfr_model(self) -> UnimernetModel | FormulaRecognizer:+ """首次访问时加载公式识别模型,并在当前 Context 内复用。"""+ return self.get_mfr_model()++ @cached_property+ def wireless_table_model(self) -> PaddleTableModel:+ """首次访问时加载无线表格识别模型,并在当前 Context 内复用。"""+ return self.get_wireless_table_model()++ @cached_property+ def wired_table_model(self) -> UnetTableModel:+ """首次访问时加载有线表格识别模型,并在当前 Context 内复用。"""+ return self.get_wired_table_model()++ @cached_property+ def table_cls_model(self) -> PaddleTableClsModel:+ """首次访问时加载表格类型分类模型,并在当前 Context 内复用。"""+ return self.get_table_cls_model()++ @cached_property+ def table_orientation_cls_model(self) -> MineruTableOrientationClsModel:+ """首次访问时加载基于 OCR 的表格方向包装器,并在当前 Context 内复用。"""+ return self.get_table_orientation_cls_model()++ @cached_property+ def seal_model(self) -> PytorchPaddleOCR:+ """首次访问时加载印章 OCR 模型,并在当前 Context 内复用。"""+ return self.get_seal_ocr_model()def get_ocr_model(self,