cli_dev.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. byte_end += byte_start - 1
  31. return s3_rw.read_jsonl(
  32. remove_non_official_s3_args(s3path),
  33. byte_start,
  34. byte_end,
  35. AbsReaderWriter.MODE_BIN,
  36. )
  37. @click.group()
  38. @click.version_option(__version__, "--version", "-v", help="显示版本信息")
  39. def cli():
  40. pass
  41. @cli.command()
  42. @click.option(
  43. "-j",
  44. "--jsonl",
  45. "jsonl",
  46. type=str,
  47. help="输入 jsonl 路径,本地或者 s3 上的文件",
  48. required=True,
  49. )
  50. @click.option(
  51. "-m",
  52. "--method",
  53. "method",
  54. type=parse_pdf_methods,
  55. help="指定解析方法。txt: 文本型 pdf 解析方法, ocr: 光学识别解析 pdf, auto: 程序智能选择解析方法",
  56. default="auto",
  57. )
  58. @click.option(
  59. "-o",
  60. "--output-dir",
  61. "output_dir",
  62. type=str,
  63. help="输出到本地目录",
  64. default="",
  65. )
  66. def jsonl(jsonl, method, output_dir):
  67. print("haha")
  68. model_config.__use_inside_model__ = False
  69. full_jsonl_path = os.path.realpath(jsonl)
  70. if output_dir == "":
  71. output_dir = os.path.join(os.path.dirname(full_jsonl_path), "output")
  72. if jsonl.startswith("s3://"):
  73. jso = json_parse.loads(read_s3_path(jsonl).decode("utf-8"))
  74. else:
  75. with open(jsonl) as f:
  76. jso = json_parse.loads(f.readline())
  77. s3_file_path = jso.get("file_location")
  78. if s3_file_path is None:
  79. s3_file_path = jso.get("path")
  80. pdf_file_name = Path(s3_file_path).stem
  81. pdf_data = read_s3_path(s3_file_path)
  82. print(pdf_file_name, jso, method)
  83. do_parse(
  84. output_dir,
  85. pdf_file_name,
  86. pdf_data,
  87. jso["doc_layout_result"],
  88. method,
  89. f_dump_content_list=True,
  90. )
  91. @cli.command()
  92. @click.option(
  93. "-p",
  94. "--pdf",
  95. "pdf",
  96. type=click.Path(exists=True),
  97. required=True,
  98. help="本地 PDF 文件",
  99. )
  100. @click.option(
  101. "-j",
  102. "--json",
  103. "json_data",
  104. type=click.Path(exists=True),
  105. required=True,
  106. help="本地模型推理出的 json 数据",
  107. )
  108. @click.option(
  109. "-o", "--output-dir", "output_dir", type=str, help="本地输出目录", default=""
  110. )
  111. @click.option(
  112. "-m",
  113. "--method",
  114. "method",
  115. type=parse_pdf_methods,
  116. help="指定解析方法。txt: 文本型 pdf 解析方法, ocr: 光学识别解析 pdf, auto: 程序智能选择解析方法",
  117. default="auto",
  118. )
  119. def pdf(pdf, json_data, output_dir, method):
  120. model_config.__use_inside_model__ = False
  121. full_pdf_path = os.path.realpath(pdf)
  122. if output_dir == "":
  123. output_dir = os.path.join(os.path.dirname(full_pdf_path), "output")
  124. def read_fn(path):
  125. disk_rw = DiskReaderWriter(os.path.dirname(path))
  126. return disk_rw.read(os.path.basename(path), AbsReaderWriter.MODE_BIN)
  127. model_json_list = json_parse.loads(read_fn(json_data).decode("utf-8"))
  128. file_name = str(Path(full_pdf_path).stem)
  129. pdf_data = read_fn(full_pdf_path)
  130. do_parse(
  131. output_dir,
  132. file_name,
  133. pdf_data,
  134. model_json_list,
  135. method,
  136. f_dump_content_list=True,
  137. )
  138. if __name__ == "__main__":
  139. cli()