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)