AmeenAktharT commited on
Commit
082ea80
·
verified ·
1 Parent(s): eec2097

Update src/main.py

Browse files
Files changed (1) hide show
  1. 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
- from src.processor import get_text_from_file
5
- from src.analyzer import run_analysis
6
-
7
- app = FastAPI()
8
- API_KEY = os.getenv("API_KEY", "sk_track2_987654321")
9
-
10
- @app.post("/api/document-analyze")
11
- async def analyze_document(x_api_key: str = Header(None), data: dict = Body(...)):
12
- # 1. Auth Check (401 Unauthorized)
13
- if x_api_key != API_KEY:
14
- raise HTTPException(status_code=401, detail="Unauthorized")
15
-
16
- # 2. Extract Base64
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
- # Extract text using your existing processor
26
- text = get_text_from_file(file_bytes, f"test.{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": [e["text"] for e in result["entities"] if e["label"] == "PERSON"],
38
- "organizations": [e["text"] for e in result["entities"] if e["label"] == "ORG"],
39
- "dates": [e["text"] for e in result["entities"] if e["label"] == "DATE"],
40
- "amounts": [e["text"] for e in result["entities"] if e["label"] == "MONEY"]
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)}