fix: refact logic

This commit is contained in:
2025-12-31 17:38:32 +08:00
parent 6ac50f7d2f
commit 35928c2484
17 changed files with 678 additions and 738 deletions

View File

@@ -5,6 +5,7 @@ from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict
import torch
from typing import Optional
class Settings(BaseSettings):
@@ -21,11 +22,10 @@ class Settings(BaseSettings):
debug: bool = False
# PaddleOCR-VL Settings
paddleocr_vl_url: str = "http://localhost:8080/v1"
paddleocr_vl_url: str = "http://127.0.0.1:8000/v1"
# Model Paths
doclayout_model_path: str = "app/model/DocLayout/best.pt"
pp_doclayout_model_dir: str = "app/model/PP-DocLayout/PP-DocLayoutV2"
pp_doclayout_model_dir: Optional[str] = "/home/yoge/.cache/modelscope/hub/models/PaddlePaddle/PP-DocLayoutV2"
# Image Processing
max_image_size_mb: int = 10
@@ -37,11 +37,6 @@ class Settings(BaseSettings):
host: str = "0.0.0.0"
port: int = 8053
@property
def doclayout_model_file(self) -> Path:
"""Get the DocLayout model file path."""
return Path(self.doclayout_model_path)
@property
def pp_doclayout_dir(self) -> Path:
"""Get the PP-DocLayout model directory path."""

View File

@@ -3,20 +3,20 @@
from app.services.image_processor import ImageProcessor
from app.services.layout_detector import LayoutDetector
from app.services.ocr_service import OCRService
from app.services.docx_converter import DocxConverter
from app.services.converter import Converter
from app.core.config import get_settings
# Global instances (initialized on startup)
_layout_detector: LayoutDetector | None = None
def init_layout_detector(model_path: str) -> None:
def init_layout_detector() -> None:
"""Initialize the global layout detector.
Called during application startup.
"""
global _layout_detector
_layout_detector = LayoutDetector(model_path=model_path)
_layout_detector.load_model()
_layout_detector = LayoutDetector()
def get_layout_detector() -> LayoutDetector:
@@ -33,10 +33,15 @@ def get_image_processor() -> ImageProcessor:
def get_ocr_service() -> OCRService:
"""Get an OCR service instance."""
return OCRService()
return OCRService(
vl_server_url=get_settings().paddleocr_vl_url,
layout_detector=get_layout_detector(),
image_processor=get_image_processor(),
converter=get_converter(),
)
def get_docx_converter() -> DocxConverter:
def get_converter() -> Converter:
"""Get a DOCX converter instance."""
return DocxConverter()
return Converter()