user_api.py 3.9 KB
Newer Older
赵小蒙's avatar
赵小蒙 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
"""
用户输入:
    model数组,每个元素代表一个页面
    pdf在s3的路径
    截图保存的s3位置

然后:
    1)根据s3路径,调用spark集群的api,拿到ak,sk,endpoint,构造出s3PDFReader
    2)根据用户输入的s3地址,调用spark集群的api,拿到ak,sk,endpoint,构造出s3ImageWriter

其余部分至于构造s3cli, 获取ak,sk都在code-clean里写代码完成。不要反向依赖!!!

"""
14
15
import re

赵小蒙's avatar
赵小蒙 committed
16
17
from loguru import logger

赵小蒙's avatar
赵小蒙 committed
18
from magic_pdf.libs.version import __version__
赵小蒙's avatar
赵小蒙 committed
19
from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
kernel.h@qq.com's avatar
kernel.h@qq.com committed
20
from magic_pdf.rw import AbsReaderWriter
赵小蒙's avatar
赵小蒙 committed
21
22
from magic_pdf.pdf_parse_by_ocr import parse_pdf_by_ocr
from magic_pdf.pdf_parse_by_txt import parse_pdf_by_txt
赵小蒙's avatar
赵小蒙 committed
23

kernel.h@qq.com's avatar
kernel.h@qq.com committed
24
25
26
PARSE_TYPE_TXT = "txt"
PARSE_TYPE_OCR = "ocr"

27

28
def parse_txt_pdf(pdf_bytes: bytes, pdf_models: list, imageWriter: AbsReaderWriter, is_debug=False,
29
                  start_page_id=0, end_page_id=None, lang=None,
30
                  *args, **kwargs):
赵小蒙's avatar
赵小蒙 committed
31
32
33
34
35
36
37
    """
    解析文本类pdf
    """
    pdf_info_dict = parse_pdf_by_txt(
        pdf_bytes,
        pdf_models,
        imageWriter,
38
39
        start_page_id=start_page_id,
        end_page_id=end_page_id,
赵小蒙's avatar
赵小蒙 committed
40
41
42
        debug_mode=is_debug,
    )

kernel.h@qq.com's avatar
kernel.h@qq.com committed
43
    pdf_info_dict["_parse_type"] = PARSE_TYPE_TXT
赵小蒙's avatar
赵小蒙 committed
44

赵小蒙's avatar
赵小蒙 committed
45
    pdf_info_dict["_version_name"] = __version__
赵小蒙's avatar
赵小蒙 committed
46

47
48
49
    if lang is not None:
        pdf_info_dict["_lang"] = lang

赵小蒙's avatar
赵小蒙 committed
50
51
52
    return pdf_info_dict


53
def parse_ocr_pdf(pdf_bytes: bytes, pdf_models: list, imageWriter: AbsReaderWriter, is_debug=False,
54
                  start_page_id=0, end_page_id=None, lang=None,
55
                  *args, **kwargs):
赵小蒙's avatar
赵小蒙 committed
56
57
58
59
60
61
62
    """
    解析ocr类pdf
    """
    pdf_info_dict = parse_pdf_by_ocr(
        pdf_bytes,
        pdf_models,
        imageWriter,
63
64
        start_page_id=start_page_id,
        end_page_id=end_page_id,
赵小蒙's avatar
赵小蒙 committed
65
66
67
        debug_mode=is_debug,
    )

kernel.h@qq.com's avatar
kernel.h@qq.com committed
68
    pdf_info_dict["_parse_type"] = PARSE_TYPE_OCR
赵小蒙's avatar
赵小蒙 committed
69

赵小蒙's avatar
赵小蒙 committed
70
    pdf_info_dict["_version_name"] = __version__
赵小蒙's avatar
赵小蒙 committed
71

72
73
74
    if lang is not None:
        pdf_info_dict["_lang"] = lang

赵小蒙's avatar
赵小蒙 committed
75
76
77
    return pdf_info_dict


78
def parse_union_pdf(pdf_bytes: bytes, pdf_models: list, imageWriter: AbsReaderWriter, is_debug=False,
79
                    input_model_is_empty: bool = False,
80
                    start_page_id=0, end_page_id=None, lang=None,
赵小蒙's avatar
赵小蒙 committed
81
82
83
84
85
86
87
88
89
90
91
                    *args, **kwargs):
    """
    ocr和文本混合的pdf,全部解析出来
    """

    def parse_pdf(method):
        try:
            return method(
                pdf_bytes,
                pdf_models,
                imageWriter,
92
93
                start_page_id=start_page_id,
                end_page_id=end_page_id,
赵小蒙's avatar
赵小蒙 committed
94
95
96
                debug_mode=is_debug,
            )
        except Exception as e:
97
            logger.exception(e)
赵小蒙's avatar
赵小蒙 committed
98
99
100
            return None

    pdf_info_dict = parse_pdf(parse_pdf_by_txt)
赵小蒙's avatar
赵小蒙 committed
101
    if pdf_info_dict is None or pdf_info_dict.get("_need_drop", False):
102
        logger.warning(f"parse_pdf_by_txt drop or error, switch to parse_pdf_by_ocr")
103
        if input_model_is_empty:
104
105
106
107
108
109
110
111
112
113
114
115
116
            layout_model = kwargs.get("layout_model", None)
            formula_enable = kwargs.get("formula_enable", None)
            table_enable = kwargs.get("table_enable", None)
            pdf_models = doc_analyze(
                pdf_bytes,
                ocr=True,
                start_page_id=start_page_id,
                end_page_id=end_page_id,
                lang=lang,
                layout_model=layout_model,
                formula_enable=formula_enable,
                table_enable=table_enable,
            )
赵小蒙's avatar
赵小蒙 committed
117
118
119
120
        pdf_info_dict = parse_pdf(parse_pdf_by_ocr)
        if pdf_info_dict is None:
            raise Exception("Both parse_pdf_by_txt and parse_pdf_by_ocr failed.")
        else:
kernel.h@qq.com's avatar
kernel.h@qq.com committed
121
            pdf_info_dict["_parse_type"] = PARSE_TYPE_OCR
赵小蒙's avatar
赵小蒙 committed
122
    else:
kernel.h@qq.com's avatar
kernel.h@qq.com committed
123
        pdf_info_dict["_parse_type"] = PARSE_TYPE_TXT
赵小蒙's avatar
赵小蒙 committed
124

赵小蒙's avatar
赵小蒙 committed
125
    pdf_info_dict["_version_name"] = __version__
赵小蒙's avatar
赵小蒙 committed
126

127
128
129
    if lang is not None:
        pdf_info_dict["_lang"] = lang

赵小蒙's avatar
赵小蒙 committed
130
    return pdf_info_dict