test_cli.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import pytest
  2. import os
  3. from conf import conf
  4. import os
  5. import json
  6. from magic_pdf.pipe.UNIPipe import UNIPipe
  7. from magic_pdf.rw.DiskReaderWriter import DiskReaderWriter
  8. from lib import common
  9. pdf_res_path = conf.conf["pdf_res_path"]
  10. code_path = conf.conf["code_path"]
  11. pdf_dev_path = conf.conf["pdf_dev_path"]
  12. class TestCli:
  13. """
  14. test cli
  15. """
  16. def test_pdf_sdk(self):
  17. """
  18. pdf sdk 方式解析
  19. """
  20. demo_names = list()
  21. pdf_path = os.path.join(pdf_dev_path, "pdf")
  22. for pdf_file in os.listdir(pdf_path):
  23. if pdf_file.endswith('.pdf'):
  24. demo_names.append(pdf_file.split('.')[0])
  25. for demo_name in demo_names:
  26. model_path = os.path.join(pdf_dev_path, f"{demo_name}_model.json")
  27. pdf_path = os.path.join(pdf_dev_path, "pdf", f"{demo_name}.pdf")
  28. pdf_bytes = open(pdf_path, "rb").read()
  29. model_json = json.loads(open(model_path, "r", encoding="utf-8").read())
  30. image_writer = DiskReaderWriter(pdf_dev_path)
  31. image_dir = str(os.path.basename(pdf_dev_path))
  32. jso_useful_key = {"_pdf_type": "", "model_list": model_json}
  33. pipe = UNIPipe(pdf_bytes, jso_useful_key, image_writer)
  34. pipe.pipe_classify()
  35. pipe.pipe_parse()
  36. md_content = pipe.pipe_mk_markdown(image_dir, drop_mode="none")
  37. dir_path = os.path.join(pdf_dev_path, "mineru")
  38. if not os.path.exists(dir_path):
  39. os.makedirs(dir_path, exist_ok=True)
  40. res_path = os.path.join(dir_path, f"{demo_name}.md")
  41. with open(res_path, "w+", encoding="utf-8") as f:
  42. f.write(md_content)
  43. common.count_folders_and_check_contents(res_path)
  44. # def test_pdf_specify_jsonl(self):
  45. # """
  46. # 输入jsonl, 默认方式解析
  47. # """
  48. # cmd = "cd %s && export PYTHONPATH=. && python magic_pdf/cli/magicpdf.py json-command --json 's3://llm-process-pperf/ebook_index_textbook_40k/中高考&竞赛知识点/part-663f1ef5e7c1-009416.jsonl?bytes=0,1133972'" % (code_path)
  49. # logging.info(cmd)
  50. # common.check_shell(cmd)
  51. # #common.count_folders_and_check_contents(pdf_res_path)
  52. # def test_pdf_specify_jsonl_txt(self):
  53. # """
  54. # 输入jsonl, txt方式解析
  55. # """
  56. # cmd = "cd %s && export PYTHONPATH=. && python magic_pdf/cli/magicpdf.py json-command --json 's3://llm-process-pperf/ebook_index_textbook_40k/中高考&竞赛知识点/part-663f1ef5e7c1-009416.jsonl?bytes=0,1133972' --method txt" % (code_path)
  57. # logging.info(cmd)
  58. # common.check_shell(cmd)
  59. # #common.count_folders_and_check_contents(pdf_res_path)
  60. #
  61. # def test_pdf_specify_jsonl_ocr(self):
  62. # """
  63. # 输入jsonl, ocr方式解析
  64. # """
  65. # cmd = "cd %s && export PYTHONPATH=. && python magic_pdf/cli/magicpdf.py json-command --json 's3://llm-process-pperf/ebook_index_textbook_40k/中高考&竞赛知识点/part-663f1ef5e7c1-009416.jsonl?bytes=0,1133972' --method ocr" % (code_path)
  66. # logging.info(cmd)
  67. # common.check_shell(cmd)
  68. # #common.count_folders_and_check_contents(pdf_res_path)
  69. if __name__ == "__main__":
  70. pytest.main()