import os import uuid import time from fastapi import FastAPI, File, UploadFile from fastapi.responses import JSONResponse from ocr_core import OCRCore app = FastAPI() ocr = OCRCore() TMP_DIR = "/tmp/ocr_api" os.makedirs(TMP_DIR, exist_ok=True) def save_tmp(file: UploadFile): ext = os.path.splitext(file.filename)[1] or ".jpg" name = f"{int(time.time())}_{uuid.uuid4().hex}{ext}" path = os.path.join(TMP_DIR, name) with open(path, "wb") as f: f.write(file.file.read()) return path @app.post("/ocr") async def run_ocr(file: UploadFile = File(...)): path = save_tmp(file) result = ocr.run(path) return JSONResponse(result)