Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI, HTTPException
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from model_loader import predictVerdict, getConfidence
|
| 4 |
from rag_service import evaluateCase
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI(title="Legal RAG Backend", version="1.0.0")
|
| 7 |
|
|
@@ -17,6 +18,8 @@ class ExplainResponse(BaseModel):
|
|
| 17 |
confidence: float
|
| 18 |
explanation: str
|
| 19 |
retrievedChunks: dict
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.get("/health")
|
| 22 |
async def healthCheck():
|
|
@@ -36,12 +39,45 @@ async def explain(request: PredictRequest):
|
|
| 36 |
try:
|
| 37 |
result = evaluateCase(request.text)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return ExplainResponse(
|
| 40 |
-
verdict=
|
| 41 |
-
confidence=
|
| 42 |
-
explanation=
|
| 43 |
-
retrievedChunks=
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from model_loader import predictVerdict, getConfidence
|
| 4 |
from rag_service import evaluateCase
|
| 5 |
+
import uvicorn
|
| 6 |
|
| 7 |
app = FastAPI(title="Legal RAG Backend", version="1.0.0")
|
| 8 |
|
|
|
|
| 18 |
confidence: float
|
| 19 |
explanation: str
|
| 20 |
retrievedChunks: dict
|
| 21 |
+
extractedKeywords: list
|
| 22 |
+
prompt: str
|
| 23 |
|
| 24 |
@app.get("/health")
|
| 25 |
async def healthCheck():
|
|
|
|
| 39 |
try:
|
| 40 |
result = evaluateCase(request.text)
|
| 41 |
|
| 42 |
+
# SAFELY extract fields (prevents KeyError)
|
| 43 |
+
verdict = (
|
| 44 |
+
result.get("verdict")
|
| 45 |
+
or result.get("finalVerdictByGemini")
|
| 46 |
+
or "unknown"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
confidence = result.get("confidence", 0.0)
|
| 50 |
+
|
| 51 |
+
explanation = (
|
| 52 |
+
result.get("explanation")
|
| 53 |
+
or result.get("geminiOutput")
|
| 54 |
+
or "No explanation generated."
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
retrievedChunks = (
|
| 58 |
+
result.get("retrievedChunks")
|
| 59 |
+
or result.get("support")
|
| 60 |
+
or {}
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
extractedKeywords = result.get("extractedKeywords", [])
|
| 64 |
+
|
| 65 |
+
prompt = (
|
| 66 |
+
result.get("prompt")
|
| 67 |
+
or result.get("promptToGemini")
|
| 68 |
+
or ""
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
return ExplainResponse(
|
| 72 |
+
verdict=verdict,
|
| 73 |
+
confidence=confidence,
|
| 74 |
+
explanation=explanation,
|
| 75 |
+
retrievedChunks=retrievedChunks,
|
| 76 |
+
extractedKeywords=extractedKeywords,
|
| 77 |
+
prompt=prompt
|
| 78 |
)
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
raise HTTPException(status_code=500, detail=str(e))
|
| 82 |
+
|
| 83 |
+
|