client.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # Copyright (c) Opendatalab. All rights reserved.
  2. import os
  3. import sys
  4. import click
  5. from pathlib import Path
  6. from loguru import logger
  7. log_level = os.getenv("MINERU_LOG_LEVEL", "INFO").upper()
  8. logger.remove() # 移除默认handler
  9. logger.add(sys.stderr, level=log_level) # 添加新handler
  10. from mineru.utils.cli_parser import arg_parse
  11. from mineru.utils.config_reader import get_device
  12. from mineru.utils.guess_suffix_or_lang import guess_suffix_by_path
  13. from mineru.utils.model_utils import get_vram
  14. from ..version import __version__
  15. from .common import do_parse, read_fn, pdf_suffixes, image_suffixes
  16. @click.command(context_settings=dict(ignore_unknown_options=True, allow_extra_args=True))
  17. @click.pass_context
  18. @click.version_option(__version__,
  19. '--version',
  20. '-v',
  21. help='display the version and exit')
  22. @click.option(
  23. '-p',
  24. '--path',
  25. 'input_path',
  26. type=click.Path(exists=True),
  27. required=True,
  28. help='local filepath or directory. support pdf, png, jpg, jpeg files',
  29. )
  30. @click.option(
  31. '-o',
  32. '--output',
  33. 'output_dir',
  34. type=click.Path(),
  35. required=True,
  36. help='output local directory',
  37. )
  38. @click.option(
  39. '-m',
  40. '--method',
  41. 'method',
  42. type=click.Choice(['auto', 'txt', 'ocr']),
  43. help="""\b
  44. the method for parsing pdf:
  45. auto: Automatically determine the method based on the file type.
  46. txt: Use text extraction method.
  47. ocr: Use OCR method for image-based PDFs.
  48. Without method specified, 'auto' will be used by default.
  49. Adapted only for the case where the backend is set to 'pipeline' and 'hybrid-*'.""",
  50. default='auto',
  51. )
  52. @click.option(
  53. '-b',
  54. '--backend',
  55. 'backend',
  56. type=click.Choice(['pipeline', 'vlm-http-client', 'hybrid-http-client', 'vlm-auto-engine', 'hybrid-auto-engine',]),
  57. help="""\b
  58. the backend for parsing pdf:
  59. pipeline: More general.
  60. vlm-auto-engine: High accuracy via local computing power.
  61. vlm-http-client: High accuracy via remote computing power(client suitable for openai-compatible servers).
  62. hybrid-auto-engine: Next-generation high accuracy solution via local computing power.
  63. hybrid-http-client: High accuracy but requires a little local computing power(client suitable for openai-compatible servers).
  64. Without method specified, hybrid-auto-engine will be used by default.""",
  65. default='hybrid-auto-engine',
  66. )
  67. @click.option(
  68. '-l',
  69. '--lang',
  70. 'lang',
  71. type=click.Choice(['ch', 'ch_server', 'ch_lite', 'en', 'korean', 'japan', 'chinese_cht', 'ta', 'te', 'ka', 'th', 'el',
  72. 'latin', 'arabic', 'east_slavic', 'cyrillic', 'devanagari']),
  73. help="""
  74. Input the languages in the pdf (if known) to improve OCR accuracy.
  75. Without languages specified, 'ch' will be used by default.
  76. Adapted only for the case where the backend is set to 'pipeline' and 'hybrid-*'.
  77. """,
  78. default='ch',
  79. )
  80. @click.option(
  81. '-u',
  82. '--url',
  83. 'server_url',
  84. type=str,
  85. help="""
  86. When the backend is `<vlm/hybrid>-http-client`, you need to specify the server_url, for example:`http://127.0.0.1:30000`
  87. """,
  88. default=None,
  89. )
  90. @click.option(
  91. '-s',
  92. '--start',
  93. 'start_page_id',
  94. type=int,
  95. help='The starting page for PDF parsing, beginning from 0.',
  96. default=0,
  97. )
  98. @click.option(
  99. '-e',
  100. '--end',
  101. 'end_page_id',
  102. type=int,
  103. help='The ending page for PDF parsing, beginning from 0.',
  104. default=None,
  105. )
  106. @click.option(
  107. '-f',
  108. '--formula',
  109. 'formula_enable',
  110. type=bool,
  111. help='Enable formula parsing. Default is True. ',
  112. default=True,
  113. )
  114. @click.option(
  115. '-t',
  116. '--table',
  117. 'table_enable',
  118. type=bool,
  119. help='Enable table parsing. Default is True. ',
  120. default=True,
  121. )
  122. @click.option(
  123. '-d',
  124. '--device',
  125. 'device_mode',
  126. type=str,
  127. help="""Device mode for model inference, e.g., "cpu", "cuda", "cuda:0", "npu", "npu:0", "mps".
  128. Adapted only for the case where the backend is set to "pipeline". """,
  129. default=None,
  130. )
  131. @click.option(
  132. '--vram',
  133. 'virtual_vram',
  134. type=int,
  135. help='Upper limit of GPU memory occupied by a single process. Adapted only for the case where the backend is set to "pipeline". ',
  136. default=None,
  137. )
  138. @click.option(
  139. '--source',
  140. 'model_source',
  141. type=click.Choice(['huggingface', 'modelscope', 'local']),
  142. help="""
  143. The source of the model repository. Default is 'huggingface'.
  144. """,
  145. default='huggingface',
  146. )
  147. def main(
  148. ctx,
  149. input_path, output_dir, method, backend, lang, server_url,
  150. start_page_id, end_page_id, formula_enable, table_enable,
  151. device_mode, virtual_vram, model_source, **kwargs
  152. ):
  153. kwargs.update(arg_parse(ctx))
  154. if not backend.endswith('-client'):
  155. def get_device_mode() -> str:
  156. if device_mode is not None:
  157. return device_mode
  158. else:
  159. return get_device()
  160. if os.getenv('MINERU_DEVICE_MODE', None) is None:
  161. os.environ['MINERU_DEVICE_MODE'] = get_device_mode()
  162. def get_virtual_vram_size() -> int:
  163. if virtual_vram is not None:
  164. return virtual_vram
  165. else:
  166. return get_vram(get_device_mode())
  167. if os.getenv('MINERU_VIRTUAL_VRAM_SIZE', None) is None:
  168. os.environ['MINERU_VIRTUAL_VRAM_SIZE']= str(get_virtual_vram_size())
  169. if os.getenv('MINERU_MODEL_SOURCE', None) is None:
  170. os.environ['MINERU_MODEL_SOURCE'] = model_source
  171. os.makedirs(output_dir, exist_ok=True)
  172. def parse_doc(path_list: list[Path]):
  173. try:
  174. file_name_list = []
  175. pdf_bytes_list = []
  176. lang_list = []
  177. for path in path_list:
  178. file_name = str(Path(path).stem)
  179. pdf_bytes = read_fn(path)
  180. file_name_list.append(file_name)
  181. pdf_bytes_list.append(pdf_bytes)
  182. lang_list.append(lang)
  183. do_parse(
  184. output_dir=output_dir,
  185. pdf_file_names=file_name_list,
  186. pdf_bytes_list=pdf_bytes_list,
  187. p_lang_list=lang_list,
  188. backend=backend,
  189. parse_method=method,
  190. formula_enable=formula_enable,
  191. table_enable=table_enable,
  192. server_url=server_url,
  193. start_page_id=start_page_id,
  194. end_page_id=end_page_id,
  195. **kwargs,
  196. )
  197. except Exception as e:
  198. logger.exception(e)
  199. if os.path.isdir(input_path):
  200. doc_path_list = []
  201. for doc_path in Path(input_path).glob('*'):
  202. if guess_suffix_by_path(doc_path) in pdf_suffixes + image_suffixes:
  203. doc_path_list.append(doc_path)
  204. parse_doc(doc_path_list)
  205. else:
  206. parse_doc([Path(input_path)])
  207. if __name__ == '__main__':
  208. main()