from __future__ import annotations import threading from pathlib import Path from typing import Callable from loguru import logger from sqlalchemy import select from sqlalchemy.orm import Session from sakura_cloud.core.config import settings from sakura_cloud.db.session import SessionLocal from sakura_cloud.models.artwork import Artwork from sakura_cloud.models.ocr import ArtworkOcrText OcrExtractor = Callable[[Path], tuple[str, float | None, str]] _semaphore = threading.BoundedSemaphore(settings.ocr_max_concurrency) def queue_ocr_for_artwork(db: Session, artwork: Artwork) -> ArtworkOcrText | None: if not settings.ocr_enabled: return None row = _get_or_create_row(db, artwork) if row.status in {"pending", "running", "done"}: return row row.status = "pending" row.error = None db.commit() return row def run_ocr_for_artwork_id(artwork_id: str, user_id: str) -> None: with SessionLocal() as db: artwork = db.get(Artwork, artwork_id) if artwork is None or artwork.user_id != user_id: return run_ocr_for_artwork(db, artwork) def run_ocr_for_artwork( db: Session, artwork: Artwork, *, extractor: OcrExtractor | None = None, ) -> ArtworkOcrText: row = _get_or_create_row(db, artwork) if not settings.ocr_enabled and extractor is None: row.status = "failed" row.error = "ocr_disabled" db.commit() return row if not _is_supported_image(artwork): row.status = "failed" row.error = "unsupported_file_type" db.commit() logger.info("OCR skipped artwork={} mime={}", artwork.id, artwork.mime_type) return row if row.status == "running": return row if row.attempts >= settings.ocr_max_attempts and extractor is None: return row acquired = _semaphore.acquire(blocking=False) if not acquired: row.status = "pending" row.error = "ocr_busy" db.commit() return row try: row.status = "running" row.attempts += 1 row.error = None db.commit() text, confidence, language = (extractor or _paddle_ocr_extract)(Path(artwork.file_path)) row.ocr_text = text.strip() row.confidence = confidence row.language = language or "mixed" row.engine = "paddleocr" row.status = "done" row.error = None db.commit() logger.info("OCR done artwork={} text_len={} confidence={}", artwork.id, len(row.ocr_text), confidence) return row except Exception as exc: row.status = "failed" row.error = exc.__class__.__name__ db.commit() logger.warning("OCR failed artwork={} attempt={} error={}", artwork.id, row.attempts, exc.__class__.__name__) return row finally: _semaphore.release() def _get_or_create_row(db: Session, artwork: Artwork) -> ArtworkOcrText: row = db.scalar( select(ArtworkOcrText).where( ArtworkOcrText.user_id == artwork.user_id, ArtworkOcrText.artwork_id == artwork.id, ) ) if row is None: row = ArtworkOcrText( user_id=artwork.user_id, artwork_id=artwork.id, file_id=artwork.file_id, source_platform=artwork.source_platform, source_id=artwork.source_id, status="pending", ) db.add(row) db.commit() db.refresh(row) else: row.file_id = artwork.file_id row.source_platform = artwork.source_platform row.source_id = artwork.source_id return row def _is_supported_image(artwork: Artwork) -> bool: mime = (artwork.mime_type or "").lower() if mime in {"image/jpeg", "image/png", "image/webp"}: return True suffix = Path(artwork.file_path or "").suffix.lower() return suffix in {".jpg", ".jpeg", ".png", ".webp"} and mime != "image/gif" def _paddle_ocr_extract(path: Path) -> tuple[str, float | None, str]: from PIL import Image from paddleocr import PaddleOCR if not path.is_file(): raise FileNotFoundError("image_not_found") work_path = path with Image.open(path) as image: original_size = image.size image.thumbnail((settings.ocr_max_image_dimension, settings.ocr_max_image_dimension)) if image.size != original_size: tmp = path.with_suffix(".ocr-resized.jpg") image.convert("RGB").save(tmp, quality=90) work_path = tmp ocr = PaddleOCR(use_angle_cls=True, lang=settings.ocr_language, use_gpu=False) result = ocr.ocr(str(work_path), cls=True) texts: list[str] = [] scores: list[float] = [] for page in result or []: for line in page or []: if len(line) >= 2 and isinstance(line[1], (list, tuple)): text = str(line[1][0]).strip() if text: texts.append(text) try: scores.append(float(line[1][1])) except (TypeError, ValueError, IndexError): pass if work_path != path: work_path.unlink(missing_ok=True) confidence = sum(scores) / len(scores) if scores else None return "\n".join(texts), confidence, settings.ocr_language