|
|
@@ -6,15 +6,14 @@ from conf import conf
|
|
|
from lib import common
|
|
|
import time
|
|
|
import magic_pdf.model as model_config
|
|
|
-import os
|
|
|
-from magic_pdf.data.data_reader_writer import FileBasedDataWriter
|
|
|
+from magic_pdf.data.read_api import read_local_images
|
|
|
+from magic_pdf.data.read_api import read_local_office
|
|
|
from magic_pdf.data.data_reader_writer import S3DataReader, S3DataWriter
|
|
|
from magic_pdf.config.make_content_config import DropMode, MakeMode
|
|
|
from magic_pdf.data.data_reader_writer import FileBasedDataWriter, FileBasedDataReader
|
|
|
from magic_pdf.data.dataset import PymuDocDataset
|
|
|
from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
|
|
|
from magic_pdf.config.enums import SupportedPdfParseMethod
|
|
|
-model_config.__use_inside_model__ = True
|
|
|
pdf_res_path = conf.conf['pdf_res_path']
|
|
|
code_path = conf.conf['code_path']
|
|
|
pdf_dev_path = conf.conf['pdf_dev_path']
|
|
|
@@ -33,7 +32,7 @@ class TestCli:
|
|
|
yield
|
|
|
|
|
|
@pytest.mark.P0
|
|
|
- def test_pdf_auto_sdk(self):
|
|
|
+ def test_pdf_local_sdk(self):
|
|
|
"""pdf sdk auto test."""
|
|
|
demo_names = list()
|
|
|
pdf_path = os.path.join(pdf_dev_path, 'pdf')
|
|
|
@@ -44,6 +43,7 @@ class TestCli:
|
|
|
pdf_path = os.path.join(pdf_dev_path, 'pdf', f'{demo_name}.pdf')
|
|
|
local_image_dir = os.path.join(pdf_dev_path, 'pdf', 'images')
|
|
|
image_dir = str(os.path.basename(local_image_dir))
|
|
|
+ name_without_suff = os.path.basename(pdf_path).split(".pdf")[0]
|
|
|
dir_path = os.path.join(pdf_dev_path, 'mineru')
|
|
|
image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
|
|
|
reader1 = FileBasedDataReader("")
|
|
|
@@ -59,13 +59,132 @@ class TestCli:
|
|
|
## pipeline
|
|
|
pipe_result = infer_result.pipe_txt_mode(image_writer)
|
|
|
common.delete_file(dir_path)
|
|
|
- infer_result.draw_model(os.path.join(dir_path, f"{demo_name}_model.pdf"))
|
|
|
- pipe_result.draw_layout(os.path.join(dir_path, f"{demo_name}_layout.pdf"))
|
|
|
- pipe_result.draw_span(os.path.join(dir_path, f"{demo_name}_spans.pdf"))
|
|
|
- pipe_result.dump_md(md_writer, f"{demo_name}.md", image_dir)
|
|
|
- pipe_result.dump_content_list(md_writer, f"{demo_name}_content_list.json", image_dir)
|
|
|
+ ### draw model result on each page
|
|
|
+ infer_result.draw_model(os.path.join(dir_path, f"{name_without_suff}_model.pdf"))
|
|
|
+
|
|
|
+ ### get model inference result
|
|
|
+ model_inference_result = infer_result.get_infer_res()
|
|
|
+
|
|
|
+ ### draw layout result on each page
|
|
|
+ pipe_result.draw_layout(os.path.join(dir_path, f"{name_without_suff}_layout.pdf"))
|
|
|
+
|
|
|
+ ### draw spans result on each page
|
|
|
+ pipe_result.draw_span(os.path.join(dir_path, f"{name_without_suff}_spans.pdf"))
|
|
|
+
|
|
|
+ ### dump markdown
|
|
|
+ md_content = pipe_result.get_markdown(image_dir)
|
|
|
+ pipe_result.dump_md(md_writer, f"{name_without_suff}.md", image_dir)
|
|
|
+ ### get content list content
|
|
|
+ content_list_content = pipe_result.get_content_list(image_dir)
|
|
|
+ pipe_result.dump_content_list(md_writer, f"{name_without_suff}_content_list.json", image_dir)
|
|
|
+
|
|
|
+ ### get middle json
|
|
|
+ middle_json_content = pipe_result.get_middle_json()
|
|
|
+ ### dump middle json
|
|
|
+ pipe_result.dump_middle_json(md_writer, f'{name_without_suff}_middle.json')
|
|
|
+ common.sdk_count_folders_and_check_contents(dir_path)
|
|
|
+
|
|
|
+ @pytest.mark.P0
|
|
|
+ def test_pdf_s3_sdk(self):
|
|
|
+ """pdf s3 sdk test."""
|
|
|
+ demo_names = list()
|
|
|
+ pdf_path = os.path.join(pdf_dev_path, 'pdf')
|
|
|
+ for pdf_file in os.listdir(pdf_path):
|
|
|
+ if pdf_file.endswith('.pdf'):
|
|
|
+ demo_names.append(pdf_file.split('.')[0])
|
|
|
+ for demo_name in demo_names:
|
|
|
+ pdf_path = os.path.join(pdf_dev_path, 'pdf', f'{demo_name}.pdf')
|
|
|
+ local_image_dir = os.path.join(pdf_dev_path, 'pdf', 'images')
|
|
|
+ image_dir = str(os.path.basename(local_image_dir))
|
|
|
+ name_without_suff = os.path.basename(pdf_path).split(".pdf")[0]
|
|
|
+ dir_path = os.path.join(pdf_dev_path, 'mineru')
|
|
|
+ pass
|
|
|
+
|
|
|
+ @pytest.mark.P0
|
|
|
+ def test_pdf_local_ppt(self):
|
|
|
+ """pdf sdk auto test."""
|
|
|
+ demo_names = list()
|
|
|
+ pdf_path = os.path.join(pdf_dev_path, 'ppt')
|
|
|
+ for pdf_file in os.listdir(pdf_path):
|
|
|
+ if pdf_file.endswith('.pptx'):
|
|
|
+ demo_names.append(pdf_file.split('.')[0])
|
|
|
+ for demo_name in demo_names:
|
|
|
+ pdf_path = os.path.join(pdf_dev_path, 'ppt', f'{demo_name}.pptx')
|
|
|
+ local_image_dir = os.path.join(pdf_dev_path, 'mineru', 'images')
|
|
|
+ image_dir = str(os.path.basename(local_image_dir))
|
|
|
+ name_without_suff = os.path.basename(pdf_path).split(".pptx")[0]
|
|
|
+ dir_path = os.path.join(pdf_dev_path, 'mineru')
|
|
|
+ image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
|
|
|
+ ds = read_local_office(pdf_path)[0]
|
|
|
+ common.delete_file(dir_path)
|
|
|
+
|
|
|
+ ds.apply(doc_analyze, ocr=True).pipe_txt_mode(image_writer).dump_md(md_writer, f"{name_without_suff}.md", image_dir)
|
|
|
+ common.sdk_count_folders_and_check_contents(dir_path)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @pytest.mark.P0
|
|
|
+ def test_pdf_local_image(self):
|
|
|
+ """pdf sdk auto test."""
|
|
|
+ demo_names = list()
|
|
|
+ pdf_path = os.path.join(pdf_dev_path, 'images')
|
|
|
+ for pdf_file in os.listdir(pdf_path):
|
|
|
+ if pdf_file.endswith('.jpg'):
|
|
|
+ demo_names.append(pdf_file.split('.')[0])
|
|
|
+ for demo_name in demo_names:
|
|
|
+ pdf_path = os.path.join(pdf_dev_path, 'images', f'{demo_name}.jpg')
|
|
|
+ local_image_dir = os.path.join(pdf_dev_path, 'mineru', 'images')
|
|
|
+ image_dir = str(os.path.basename(local_image_dir))
|
|
|
+ name_without_suff = os.path.basename(pdf_path).split(".jpg")[0]
|
|
|
+ dir_path = os.path.join(pdf_dev_path, 'mineru')
|
|
|
+ common.delete_file(dir_path)
|
|
|
+ image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
|
|
|
+ ds = read_local_images(pdf_path)[0]
|
|
|
+ ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(
|
|
|
+ md_writer, f"{name_without_suff}.md", image_dir)
|
|
|
common.sdk_count_folders_and_check_contents(dir_path)
|
|
|
|
|
|
+
|
|
|
+ @pytest.mark.P0
|
|
|
+ def test_local_image_dir(self):
|
|
|
+ """local image dir."""
|
|
|
+ demo_names = list()
|
|
|
+ pdf_path = os.path.join(pdf_dev_path, 'images')
|
|
|
+ dir_path = os.path.join(pdf_dev_path, 'mineru')
|
|
|
+ local_image_dir = os.path.join(pdf_dev_path, 'mineru', 'images')
|
|
|
+ image_dir = str(os.path.basename(local_image_dir))
|
|
|
+ image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
|
|
|
+ common.delete_file(dir_path)
|
|
|
+ dss = read_local_images(pdf_path, suffixes=['.png', '.jpg'])
|
|
|
+ count = 0
|
|
|
+ for ds in dss:
|
|
|
+ ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(md_writer, f"{count}.md", image_dir)
|
|
|
+ count += 1
|
|
|
+ common.sdk_count_folders_and_check_contents(dir_path)
|
|
|
+
|
|
|
+ def test_local_doc_parse(self):
|
|
|
+ """
|
|
|
+ doc 解析
|
|
|
+ """
|
|
|
+ demo_names = list()
|
|
|
+ pdf_path = os.path.join(pdf_dev_path, 'doc')
|
|
|
+ for pdf_file in os.listdir(pdf_path):
|
|
|
+ if pdf_file.endswith('.docx'):
|
|
|
+ demo_names.append(pdf_file.split('.')[0])
|
|
|
+ for demo_name in demo_names:
|
|
|
+ pdf_path = os.path.join(pdf_dev_path, 'doc', f'{demo_name}.docx')
|
|
|
+ local_image_dir = os.path.join(pdf_dev_path, 'mineru', 'images')
|
|
|
+ image_dir = str(os.path.basename(local_image_dir))
|
|
|
+ name_without_suff = os.path.basename(pdf_path).split(".docx")[0]
|
|
|
+ dir_path = os.path.join(pdf_dev_path, 'mineru')
|
|
|
+ image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
|
|
|
+ ds = read_local_office(pdf_path)[0]
|
|
|
+ common.delete_file(dir_path)
|
|
|
+
|
|
|
+ ds.apply(doc_analyze, ocr=True).pipe_txt_mode(image_writer).dump_md(md_writer, f"{name_without_suff}.md", image_dir)
|
|
|
+ common.sdk_count_folders_and_check_contents(dir_path)
|
|
|
+
|
|
|
+
|
|
|
@pytest.mark.P0
|
|
|
def test_pdf_cli_auto(self):
|
|
|
"""magic_pdf cli test auto."""
|
|
|
@@ -203,63 +322,7 @@ class TestCli:
|
|
|
cmd = 'magic-pdf-dev --pdf %s --json %s --method %s' % (pdf_path, json_path, 'auto')
|
|
|
logging.info(cmd)
|
|
|
os.system(cmd)
|
|
|
-
|
|
|
-
|
|
|
- @pytest.mark.P1
|
|
|
- def test_s3_sdk_auto(self):
|
|
|
- """
|
|
|
- test s3 sdk auto.
|
|
|
- """
|
|
|
- time.sleep(2)
|
|
|
- pdf_ak = os.getenv('pdf_ak')
|
|
|
- print (pdf_ak)
|
|
|
- pdf_sk = os.environ.get('pdf_sk', "")
|
|
|
- pdf_bucket = os.environ.get('bucket', "")
|
|
|
- pdf_endpoint = os.environ.get('pdf_endpoint', "")
|
|
|
- s3_pdf_path = conf.conf["s3_pdf_path"]
|
|
|
- image_dir = "s3://" + pdf_bucket + "/mineru/test/output"
|
|
|
- prefix = "mineru/test/output"
|
|
|
- reader = S3DataReader(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
|
|
|
- writer = S3DataWriter(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
|
|
|
- # = S3DataWriter(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
|
|
|
- image_writer = S3DataWriter(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
|
|
|
- local_dir = "output"
|
|
|
- name_without_suff = os.path.basename(s3_pdf_path).split(".")[0]
|
|
|
-
|
|
|
- # read bytes
|
|
|
- pdf_bytes = reader.read(s3_pdf_path) # read the pdf content
|
|
|
-
|
|
|
- # proc
|
|
|
- ## Create Dataset Instance
|
|
|
- ds = PymuDocDataset(pdf_bytes)
|
|
|
-
|
|
|
- ## inference
|
|
|
- if ds.classify() == SupportedPdfParseMethod.OCR:
|
|
|
- infer_result = ds.apply(doc_analyze, ocr=True)
|
|
|
-
|
|
|
- ## pipeline
|
|
|
- pipe_result = infer_result.pipe_ocr_mode(image_writer)
|
|
|
- else:
|
|
|
- infer_result = ds.apply(doc_analyze, ocr=False)
|
|
|
-
|
|
|
- ## pipeline
|
|
|
- pipe_result = infer_result.pipe_txt_mode(image_writer)
|
|
|
-
|
|
|
- ### draw model result on each page
|
|
|
- infer_result.draw_model(os.path.join(local_dir, f'{name_without_suff}_model.pdf')) # dump to local
|
|
|
-
|
|
|
- ### draw layout result on each page
|
|
|
- pipe_result.draw_layout(os.path.join(local_dir, f'{name_without_suff}_layout.pdf')) # dump to local
|
|
|
-
|
|
|
- ### draw spans result on each page
|
|
|
- pipe_result.draw_span(os.path.join(local_dir, f'{name_without_suff}_spans.pdf')) # dump to local
|
|
|
-
|
|
|
- ### dump markdown
|
|
|
- pipe_result.dump_md(writer, f'{name_without_suff}.md', "unittest/tmp/images") # dump to remote s3
|
|
|
-
|
|
|
- ### dump content list
|
|
|
- pipe_result.dump_content_list(writer, f"{name_without_suff}_content_list.json", image_dir)
|
|
|
-
|
|
|
+
|
|
|
|
|
|
@pytest.mark.P1
|
|
|
def test_local_magic_pdf_open_st_table(self):
|