demo_zhch_v1.py 1.2 KB

1234567891011121314151617181920212223242526272829
  1. import os
  2. from loguru import logger
  3. from magic_pdf.data.data_reader_writer import FileBasedDataWriter
  4. from magic_pdf.pipe.UNIPipe import UNIPipe
  5. from dotenv import load_dotenv; load_dotenv()
  6. print(f"os.environ['CUDA_VISIBLE_DEVICES']: {os.environ['CUDA_VISIBLE_DEVICES']}")
  7. print(f"os.environ['MINERU_TOOLS_CONFIG_JSON']: {os.environ['MINERU_TOOLS_CONFIG_JSON']}")
  8. try:
  9. current_script_dir = os.path.dirname(os.path.abspath(__file__))
  10. demo_name = '600916_中国黄金_2002年报_83_94'
  11. pdf_path = os.path.join(current_script_dir, f'{demo_name}.pdf')
  12. pdf_bytes = open(pdf_path, 'rb').read()
  13. jso_useful_key = {'_pdf_type': '', 'model_list': []}
  14. local_image_dir = os.path.join(current_script_dir, 'images')
  15. image_dir = str(os.path.basename(local_image_dir))
  16. image_writer = FileBasedDataWriter(local_image_dir)
  17. pipe = UNIPipe(pdf_bytes, jso_useful_key, image_writer)
  18. pipe.pipe_classify()
  19. pipe.pipe_analyze()
  20. pipe.pipe_parse()
  21. md_content = pipe.pipe_mk_markdown(image_dir, drop_mode='none')
  22. with open(f'{demo_name}.md', 'w', encoding='utf-8') as f:
  23. f.write(md_content)
  24. except Exception as e:
  25. logger.exception(e)