|
@@ -0,0 +1,58 @@
|
|
|
|
|
+import os
|
|
|
|
|
+from fastapi import FastAPI, UploadFile, File, Header, HTTPException, status
|
|
|
|
|
+from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
+from typing import Optional
|
|
|
|
|
+from services.openai_service import extract_receipt_data
|
|
|
|
|
+from schemas import ExtractionResponse
|
|
|
|
|
+from dotenv import load_dotenv
|
|
|
|
|
+
|
|
|
|
|
+load_dotenv()
|
|
|
|
|
+
|
|
|
|
|
+app = FastAPI(title="AI-Assisted Data Entry API")
|
|
|
|
|
+
|
|
|
|
|
+# Configure CORS
|
|
|
|
|
+app.add_middleware(
|
|
|
|
|
+ CORSMiddleware,
|
|
|
|
|
+ allow_origins=["*"], # Adjust as needed for Angular frontend
|
|
|
|
|
+ allow_credentials=True,
|
|
|
|
|
+ allow_methods=["*"],
|
|
|
|
|
+ allow_headers=["*"],
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+@app.get("/health")
|
|
|
|
|
+async def health_check():
|
|
|
|
|
+ return {"status": "healthy"}
|
|
|
|
|
+
|
|
|
|
|
+@app.post("/api/v1/extract", response_model=ExtractionResponse)
|
|
|
|
|
+async def extract_receipt(
|
|
|
|
|
+ file: UploadFile = File(...),
|
|
|
|
|
+ user_id: Optional[str] = Header(None),
|
|
|
|
|
+ user_name: str = "Unknown Employee",
|
|
|
|
|
+ department: str = "Unknown Department"
|
|
|
|
|
+):
|
|
|
|
|
+ if not file.content_type.startswith("image/"):
|
|
|
|
|
+ raise HTTPException(
|
|
|
|
|
+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
|
+ detail="File provided is not an image."
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ content = await file.read()
|
|
|
|
|
+ extraction_result = await extract_receipt_data(content, user_name, department)
|
|
|
|
|
+
|
|
|
|
|
+ if extraction_result is None:
|
|
|
|
|
+ raise HTTPException(
|
|
|
|
|
+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
|
|
|
|
+ detail="Could not extract data from the provided image."
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ return extraction_result
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ raise HTTPException(
|
|
|
|
|
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
+ detail=f"An error occurred during extraction: {str(e)}"
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == "__main__":
|
|
|
|
|
+ import uvicorn
|
|
|
|
|
+ uvicorn.run(app, host="0.0.0.0", port=8000)
|