Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,11 @@ from fastapi.responses import JSONResponse
|
|
| 3 |
import shutil
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# ✅ Fix config/cache issues in Hugging Face Docker Space
|
| 7 |
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
| 8 |
os.environ["YOLO_CONFIG_DIR"] = "/tmp/ultralytics"
|
| 9 |
os.environ["XDG_CACHE_HOME"] = "/tmp"
|
| 10 |
os.environ["FONTCONFIG_PATH"] = "/tmp"
|
| 11 |
|
| 12 |
-
# Create required directories if they don't exist
|
| 13 |
os.makedirs("/tmp/matplotlib", exist_ok=True)
|
| 14 |
os.makedirs("/tmp/ultralytics", exist_ok=True)
|
| 15 |
os.makedirs("/tmp/fontconfig", exist_ok=True)
|
|
@@ -23,15 +21,12 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
| 23 |
|
| 24 |
@app.post("/extract-invoice")
|
| 25 |
async def extract_invoice(file: UploadFile = File(...)):
|
| 26 |
-
# Save the uploaded image temporarily
|
| 27 |
file_location = os.path.join(UPLOAD_DIR, file.filename)
|
| 28 |
with open(file_location, "wb") as f:
|
| 29 |
shutil.copyfileobj(file.file, f)
|
| 30 |
|
| 31 |
-
# Run OCR + detection
|
| 32 |
extracted_data = extract_invoice_data_from_image(file_location)
|
| 33 |
|
| 34 |
-
# Optionally, clean up the uploaded file
|
| 35 |
os.remove(file_location)
|
| 36 |
|
| 37 |
return JSONResponse(content=extracted_data)
|
|
|
|
| 3 |
import shutil
|
| 4 |
import os
|
| 5 |
|
|
|
|
| 6 |
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
| 7 |
os.environ["YOLO_CONFIG_DIR"] = "/tmp/ultralytics"
|
| 8 |
os.environ["XDG_CACHE_HOME"] = "/tmp"
|
| 9 |
os.environ["FONTCONFIG_PATH"] = "/tmp"
|
| 10 |
|
|
|
|
| 11 |
os.makedirs("/tmp/matplotlib", exist_ok=True)
|
| 12 |
os.makedirs("/tmp/ultralytics", exist_ok=True)
|
| 13 |
os.makedirs("/tmp/fontconfig", exist_ok=True)
|
|
|
|
| 21 |
|
| 22 |
@app.post("/extract-invoice")
|
| 23 |
async def extract_invoice(file: UploadFile = File(...)):
|
|
|
|
| 24 |
file_location = os.path.join(UPLOAD_DIR, file.filename)
|
| 25 |
with open(file_location, "wb") as f:
|
| 26 |
shutil.copyfileobj(file.file, f)
|
| 27 |
|
|
|
|
| 28 |
extracted_data = extract_invoice_data_from_image(file_location)
|
| 29 |
|
|
|
|
| 30 |
os.remove(file_location)
|
| 31 |
|
| 32 |
return JSONResponse(content=extracted_data)
|