test_cli_sdk.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. from magic_pdf.data.read_api import read_local_images
  10. from magic_pdf.data.read_api import read_local_office
  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. pdf_res_path = conf.conf['pdf_res_path']
  18. code_path = conf.conf['code_path']
  19. pdf_dev_path = conf.conf['pdf_dev_path']
  20. magic_pdf_config = "/home/quyuan/magic-pdf.json"
  21. class TestCli:
  22. """test cli."""
  23. @pytest.fixture(autouse=True)
  24. def setup(self):
  25. """
  26. init
  27. """
  28. common.clear_gpu_memory()
  29. common.update_config_file(magic_pdf_config, "device-mode", "cuda")
  30. # 这里可以添加任何前置操作
  31. yield
  32. @pytest.mark.P0
  33. def test_pdf_local_sdk(self):
  34. """pdf sdk auto test."""
  35. demo_names = list()
  36. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  37. for pdf_file in os.listdir(pdf_path):
  38. if pdf_file.endswith('.pdf'):
  39. demo_names.append(pdf_file.split('.')[0])
  40. for demo_name in demo_names:
  41. pdf_path = os.path.join(pdf_dev_path, 'pdf', f'{demo_name}.pdf')
  42. local_image_dir = os.path.join(pdf_dev_path, 'pdf', 'images')
  43. image_dir = str(os.path.basename(local_image_dir))
  44. name_without_suff = os.path.basename(pdf_path).split(".pdf")[0]
  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. ### draw model result on each page
  61. infer_result.draw_model(os.path.join(dir_path, f"{name_without_suff}_model.pdf"))
  62. ### get model inference result
  63. model_inference_result = infer_result.get_infer_res()
  64. ### draw layout result on each page
  65. pipe_result.draw_layout(os.path.join(dir_path, f"{name_without_suff}_layout.pdf"))
  66. ### draw spans result on each page
  67. pipe_result.draw_span(os.path.join(dir_path, f"{name_without_suff}_spans.pdf"))
  68. ### dump markdown
  69. md_content = pipe_result.get_markdown(image_dir)
  70. pipe_result.dump_md(md_writer, f"{name_without_suff}.md", image_dir)
  71. ### get content list content
  72. content_list_content = pipe_result.get_content_list(image_dir)
  73. pipe_result.dump_content_list(md_writer, f"{name_without_suff}_content_list.json", image_dir)
  74. ### get middle json
  75. middle_json_content = pipe_result.get_middle_json()
  76. ### dump middle json
  77. pipe_result.dump_middle_json(md_writer, f'{name_without_suff}_middle.json')
  78. common.sdk_count_folders_and_check_contents(dir_path)
  79. @pytest.mark.P0
  80. def test_pdf_s3_sdk(self):
  81. """pdf s3 sdk test."""
  82. demo_names = list()
  83. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  84. for pdf_file in os.listdir(pdf_path):
  85. if pdf_file.endswith('.pdf'):
  86. demo_names.append(pdf_file.split('.')[0])
  87. for demo_name in demo_names:
  88. pdf_path = os.path.join(pdf_dev_path, 'pdf', f'{demo_name}.pdf')
  89. local_image_dir = os.path.join(pdf_dev_path, 'pdf', 'images')
  90. image_dir = str(os.path.basename(local_image_dir))
  91. name_without_suff = os.path.basename(pdf_path).split(".pdf")[0]
  92. dir_path = os.path.join(pdf_dev_path, 'mineru')
  93. pass
  94. @pytest.mark.P0
  95. def test_pdf_local_ppt(self):
  96. """pdf sdk auto test."""
  97. demo_names = list()
  98. pdf_path = os.path.join(pdf_dev_path, 'ppt')
  99. for pdf_file in os.listdir(pdf_path):
  100. if pdf_file.endswith('.pptx'):
  101. demo_names.append(pdf_file.split('.')[0])
  102. for demo_name in demo_names:
  103. pdf_path = os.path.join(pdf_dev_path, 'ppt', f'{demo_name}.pptx')
  104. local_image_dir = os.path.join(pdf_dev_path, 'mineru', 'images')
  105. image_dir = str(os.path.basename(local_image_dir))
  106. name_without_suff = os.path.basename(pdf_path).split(".pptx")[0]
  107. dir_path = os.path.join(pdf_dev_path, 'mineru')
  108. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
  109. ds = read_local_office(pdf_path)[0]
  110. common.delete_file(dir_path)
  111. ds.apply(doc_analyze, ocr=True).pipe_txt_mode(image_writer).dump_md(md_writer, f"{name_without_suff}.md", image_dir)
  112. common.sdk_count_folders_and_check_contents(dir_path)
  113. @pytest.mark.P0
  114. def test_pdf_local_image(self):
  115. """pdf sdk auto test."""
  116. demo_names = list()
  117. pdf_path = os.path.join(pdf_dev_path, 'images')
  118. for pdf_file in os.listdir(pdf_path):
  119. if pdf_file.endswith('.jpg'):
  120. demo_names.append(pdf_file.split('.')[0])
  121. for demo_name in demo_names:
  122. pdf_path = os.path.join(pdf_dev_path, 'images', f'{demo_name}.jpg')
  123. local_image_dir = os.path.join(pdf_dev_path, 'mineru', 'images')
  124. image_dir = str(os.path.basename(local_image_dir))
  125. name_without_suff = os.path.basename(pdf_path).split(".jpg")[0]
  126. dir_path = os.path.join(pdf_dev_path, 'mineru')
  127. common.delete_file(dir_path)
  128. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
  129. ds = read_local_images(pdf_path)[0]
  130. ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(
  131. md_writer, f"{name_without_suff}.md", image_dir)
  132. common.sdk_count_folders_and_check_contents(dir_path)
  133. @pytest.mark.P0
  134. def test_local_image_dir(self):
  135. """local image dir."""
  136. demo_names = list()
  137. pdf_path = os.path.join(pdf_dev_path, 'images')
  138. dir_path = os.path.join(pdf_dev_path, 'mineru')
  139. local_image_dir = os.path.join(pdf_dev_path, 'mineru', 'images')
  140. image_dir = str(os.path.basename(local_image_dir))
  141. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
  142. common.delete_file(dir_path)
  143. dss = read_local_images(pdf_path, suffixes=['.png', '.jpg'])
  144. count = 0
  145. for ds in dss:
  146. ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(md_writer, f"{count}.md", image_dir)
  147. count += 1
  148. common.sdk_count_folders_and_check_contents(dir_path)
  149. def test_local_doc_parse(self):
  150. """
  151. doc 解析
  152. """
  153. demo_names = list()
  154. pdf_path = os.path.join(pdf_dev_path, 'doc')
  155. for pdf_file in os.listdir(pdf_path):
  156. if pdf_file.endswith('.docx'):
  157. demo_names.append(pdf_file.split('.')[0])
  158. for demo_name in demo_names:
  159. pdf_path = os.path.join(pdf_dev_path, 'doc', f'{demo_name}.docx')
  160. local_image_dir = os.path.join(pdf_dev_path, 'mineru', 'images')
  161. image_dir = str(os.path.basename(local_image_dir))
  162. name_without_suff = os.path.basename(pdf_path).split(".docx")[0]
  163. dir_path = os.path.join(pdf_dev_path, 'mineru')
  164. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
  165. ds = read_local_office(pdf_path)[0]
  166. common.delete_file(dir_path)
  167. ds.apply(doc_analyze, ocr=True).pipe_txt_mode(image_writer).dump_md(md_writer, f"{name_without_suff}.md", image_dir)
  168. common.sdk_count_folders_and_check_contents(dir_path)
  169. @pytest.mark.P0
  170. def test_pdf_cli_auto(self):
  171. """magic_pdf cli test auto."""
  172. time.sleep(2)
  173. demo_names = []
  174. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  175. for pdf_file in os.listdir(pdf_path):
  176. if pdf_file.endswith('.pdf'):
  177. demo_names.append(pdf_file.split('.')[0])
  178. for demo_name in demo_names:
  179. res_path = os.path.join(pdf_dev_path, 'mineru')
  180. common.delete_file(res_path)
  181. cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
  182. pdf_path, f'{demo_name}.pdf'), res_path, 'auto')
  183. logging.info(cmd)
  184. os.system(cmd)
  185. common.cli_count_folders_and_check_contents(
  186. os.path.join(res_path, demo_name, 'auto'))
  187. @pytest.mark.P0
  188. def test_pdf_cli_txt(self):
  189. """magic_pdf cli test txt."""
  190. time.sleep(2)
  191. demo_names = []
  192. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  193. for pdf_file in os.listdir(pdf_path):
  194. if pdf_file.endswith('.pdf'):
  195. demo_names.append(pdf_file.split('.')[0])
  196. for demo_name in demo_names:
  197. res_path = os.path.join(pdf_dev_path, 'mineru')
  198. common.delete_file(res_path)
  199. cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
  200. pdf_path, f'{demo_name}.pdf'), res_path, 'txt')
  201. logging.info(cmd)
  202. os.system(cmd)
  203. common.cli_count_folders_and_check_contents(
  204. os.path.join(res_path, demo_name, 'txt'))
  205. @pytest.mark.P0
  206. def test_pdf_cli_ocr(self):
  207. """magic_pdf cli test ocr."""
  208. time.sleep(2)
  209. demo_names = []
  210. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  211. for pdf_file in os.listdir(pdf_path):
  212. if pdf_file.endswith('.pdf'):
  213. demo_names.append(pdf_file.split('.')[0])
  214. for demo_name in demo_names:
  215. res_path = os.path.join(pdf_dev_path, 'mineru')
  216. common.delete_file(res_path)
  217. cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
  218. pdf_path, f'{demo_name}.pdf'), res_path, 'ocr')
  219. logging.info(cmd)
  220. os.system(cmd)
  221. common.cli_count_folders_and_check_contents(
  222. os.path.join(res_path, demo_name, 'ocr'))
  223. @pytest.mark.skip(reason='out-of-date api')
  224. @pytest.mark.P1
  225. def test_pdf_dev_cli_local_jsonl_txt(self):
  226. """magic_pdf_dev cli local txt."""
  227. time.sleep(2)
  228. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  229. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, "txt")
  230. logging.info(cmd)
  231. os.system(cmd)
  232. @pytest.mark.skip(reason='out-of-date api')
  233. @pytest.mark.P1
  234. def test_pdf_dev_cli_local_jsonl_ocr(self):
  235. """magic_pdf_dev cli local ocr."""
  236. time.sleep(2)
  237. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  238. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'ocr')
  239. logging.info(cmd)
  240. os.system(cmd)
  241. @pytest.mark.skip(reason='out-of-date api')
  242. @pytest.mark.P1
  243. def test_pdf_dev_cli_local_jsonl_auto(self):
  244. """magic_pdf_dev cli local auto."""
  245. time.sleep(2)
  246. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  247. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'auto')
  248. logging.info(cmd)
  249. os.system(cmd)
  250. @pytest.mark.skip(reason='out-of-date api')
  251. @pytest.mark.P1
  252. def test_pdf_dev_cli_s3_jsonl_txt(self):
  253. """magic_pdf_dev cli s3 txt."""
  254. time.sleep(2)
  255. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  256. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, "txt")
  257. logging.info(cmd)
  258. os.system(cmd)
  259. @pytest.mark.skip(reason='out-of-date api')
  260. @pytest.mark.P1
  261. def test_pdf_dev_cli_s3_jsonl_ocr(self):
  262. """magic_pdf_dev cli s3 ocr."""
  263. time.sleep(2)
  264. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  265. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'ocr')
  266. logging.info(cmd)
  267. os.system(cmd)
  268. @pytest.mark.skip(reason='out-of-date api')
  269. @pytest.mark.P1
  270. def test_pdf_dev_cli_s3_jsonl_auto(self):
  271. """magic_pdf_dev cli s3 auto."""
  272. time.sleep(2)
  273. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  274. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'auto')
  275. logging.info(cmd)
  276. os.system(cmd)
  277. @pytest.mark.P1
  278. def test_pdf_dev_cli_pdf_json_auto(self):
  279. """magic_pdf_dev cli pdf+json auto."""
  280. time.sleep(2)
  281. json_path = os.path.join(pdf_dev_path, 'test_model.json')
  282. pdf_path = os.path.join(pdf_dev_path, 'pdf', 'test_rearch_report.pdf')
  283. cmd = 'magic-pdf-dev --pdf %s --json %s --method %s' % (pdf_path, json_path, 'auto')
  284. logging.info(cmd)
  285. os.system(cmd)
  286. @pytest.mark.skip(reason='out-of-date api')
  287. @pytest.mark.P1
  288. def test_pdf_dev_cli_pdf_json_ocr(self):
  289. """magic_pdf_dev cli pdf+json ocr."""
  290. time.sleep(2)
  291. json_path = os.path.join(pdf_dev_path, 'test_model.json')
  292. pdf_path = os.path.join(pdf_dev_path, 'pdf', 'test_rearch_report.pdf')
  293. cmd = 'magic-pdf-dev --pdf %s --json %s --method %s' % (pdf_path, json_path, 'auto')
  294. logging.info(cmd)
  295. os.system(cmd)
  296. @pytest.mark.skip(reason="st废弃")
  297. @pytest.mark.P1
  298. def test_local_magic_pdf_open_st_table(self):
  299. """magic pdf cli open st table."""
  300. time.sleep(2)
  301. #pre_cmd = "cp ~/magic_pdf_st.json ~/magic-pdf.json"
  302. value = {
  303. "model": "struct_eqtable",
  304. "enable": True,
  305. "max_time": 400
  306. }
  307. common.update_config_file(magic_pdf_config, "table-config", value)
  308. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  309. common.delete_file(pdf_res_path)
  310. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  311. os.system(cli_cmd)
  312. res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  313. assert res is True
  314. @pytest.mark.P1
  315. def test_local_magic_pdf_open_rapidai_table(self):
  316. """magic pdf cli open rapid ai table."""
  317. time.sleep(2)
  318. #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
  319. #os.system(pre_cmd)
  320. value = {
  321. "model": "rapid_table",
  322. "enable": True,
  323. "sub_model": "slanet_plus",
  324. "max_time": 400
  325. }
  326. common.update_config_file(magic_pdf_config, "table-config", value)
  327. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  328. common.delete_file(pdf_res_path)
  329. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  330. os.system(cli_cmd)
  331. res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  332. assert res is True
  333. @pytest.mark.P1
  334. def test_local_magic_pdf_doclayout_yolo(self):
  335. """magic pdf cli open doclyaout yolo."""
  336. time.sleep(2)
  337. #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
  338. #os.system(pre_cmd)
  339. value = {
  340. "model": "doclayout_yolo"
  341. }
  342. common.update_config_file(magic_pdf_config, "layout-config", value)
  343. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  344. common.delete_file(pdf_res_path)
  345. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  346. os.system(cli_cmd)
  347. common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
  348. @pytest.mark.skip(reason="layoutlmv3废弃")
  349. @pytest.mark.P1
  350. def test_local_magic_pdf_layoutlmv3_yolo(self):
  351. """magic pdf cli open layoutlmv3."""
  352. time.sleep(2)
  353. value = {
  354. "model": "layoutlmv3"
  355. }
  356. common.update_config_file(magic_pdf_config, "layout-config", value)
  357. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  358. common.delete_file(pdf_res_path)
  359. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  360. os.system(cli_cmd)
  361. common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
  362. #res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  363. @pytest.mark.P1
  364. def test_magic_pdf_cpu(self):
  365. """magic pdf cli cpu mode."""
  366. time.sleep(2)
  367. #pre_cmd = "cp ~/magic_pdf_html_table_cpu.json ~/magic-pdf.json"
  368. #os.system(pre_cmd)
  369. value = {
  370. "model": "rapid_table",
  371. "enable": False,
  372. "sub_model": "slanet_plus",
  373. "max_time": 400
  374. }
  375. common.update_config_file(magic_pdf_config, "table-config", value)
  376. common.update_config_file(magic_pdf_config, "device-mode", "cpu")
  377. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  378. common.delete_file(pdf_res_path)
  379. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  380. os.system(cli_cmd)
  381. common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
  382. @pytest.mark.P1
  383. def test_local_magic_pdf_close_html_table(self):
  384. """magic pdf cli close table."""
  385. time.sleep(2)
  386. #pre_cmd = "cp ~/magic_pdf_close_table.json ~/magic-pdf.json"
  387. #os.system(pre_cmd)
  388. value = {
  389. "model": "rapid_table",
  390. "enable": False,
  391. "sub_model": "slanet_plus",
  392. "max_time": 400
  393. }
  394. common.update_config_file(magic_pdf_config, "table-config", value)
  395. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  396. common.delete_file(pdf_res_path)
  397. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  398. os.system(cli_cmd)
  399. res = common.check_close_tables(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  400. assert res is True
  401. if __name__ == '__main__':
  402. pytest.main()