File size: 18,580 Bytes
dc61d35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
2b11880
dc61d35
 
 
2b11880
dc61d35
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
 
 
2b11880
 
dc61d35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
2b11880
dc61d35
 
 
2b11880
 
dc61d35
 
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b11880
 
dc61d35
 
 
2b11880
 
 
 
 
 
 
 
 
 
dc61d35
 
 
 
 
 
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
 
2b11880
 
 
 
dc61d35
 
 
 
 
2b11880
 
 
dc61d35
2b11880
 
 
dc61d35
2b11880
 
 
 
 
 
 
 
dc61d35
 
 
 
2b11880
dc61d35
 
 
2b11880
dc61d35
 
 
2b11880
dc61d35
 
2b11880
dc61d35
2b11880
 
dc61d35
 
2b11880
dc61d35
 
2b11880
 
 
dc61d35
 
 
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
 
 
 
 
 
 
2b11880
 
 
 
 
 
 
 
 
dc61d35
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
 
 
2b11880
dc61d35
 
2b11880
dc61d35
 
2b11880
dc61d35
 
 
2b11880
dc61d35
2b11880
dc61d35
 
 
 
 
 
 
2b11880
dc61d35
2b11880
dc61d35
 
 
2b11880
dc61d35
 
 
2b11880
dc61d35
 
2b11880
dc61d35
 
 
 
 
 
 
 
 
2b11880
 
dc61d35
 
 
 
2b11880
dc61d35
 
 
2b11880
 
dc61d35
 
 
 
 
 
 
 
 
 
 
 
2b11880
dc61d35
 
 
 
 
2b11880
dc61d35
 
 
2b11880
dc61d35
 
 
2b11880
dc61d35
2b11880
 
 
dc61d35
 
 
 
 
 
 
 
 
2b11880
dc61d35
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import os
import json
import shutil
import argparse
import logging
import multiprocessing as mp
from concurrent.futures import ProcessPoolExecutor, as_completed
import torch
import psutil
import numpy as np
from tqdm import tqdm
from magic_pdf.pipe.UNIPipe import UNIPipe
from magic_pdf.libs.commons import read_file
from magic_pdf.libs.config_reader import get_device
from magic_pdf.tools.common import do_parse
from magic_pdf.libs.pdf_image_tools import cut_image
from magic_pdf.rw.DiskReaderWriter import DiskReaderWriter
from magic_pdf.filter.pdf_meta_scan import pdf_meta_scan
from magic_pdf.filter.pdf_classify_by_type import classify
import fitz  # PyMuPDF
import time
import signal
import traceback

# Set up logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# Minimum batch size
MIN_BATCH_SIZE = 1


def parse_arguments():
    parser = argparse.ArgumentParser(description="Process multiple PDFs using Magic PDF")
    parser.add_argument("--input", default="input", help="Input folder containing PDF files")
    parser.add_argument("--output", default="output", help="Output folder for processed files")
    parser.add_argument("--config", default="magic-pdf.template.json", help="Path to configuration file")
    parser.add_argument("--timeout", type=int, default=240, help="Timeout for processing each PDF (in seconds)")
    parser.add_argument("--max-workers", type=int, default=None, help="Maximum number of worker processes")
    parser.add_argument("--use-bf16", action="store_true", help="Use bfloat16 precision for model inference")
    parser.add_argument("--initial-batch-size", type=int, default=1, help="Initial batch size for processing")
    return parser.parse_args()


def load_config(config_path):
    with open(config_path, 'r') as f:
        return json.load(f)


def get_available_memory(gpu_id):
    return torch.cuda.get_device_properties(gpu_id).total_memory - torch.cuda.memory_allocated(gpu_id)


def extract_images(pdf_path, output_folder):
    doc = fitz.open(pdf_path)
    pdf_name = os.path.splitext(os.path.basename(pdf_path))[0]
    images_folder = os.path.join(output_folder, 'images')
    os.makedirs(images_folder, exist_ok=True)

    for page_num, page in enumerate(doc):
        for img_index, img in enumerate(page.get_images(full=True)):
            xref = img[0]
            base_image = doc.extract_image(xref)
            image_bytes = base_image["image"]
            image_ext = base_image["ext"]
            image_filename = f'{pdf_name}_{page_num + 1:03d}_{img_index + 1:03d}.{image_ext}'
            image_path = os.path.join(images_folder, image_filename)
            with open(image_path, "wb") as image_file:
                image_file.write(image_bytes)
    doc.close()


class MagicModel:
    def __init__(self, config):
        self.config = config

    def process_pdf(self, pdf_data, parse_type, layout_info, log_file_path):
        processed_pages = []
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Entering process_pdf\n")
            log_file.write(f"  parse_type: {parse_type}, (expected: str)\n")
            log_file.write(
                f"  layout_info (length: {len(layout_info)}), (expected: list of dicts): {layout_info}\n")
        for page_index, page_info in enumerate(layout_info):
            try:
                with open(log_file_path, 'a') as log_file:
                    log_file.write(f"Processing page {page_index}\n")
                    log_file.write(f"  Page info (expected: dict): {page_info}\n")
                processed_page = self.process_page(page_info, parse_type)
                processed_pages.append(processed_page)
            except Exception as e:
                with open(log_file_path, 'a') as log_file:
                    log_file.write(f"Error processing page {page_index} in process_pdf: {str(e)}\n")
                    log_file.write(f"Page info (expected: dict): {page_info}\n")
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Exiting process_pdf\n")
        return {
            "processed_pages": processed_pages,
            "parse_type": parse_type,
        }

    def process_page(self, page_info, parse_type):
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Entering process_page\n")
            log_file.write(f"  page_info (expected: dict): {page_info}\n")
            log_file.write(f"  parse_type (expected: str): {parse_type}\n")
        result = {
            "page_no": page_info.get("page_info", {}).get("page_no", "unknown"),
            "content": "Processed page content",
            "parse_type": parse_type
        }
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Exiting process_page\n")
        return result


def process_single_pdf(input_file, output_folder, gpu_id, config, timeout, use_bf16, model, log_file_path):
    start_time = time.time()
    pdf_name = os.path.splitext(os.path.basename(input_file))[0]
    output_subfolder = os.path.join(output_folder, pdf_name, 'auto')
    os.makedirs(output_subfolder, exist_ok=True)

    def timeout_handler(signum, frame):
        raise TimeoutError("PDF processing timed out")

    try:
        signal.signal(signal.SIGALRM, timeout_handler)
        signal.alarm(timeout)

        if gpu_id >= 0:
            torch.cuda.set_device(gpu_id)
            if use_bf16 and torch.cuda.is_bf16_supported():
                torch.set_default_dtype(torch.bfloat16)
            else:
                torch.set_default_dtype(torch.float32)
            torch.set_default_device(f'cuda:{gpu_id}')
        else:
            if use_bf16:
                torch.set_default_dtype(torch.bfloat16)
            else:
                torch.set_default_dtype(torch.float32)
            torch.set_default_device('cpu')

        pdf_data = read_file(input_file, 'rb')

        # Perform PDF metadata scan
        metadata = pdf_meta_scan(pdf_data)
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Processing PDF: {input_file}\n")
            log_file.write(f"Metadata (expected: dict): {json.dumps(metadata, indent=2)}\n")

        # Check if metadata indicates the PDF should be dropped
        if metadata.get("_need_drop", False):
            with open(log_file_path, 'a') as log_file:
                log_file.write(
                    f"Dropping PDF {input_file}: {metadata.get('_drop_reason', 'Unknown reason')}\n")
            return input_file, "Dropped", None

        # Check if all required fields are present in metadata
        required_fields = ['total_page', 'page_width_pts', 'page_height_pts', 'image_info_per_page',
                           'text_len_per_page', 'imgs_per_page', 'text_layout_per_page', 'invalid_chars']
        for field in required_fields:
            if field not in metadata:
                raise ValueError(f"Required field '{field}' not found in metadata for {input_file}")

        # Extract required fields for classify function
        total_page = metadata['total_page']
        page_width = metadata['page_width_pts']
        page_height = metadata['page_height_pts']
        img_sz_list = metadata['image_info_per_page']
        text_len_list = metadata['text_len_per_page']
        img_num_list = metadata['imgs_per_page']
        text_layout_list = metadata['text_layout_per_page']
        invalid_chars = metadata['invalid_chars']

        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Classify parameters:\n")
            log_file.write(f"  total_page (expected: int): {total_page}\n")
            log_file.write(f"  page_width (expected: int): {page_width}\n")
            log_file.write(f"  page_height (expected: int): {page_height}\n")
            log_file.write(f"  img_sz_list (expected: list of lists): {img_sz_list[:5]}...\n")
            log_file.write(f"  text_len_list (expected: list of ints): {text_len_list[:5]}...\n")
            log_file.write(f"  img_num_list (expected: list of ints): {img_num_list[:5]}...\n")
            log_file.write(
                f"  text_layout_list (expected: list of strs): {text_layout_list[:5]}...\n")
            log_file.write(f"  invalid_chars (expected: bool): {invalid_chars}\n")

        # Classify PDF
        is_text_pdf, classification_results = classify(
            total_page, page_width, page_height, img_sz_list[:total_page],
            text_len_list[:total_page], img_num_list[:total_page],
            text_layout_list[:len(text_layout_list)], invalid_chars
        )
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Classification Results:\n")
            log_file.write(f"  is_text_pdf (expected: bool): {is_text_pdf}\n")
            log_file.write(
                f"  classification_results (expected: dict): {classification_results}\n")

        image_writer = DiskReaderWriter(output_subfolder)
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Image writer initialized: {image_writer}\n")

        # Create jso_useful_key as a dictionary
        model_json = []  # Or load your model data here
        jso_useful_key = {"_pdf_type": "", "model_list": model_json}

        unipipe = UNIPipe(pdf_data, jso_useful_key, image_writer)
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"UNIPipe initialized: {unipipe}\n")

        parse_type = unipipe.pipe_classify()
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"pipe_classify result (expected: str): {parse_type}\n")

        # Add detailed logging for pipe_analyze inputs and output
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Detailed pipe_analyze Inputs for {input_file}:\n")
            log_file.write(f"  parse_type (expected: str): {parse_type}\n")
        layout_info = unipipe.pipe_analyze()
        with open(log_file_path, 'a') as log_file:
            log_file.write(
                f"pipe_analyze Results (expected: list of dicts, length: {len(layout_info)}): {layout_info}\n")

        # Use OCR if it's not classified as a text PDF
        if not is_text_pdf:
            parse_type = 'ocr'
        with open(log_file_path, 'a') as log_file:
            log_file.write(
                f"parse_type after OCR check (expected: str): {parse_type}\n")

        # Process the PDF using the model
        parse_result = model.process_pdf(pdf_data, parse_type, layout_info, log_file_path)
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Model process_pdf result (expected: dict): {parse_result}\n")

        markdown_content = unipipe.pipe_mk_markdown(parse_result)
        with open(log_file_path, 'a') as log_file:
            log_file.write(
                f"pipe_mk_markdown result (expected: str, length: {len(markdown_content)}): {markdown_content}\n")

        uni_format = unipipe.pipe_mk_uni_format(parse_result)
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"pipe_mk_uni_format result (expected: dict): {uni_format}\n")

        # Write markdown content
        with open(os.path.join(output_subfolder, f'{pdf_name}.md'), 'w', encoding='utf-8') as f:
            f.write(markdown_content)

        # Write middle.json
        with open(os.path.join(output_subfolder, 'middle.json'), 'w', encoding='utf-8') as f:
            json.dump(parse_result, f, ensure_ascii=False, indent=2)

        # Write model.json
        with open(os.path.join(output_subfolder, 'model.json'), 'w', encoding='utf-8') as f:
            json.dump(uni_format, f, ensure_ascii=False, indent=2)

        # Copy original PDF
        shutil.copy(input_file, os.path.join(output_subfolder, f'{pdf_name}.pdf'))

        # Generate layout.pdf and spans.pdf
        do_parse(input_file, parse_type, output_subfolder, draw_bbox=True)

        # Extract images
        extract_images(input_file, output_subfolder)

        processing_time = time.time() - start_time
        with open(log_file_path, 'a') as log_file:
            log_file.write(
                f"Successfully processed {input_file} on GPU {gpu_id} in {processing_time:.2f} seconds\n")

        # Prepare result for JSONL output
        result = {
            "file_name": pdf_name,
            "processing_time": processing_time,
            "parse_type": parse_type,
            "metadata": metadata,
            "classification": classification_results,
            "is_text_pdf": is_text_pdf
        }

        return input_file, "Success", result

    except ValueError as ve:
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Metadata error: {str(ve)}\n")
        return input_file, f"Metadata Error: {str(ve)}", None

    except TimeoutError:
        with open(log_file_path, 'a') as log_file:
            log_file.write(f"Processing timed out after {timeout} seconds\n")
        return input_file, "Timeout", None

    except Exception as e:
        # Save full traceback to a file
        traceback_file = os.path.join(output_folder, 'traceback.txt')
        with open(traceback_file, 'w') as f:
            f.write(traceback.format_exc())

        # Print error message and traceback location to CLI
        print(f"Error occurred: {e}")
        print(f"Full traceback saved to: {traceback_file}")
        exit(1)  # Terminate the script

    finally:
        signal.alarm(0)  # Cancel the alarm
        if gpu_id >= 0:
            torch.cuda.empty_cache()


def process_pdf_batch(batch, output_folder, gpu_id, config, timeout, use_bf16, model, log_file_path):
    results = []
    for pdf_file in batch:
        result = process_single_pdf(pdf_file, output_folder, gpu_id, config, timeout, use_bf16, model, log_file_path)
        results.append(result)
    return results


def write_to_jsonl(results, output_file):
    with open(output_file, 'a') as f:
        for result in results:
            if result[2]:  # Check if result is not None
                json.dump(result[2], f)
                f.write('\n')


def get_gpu_memory_usage(gpu_id):
    if gpu_id < 0:
        return 0, 0  # CPU mode
    total_memory = torch.cuda.get_device_properties(gpu_id).total_memory
    allocated_memory = torch.cuda.memory_allocated(gpu_id)
    return allocated_memory, total_memory


def main():
    mp.set_start_method('spawn', force=True)

    args = parse_arguments()
    config = load_config(args.config)

    input_folder = args.input
    output_folder = args.output
    os.makedirs(output_folder, exist_ok=True)

    pdf_files = [os.path.join(input_folder, f) for f in os.listdir(input_folder) if f.endswith('.pdf')]

    num_gpus = torch.cuda.device_count()
    if num_gpus == 0:
        print("No GPUs available. Using CPU.")
        num_gpus = 1
        gpu_ids = [-1]
    else:
        gpu_ids = list(range(num_gpus))

    num_workers = args.max_workers or min(num_gpus, os.cpu_count())

    main_jsonl = os.path.join(output_folder, 'processing_results.jsonl')
    temp_jsonl = os.path.join(output_folder, 'temp_results.jsonl')
    log_file_path = os.path.join(output_folder, 'processing_log.txt')

    # Enable deterministic mode
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    # Load the model
    model = MagicModel(config)

    results = []
    with ProcessPoolExecutor(max_workers=num_workers) as executor:
        for gpu_id in gpu_ids:
            batch_size = args.initial_batch_size
            pdf_index = 0
            oom_occurred = False
            while pdf_index < len(pdf_files):
                batch = pdf_files[pdf_index:pdf_index + batch_size]
                try:
                    future = executor.submit(process_pdf_batch, batch, output_folder, gpu_id, config, args.timeout,
                                            args.use_bf16, model, log_file_path)
                    batch_results = future.result()
                    results.extend(batch_results)
                    for result in batch_results:
                        write_to_jsonl([result], temp_jsonl)

                    # Print VRAM usage
                    allocated, total = get_gpu_memory_usage(gpu_id)
                    with open(log_file_path, 'a') as log_file:
                        log_file.write(
                            f"GPU {gpu_id} - Batch size: {batch_size}, VRAM usage: {allocated / 1024 ** 3:.2f}GB / {total / 1024 ** 3:.2f}GB\n")
                    # If successful and OOM hasn't occurred yet, increase batch size
                    if not oom_occurred:
                        batch_size += 1
                    pdf_index += len(batch)
                except torch.cuda.OutOfMemoryError:
                    # If OOM occurs, reduce batch size
                    oom_occurred = True
                    batch_size = max(MIN_BATCH_SIZE, batch_size - 1)
                    with open(log_file_path, 'a') as log_file:
                        log_file.write(f"OOM error occurred. Reducing batch size to {batch_size}\n")
                    torch.cuda.empty_cache()
                    continue

                # After processing each batch, move temp JSONL to main JSONL
                if os.path.exists(temp_jsonl):
                    with open(temp_jsonl, 'r') as temp, open(main_jsonl, 'a') as main:
                        shutil.copyfileobj(temp, main)
                    os.remove(temp_jsonl)

                # Clear GPU cache after each batch
                if gpu_id >= 0:
                    torch.cuda.empty_cache()

    success_count = sum(1 for _, status, _ in results if status == "Success")
    timeout_count = sum(1 for _, status, _ in results if status == "Timeout")
    error_count = len(results) - success_count - timeout_count

    with open(log_file_path, 'a') as log_file:
        log_file.write(
            f"Processed {len(results)} PDFs. {success_count} succeeded, {timeout_count} timed out, {error_count} failed.\n")

    with open(os.path.join(output_folder, 'processing_summary.txt'), 'w') as summary:
        summary.write(f"Total PDFs processed: {len(results)}\n")
        summary.write(f"Successful: {success_count}\n")
        summary.write(f"Timed out: {timeout_count}\n")
        summary.write(f"Failed: {error_count}\n\n")
        summary.write("Failed PDFs:\n")
        for pdf, status, _ in [result for result in results if result[1] != "Success"]:
            summary.write(f"  - {pdf}: {status}\n")


if __name__ == '__main__':
    main()