test_cli_sdk.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. """test cli and sdk."""
  2. import logging
  3. import os
  4. import pytest
  5. from conf import conf
  6. from lib import common
  7. import time
  8. import magic_pdf.model as model_config
  9. import os
  10. from magic_pdf.data.data_reader_writer import FileBasedDataWriter
  11. from magic_pdf.data.data_reader_writer import S3DataReader, S3DataWriter
  12. from magic_pdf.config.make_content_config import DropMode, MakeMode
  13. from magic_pdf.data.data_reader_writer import FileBasedDataWriter, FileBasedDataReader
  14. from magic_pdf.data.dataset import PymuDocDataset
  15. from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
  16. from magic_pdf.config.enums import SupportedPdfParseMethod
  17. model_config.__use_inside_model__ = True
  18. pdf_res_path = conf.conf['pdf_res_path']
  19. code_path = conf.conf['code_path']
  20. pdf_dev_path = conf.conf['pdf_dev_path']
  21. magic_pdf_config = "/home/quyuan/magic-pdf.json"
  22. class TestCli:
  23. """test cli."""
  24. @pytest.fixture(autouse=True)
  25. def setup(self):
  26. """
  27. init
  28. """
  29. common.clear_gpu_memory()
  30. common.update_config_file(magic_pdf_config, "device-mode", "cuda")
  31. # 这里可以添加任何前置操作
  32. yield
  33. @pytest.mark.P0
  34. def test_pdf_auto_sdk(self):
  35. """pdf sdk auto test."""
  36. demo_names = list()
  37. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  38. for pdf_file in os.listdir(pdf_path):
  39. if pdf_file.endswith('.pdf'):
  40. demo_names.append(pdf_file.split('.')[0])
  41. for demo_name in demo_names:
  42. pdf_path = os.path.join(pdf_dev_path, 'pdf', f'{demo_name}.pdf')
  43. local_image_dir = os.path.join(pdf_dev_path, 'pdf', 'images')
  44. image_dir = str(os.path.basename(local_image_dir))
  45. dir_path = os.path.join(pdf_dev_path, 'mineru')
  46. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
  47. reader1 = FileBasedDataReader("")
  48. pdf_bytes = reader1.read(pdf_path)
  49. ds = PymuDocDataset(pdf_bytes)
  50. ## inference
  51. if ds.classify() == SupportedPdfParseMethod.OCR:
  52. infer_result = ds.apply(doc_analyze, ocr=True)
  53. ## pipeline
  54. pipe_result = infer_result.pipe_ocr_mode(image_writer)
  55. else:
  56. infer_result = ds.apply(doc_analyze, ocr=False)
  57. ## pipeline
  58. pipe_result = infer_result.pipe_txt_mode(image_writer)
  59. common.delete_file(dir_path)
  60. infer_result.draw_model(os.path.join(dir_path, f"{demo_name}_model.pdf"))
  61. pipe_result.draw_layout(os.path.join(dir_path, f"{demo_name}_layout.pdf"))
  62. pipe_result.draw_span(os.path.join(dir_path, f"{demo_name}_spans.pdf"))
  63. pipe_result.dump_md(md_writer, f"{demo_name}.md", image_dir)
  64. pipe_result.dump_content_list(md_writer, f"{demo_name}_content_list.json", image_dir)
  65. common.sdk_count_folders_and_check_contents(dir_path)
  66. @pytest.mark.P0
  67. def test_pdf_cli_auto(self):
  68. """magic_pdf cli test auto."""
  69. time.sleep(2)
  70. demo_names = []
  71. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  72. for pdf_file in os.listdir(pdf_path):
  73. if pdf_file.endswith('.pdf'):
  74. demo_names.append(pdf_file.split('.')[0])
  75. for demo_name in demo_names:
  76. res_path = os.path.join(pdf_dev_path, 'mineru')
  77. common.delete_file(res_path)
  78. cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
  79. pdf_path, f'{demo_name}.pdf'), res_path, 'auto')
  80. logging.info(cmd)
  81. os.system(cmd)
  82. common.cli_count_folders_and_check_contents(
  83. os.path.join(res_path, demo_name, 'auto'))
  84. @pytest.mark.P0
  85. def test_pdf_cli_txt(self):
  86. """magic_pdf cli test txt."""
  87. time.sleep(2)
  88. demo_names = []
  89. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  90. for pdf_file in os.listdir(pdf_path):
  91. if pdf_file.endswith('.pdf'):
  92. demo_names.append(pdf_file.split('.')[0])
  93. for demo_name in demo_names:
  94. res_path = os.path.join(pdf_dev_path, 'mineru')
  95. common.delete_file(res_path)
  96. cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
  97. pdf_path, f'{demo_name}.pdf'), res_path, 'txt')
  98. logging.info(cmd)
  99. os.system(cmd)
  100. common.cli_count_folders_and_check_contents(
  101. os.path.join(res_path, demo_name, 'txt'))
  102. @pytest.mark.P0
  103. def test_pdf_cli_ocr(self):
  104. """magic_pdf cli test ocr."""
  105. time.sleep(2)
  106. demo_names = []
  107. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  108. for pdf_file in os.listdir(pdf_path):
  109. if pdf_file.endswith('.pdf'):
  110. demo_names.append(pdf_file.split('.')[0])
  111. for demo_name in demo_names:
  112. res_path = os.path.join(pdf_dev_path, 'mineru')
  113. common.delete_file(res_path)
  114. cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
  115. pdf_path, f'{demo_name}.pdf'), res_path, 'ocr')
  116. logging.info(cmd)
  117. os.system(cmd)
  118. common.cli_count_folders_and_check_contents(
  119. os.path.join(res_path, demo_name, 'ocr'))
  120. @pytest.mark.skip(reason='out-of-date api')
  121. @pytest.mark.P1
  122. def test_pdf_dev_cli_local_jsonl_txt(self):
  123. """magic_pdf_dev cli local txt."""
  124. time.sleep(2)
  125. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  126. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, "txt")
  127. logging.info(cmd)
  128. os.system(cmd)
  129. @pytest.mark.skip(reason='out-of-date api')
  130. @pytest.mark.P1
  131. def test_pdf_dev_cli_local_jsonl_ocr(self):
  132. """magic_pdf_dev cli local ocr."""
  133. time.sleep(2)
  134. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  135. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'ocr')
  136. logging.info(cmd)
  137. os.system(cmd)
  138. @pytest.mark.skip(reason='out-of-date api')
  139. @pytest.mark.P1
  140. def test_pdf_dev_cli_local_jsonl_auto(self):
  141. """magic_pdf_dev cli local auto."""
  142. time.sleep(2)
  143. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  144. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'auto')
  145. logging.info(cmd)
  146. os.system(cmd)
  147. @pytest.mark.skip(reason='out-of-date api')
  148. @pytest.mark.P1
  149. def test_pdf_dev_cli_s3_jsonl_txt(self):
  150. """magic_pdf_dev cli s3 txt."""
  151. time.sleep(2)
  152. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  153. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, "txt")
  154. logging.info(cmd)
  155. os.system(cmd)
  156. @pytest.mark.skip(reason='out-of-date api')
  157. @pytest.mark.P1
  158. def test_pdf_dev_cli_s3_jsonl_ocr(self):
  159. """magic_pdf_dev cli s3 ocr."""
  160. time.sleep(2)
  161. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  162. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'ocr')
  163. logging.info(cmd)
  164. os.system(cmd)
  165. @pytest.mark.skip(reason='out-of-date api')
  166. @pytest.mark.P1
  167. def test_pdf_dev_cli_s3_jsonl_auto(self):
  168. """magic_pdf_dev cli s3 auto."""
  169. time.sleep(2)
  170. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  171. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'auto')
  172. logging.info(cmd)
  173. os.system(cmd)
  174. @pytest.mark.P1
  175. def test_pdf_dev_cli_pdf_json_auto(self):
  176. """magic_pdf_dev cli pdf+json auto."""
  177. time.sleep(2)
  178. json_path = os.path.join(pdf_dev_path, 'test_model.json')
  179. pdf_path = os.path.join(pdf_dev_path, 'pdf', 'test_rearch_report.pdf')
  180. cmd = 'magic-pdf-dev --pdf %s --json %s --method %s' % (pdf_path, json_path, 'auto')
  181. logging.info(cmd)
  182. os.system(cmd)
  183. @pytest.mark.skip(reason='out-of-date api')
  184. @pytest.mark.P1
  185. def test_pdf_dev_cli_pdf_json_ocr(self):
  186. """magic_pdf_dev cli pdf+json ocr."""
  187. time.sleep(2)
  188. json_path = os.path.join(pdf_dev_path, 'test_model.json')
  189. pdf_path = os.path.join(pdf_dev_path, 'pdf', 'test_rearch_report.pdf')
  190. cmd = 'magic-pdf-dev --pdf %s --json %s --method %s' % (pdf_path, json_path, 'auto')
  191. logging.info(cmd)
  192. os.system(cmd)
  193. @pytest.mark.P1
  194. def test_s3_sdk_auto(self):
  195. """
  196. test s3 sdk auto.
  197. """
  198. time.sleep(2)
  199. pdf_ak = os.getenv('pdf_ak')
  200. print (pdf_ak)
  201. pdf_sk = os.environ.get('pdf_sk', "")
  202. pdf_bucket = os.environ.get('bucket', "")
  203. pdf_endpoint = os.environ.get('pdf_endpoint', "")
  204. s3_pdf_path = conf.conf["s3_pdf_path"]
  205. image_dir = "s3://" + pdf_bucket + "/mineru/test/output"
  206. prefix = "mineru/test/output"
  207. reader = S3DataReader(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
  208. writer = S3DataWriter(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
  209. # = S3DataWriter(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
  210. image_writer = S3DataWriter(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
  211. local_dir = "output"
  212. name_without_suff = os.path.basename(s3_pdf_path).split(".")[0]
  213. # read bytes
  214. pdf_bytes = reader.read(s3_pdf_path) # read the pdf content
  215. # proc
  216. ## Create Dataset Instance
  217. ds = PymuDocDataset(pdf_bytes)
  218. ## inference
  219. if ds.classify() == SupportedPdfParseMethod.OCR:
  220. infer_result = ds.apply(doc_analyze, ocr=True)
  221. ## pipeline
  222. pipe_result = infer_result.pipe_ocr_mode(image_writer)
  223. else:
  224. infer_result = ds.apply(doc_analyze, ocr=False)
  225. ## pipeline
  226. pipe_result = infer_result.pipe_txt_mode(image_writer)
  227. ### draw model result on each page
  228. infer_result.draw_model(os.path.join(local_dir, f'{name_without_suff}_model.pdf')) # dump to local
  229. ### draw layout result on each page
  230. pipe_result.draw_layout(os.path.join(local_dir, f'{name_without_suff}_layout.pdf')) # dump to local
  231. ### draw spans result on each page
  232. pipe_result.draw_span(os.path.join(local_dir, f'{name_without_suff}_spans.pdf')) # dump to local
  233. ### dump markdown
  234. pipe_result.dump_md(writer, f'{name_without_suff}.md', "unittest/tmp/images") # dump to remote s3
  235. ### dump content list
  236. pipe_result.dump_content_list(writer, f"{name_without_suff}_content_list.json", image_dir)
  237. @pytest.mark.P1
  238. def test_local_magic_pdf_open_st_table(self):
  239. """magic pdf cli open st table."""
  240. time.sleep(2)
  241. #pre_cmd = "cp ~/magic_pdf_st.json ~/magic-pdf.json"
  242. value = {
  243. "model": "struct_eqtable",
  244. "enable": True,
  245. "max_time": 400
  246. }
  247. common.update_config_file(magic_pdf_config, "table-config", value)
  248. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  249. common.delete_file(pdf_res_path)
  250. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  251. os.system(cli_cmd)
  252. res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  253. assert res is True
  254. @pytest.mark.P1
  255. def test_local_magic_pdf_open_tablemaster_cuda(self):
  256. """magic pdf cli open table master html table cuda mode."""
  257. time.sleep(2)
  258. #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
  259. #os.system(pre_cmd)
  260. value = {
  261. "model": "tablemaster",
  262. "enable": True,
  263. "max_time": 400
  264. }
  265. common.update_config_file(magic_pdf_config, "table-config", value)
  266. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  267. common.delete_file(pdf_res_path)
  268. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  269. os.system(cli_cmd)
  270. res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  271. assert res is True
  272. @pytest.mark.P1
  273. def test_local_magic_pdf_open_rapidai_table(self):
  274. """magic pdf cli open rapid ai table."""
  275. time.sleep(2)
  276. #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
  277. #os.system(pre_cmd)
  278. value = {
  279. "model": "rapid_table",
  280. "enable": True,
  281. "max_time": 400
  282. }
  283. common.update_config_file(magic_pdf_config, "table-config", value)
  284. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  285. common.delete_file(pdf_res_path)
  286. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  287. os.system(cli_cmd)
  288. res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  289. assert res is True
  290. @pytest.mark.P1
  291. def test_local_magic_pdf_doclayout_yolo(self):
  292. """magic pdf cli open doclyaout yolo."""
  293. time.sleep(2)
  294. #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
  295. #os.system(pre_cmd)
  296. value = {
  297. "model": "doclayout_yolo"
  298. }
  299. common.update_config_file(magic_pdf_config, "layout-config", value)
  300. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  301. common.delete_file(pdf_res_path)
  302. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  303. os.system(cli_cmd)
  304. common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
  305. @pytest.mark.P1
  306. def test_local_magic_pdf_layoutlmv3_yolo(self):
  307. """magic pdf cli open layoutlmv3."""
  308. time.sleep(2)
  309. value = {
  310. "model": "layoutlmv3"
  311. }
  312. common.update_config_file(magic_pdf_config, "layout-config", value)
  313. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  314. common.delete_file(pdf_res_path)
  315. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  316. os.system(cli_cmd)
  317. common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
  318. #res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  319. @pytest.mark.P1
  320. def test_magic_pdf_cpu(self):
  321. """magic pdf cli cpu mode."""
  322. time.sleep(2)
  323. #pre_cmd = "cp ~/magic_pdf_html_table_cpu.json ~/magic-pdf.json"
  324. #os.system(pre_cmd)
  325. value = {
  326. "model": "tablemaster",
  327. "enable": False,
  328. "max_time": 400
  329. }
  330. common.update_config_file(magic_pdf_config, "table-config", value)
  331. common.update_config_file(magic_pdf_config, "device-mode", "cpu")
  332. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  333. common.delete_file(pdf_res_path)
  334. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  335. os.system(cli_cmd)
  336. common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
  337. @pytest.mark.P1
  338. def test_local_magic_pdf_close_html_table(self):
  339. """magic pdf cli close table."""
  340. time.sleep(2)
  341. #pre_cmd = "cp ~/magic_pdf_close_table.json ~/magic-pdf.json"
  342. #os.system(pre_cmd)
  343. value = {
  344. "model": "tablemaster",
  345. "enable": False,
  346. "max_time": 400
  347. }
  348. common.update_config_file(magic_pdf_config, "table-config", value)
  349. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  350. common.delete_file(pdf_res_path)
  351. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  352. os.system(cli_cmd)
  353. res = common.check_close_tables(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  354. assert res is True
  355. if __name__ == '__main__':
  356. pytest.main()