cli_dev.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. def read_s3_path(s3path):
  19. bucket, key = parse_s3path(s3path)
  20. s3_ak, s3_sk, s3_endpoint = get_s3_config(bucket)
  21. s3_rw = S3ReaderWriter(
  22. s3_ak, s3_sk, s3_endpoint, "auto", remove_non_official_s3_args(s3path)
  23. )
  24. may_range_params = parse_s3_range_params(s3path)
  25. if may_range_params is None or 2 != len(may_range_params):
  26. byte_start, byte_end = 0, None
  27. else:
  28. byte_start, byte_end = int(may_range_params[0]), int(may_range_params[1])
  29. byte_end += byte_start - 1
  30. return s3_rw.read_jsonl(
  31. remove_non_official_s3_args(s3path),
  32. byte_start,
  33. byte_end,
  34. AbsReaderWriter.MODE_BIN,
  35. )
  36. @click.group()
  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. print("haha")
  66. model_config.__use_inside_model__ = False
  67. full_jsonl_path = os.path.realpath(jsonl)
  68. if output_dir == "":
  69. output_dir = os.path.join(os.path.dirname(full_jsonl_path), "output")
  70. if jsonl.startswith("s3://"):
  71. jso = json_parse.loads(read_s3_path(jsonl).decode("utf-8"))
  72. else:
  73. with open(jsonl) as f:
  74. jso = json_parse.loads(f.readline())
  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. )
  89. @cli.command()
  90. @click.option(
  91. "-p",
  92. "--pdf",
  93. "pdf",
  94. type=click.Path(exists=True),
  95. required=True,
  96. help="本地 PDF 文件",
  97. )
  98. @click.option(
  99. "-j",
  100. "--json",
  101. "json_data",
  102. type=click.Path(exists=True),
  103. required=True,
  104. help="本地模型推理出的 json 数据",
  105. )
  106. @click.option(
  107. "-o", "--output-dir", "output_dir", type=str, help="本地输出目录", default=""
  108. )
  109. @click.option(
  110. "-m",
  111. "--method",
  112. "method",
  113. type=parse_pdf_methods,
  114. help="指定解析方法。txt: 文本型 pdf 解析方法, ocr: 光学识别解析 pdf, auto: 程序智能选择解析方法",
  115. default="auto",
  116. )
  117. def pdf(pdf, json_data, output_dir, method):
  118. model_config.__use_inside_model__ = False
  119. full_pdf_path = os.path.realpath(pdf)
  120. if output_dir == "":
  121. output_dir = os.path.join(os.path.dirname(full_pdf_path), "output")
  122. def read_fn(path):
  123. disk_rw = DiskReaderWriter(os.path.dirname(path))
  124. return disk_rw.read(os.path.basename(path), AbsReaderWriter.MODE_BIN)
  125. model_json_list = json_parse.loads(read_fn(json_data).decode("utf-8"))
  126. file_name = str(Path(full_pdf_path).stem)
  127. pdf_data = read_fn(full_pdf_path)
  128. do_parse(
  129. output_dir,
  130. file_name,
  131. pdf_data,
  132. model_json_list,
  133. method,
  134. f_dump_content_list=True,
  135. )
  136. if __name__ == "__main__":
  137. cli()