"""OCR extraction endpoints.""" import asyncio from fastapi import APIRouter, File, UploadFile router = APIRouter(prefix="/api/ocr") def get_chart_extractor(): from core.bootstrap import get_chart_extractor_service as _get return _get() @router.post("") async def ocr_extract(file: UploadFile = File(...)): content = await file.read() extractor = get_chart_extractor() structured = await asyncio.to_thread( extractor.extract_structured, content, file.filename ) preview_b64 = await asyncio.to_thread( extractor.preview_page, content, file.filename ) return { "text": structured.get("text", ""), "structured": structured, "filename": file.filename, "format": "structured", "preview_b64": preview_b64, } @router.post("/chart") async def ocr_chart(file: UploadFile = File(...)): content = await file.read() extractor = get_chart_extractor() chart_data = await asyncio.to_thread( extractor.extract_chart, content, file.filename ) return {"chart_data": chart_data, "filename": file.filename} @router.post("/tables") async def ocr_tables(file: UploadFile = File(...)): content = await file.read() extractor = get_chart_extractor() tables = await asyncio.to_thread(extractor.extract_tables, content, file.filename) return {"tables": tables, "filename": file.filename}