pipeline.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 流水线管道
  2. ===========
  3. 极简示例
  4. ^^^^^^^^
  5. .. code:: python
  6. import os
  7. from magic_pdf.data.data_reader_writer import FileBasedDataWriter, FileBasedDataReader
  8. from magic_pdf.data.dataset import PymuDocDataset
  9. from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
  10. # args
  11. pdf_file_name = "abc.pdf" # replace with the real pdf path
  12. name_without_suff = pdf_file_name.split(".")[0]
  13. # prepare env
  14. local_image_dir, local_md_dir = "output/images", "output"
  15. image_dir = str(os.path.basename(local_image_dir))
  16. os.makedirs(local_image_dir, exist_ok=True)
  17. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(
  18. local_md_dir
  19. )
  20. image_dir = str(os.path.basename(local_image_dir))
  21. # read bytes
  22. reader1 = FileBasedDataReader("")
  23. pdf_bytes = reader1.read(pdf_file_name) # read the pdf content
  24. # proc
  25. ## Create Dataset Instance
  26. ds = PymuDocDataset(pdf_bytes)
  27. ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(md_writer, f"{name_without_suff}.md", image_dir)
  28. 运行以上的代码,会得到如下的结果
  29. .. code:: bash
  30. output/
  31. ├── abc.md
  32. └── images
  33. 除去初始化环境,如建立目录、导入依赖库等逻辑。真正执行将 `pdf` 转换为 `markdown` 的代码片段如下
  34. .. code::
  35. # read bytes
  36. reader1 = FileBasedDataReader("")
  37. pdf_bytes = reader1.read(pdf_file_name) # read the pdf content
  38. # proc
  39. ## Create Dataset Instance
  40. ds = PymuDocDataset(pdf_bytes)
  41. ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(md_writer, f"{name_without_suff}.md", image_dir)
  42. `ds.apply(doc_analyze, ocr=True)` 会生成 `InferenceResult` 对象。 `InferenceResult` 的对象执行 `pipe_ocr_mode` 方法会生成 `PipeResult` 对象。
  43. `PipeResult` 对象执行 `dump_md` 会在指定位置生成 `markdown` 文件。
  44. pipeline 的执行过程如下图所示
  45. .. image:: ../../_static/image/pipeline.drawio.svg
  46. .. admonition:: Tip
  47. :class: tip
  48. 要想获得更多有关 Dataset、InferenceResult、PipeResult 的使用示例子,请前往 :doc:`../quick_start/to_markdown`
  49. 要想获得更多有关 Dataset、InferenceResult、PipeResult 的细节信息请前往英文版 MinerU 文档进行查看!
  50. 管道组合
  51. ^^^^^^^^^
  52. .. code:: python
  53. class Dataset(ABC):
  54. @abstractmethod
  55. def apply(self, proc: Callable, *args, **kwargs):
  56. """Apply callable method which.
  57. Args:
  58. proc (Callable): invoke proc as follows:
  59. proc(self, *args, **kwargs)
  60. Returns:
  61. Any: return the result generated by proc
  62. """
  63. pass
  64. class InferenceResult(InferenceResultBase):
  65. def apply(self, proc: Callable, *args, **kwargs):
  66. """Apply callable method which.
  67. Args:
  68. proc (Callable): invoke proc as follows:
  69. proc(inference_result, *args, **kwargs)
  70. Returns:
  71. Any: return the result generated by proc
  72. """
  73. return proc(copy.deepcopy(self._infer_res), *args, **kwargs)
  74. def pipe_ocr_mode(
  75. self,
  76. imageWriter: DataWriter,
  77. start_page_id=0,
  78. end_page_id=None,
  79. debug_mode=False,
  80. lang=None,
  81. ) -> PipeResult:
  82. pass
  83. class PipeResult:
  84. def apply(self, proc: Callable, *args, **kwargs):
  85. """Apply callable method which.
  86. Args:
  87. proc (Callable): invoke proc as follows:
  88. proc(pipeline_result, *args, **kwargs)
  89. Returns:
  90. Any: return the result generated by proc
  91. """
  92. return proc(copy.deepcopy(self._pipe_res), *args, **kwargs)
  93. `Dataset` 、 `InferenceResult` 和 `PipeResult` 类均有 `apply` method。可用于组合不同阶段的运算过程。
  94. 如下所示,`MinerU` 提供一套组合这些类的计算过程。
  95. .. code:: python
  96. # proc
  97. ## Create Dataset Instance
  98. ds = PymuDocDataset(pdf_bytes)
  99. ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(md_writer, f"{name_without_suff}.md", image_dir)
  100. 用户可以根据的需求,自行实现一些组合用的函数。比如用户通过 `apply` 方法实现一个统计 `pdf` 文件页数的功能。
  101. .. code:: python
  102. from magic_pdf.data.data_reader_writer import FileBasedDataReader
  103. from magic_pdf.data.dataset import PymuDocDataset
  104. # args
  105. pdf_file_name = "abc.pdf" # replace with the real pdf path
  106. # read bytes
  107. reader1 = FileBasedDataReader("")
  108. pdf_bytes = reader1.read(pdf_file_name) # read the pdf content
  109. # proc
  110. ## Create Dataset Instance
  111. ds = PymuDocDataset(pdf_bytes)
  112. def count_page(ds)-> int:
  113. return len(ds)
  114. print("page number: ", ds.apply(count_page)) # will output the page count of `abc.pdf`