Spaces:
Sleeping
Sleeping
Update src/main.py
Browse files- src/main.py +44 -44
src/main.py
CHANGED
|
@@ -1,45 +1,45 @@
|
|
| 1 |
-
import base64
|
| 2 |
-
import os
|
| 3 |
-
from fastapi import FastAPI, Header, HTTPException, Body
|
| 4 |
-
|
| 5 |
-
from src.
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
try:
|
| 18 |
-
file_b64 = data.get("fileBase64")
|
| 19 |
-
file_name = data.get("fileName")
|
| 20 |
-
file_type = data.get("fileType") # pdf/docx/image
|
| 21 |
-
|
| 22 |
-
# Decode Base64 string to bytes
|
| 23 |
-
file_bytes = base64.b64decode(file_b64)
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
text =
|
| 27 |
-
|
| 28 |
-
# Run AI analysis
|
| 29 |
-
result = run_analysis(text)
|
| 30 |
-
|
| 31 |
-
# 3. Return exactly what the rubric asks for
|
| 32 |
-
return {
|
| 33 |
-
"status": "success",
|
| 34 |
-
"fileName": file_name,
|
| 35 |
-
"summary": result["summary"],
|
| 36 |
-
"entities": {
|
| 37 |
-
"names":
|
| 38 |
-
"organizations":
|
| 39 |
-
"dates":
|
| 40 |
-
"amounts":
|
| 41 |
-
},
|
| 42 |
-
"sentiment": result["sentiment"]
|
| 43 |
-
}
|
| 44 |
-
except Exception as e:
|
| 45 |
return {"status": "error", "message": str(e)}
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import os
|
| 3 |
+
from fastapi import FastAPI, Header, HTTPException, Body
|
| 4 |
+
# FIX: Match the function name in processor.py
|
| 5 |
+
from src.processor import get_text_from_base64
|
| 6 |
+
from src.analyzer import run_analysis
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
API_KEY = os.getenv("API_KEY", "sk_track2_987654321")
|
| 10 |
+
|
| 11 |
+
@app.post("/api/document-analyze")
|
| 12 |
+
async def analyze_document(x_api_key: str = Header(None), data: dict = Body(...)):
|
| 13 |
+
# 1. Auth Check
|
| 14 |
+
if x_api_key != API_KEY:
|
| 15 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
file_b64 = data.get("fileBase64")
|
| 19 |
+
file_name = data.get("fileName")
|
| 20 |
+
file_type = data.get("fileType") # pdf/docx/image
|
| 21 |
+
|
| 22 |
+
# Decode Base64 string to bytes
|
| 23 |
+
file_bytes = base64.b64decode(file_b64)
|
| 24 |
+
|
| 25 |
+
# FIX: Call the correct function name here too
|
| 26 |
+
text = get_text_from_base64(file_bytes, file_type)
|
| 27 |
+
|
| 28 |
+
# Run AI analysis
|
| 29 |
+
result = run_analysis(text)
|
| 30 |
+
|
| 31 |
+
# 3. Return exactly what the rubric asks for
|
| 32 |
+
return {
|
| 33 |
+
"status": "success",
|
| 34 |
+
"fileName": file_name,
|
| 35 |
+
"summary": result["summary"],
|
| 36 |
+
"entities": {
|
| 37 |
+
"names": result["entities"].get("names", []),
|
| 38 |
+
"organizations": result["entities"].get("organizations", []),
|
| 39 |
+
"dates": result["entities"].get("dates", []),
|
| 40 |
+
"amounts": result["entities"].get("amounts", [])
|
| 41 |
+
},
|
| 42 |
+
"sentiment": result["sentiment"]
|
| 43 |
+
}
|
| 44 |
+
except Exception as e:
|
| 45 |
return {"status": "error", "message": str(e)}
|