test_cli_sdk.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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.pipe.UNIPipe import UNIPipe
  10. from magic_pdf.rw.DiskReaderWriter import DiskReaderWriter
  11. from magic_pdf.rw.S3ReaderWriter import S3ReaderWriter
  12. model_config.__use_inside_model__ = True
  13. pdf_res_path = conf.conf['pdf_res_path']
  14. code_path = conf.conf['code_path']
  15. pdf_dev_path = conf.conf['pdf_dev_path']
  16. magic_pdf_config = "/home/quyuan/magic-pdf.json"
  17. class TestCli:
  18. """test cli."""
  19. @pytest.fixture(autouse=True)
  20. def setup(self):
  21. """
  22. init
  23. """
  24. common.clear_gpu_memory()
  25. common.update_config_file(magic_pdf_config, "device-mode", "cuda")
  26. # 这里可以添加任何前置操作
  27. yield
  28. @pytest.mark.P0
  29. def test_pdf_auto_sdk(self):
  30. """pdf sdk auto test."""
  31. demo_names = list()
  32. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  33. for pdf_file in os.listdir(pdf_path):
  34. if pdf_file.endswith('.pdf'):
  35. demo_names.append(pdf_file.split('.')[0])
  36. for demo_name in demo_names:
  37. pdf_path = os.path.join(pdf_dev_path, 'pdf', f'{demo_name}.pdf')
  38. print(pdf_path)
  39. pdf_bytes = open(pdf_path, 'rb').read()
  40. local_image_dir = os.path.join(pdf_dev_path, 'pdf', 'images')
  41. image_dir = str(os.path.basename(local_image_dir))
  42. image_writer = DiskReaderWriter(local_image_dir)
  43. model_json = list()
  44. jso_useful_key = {'_pdf_type': '', 'model_list': model_json}
  45. pipe = UNIPipe(pdf_bytes, jso_useful_key, image_writer)
  46. pipe.pipe_classify()
  47. if len(model_json) == 0:
  48. if model_config.__use_inside_model__:
  49. pipe.pipe_analyze()
  50. else:
  51. exit(1)
  52. pipe.pipe_parse()
  53. md_content = pipe.pipe_mk_markdown(image_dir, drop_mode='none')
  54. dir_path = os.path.join(pdf_dev_path, 'mineru')
  55. if not os.path.exists(dir_path):
  56. os.makedirs(dir_path, exist_ok=True)
  57. res_path = os.path.join(dir_path, f'{demo_name}.md')
  58. common.delete_file(res_path)
  59. with open(res_path, 'w+', encoding='utf-8') as f:
  60. f.write(md_content)
  61. common.sdk_count_folders_and_check_contents(res_path)
  62. @pytest.mark.P0
  63. def test_pdf_ocr_sdk(self):
  64. """pdf sdk ocr test."""
  65. time.sleep(2)
  66. demo_names = list()
  67. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  68. for pdf_file in os.listdir(pdf_path):
  69. if pdf_file.endswith('.pdf'):
  70. demo_names.append(pdf_file.split('.')[0])
  71. for demo_name in demo_names:
  72. pdf_path = os.path.join(pdf_dev_path, 'pdf', f'{demo_name}.pdf')
  73. print(pdf_path)
  74. pdf_bytes = open(pdf_path, 'rb').read()
  75. local_image_dir = os.path.join(pdf_dev_path, 'pdf', 'images')
  76. image_dir = str(os.path.basename(local_image_dir))
  77. image_writer = DiskReaderWriter(local_image_dir)
  78. model_json = list()
  79. jso_useful_key = {'_pdf_type': 'ocr', 'model_list': model_json}
  80. pipe = UNIPipe(pdf_bytes, jso_useful_key, image_writer)
  81. pipe.pipe_classify()
  82. if len(model_json) == 0:
  83. if model_config.__use_inside_model__:
  84. pipe.pipe_analyze()
  85. else:
  86. exit(1)
  87. pipe.pipe_parse()
  88. md_content = pipe.pipe_mk_markdown(image_dir, drop_mode='none')
  89. dir_path = os.path.join(pdf_dev_path, 'mineru')
  90. if not os.path.exists(dir_path):
  91. os.makedirs(dir_path, exist_ok=True)
  92. res_path = os.path.join(dir_path, f'{demo_name}.md')
  93. common.delete_file(res_path)
  94. with open(res_path, 'w+', encoding='utf-8') as f:
  95. f.write(md_content)
  96. common.sdk_count_folders_and_check_contents(res_path)
  97. @pytest.mark.P0
  98. def test_pdf_txt_sdk(self):
  99. """pdf sdk txt test."""
  100. time.sleep(2)
  101. demo_names = list()
  102. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  103. for pdf_file in os.listdir(pdf_path):
  104. if pdf_file.endswith('.pdf'):
  105. demo_names.append(pdf_file.split('.')[0])
  106. for demo_name in demo_names:
  107. pdf_path = os.path.join(pdf_dev_path, 'pdf', f'{demo_name}.pdf')
  108. pdf_bytes = open(pdf_path, 'rb').read()
  109. local_image_dir = os.path.join(pdf_dev_path, 'pdf', 'images')
  110. image_dir = str(os.path.basename(local_image_dir))
  111. image_writer = DiskReaderWriter(local_image_dir)
  112. model_json = list()
  113. jso_useful_key = {'_pdf_type': 'txt', 'model_list': model_json}
  114. pipe = UNIPipe(pdf_bytes, jso_useful_key, image_writer)
  115. pipe.pipe_classify()
  116. if len(model_json) == 0:
  117. if model_config.__use_inside_model__:
  118. pipe.pipe_analyze()
  119. else:
  120. exit(1)
  121. pipe.pipe_parse()
  122. md_content = pipe.pipe_mk_markdown(image_dir, drop_mode='none')
  123. dir_path = os.path.join(pdf_dev_path, 'mineru')
  124. if not os.path.exists(dir_path):
  125. os.makedirs(dir_path, exist_ok=True)
  126. res_path = os.path.join(dir_path, f'{demo_name}.md')
  127. common.delete_file(res_path)
  128. with open(res_path, 'w+', encoding='utf-8') as f:
  129. f.write(md_content)
  130. common.sdk_count_folders_and_check_contents(res_path)
  131. @pytest.mark.P0
  132. def test_pdf_cli_auto(self):
  133. """magic_pdf cli test auto."""
  134. time.sleep(2)
  135. demo_names = []
  136. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  137. for pdf_file in os.listdir(pdf_path):
  138. if pdf_file.endswith('.pdf'):
  139. demo_names.append(pdf_file.split('.')[0])
  140. for demo_name in demo_names:
  141. res_path = os.path.join(pdf_dev_path, 'mineru')
  142. common.delete_file(res_path)
  143. cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
  144. pdf_path, f'{demo_name}.pdf'), res_path, 'auto')
  145. logging.info(cmd)
  146. os.system(cmd)
  147. common.cli_count_folders_and_check_contents(
  148. os.path.join(res_path, demo_name, 'auto'))
  149. @pytest.mark.P0
  150. def test_pdf_cli_txt(self):
  151. """magic_pdf cli test txt."""
  152. time.sleep(2)
  153. demo_names = []
  154. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  155. for pdf_file in os.listdir(pdf_path):
  156. if pdf_file.endswith('.pdf'):
  157. demo_names.append(pdf_file.split('.')[0])
  158. for demo_name in demo_names:
  159. res_path = os.path.join(pdf_dev_path, 'mineru')
  160. common.delete_file(res_path)
  161. cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
  162. pdf_path, f'{demo_name}.pdf'), res_path, 'txt')
  163. logging.info(cmd)
  164. os.system(cmd)
  165. common.cli_count_folders_and_check_contents(
  166. os.path.join(res_path, demo_name, 'txt'))
  167. @pytest.mark.P0
  168. def test_pdf_cli_ocr(self):
  169. """magic_pdf cli test ocr."""
  170. time.sleep(2)
  171. demo_names = []
  172. pdf_path = os.path.join(pdf_dev_path, 'pdf')
  173. for pdf_file in os.listdir(pdf_path):
  174. if pdf_file.endswith('.pdf'):
  175. demo_names.append(pdf_file.split('.')[0])
  176. for demo_name in demo_names:
  177. res_path = os.path.join(pdf_dev_path, 'mineru')
  178. common.delete_file(res_path)
  179. cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
  180. pdf_path, f'{demo_name}.pdf'), res_path, 'ocr')
  181. logging.info(cmd)
  182. os.system(cmd)
  183. common.cli_count_folders_and_check_contents(
  184. os.path.join(res_path, demo_name, 'ocr'))
  185. @pytest.mark.skip(reason='out-of-date api')
  186. @pytest.mark.P1
  187. def test_pdf_dev_cli_local_jsonl_txt(self):
  188. """magic_pdf_dev cli local txt."""
  189. time.sleep(2)
  190. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  191. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, "txt")
  192. logging.info(cmd)
  193. os.system(cmd)
  194. @pytest.mark.skip(reason='out-of-date api')
  195. @pytest.mark.P1
  196. def test_pdf_dev_cli_local_jsonl_ocr(self):
  197. """magic_pdf_dev cli local ocr."""
  198. time.sleep(2)
  199. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  200. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'ocr')
  201. logging.info(cmd)
  202. os.system(cmd)
  203. @pytest.mark.skip(reason='out-of-date api')
  204. @pytest.mark.P1
  205. def test_pdf_dev_cli_local_jsonl_auto(self):
  206. """magic_pdf_dev cli local auto."""
  207. time.sleep(2)
  208. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  209. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'auto')
  210. logging.info(cmd)
  211. os.system(cmd)
  212. @pytest.mark.skip(reason='out-of-date api')
  213. @pytest.mark.P1
  214. def test_pdf_dev_cli_s3_jsonl_txt(self):
  215. """magic_pdf_dev cli s3 txt."""
  216. time.sleep(2)
  217. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  218. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, "txt")
  219. logging.info(cmd)
  220. os.system(cmd)
  221. @pytest.mark.skip(reason='out-of-date api')
  222. @pytest.mark.P1
  223. def test_pdf_dev_cli_s3_jsonl_ocr(self):
  224. """magic_pdf_dev cli s3 ocr."""
  225. time.sleep(2)
  226. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  227. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'ocr')
  228. logging.info(cmd)
  229. os.system(cmd)
  230. @pytest.mark.skip(reason='out-of-date api')
  231. @pytest.mark.P1
  232. def test_pdf_dev_cli_s3_jsonl_auto(self):
  233. """magic_pdf_dev cli s3 auto."""
  234. time.sleep(2)
  235. jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
  236. cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'auto')
  237. logging.info(cmd)
  238. os.system(cmd)
  239. @pytest.mark.P1
  240. def test_pdf_dev_cli_pdf_json_auto(self):
  241. """magic_pdf_dev cli pdf+json auto."""
  242. time.sleep(2)
  243. json_path = os.path.join(pdf_dev_path, 'test_model.json')
  244. pdf_path = os.path.join(pdf_dev_path, 'pdf', 'test_rearch_report.pdf')
  245. cmd = 'magic-pdf-dev --pdf %s --json %s --method %s' % (pdf_path, json_path, 'auto')
  246. logging.info(cmd)
  247. os.system(cmd)
  248. @pytest.mark.skip(reason='out-of-date api')
  249. @pytest.mark.P1
  250. def test_pdf_dev_cli_pdf_json_ocr(self):
  251. """magic_pdf_dev cli pdf+json ocr."""
  252. time.sleep(2)
  253. json_path = os.path.join(pdf_dev_path, 'test_model.json')
  254. pdf_path = os.path.join(pdf_dev_path, 'pdf', 'test_rearch_report.pdf')
  255. cmd = 'magic-pdf-dev --pdf %s --json %s --method %s' % (pdf_path, json_path, 'auto')
  256. logging.info(cmd)
  257. os.system(cmd)
  258. @pytest.mark.P1
  259. def test_s3_sdk_suto(self):
  260. """
  261. test s3 sdk auto.
  262. """
  263. time.sleep(2)
  264. pdf_ak = os.getenv('pdf_ak')
  265. print (pdf_ak)
  266. pdf_sk = os.environ.get('pdf_sk', "")
  267. pdf_bucket = os.environ.get('bucket', "")
  268. pdf_endpoint = os.environ.get('pdf_endpoint', "")
  269. s3_pdf_path = conf.conf["s3_pdf_path"]
  270. image_dir = "s3://" + pdf_bucket + "/mineru/test/output"
  271. print (image_dir)
  272. s3pdf_cli = S3ReaderWriter(pdf_ak, pdf_sk, pdf_endpoint)
  273. s3image_cli = S3ReaderWriter(pdf_ak, pdf_sk, pdf_endpoint, parent_path=image_dir)
  274. pdf_bytes = s3pdf_cli.read(s3_pdf_path, mode=s3pdf_cli.MODE_BIN)
  275. jso_useful_key = {"_pdf_type": "", "model_list": []}
  276. pipe = UNIPipe(pdf_bytes, jso_useful_key, s3image_cli)
  277. pipe.pipe_classify()
  278. pipe.pipe_analyze()
  279. pipe.pipe_parse()
  280. md_content = pipe.pipe_mk_markdown(image_dir, drop_mode="none")
  281. assert len(md_content) > 0
  282. @pytest.mark.P1
  283. def test_local_magic_pdf_open_st_table(self):
  284. """magic pdf cli open st table."""
  285. time.sleep(2)
  286. #pre_cmd = "cp ~/magic_pdf_st.json ~/magic-pdf.json"
  287. value = {
  288. "model": "struct_eqtable",
  289. "enable": True,
  290. "max_time": 400
  291. }
  292. common.update_config_file(magic_pdf_config, "table-config", value)
  293. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  294. common.delete_file(pdf_res_path)
  295. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  296. os.system(cli_cmd)
  297. res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  298. assert res is True
  299. @pytest.mark.P1
  300. def test_local_magic_pdf_open_tablemaster_cuda(self):
  301. """magic pdf cli open table master html table cuda mode."""
  302. time.sleep(2)
  303. #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
  304. #os.system(pre_cmd)
  305. value = {
  306. "model": "tablemaster",
  307. "enable": True,
  308. "max_time": 400
  309. }
  310. common.update_config_file(magic_pdf_config, "table-config", value)
  311. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  312. common.delete_file(pdf_res_path)
  313. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  314. os.system(cli_cmd)
  315. res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  316. assert res is True
  317. @pytest.mark.P1
  318. def test_local_magic_pdf_open_rapidai_table(self):
  319. """magic pdf cli open rapid ai table."""
  320. time.sleep(2)
  321. #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
  322. #os.system(pre_cmd)
  323. value = {
  324. "model": "rapid_table",
  325. "enable": True,
  326. "max_time": 400
  327. }
  328. common.update_config_file(magic_pdf_config, "table-config", value)
  329. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  330. common.delete_file(pdf_res_path)
  331. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  332. os.system(cli_cmd)
  333. res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  334. assert res is True
  335. @pytest.mark.P1
  336. def test_local_magic_pdf_doclayout_yolo(self):
  337. """magic pdf cli open doclyaout yolo."""
  338. time.sleep(2)
  339. #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
  340. #os.system(pre_cmd)
  341. value = {
  342. "model": "doclayout_yolo"
  343. }
  344. common.update_config_file(magic_pdf_config, "layout-config", value)
  345. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  346. common.delete_file(pdf_res_path)
  347. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  348. os.system(cli_cmd)
  349. common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
  350. @pytest.mark.P1
  351. def test_local_magic_pdf_layoutlmv3_yolo(self):
  352. """magic pdf cli open layoutlmv3."""
  353. time.sleep(2)
  354. value = {
  355. "model": "layoutlmv3"
  356. }
  357. common.update_config_file(magic_pdf_config, "layout-config", value)
  358. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  359. common.delete_file(pdf_res_path)
  360. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  361. os.system(cli_cmd)
  362. common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
  363. #res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  364. @pytest.mark.P1
  365. def test_magic_pdf_open_tablemaster_table_cpu(self):
  366. """magic pdf cli close html table cpu mode."""
  367. time.sleep(2)
  368. #pre_cmd = "cp ~/magic_pdf_html_table_cpu.json ~/magic-pdf.json"
  369. #os.system(pre_cmd)
  370. value = {
  371. "model": "tablemaster",
  372. "enable": False,
  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": "tablemaster",
  390. "enable": False,
  391. "max_time": 400
  392. }
  393. common.update_config_file(magic_pdf_config, "table-config", value)
  394. pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
  395. common.delete_file(pdf_res_path)
  396. cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
  397. os.system(cli_cmd)
  398. res = common.check_close_tables(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
  399. assert res is True
  400. if __name__ == '__main__':
  401. pytest.main()