cli_dev.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import os
  2. import json as json_parse
  3. import click
  4. from pathlib import Path
  5. from magic_pdf.libs.path_utils import (
  6. parse_s3path,
  7. parse_s3_range_params,
  8. remove_non_official_s3_args,
  9. )
  10. from magic_pdf.libs.config_reader import (
  11. get_s3_config,
  12. )
  13. from magic_pdf.rw.S3ReaderWriter import S3ReaderWriter
  14. from magic_pdf.rw.DiskReaderWriter import DiskReaderWriter
  15. from magic_pdf.rw.AbsReaderWriter import AbsReaderWriter
  16. import magic_pdf.model as model_config
  17. from magic_pdf.tools.common import parse_pdf_methods, do_parse
  18. from magic_pdf.libs.version import __version__
  19. def read_s3_path(s3path):
  20. bucket, key = parse_s3path(s3path)
  21. s3_ak, s3_sk, s3_endpoint = get_s3_config(bucket)
  22. s3_rw = S3ReaderWriter(
  23. s3_ak, s3_sk, s3_endpoint, "auto", remove_non_official_s3_args(s3path)
  24. )
  25. may_range_params = parse_s3_range_params(s3path)
  26. if may_range_params is None or 2 != len(may_range_params):
  27. byte_start, byte_end = 0, None
  28. else:
  29. byte_start, byte_end = int(may_range_params[0]), int(may_range_params[1])
  30. return s3_rw.read_offset(
  31. remove_non_official_s3_args(s3path),
  32. byte_start,
  33. byte_end,
  34. )
  35. @click.group()
  36. @click.version_option(__version__, "--version", "-v", help="显示版本信息")
  37. def cli():
  38. pass
  39. @cli.command()
  40. @click.option(
  41. "-j",
  42. "--jsonl",
  43. "jsonl",
  44. type=str,
  45. help="输入 jsonl 路径,本地或者 s3 上的文件",
  46. required=True,
  47. )
  48. @click.option(
  49. "-m",
  50. "--method",
  51. "method",
  52. type=parse_pdf_methods,
  53. help="指定解析方法。txt: 文本型 pdf 解析方法, ocr: 光学识别解析 pdf, auto: 程序智能选择解析方法",
  54. default="auto",
  55. )
  56. @click.option(
  57. "-o",
  58. "--output-dir",
  59. "output_dir",
  60. type=str,
  61. help="输出到本地目录",
  62. default="",
  63. )
  64. def jsonl(jsonl, method, output_dir):
  65. model_config.__use_inside_model__ = False
  66. if jsonl.startswith("s3://"):
  67. jso = json_parse.loads(read_s3_path(jsonl).decode("utf-8"))
  68. full_jsonl_path = "."
  69. else:
  70. full_jsonl_path = os.path.realpath(jsonl)
  71. with open(jsonl) as f:
  72. jso = json_parse.loads(f.readline())
  73. if output_dir == "":
  74. output_dir = os.path.join(os.path.dirname(full_jsonl_path), "output")
  75. s3_file_path = jso.get("file_location")
  76. if s3_file_path is None:
  77. s3_file_path = jso.get("path")
  78. pdf_file_name = Path(s3_file_path).stem
  79. pdf_data = read_s3_path(s3_file_path)
  80. print(pdf_file_name, jso, method)
  81. do_parse(
  82. output_dir,
  83. pdf_file_name,
  84. pdf_data,
  85. jso["doc_layout_result"],
  86. method,
  87. f_dump_content_list=True,
  88. f_draw_model_bbox=True,
  89. )
  90. @cli.command()
  91. @click.option(
  92. "-p",
  93. "--pdf",
  94. "pdf",
  95. type=click.Path(exists=True),
  96. required=True,
  97. help="本地 PDF 文件",
  98. )
  99. @click.option(
  100. "-j",
  101. "--json",
  102. "json_data",
  103. type=click.Path(exists=True),
  104. required=True,
  105. help="本地模型推理出的 json 数据",
  106. )
  107. @click.option(
  108. "-o", "--output-dir", "output_dir", type=str, help="本地输出目录", default=""
  109. )
  110. @click.option(
  111. "-m",
  112. "--method",
  113. "method",
  114. type=parse_pdf_methods,
  115. help="指定解析方法。txt: 文本型 pdf 解析方法, ocr: 光学识别解析 pdf, auto: 程序智能选择解析方法",
  116. default="auto",
  117. )
  118. def pdf(pdf, json_data, output_dir, method):
  119. model_config.__use_inside_model__ = False
  120. full_pdf_path = os.path.realpath(pdf)
  121. if output_dir == "":
  122. output_dir = os.path.join(os.path.dirname(full_pdf_path), "output")
  123. def read_fn(path):
  124. disk_rw = DiskReaderWriter(os.path.dirname(path))
  125. return disk_rw.read(os.path.basename(path), AbsReaderWriter.MODE_BIN)
  126. model_json_list = json_parse.loads(read_fn(json_data).decode("utf-8"))
  127. file_name = str(Path(full_pdf_path).stem)
  128. pdf_data = read_fn(full_pdf_path)
  129. do_parse(
  130. output_dir,
  131. file_name,
  132. pdf_data,
  133. model_json_list,
  134. method,
  135. f_dump_content_list=True,
  136. f_draw_model_bbox=True,
  137. )
  138. if __name__ == "__main__":
  139. cli()