fix: stabilize backend services and frontend pointer
This commit is contained in:
@@ -26,6 +26,18 @@ from fastapi_modules.fastapi_leaudit.services.impl.ossServiceImpl import OssServ
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_PAGE_QUALITY_VLM_PROMPT = """
|
||||
你是文档扫描图片质量检测员。请判断这 1 页文档图片是否适合继续做 OCR 与合同/公文评查。
|
||||
|
||||
判定标准:
|
||||
1. pass:文字主体清晰、方向正常、没有明显截断,能稳定阅读。
|
||||
2. review:存在轻微模糊、倾斜、阴影、低对比度、局部遮挡、轻微截断,建议人工确认但仍可能可读。
|
||||
3. reject:严重模糊、重影、过曝/过暗、页面大面积缺失、关键文字不可辨认、方向严重错误、空白页或非文档页,建议重拍。
|
||||
|
||||
只输出 JSON,不要输出 Markdown,不要解释额外文本:
|
||||
{"status":"pass|review|reject","score":0.0到1.0,"reason":"20字以内中文原因"}
|
||||
""".strip()
|
||||
|
||||
|
||||
class PageQualityServiceImpl(IPageQualityService):
|
||||
"""页级图片质量服务实现。"""
|
||||
@@ -33,6 +45,7 @@ class PageQualityServiceImpl(IPageQualityService):
|
||||
def __init__(self) -> None:
|
||||
self.OssService = OssServiceImpl()
|
||||
self.DocumentService = None
|
||||
self.VlmClient = None
|
||||
|
||||
async def DispatchForDocument(
|
||||
self,
|
||||
@@ -282,7 +295,7 @@ class PageQualityServiceImpl(IPageQualityService):
|
||||
reject_pages = 0
|
||||
async with GetAsyncSession() as session:
|
||||
for page_num, page_image in page_images:
|
||||
status, score, reason = self._classify_page_image(page_image)
|
||||
status, score, reason = await self._classify_page_image_by_vlm(page_image)
|
||||
if status == "review":
|
||||
review_pages += 1
|
||||
elif status == "reject":
|
||||
@@ -466,13 +479,52 @@ class PageQualityServiceImpl(IPageQualityService):
|
||||
finally:
|
||||
doc.close()
|
||||
|
||||
def _classify_page_image(self, image_bytes: bytes) -> tuple[str, float, str | None]:
|
||||
size = len(image_bytes)
|
||||
if size < 25_000:
|
||||
return "reject", 0.2, "页面图像内容过少或清晰度较低,建议重拍"
|
||||
if size < 60_000:
|
||||
return "review", 0.45, "页面疑似存在模糊,建议人工确认"
|
||||
return "pass", 0.9, None
|
||||
async def _classify_page_image_by_vlm(self, image_bytes: bytes) -> tuple[str, float, str | None]:
|
||||
"""使用 VLM 对单页图片做质量判定。VLM 异常不能默认为通过。"""
|
||||
client = self._vlm_client()
|
||||
if client is None:
|
||||
return "review", 0.5, "VLM未配置,需人工确认图片质量"
|
||||
|
||||
try:
|
||||
result = await client.extract_multifield(
|
||||
prompt=_PAGE_QUALITY_VLM_PROMPT,
|
||||
images_data_urls=[self._image_data_url(image_bytes)],
|
||||
max_tokens=300,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning("VLM page quality detection failed: %s", exc)
|
||||
return "review", 0.5, "VLM图片质量检测失败,需人工确认"
|
||||
|
||||
status = str((result or {}).get("status") or "").strip().lower()
|
||||
if status not in {"pass", "review", "reject"}:
|
||||
return "review", 0.5, "VLM返回结果不可用,需人工确认"
|
||||
|
||||
score = self._normalize_quality_score((result or {}).get("score"), status)
|
||||
reason = str((result or {}).get("reason") or "").strip() or None
|
||||
if status != "pass" and not reason:
|
||||
reason = "页面图片质量需人工确认"
|
||||
return status, score, reason
|
||||
|
||||
def _vlm_client(self):
|
||||
if self.VlmClient is None:
|
||||
from fastapi_modules.fastapi_leaudit.leaudit_bridge.client_factory import create_vlm_client
|
||||
|
||||
self.VlmClient = create_vlm_client()
|
||||
return self.VlmClient
|
||||
|
||||
def _image_data_url(self, image_bytes: bytes) -> str:
|
||||
import base64
|
||||
|
||||
encoded = base64.b64encode(image_bytes).decode()
|
||||
return f"data:image/png;base64,{encoded}"
|
||||
|
||||
def _normalize_quality_score(self, raw_score: Any, status: str) -> float:
|
||||
defaults = {"pass": 0.9, "review": 0.5, "reject": 0.2}
|
||||
try:
|
||||
score = float(raw_score)
|
||||
except (TypeError, ValueError):
|
||||
return defaults[status]
|
||||
return max(0.0, min(1.0, score))
|
||||
|
||||
def _document_service(self):
|
||||
if self.DocumentService is None:
|
||||
|
||||
Reference in New Issue
Block a user