File size: 909 Bytes
a72da64
 
 
 
 
699048b
a72da64
699048b
a72da64
699048b
a72da64
 
d567322
699048b
a2c3b1f
 
 
 
a72da64
699048b
a72da64
 
 
 
 
 
 
 
 
 
d567322
a72da64
 
 
 
699048b
a72da64
 
 
 
 
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
37
38
39
40
41
42
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)