File size: 765 Bytes
26c450d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
import shutil
import os
from model_utils import extract_invoice_data_from_image

app = FastAPI()

UPLOAD_DIR = "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)

@app.post("/extract-invoice")
async def extract_invoice(file: UploadFile = File(...)):
    # Save the uploaded image temporarily
    file_location = os.path.join(UPLOAD_DIR, file.filename)
    with open(file_location, "wb") as f:
        shutil.copyfileobj(file.file, f)

    # Run OCR + detection
    extracted_data = extract_invoice_data_from_image(file_location)

    # Optionally, clean up the uploaded file
    os.remove(file_location)

    return JSONResponse(content=extracted_data)