from __future__ import annotations from PIL import Image from ocr_studio.config import MAX_NEW_TOKENS, QUALITY_SHORT_CHARS, QUALITY_SPARSE_RATIO from ocr_studio.spotting import TextSpan def assess_quality( text: str, pages: list[Image.Image], spans_by_page: list[list[TextSpan]], truncated_pages: bool, deskew_angles: list[float], ) -> list[tuple[str, dict[str, object]]]: warnings: list[tuple[str, dict[str, object]]] = [] chars = len((text or "").strip()) if chars < QUALITY_SHORT_CHARS: warnings.append(("warn.short", {})) area = sum(max(1, image.width * image.height) for image in pages) or 1 if chars / float(area) < QUALITY_SPARSE_RATIO and chars >= QUALITY_SHORT_CHARS: warnings.append(("warn.sparse", {})) if any(not page_spans for page_spans in spans_by_page) and any(pages): warnings.append(("warn.no_boxes", {})) if truncated_pages: warnings.append(("warn.truncated_pages", {"count": len(pages)})) notable = [angle for angle in deskew_angles if abs(angle) >= 0.8] if notable: warnings.append(("warn.deskew", {"angle": notable[0]})) if chars >= int(MAX_NEW_TOKENS * 2.2): warnings.append(("warn.tokens", {})) return warnings