Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import shutil
|
| 4 |
+
import os
|
| 5 |
+
from model_utils import extract_invoice_data_from_image
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
UPLOAD_DIR = "uploads"
|
| 10 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
@app.post("/extract-invoice")
|
| 13 |
+
async def extract_invoice(file: UploadFile = File(...)):
|
| 14 |
+
# Save the uploaded image temporarily
|
| 15 |
+
file_location = os.path.join(UPLOAD_DIR, file.filename)
|
| 16 |
+
with open(file_location, "wb") as f:
|
| 17 |
+
shutil.copyfileobj(file.file, f)
|
| 18 |
+
|
| 19 |
+
# Run OCR + detection
|
| 20 |
+
extracted_data = extract_invoice_data_from_image(file_location)
|
| 21 |
+
|
| 22 |
+
# Optionally, clean up the uploaded file
|
| 23 |
+
os.remove(file_location)
|
| 24 |
+
|
| 25 |
+
return JSONResponse(content=extracted_data)
|