Add docTR as primary OCR engine with 2-tier sequential processing, OCR metrics tracking, and simplified engine selection. Features: - docTR OCR engine with light+medium preprocessing tiers - doctr_plus mode with early exit optimization (~65% fast path) - OCR metrics dashboard with per-engine statistics - User OCR preference persistence - Parallel worker pool for OCR processing - Cross-validation for extraction quality Engine options: tesseract, doctr, doctr_plus (recommended), paddleocr 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
"""Database configuration and session management using SQLModel."""
|
|
|
|
from pathlib import Path
|
|
from typing import AsyncGenerator
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlmodel import SQLModel
|
|
|
|
from backend.modules.data_entry.config import settings
|
|
|
|
|
|
# Create async engine
|
|
# Note: echo=False to disable SQL query logging (too verbose)
|
|
engine = create_async_engine(
|
|
settings.database_url,
|
|
echo=False,
|
|
future=True,
|
|
)
|
|
|
|
# Create async session factory
|
|
async_session_maker = sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False,
|
|
)
|
|
|
|
|
|
async def init_db() -> None:
|
|
"""Initialize database - create tables if they don't exist."""
|
|
# Ensure data directory exists
|
|
db_path = Path(settings.sqlite_database_path)
|
|
db_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(SQLModel.metadata.create_all)
|
|
|
|
|
|
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
|
"""Get async database session for dependency injection."""
|
|
async with async_session_maker() as session:
|
|
try:
|
|
yield session
|
|
finally:
|
|
await session.close()
|
|
|
|
|
|
# Convenience function for manual session usage
|
|
async def get_db_session() -> AsyncSession:
|
|
"""Get a new database session (manual management)."""
|
|
return async_session_maker()
|