UNIPipe.py 4.89 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
from magic_pdf.data.data_reader_writer import DataWriter
7
from magic_pdf.data.dataset import Dataset
8
from magic_pdf.libs.commons import join_path
赵小蒙's avatar
赵小蒙 committed
9
from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
赵小蒙's avatar
赵小蒙 committed
10
from magic_pdf.pipe.AbsPipe import AbsPipe
11
from magic_pdf.user_api import parse_ocr_pdf, parse_union_pdf
12
13


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

16
17
18
19
20
21
22
23
24
25
26
27
28
    def __init__(
        self,
        dataset: Dataset,
        jso_useful_key: dict,
        image_writer: DataWriter,
        is_debug: bool = False,
        start_page_id=0,
        end_page_id=None,
        lang=None,
        layout_model=None,
        formula_enable=None,
        table_enable=None,
    ):
29
        self.pdf_type = jso_useful_key['_pdf_type']
30
31
32
33
34
35
36
37
38
39
40
41
        super().__init__(
            dataset,
            jso_useful_key['model_list'],
            image_writer,
            is_debug,
            start_page_id,
            end_page_id,
            lang,
            layout_model,
            formula_enable,
            table_enable,
        )
42
43
44
45
        if len(self.model_list) == 0:
            self.input_model_is_empty = True
        else:
            self.input_model_is_empty = False
46

赵小蒙's avatar
赵小蒙 committed
47
    def pipe_classify(self):
48
        self.pdf_type = AbsPipe.classify(self.pdf_bytes)
49

50
51
    def pipe_analyze(self):
        if self.pdf_type == self.PIP_TXT:
52
53
54
55
56
57
58
59
60
61
            self.model_list = doc_analyze(
                self.dataset,
                ocr=False,
                start_page_id=self.start_page_id,
                end_page_id=self.end_page_id,
                lang=self.lang,
                layout_model=self.layout_model,
                formula_enable=self.formula_enable,
                table_enable=self.table_enable,
            )
62
        elif self.pdf_type == self.PIP_OCR:
63
64
65
66
67
68
69
70
71
72
            self.model_list = doc_analyze(
                self.dataset,
                ocr=True,
                start_page_id=self.start_page_id,
                end_page_id=self.end_page_id,
                lang=self.lang,
                layout_model=self.layout_model,
                formula_enable=self.formula_enable,
                table_enable=self.table_enable,
            )
73

赵小蒙's avatar
赵小蒙 committed
74
    def pipe_parse(self):
kernel.h@qq.com's avatar
kernel.h@qq.com committed
75
        if self.pdf_type == self.PIP_TXT:
76
77
78
79
80
81
82
83
84
85
86
87
            self.pdf_mid_data = parse_union_pdf(
                self.dataset,
                self.model_list,
                self.image_writer,
                is_debug=self.is_debug,
                start_page_id=self.start_page_id,
                end_page_id=self.end_page_id,
                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
88
        elif self.pdf_type == self.PIP_OCR:
89
90
91
92
93
94
95
96
97
98
99
100
101
            self.pdf_mid_data = parse_ocr_pdf(
                self.dataset,
                self.model_list,
                self.image_writer,
                is_debug=self.is_debug,
                start_page_id=self.start_page_id,
                end_page_id=self.end_page_id,
                lang=self.lang,
            )

    def pipe_mk_uni_format(
        self, img_parent_path: str, drop_mode=DropMode.NONE_WITH_REASON
    ):
102
        result = super().pipe_mk_uni_format(img_parent_path, drop_mode)
103
        logger.info('uni_pipe mk content list finished')
104
105
        return result

106
107
108
109
110
111
    def pipe_mk_markdown(
        self,
        img_parent_path: str,
        drop_mode=DropMode.WHOLE_PDF,
        md_make_mode=MakeMode.MM_MD,
    ):
112
        result = super().pipe_mk_markdown(img_parent_path, drop_mode, md_make_mode)
113
        logger.info(f'uni_pipe mk {md_make_mode} finished')
114
        return result
115

赵小蒙's avatar
赵小蒙 committed
116

117
118
if __name__ == '__main__':
    # 测试
119
    from magic_pdf.data.data_reader_writer import DataReader
120

121
    drw = DataReader(r'D:/project/20231108code-clean')
赵小蒙's avatar
赵小蒙 committed
122

123
124
125
126
    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
127
    model_list = json.loads(model_json_txt)
128
129
130
    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
131

132
133
134
135
136
137
    # pdf_type = UNIPipe.classify(pdf_bytes)
    # jso_useful_key = {
    #     "_pdf_type": pdf_type,
    #     "model_list": model_list
    # }

138
    jso_useful_key = {'_pdf_type': '', 'model_list': model_list}
139
    pipe = UNIPipe(pdf_bytes, jso_useful_key, img_writer)
赵小蒙's avatar
赵小蒙 committed
140
141
    pipe.pipe_classify()
    pipe.pipe_parse()
142
143
    md_content = pipe.pipe_mk_markdown(img_bucket_path)
    content_list = pipe.pipe_mk_uni_format(img_bucket_path)
赵小蒙's avatar
赵小蒙 committed
144

145
146
    md_writer = DataWriter(write_path)
    md_writer.write_string('19983-00.md', md_content)
147
148
149
    md_writer.write_string(
        '19983-00.json', json.dumps(pipe.pdf_mid_data, ensure_ascii=False, indent=4)
    )
150
    md_writer.write_string('19983-00.txt', str(content_list))