test_cli_sdk.py 16 KB

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