File size: 916 Bytes
6e9f256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""API FastAPI: POST /ocr com uma imagem -> {text, confidence, chars}."""
import io
import time

from fastapi import FastAPI, File, UploadFile
from PIL import Image

from .infer import OCREngine

app = FastAPI(title="SNN-OCR", description="OCR com Spiking Neural Network proprietaria")
engine: OCREngine | None = None


@app.on_event("startup")
def load_model():
    global engine
    engine = OCREngine()


@app.post("/ocr")
async def ocr(file: UploadFile = File(...)):
    t0 = time.perf_counter()
    img = Image.open(io.BytesIO(await file.read()))
    text, conf = engine.predict_image(img)
    return {
        "text": text,
        "confidence": round(conf, 4),
        "chars": len(text),
        "latency_ms": round((time.perf_counter() - t0) * 1000, 2),
    }


@app.get("/health")
def health():
    return {"status": "ok", "model": "SpikingCNN", "device": engine.device}