from fastapi import FastAPI, UploadFile, File from fastapi.responses import JSONResponse import shutil import os import uuid from app.ocr import run_ocr app = FastAPI(title="Invoice OCR API") UPLOAD_DIR = "temp" os.makedirs(UPLOAD_DIR, exist_ok=True) @app.get("/") def home(): return{"status":"ok"} @app.post("/ocr") async def ocr_endpoint(file: UploadFile = File(...)): try: file_id = str(uuid.uuid4()) file_path = os.path.join(UPLOAD_DIR, f"{file_id}.jpg") with open(file_path, "wb") as buffer: shutil.copyfileobj(file.file, buffer) result = run_ocr(file_path) os.remove(file_path) return JSONResponse({ "status": "success", "text_blocks": result }) except Exception as e: return JSONResponse({ "status": "error", "message": str(e) }, status_code=500)