text_demo.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import json
  2. import os
  3. import sys
  4. from pathlib import Path
  5. import click
  6. from demo.demo_commons import get_json_from_local_or_s3, write_json_to_local, local_jsonl_path, local_json_path
  7. from magic_pdf.dict2md.mkcontent import mk_mm_markdown
  8. from magic_pdf.pipeline import (
  9. meta_scan,
  10. classify_by_type,
  11. parse_pdf,
  12. pdf_intermediate_dict_to_markdown,
  13. save_tables_to_s3,
  14. )
  15. from magic_pdf.libs.commons import join_path
  16. from loguru import logger
  17. def demo_parse_pdf(book_name=None, start_page_id=0, debug_mode=True):
  18. json_object = get_json_from_local_or_s3(book_name)
  19. jso = parse_pdf(json_object, start_page_id=start_page_id, debug_mode=debug_mode)
  20. logger.info(f"pdf_parse_time: {jso['parse_time']}")
  21. write_json_to_local(jso, book_name)
  22. jso_md = pdf_intermediate_dict_to_markdown(jso, debug_mode=debug_mode)
  23. content = jso_md.get("content_list")
  24. markdown_content = mk_mm_markdown(content)
  25. if book_name is not None:
  26. save_tmp_path = os.path.join(os.path.dirname(__file__), "../..", "tmp", "unittest", "md", book_name)
  27. uni_format_save_path = join_path(save_tmp_path, "book" + ".json")
  28. markdown_save_path = join_path(save_tmp_path, "book" + ".md")
  29. with open(uni_format_save_path, "w", encoding="utf-8") as f:
  30. f.write(json.dumps(content, ensure_ascii=False, indent=4))
  31. with open(markdown_save_path, "w", encoding="utf-8") as f:
  32. f.write(markdown_content)
  33. else:
  34. logger.info(json.dumps(content, ensure_ascii=False))
  35. def demo_save_tables(book_name=None, start_page_id=0, debug_mode=True):
  36. json_object = get_json_from_local_or_s3(book_name)
  37. jso = parse_pdf(json_object, start_page_id=start_page_id, debug_mode=debug_mode)
  38. logger.info(f"pdf_parse_time: {jso['parse_time']}")
  39. write_json_to_local(jso, book_name)
  40. save_tables_to_s3(jso, debug_mode=debug_mode)
  41. def demo_classify_by_type(book_name=None, debug_mode=True):
  42. json_object = get_json_from_local_or_s3(book_name)
  43. jso = classify_by_type(json_object, debug_mode=debug_mode)
  44. logger.info(json.dumps(jso, ensure_ascii=False))
  45. logger.info(f"classify_time: {jso['classify_time']}")
  46. write_json_to_local(jso, book_name)
  47. def demo_meta_scan(book_name=None, debug_mode=True):
  48. json_object = get_json_from_local_or_s3(book_name)
  49. # doc_layout_check=False
  50. jso = meta_scan(json_object, doc_layout_check=True)
  51. logger.info(json.dumps(jso, ensure_ascii=False))
  52. logger.info(f"meta_scan_time: {jso['meta_scan_time']}")
  53. write_json_to_local(jso, book_name)
  54. def demo_meta_scan_from_jsonl():
  55. with open(local_jsonl_path, "r", encoding="utf-8") as jsonl_file:
  56. for line in jsonl_file:
  57. jso = json.loads(line)
  58. jso = meta_scan(jso)
  59. logger.info(f"pdf_path: {jso['content']['pdf_path']}")
  60. logger.info(f"read_file_time: {jso['read_file_time']}")
  61. logger.info(f"meta_scan_time: {jso['meta_scan_time']}")
  62. def demo_test5():
  63. with open(local_json_path, "r", encoding="utf-8") as json_file:
  64. json_line = json_file.read()
  65. jso = json.loads(json_line)
  66. img_list_len = len(jso["content"]["image_info_per_page"])
  67. logger.info(f"img_list_len: {img_list_len}")
  68. def read_more_para_test_samples(type="scihub"):
  69. # 读取多段落测试样本
  70. curr_dir = Path(__file__).parent
  71. files_path = ""
  72. if type == "gift":
  73. relative_path = "../tests/assets/more_para_test_samples/gift_files.txt"
  74. files_path = os.path.join(curr_dir, relative_path)
  75. if type == "scihub":
  76. relative_path = "../tests/assets/more_para_test_samples/scihub_files.txt"
  77. files_path = os.path.join(curr_dir, relative_path)
  78. if type == "zlib":
  79. relative_path = "../tests/assets/more_para_test_samples/zlib_files.txt"
  80. files_path = os.path.join(curr_dir, relative_path)
  81. # check if file exists
  82. if not os.path.exists(files_path):
  83. print("File not exist!")
  84. sys.exit(0)
  85. with open(files_path, "r", encoding="utf-8") as f:
  86. lines = f.readlines()
  87. # print("lines", lines)
  88. return lines
  89. def batch_test_more_para(type="scihub"):
  90. # 批量测试多段落
  91. para_test_files = read_more_para_test_samples(type)
  92. for file in para_test_files:
  93. file = file.strip()
  94. print(file)
  95. demo_parse_pdf(book_name=file)
  96. @click.command()
  97. @click.option("--book-name", help="s3上pdf文件的路径")
  98. def main(book_name: str):
  99. demo_parse_pdf(book_name, start_page_id=0)
  100. if __name__ == "__main__":
  101. main()