Files
item_bank/app/services/ocr/pipeline.py
T
2026-08-01 16:50:55 +08:00

36 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""OCR 编排:Pix2Text 优先,失败/低置信/未部署时降级到多模态 VLM。"""
import logging
from app.config import settings
from app.services.llm.client import LLMClient
from app.services.ocr.base import OcrResult
from app.services.ocr.pix2text_client import Pix2TextClient
from app.services.ocr.vlm_engine import VlmOcrEngine
logger = logging.getLogger(__name__)
# Pix2Text 结果置信度低于此值则降级到 VLM
_CONFIDENCE_THRESHOLD = 0.5
async def run_ocr(image_bytes: bytes, mime: str, llm: LLMClient) -> OcrResult:
"""返回识别结果,engine 字段标明最终使用的引擎。"""
# 1. 若配置了 Pix2Text 服务且健康,优先用
if settings.ocr_service_url:
client = Pix2TextClient(settings.ocr_service_url)
if await client.healthy():
try:
result = await client.recognize(image_bytes, mime)
if result.markdown.strip() and result.confidence >= _CONFIDENCE_THRESHOLD:
return result
logger.info(
"Pix2Text 置信度低(%.2f)或结果为空,降级 VLM", result.confidence
)
except Exception as e: # noqa: BLE001
logger.warning("Pix2Text 识别失败,降级 VLM%s", e)
else:
logger.info("Pix2Text 服务不健康,降级 VLM")
# 2. 降级 / 默认:多模态大模型
return await VlmOcrEngine(llm).recognize(image_bytes, mime)