feat: optimize the format convert
This commit is contained in:
@@ -2,11 +2,12 @@
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from app.core.dependencies import get_image_processor, get_layout_detector, get_ocr_service, get_mineru_ocr_service
|
||||
from app.schemas.image import ImageOCRRequest, ImageOCRResponse
|
||||
from app.core.dependencies import get_image_processor, get_layout_detector, get_ocr_service, get_mineru_ocr_service, get_converter
|
||||
from app.schemas.image import ImageOCRRequest, ImageOCRResponse, LatexToOmmlRequest, LatexToOmmlResponse
|
||||
from app.services.image_processor import ImageProcessor
|
||||
from app.services.layout_detector import LayoutDetector
|
||||
from app.services.ocr_service import OCRService, MineruOCRService
|
||||
from app.services.converter import Converter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -28,6 +29,9 @@ async def process_image_ocr(
|
||||
- If plain text exists: use PP-DocLayoutV2 for mixed recognition
|
||||
- Otherwise: use PaddleOCR-VL with formula prompt
|
||||
4. Convert output to LaTeX, Markdown, and MathML formats
|
||||
|
||||
Note: OMML conversion is not included due to performance overhead.
|
||||
Use the /latex-to-omml endpoint to convert LaTeX to OMML separately.
|
||||
"""
|
||||
|
||||
image = image_processor.preprocess(
|
||||
@@ -49,4 +53,34 @@ async def process_image_ocr(
|
||||
latex=ocr_result.get("latex", ""),
|
||||
markdown=ocr_result.get("markdown", ""),
|
||||
mathml=ocr_result.get("mathml", ""),
|
||||
mml=ocr_result.get("mml", ""),
|
||||
)
|
||||
|
||||
|
||||
@router.post("/latex-to-omml", response_model=LatexToOmmlResponse)
|
||||
async def convert_latex_to_omml(
|
||||
request: LatexToOmmlRequest,
|
||||
converter: Converter = Depends(get_converter),
|
||||
) -> LatexToOmmlResponse:
|
||||
"""Convert LaTeX formula to OMML (Office Math Markup Language).
|
||||
|
||||
OMML is the math format used by Microsoft Word and other Office applications.
|
||||
This endpoint is separate from the main OCR endpoint due to the performance
|
||||
overhead of OMML conversion (requires creating a temporary DOCX file).
|
||||
|
||||
Args:
|
||||
request: Contains the LaTeX formula to convert (without $ or $$ delimiters).
|
||||
|
||||
Returns:
|
||||
OMML representation of the formula.
|
||||
"""
|
||||
if not request.latex or not request.latex.strip():
|
||||
raise HTTPException(status_code=400, detail="LaTeX formula cannot be empty")
|
||||
|
||||
try:
|
||||
omml = converter.convert_to_omml(request.latex)
|
||||
return LatexToOmmlResponse(omml=omml)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except RuntimeError as e:
|
||||
raise HTTPException(status_code=503, detail=str(e))
|
||||
|
||||
Reference in New Issue
Block a user