From cedd4a80cfd86afd0e7f23424a3ffd078d52d8c5 Mon Sep 17 00:00:00 2001 From: Jake Poznanski Date: Fri, 19 Sep 2025 16:57:05 +0000 Subject: [PATCH] Fixing paddle ocr to run fast --- olmocr/bench/runners/run_paddlepaddle.py | 33 ++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/olmocr/bench/runners/run_paddlepaddle.py b/olmocr/bench/runners/run_paddlepaddle.py index 425b31b..2667df3 100644 --- a/olmocr/bench/runners/run_paddlepaddle.py +++ b/olmocr/bench/runners/run_paddlepaddle.py @@ -1,8 +1,4 @@ -import json -import os -from typing import Literal - -from openai import OpenAI +from threading import Lock from paddleocr import PPStructureV3 @@ -10,24 +6,29 @@ from paddleocr import PPStructureV3 # Run's paddle paddle as in the docs here: https://huggingface.co/PaddlePaddle/PP-OCRv5_server_det # text_detection_model_name="PP-OCRv5_server_det", # and using the PP-StructureV3 pipeline to create markdown - +paddle_pipeline = None +paddle_pipeline_lock = Lock() def run_paddlepaddle( pdf_path: str, page_num: int = 1, **kwargs ) -> str: - pipeline = PPStructureV3( - text_detection_model_name="PP-OCRv5_server_det", - use_doc_orientation_classify=False, # Use use_doc_orientation_classify to enable/disable document orientation classification model - use_doc_unwarping=False, # Use use_doc_unwarping to enable/disable document unwarping module - use_textline_orientation=False, # Use use_textline_orientation to enable/disable textline orientation classification model - device="gpu:0", # Use device to specify GPU for model inference - ) - output = pipeline.predict(pdf_path) + global paddle_pipeline + + with paddle_pipeline_lock: + if paddle_pipeline is None: + paddle_pipeline = PPStructureV3( + text_detection_model_name="PP-OCRv5_server_det", + use_doc_orientation_classify=False, # Use use_doc_orientation_classify to enable/disable document orientation classification model + use_doc_unwarping=False, # Use use_doc_unwarping to enable/disable document unwarping module + use_textline_orientation=False, # Use use_textline_orientation to enable/disable textline orientation classification model + device="gpu:0", # Use device to specify GPU for model inference + ) + + output = paddle_pipeline.predict(pdf_path) for cur_page_0_indexed, res in enumerate(output): if cur_page_0_indexed == page_num - 1: - print(res.markdown) - return res.markdown + return res.markdown["markdown_texts"] raise ValueError(f"Did not get markdown for page {page_num}")