UNIPipe.py 4.69 KB
Newer Older
赵小蒙's avatar
赵小蒙 committed
1
2
import json

3
from loguru import logger
4

5
from magic_pdf.config.make_content_config import DropMode, MakeMode
6
7
from magic_pdf.data.data_reader_writer import DataWriter
from magic_pdf.libs.commons import join_path
赵小蒙's avatar
赵小蒙 committed
8
from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
赵小蒙's avatar
赵小蒙 committed
9
from magic_pdf.pipe.AbsPipe import AbsPipe
10
from magic_pdf.user_api import parse_ocr_pdf, parse_union_pdf
11
12


赵小蒙's avatar
赵小蒙 committed
13
class UNIPipe(AbsPipe):
14

15
    def __init__(self, pdf_bytes: bytes, jso_useful_key: dict, image_writer: DataWriter, is_debug: bool = False,
16
17
                 start_page_id=0, end_page_id=None, lang=None,
                 layout_model=None, formula_enable=None, table_enable=None):
18
19
        self.pdf_type = jso_useful_key['_pdf_type']
        super().__init__(pdf_bytes, jso_useful_key['model_list'], image_writer, is_debug, start_page_id, end_page_id,
20
                         lang, layout_model, formula_enable, table_enable)
21
22
23
24
        if len(self.model_list) == 0:
            self.input_model_is_empty = True
        else:
            self.input_model_is_empty = False
25

赵小蒙's avatar
赵小蒙 committed
26
    def pipe_classify(self):
27
        self.pdf_type = AbsPipe.classify(self.pdf_bytes)
28

29
30
    def pipe_analyze(self):
        if self.pdf_type == self.PIP_TXT:
31
            self.model_list = doc_analyze(self.pdf_bytes, ocr=False,
32
                                          start_page_id=self.start_page_id, end_page_id=self.end_page_id,
33
34
                                          lang=self.lang, layout_model=self.layout_model,
                                          formula_enable=self.formula_enable, table_enable=self.table_enable)
35
        elif self.pdf_type == self.PIP_OCR:
36
            self.model_list = doc_analyze(self.pdf_bytes, ocr=True,
37
                                          start_page_id=self.start_page_id, end_page_id=self.end_page_id,
38
39
                                          lang=self.lang, layout_model=self.layout_model,
                                          formula_enable=self.formula_enable, table_enable=self.table_enable)
40

赵小蒙's avatar
赵小蒙 committed
41
    def pipe_parse(self):
kernel.h@qq.com's avatar
kernel.h@qq.com committed
42
        if self.pdf_type == self.PIP_TXT:
赵小蒙's avatar
赵小蒙 committed
43
            self.pdf_mid_data = parse_union_pdf(self.pdf_bytes, self.model_list, self.image_writer,
44
                                                is_debug=self.is_debug, input_model_is_empty=self.input_model_is_empty,
45
                                                start_page_id=self.start_page_id, end_page_id=self.end_page_id,
46
47
                                                lang=self.lang, layout_model=self.layout_model,
                                                formula_enable=self.formula_enable, table_enable=self.table_enable)
kernel.h@qq.com's avatar
kernel.h@qq.com committed
48
        elif self.pdf_type == self.PIP_OCR:
赵小蒙's avatar
赵小蒙 committed
49
            self.pdf_mid_data = parse_ocr_pdf(self.pdf_bytes, self.model_list, self.image_writer,
50
                                              is_debug=self.is_debug,
51
52
                                              start_page_id=self.start_page_id, end_page_id=self.end_page_id,
                                              lang=self.lang)
53

54
    def pipe_mk_uni_format(self, img_parent_path: str, drop_mode=DropMode.NONE_WITH_REASON):
55
        result = super().pipe_mk_uni_format(img_parent_path, drop_mode)
56
        logger.info('uni_pipe mk content list finished')
57
58
59
60
        return result

    def pipe_mk_markdown(self, img_parent_path: str, drop_mode=DropMode.WHOLE_PDF, md_make_mode=MakeMode.MM_MD):
        result = super().pipe_mk_markdown(img_parent_path, drop_mode, md_make_mode)
61
        logger.info(f'uni_pipe mk {md_make_mode} finished')
62
        return result
63

赵小蒙's avatar
赵小蒙 committed
64

65
66
if __name__ == '__main__':
    # 测试
67
68
    from magic_pdf.data.data_reader_writer import DataReader
    drw = DataReader(r'D:/project/20231108code-clean')
赵小蒙's avatar
赵小蒙 committed
69

70
71
72
73
    pdf_file_path = r'linshixuqiu\19983-00.pdf'
    model_file_path = r'linshixuqiu\19983-00.json'
    pdf_bytes = drw.read(pdf_file_path)
    model_json_txt = drw.read(model_file_path).decode()
赵小蒙's avatar
赵小蒙 committed
74
    model_list = json.loads(model_json_txt)
75
76
77
    write_path = r'D:\project\20231108code-clean\linshixuqiu\19983-00'
    img_bucket_path = 'imgs'
    img_writer = DataWriter(join_path(write_path, img_bucket_path))
赵小蒙's avatar
赵小蒙 committed
78

79
80
81
82
83
84
85
    # pdf_type = UNIPipe.classify(pdf_bytes)
    # jso_useful_key = {
    #     "_pdf_type": pdf_type,
    #     "model_list": model_list
    # }

    jso_useful_key = {
86
87
        '_pdf_type': '',
        'model_list': model_list
88
89
    }
    pipe = UNIPipe(pdf_bytes, jso_useful_key, img_writer)
赵小蒙's avatar
赵小蒙 committed
90
91
    pipe.pipe_classify()
    pipe.pipe_parse()
92
93
    md_content = pipe.pipe_mk_markdown(img_bucket_path)
    content_list = pipe.pipe_mk_uni_format(img_bucket_path)
赵小蒙's avatar
赵小蒙 committed
94

95
96
97
98
    md_writer = DataWriter(write_path)
    md_writer.write_string('19983-00.md', md_content)
    md_writer.write_string('19983-00.json', json.dumps(pipe.pdf_mid_data, ensure_ascii=False, indent=4))
    md_writer.write_string('19983-00.txt', str(content_list))