pipeline.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. .. raw:: html
  47. <br> </br>
  48. 目前划分出数据、推理、程序处理三个阶段,分别对应着图上的 ``Dataset``, ``InferenceResult``, ``PipeResult`` 这三个实体。通过 ``apply`` , ``doc_analyze`` 或 ``pipe_ocr_mode`` 等方法链接在一起。
  49. .. admonition:: Tip
  50. :class: tip
  51. 要想获得更多有关 Dataset、InferenceResult、PipeResult 的使用示例子,请前往 :doc:`../quick_start/to_markdown`
  52. 要想获得更多有关 Dataset、InferenceResult、PipeResult 的细节信息请前往英文版 MinerU 文档进行查看!
  53. 管道组合
  54. ^^^^^^^^^
  55. .. code:: python
  56. class Dataset(ABC):
  57. @abstractmethod
  58. def apply(self, proc: Callable, *args, **kwargs):
  59. """Apply callable method which.
  60. Args:
  61. proc (Callable): invoke proc as follows:
  62. proc(self, *args, **kwargs)
  63. Returns:
  64. Any: return the result generated by proc
  65. """
  66. pass
  67. class InferenceResult(InferenceResultBase):
  68. def apply(self, proc: Callable, *args, **kwargs):
  69. """Apply callable method which.
  70. Args:
  71. proc (Callable): invoke proc as follows:
  72. proc(inference_result, *args, **kwargs)
  73. Returns:
  74. Any: return the result generated by proc
  75. """
  76. return proc(copy.deepcopy(self._infer_res), *args, **kwargs)
  77. def pipe_ocr_mode(
  78. self,
  79. imageWriter: DataWriter,
  80. start_page_id=0,
  81. end_page_id=None,
  82. debug_mode=False,
  83. lang=None,
  84. ) -> PipeResult:
  85. pass
  86. class PipeResult:
  87. def apply(self, proc: Callable, *args, **kwargs):
  88. """Apply callable method which.
  89. Args:
  90. proc (Callable): invoke proc as follows:
  91. proc(pipeline_result, *args, **kwargs)
  92. Returns:
  93. Any: return the result generated by proc
  94. """
  95. return proc(copy.deepcopy(self._pipe_res), *args, **kwargs)
  96. ``Dataset`` 、 ``InferenceResult`` 和 ``PipeResult`` 类均有 ``apply`` method。可用于组合不同阶段的运算过程。
  97. 如下所示,``MinerU`` 提供一套组合这些类的计算过程。
  98. .. code:: python
  99. # proc
  100. ## Create Dataset Instance
  101. ds = PymuDocDataset(pdf_bytes)
  102. ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(md_writer, f"{name_without_suff}.md", image_dir)
  103. 用户可以根据的需求,自行实现一些组合用的函数。比如用户通过 ``apply`` 方法实现一个统计 ``pdf`` 文件页数的功能。
  104. .. code:: python
  105. from magic_pdf.data.data_reader_writer import FileBasedDataReader
  106. from magic_pdf.data.dataset import PymuDocDataset
  107. # args
  108. pdf_file_name = "abc.pdf" # replace with the real pdf path
  109. # read bytes
  110. reader1 = FileBasedDataReader("")
  111. pdf_bytes = reader1.read(pdf_file_name) # read the pdf content
  112. # proc
  113. ## Create Dataset Instance
  114. ds = PymuDocDataset(pdf_bytes)
  115. def count_page(ds)-> int:
  116. return len(ds)
  117. print("page number: ", ds.apply(count_page)) # will output the page count of `abc.pdf`