test_cli_sdk.py 15.8 KB
Newer Older
yyy's avatar
yyy committed
1
2
3
4
5
6
"""test cli and sdk."""
import logging
import os
import pytest
from conf import conf
from lib import common
yyy's avatar
yyy committed
7
import time
yyy's avatar
yyy committed
8
9
import magic_pdf.model as model_config
from magic_pdf.pipe.UNIPipe import UNIPipe
dt-yy's avatar
dt-yy committed
10
11
12
13
14
import os
from magic_pdf.data.data_reader_writer import FileBasedDataWriter
from magic_pdf.data.data_reader_writer import S3DataReader, S3DataWriter
from magic_pdf.config.make_content_config import DropMode, MakeMode
from magic_pdf.pipe.OCRPipe import OCRPipe
dt-yy's avatar
dt-yy committed
15
16
17
18
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
yyy's avatar
yyy committed
19
20
21
22
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']
quyuan's avatar
quyuan committed
23
magic_pdf_config = "/home/quyuan/magic-pdf.json"
yyy's avatar
yyy committed
24
25
26

class TestCli:
    """test cli."""
quyuan's avatar
quyuan committed
27
28
29
30
31
32
33
34
35
    @pytest.fixture(autouse=True)
    def setup(self):
        """
        init
        """
        common.clear_gpu_memory()
        common.update_config_file(magic_pdf_config, "device-mode", "cuda")
        # 这里可以添加任何前置操作
        yield
yyy's avatar
yyy committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49

    @pytest.mark.P0
    def test_pdf_auto_sdk(self):
        """pdf sdk auto 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))
            dir_path = os.path.join(pdf_dev_path, 'mineru')
dt-yy's avatar
dt-yy committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
            image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(dir_path)
            reader1 = FileBasedDataReader("")
            pdf_bytes = reader1.read(pdf_path)
            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)
            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)
            common.sdk_count_folders_and_check_contents(dir_path)
yyy's avatar
yyy committed
70
71
72
73

    @pytest.mark.P0
    def test_pdf_cli_auto(self):
        """magic_pdf cli test auto."""
yyy's avatar
yyy committed
74
        time.sleep(2)
yyy's avatar
yyy committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        demo_names = []
        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:
            res_path = os.path.join(pdf_dev_path, 'mineru')
            common.delete_file(res_path)
            cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
                pdf_path, f'{demo_name}.pdf'), res_path, 'auto')
            logging.info(cmd)
            os.system(cmd)
            common.cli_count_folders_and_check_contents(
                os.path.join(res_path, demo_name, 'auto'))
dt-yy's avatar
dt-yy committed
89
  
yyy's avatar
yyy committed
90
    @pytest.mark.P0
yyy's avatar
yyy committed
91
    def test_pdf_cli_txt(self):
yyy's avatar
yyy committed
92
        """magic_pdf cli test txt."""
yyy's avatar
yyy committed
93
        time.sleep(2)
yyy's avatar
yyy committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        demo_names = []
        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:
            res_path = os.path.join(pdf_dev_path, 'mineru')
            common.delete_file(res_path)
            cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
                pdf_path, f'{demo_name}.pdf'), res_path, 'txt')
            logging.info(cmd)
            os.system(cmd)
            common.cli_count_folders_and_check_contents(
                os.path.join(res_path, demo_name, 'txt'))
yyy's avatar
yyy committed
108
   
yyy's avatar
yyy committed
109
    @pytest.mark.P0
yyy's avatar
yyy committed
110
    def test_pdf_cli_ocr(self):
yyy's avatar
yyy committed
111
        """magic_pdf cli test ocr."""
yyy's avatar
yyy committed
112
        time.sleep(2)
yyy's avatar
yyy committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        demo_names = []
        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:
            res_path = os.path.join(pdf_dev_path, 'mineru')
            common.delete_file(res_path)
            cmd = 'magic-pdf -p %s -o %s -m %s' % (os.path.join(
                pdf_path, f'{demo_name}.pdf'), res_path, 'ocr')
            logging.info(cmd)
            os.system(cmd)
            common.cli_count_folders_and_check_contents(
                os.path.join(res_path, demo_name, 'ocr'))
yyy's avatar
yyy committed
127
128
    
    @pytest.mark.skip(reason='out-of-date api')
quyuan's avatar
quyuan committed
129
130
131
    @pytest.mark.P1
    def test_pdf_dev_cli_local_jsonl_txt(self):
        """magic_pdf_dev cli local txt."""
yyy's avatar
yyy committed
132
        time.sleep(2)
quyuan's avatar
quyuan committed
133
        jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
quyuan's avatar
quyuan committed
134
        cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, "txt")
quyuan's avatar
quyuan committed
135
136
137
        logging.info(cmd)
        os.system(cmd)

yyy's avatar
yyy committed
138
    @pytest.mark.skip(reason='out-of-date api')
quyuan's avatar
quyuan committed
139
140
141
    @pytest.mark.P1
    def test_pdf_dev_cli_local_jsonl_ocr(self):
        """magic_pdf_dev cli local ocr."""
yyy's avatar
yyy committed
142
        time.sleep(2)
quyuan's avatar
quyuan committed
143
        jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
quyuan's avatar
quyuan committed
144
        cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'ocr')
quyuan's avatar
quyuan committed
145
146
147
        logging.info(cmd)
        os.system(cmd)

yyy's avatar
yyy committed
148
    @pytest.mark.skip(reason='out-of-date api')
quyuan's avatar
quyuan committed
149
150
151
    @pytest.mark.P1
    def test_pdf_dev_cli_local_jsonl_auto(self):
        """magic_pdf_dev cli local auto."""
yyy's avatar
yyy committed
152
        time.sleep(2)
quyuan's avatar
quyuan committed
153
        jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
quyuan's avatar
quyuan committed
154
        cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'auto')
quyuan's avatar
quyuan committed
155
156
        logging.info(cmd)
        os.system(cmd)
yyy's avatar
yyy committed
157
158
    
    @pytest.mark.skip(reason='out-of-date api')
quyuan's avatar
quyuan committed
159
160
161
    @pytest.mark.P1
    def test_pdf_dev_cli_s3_jsonl_txt(self):
        """magic_pdf_dev cli s3 txt."""
yyy's avatar
yyy committed
162
        time.sleep(2)
quyuan's avatar
quyuan committed
163
        jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
quyuan's avatar
quyuan committed
164
        cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, "txt")
quyuan's avatar
quyuan committed
165
166
167
        logging.info(cmd)
        os.system(cmd)

yyy's avatar
yyy committed
168
    @pytest.mark.skip(reason='out-of-date api')
quyuan's avatar
quyuan committed
169
170
171
    @pytest.mark.P1
    def test_pdf_dev_cli_s3_jsonl_ocr(self):
        """magic_pdf_dev cli s3 ocr."""
yyy's avatar
yyy committed
172
        time.sleep(2)
quyuan's avatar
quyuan committed
173
        jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
quyuan's avatar
quyuan committed
174
        cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'ocr')
quyuan's avatar
quyuan committed
175
176
177
        logging.info(cmd)
        os.system(cmd)

yyy's avatar
yyy committed
178
    @pytest.mark.skip(reason='out-of-date api')
quyuan's avatar
quyuan committed
179
180
181
    @pytest.mark.P1
    def test_pdf_dev_cli_s3_jsonl_auto(self):
        """magic_pdf_dev cli s3 auto."""
yyy's avatar
yyy committed
182
        time.sleep(2)
quyuan's avatar
quyuan committed
183
184
185
186
187
        jsonl_path = os.path.join(pdf_dev_path, 'line1.jsonl')
        cmd = 'magic-pdf-dev --jsonl %s --method %s' % (jsonl_path, 'auto')
        logging.info(cmd)
        os.system(cmd)

quyuan's avatar
quyuan committed
188
189
190
    @pytest.mark.P1
    def test_pdf_dev_cli_pdf_json_auto(self):
        """magic_pdf_dev cli pdf+json auto."""
yyy's avatar
yyy committed
191
        time.sleep(2)
quyuan's avatar
quyuan committed
192
        json_path = os.path.join(pdf_dev_path, 'test_model.json')
yyy's avatar
yyy committed
193
        pdf_path = os.path.join(pdf_dev_path, 'pdf', 'test_rearch_report.pdf')
quyuan's avatar
quyuan committed
194
195
196
        cmd = 'magic-pdf-dev --pdf %s --json %s --method %s' % (pdf_path, json_path, 'auto')
        logging.info(cmd)
        os.system(cmd)
yyy's avatar
yyy committed
197
198
   
    @pytest.mark.skip(reason='out-of-date api')
quyuan's avatar
quyuan committed
199
200
201
    @pytest.mark.P1
    def test_pdf_dev_cli_pdf_json_ocr(self):
        """magic_pdf_dev cli pdf+json ocr."""
yyy's avatar
yyy committed
202
        time.sleep(2)
quyuan's avatar
quyuan committed
203
        json_path = os.path.join(pdf_dev_path, 'test_model.json')
yyy's avatar
yyy committed
204
        pdf_path = os.path.join(pdf_dev_path, 'pdf', 'test_rearch_report.pdf')
quyuan's avatar
quyuan committed
205
206
207
        cmd = 'magic-pdf-dev --pdf %s --json %s --method %s' % (pdf_path, json_path, 'auto')
        logging.info(cmd)
        os.system(cmd)
dt-yy's avatar
dt-yy committed
208
209
   

quyuan's avatar
quyuan committed
210
    @pytest.mark.P1
dt-yy's avatar
dt-yy committed
211
    def test_s3_sdk_auto(self):
yyy's avatar
yyy committed
212
213
214
215
216
217
        """
        test s3 sdk auto.
        """
        time.sleep(2)
        pdf_ak = os.getenv('pdf_ak')
        print (pdf_ak)
quyuan's avatar
quyuan committed
218
219
220
221
        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"]
yyy's avatar
yyy committed
222
        image_dir = "s3://" + pdf_bucket + "/mineru/test/output"
dt-yy's avatar
dt-yy committed
223
224
        prefix = "mineru/test/output"
        reader = S3DataReader(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
dt-yy's avatar
dt-yy committed
225
        writer = S3DataWriter(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
dt-yy's avatar
dt-yy committed
226
227
        # = S3DataWriter(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
        image_writer = S3DataWriter(prefix, pdf_bucket, pdf_ak, pdf_sk, pdf_endpoint)
dt-yy's avatar
dt-yy committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        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)

quyuan's avatar
quyuan committed
265

yyy's avatar
yyy committed
266
267
268
269
    @pytest.mark.P1
    def test_local_magic_pdf_open_st_table(self):
        """magic pdf cli open st table."""
        time.sleep(2)
quyuan's avatar
quyuan committed
270
271
272
273
274
275
276
        #pre_cmd = "cp ~/magic_pdf_st.json ~/magic-pdf.json"
        value = {
        "model": "struct_eqtable",
        "enable": True,
        "max_time": 400
        }   
        common.update_config_file(magic_pdf_config, "table-config", value)
yyy's avatar
yyy committed
277
278
279
280
        pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
        common.delete_file(pdf_res_path)
        cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
        os.system(cli_cmd)
quyuan's avatar
quyuan committed
281
        res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
yyy's avatar
yyy committed
282
283
284
        assert res is True
  
    @pytest.mark.P1
quyuan's avatar
quyuan committed
285
286
    def test_local_magic_pdf_open_tablemaster_cuda(self):
        """magic pdf cli open table master html table cuda mode."""
yyy's avatar
yyy committed
287
        time.sleep(2)
quyuan's avatar
quyuan committed
288
289
290
291
292
293
294
295
        #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
        #os.system(pre_cmd)
        value = {
        "model": "tablemaster",
        "enable": True,
        "max_time": 400
        }   
        common.update_config_file(magic_pdf_config, "table-config", value)
yyy's avatar
yyy committed
296
297
298
299
300
301
302
303
        pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
        common.delete_file(pdf_res_path)
        cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
        os.system(cli_cmd)
        res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
        assert res is True
    
    @pytest.mark.P1
quyuan's avatar
quyuan committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
    def test_local_magic_pdf_open_rapidai_table(self):
        """magic pdf cli open rapid ai table."""
        time.sleep(2)
        #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
        #os.system(pre_cmd)
        value = {
        "model": "rapid_table",
        "enable": True,
        "max_time": 400
        }   
        common.update_config_file(magic_pdf_config, "table-config", value)
        pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
        common.delete_file(pdf_res_path)
        cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
        os.system(cli_cmd)
        res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
        assert res is True
    
    
    @pytest.mark.P1
    def test_local_magic_pdf_doclayout_yolo(self):
        """magic pdf cli open doclyaout yolo."""
        time.sleep(2)
        #pre_cmd = "cp ~/magic_pdf_html.json ~/magic-pdf.json"
        #os.system(pre_cmd)
        value = {
        "model": "doclayout_yolo"
        }   
        common.update_config_file(magic_pdf_config, "layout-config", value)
        pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
        common.delete_file(pdf_res_path)
        cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
        os.system(cli_cmd)
        common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))

    @pytest.mark.P1
    def test_local_magic_pdf_layoutlmv3_yolo(self):
        """magic pdf cli open layoutlmv3."""
        time.sleep(2)
        value = {
        "model": "layoutlmv3"
        }   
        common.update_config_file(magic_pdf_config, "layout-config", value)
        pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
        common.delete_file(pdf_res_path)
        cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
        os.system(cli_cmd)
        common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
        #res = common.check_html_table_exists(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))

    @pytest.mark.P1
quyuan's avatar
quyuan committed
355
356
    def test_magic_pdf_cpu(self):
        """magic pdf cli cpu mode."""
yyy's avatar
yyy committed
357
        time.sleep(2)
quyuan's avatar
quyuan committed
358
359
360
361
        #pre_cmd = "cp ~/magic_pdf_html_table_cpu.json ~/magic-pdf.json"
        #os.system(pre_cmd)
        value = {
        "model": "tablemaster",
quyuan's avatar
quyuan committed
362
        "enable": False,
quyuan's avatar
quyuan committed
363
364
365
366
        "max_time": 400
        }   
        common.update_config_file(magic_pdf_config, "table-config", value)
        common.update_config_file(magic_pdf_config, "device-mode", "cpu")
yyy's avatar
yyy committed
367
368
369
370
        pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
        common.delete_file(pdf_res_path)
        cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
        os.system(cli_cmd)
quyuan's avatar
quyuan committed
371
        common.cli_count_folders_and_check_contents(os.path.join(pdf_res_path, "test_rearch_report", "auto"))
yyy's avatar
yyy committed
372

quyuan's avatar
quyuan committed
373

yyy's avatar
yyy committed
374
375
376
377
    @pytest.mark.P1
    def test_local_magic_pdf_close_html_table(self):
        """magic pdf cli close table."""
        time.sleep(2)
quyuan's avatar
quyuan committed
378
379
380
381
382
383
384
385
        #pre_cmd = "cp ~/magic_pdf_close_table.json ~/magic-pdf.json"
        #os.system(pre_cmd)
        value = {
        "model": "tablemaster",
        "enable": False,
        "max_time": 400
        }   
        common.update_config_file(magic_pdf_config, "table-config", value)
yyy's avatar
yyy committed
386
387
388
389
390
391
392
        pdf_path = os.path.join(pdf_dev_path, "pdf", "test_rearch_report.pdf")
        common.delete_file(pdf_res_path)
        cli_cmd = "magic-pdf -p %s -o %s" % (pdf_path, pdf_res_path)
        os.system(cli_cmd)
        res = common.check_close_tables(os.path.join(pdf_res_path, "test_rearch_report", "auto", "test_rearch_report.md"))
        assert res is True
    
quyuan's avatar
quyuan committed
393

yyy's avatar
yyy committed
394
 
yyy's avatar
yyy committed
395
396
if __name__ == '__main__':
    pytest.main()
dt-yy's avatar
dt-yy committed
397