model / server.py
P2THE28's picture
Create server.py
c118517 verified
Raw History Blame Contribute Delete
2.22 kB
import sys
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
# ============================================================
# PROJECT PATH
# ============================================================
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.append(str(PROJECT_ROOT))
# ============================================================
# RIGHTS NAVIGATOR
# ============================================================
from rights_navigator.rights_navigator import get_rights_response
# ============================================================
# FASTAPI
# ============================================================
app = FastAPI(
title="CaseLoop Rights Navigator API"
)
# ============================================================
# CORS
# ============================================================
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ============================================================
# REQUEST MODEL
# ============================================================
class RightsRequest(BaseModel):
grievance: str
# ============================================================
# HEALTH CHECK
# ============================================================
@app.get("/")
async def root():
return {
"status": "online",
"service": "CaseLoop Rights Navigator",
"endpoint": "/rights"
}
# ============================================================
# RIGHTS NAVIGATOR
# ============================================================
@app.post("/rights")
@app.post("/api/rights")
async def rights_endpoint(request: RightsRequest):
if not request.grievance.strip():
raise HTTPException(
status_code=400,
detail="Grievance cannot be empty."
)
try:
result = get_rights_response(
request.grievance
)
return result
except Exception as e:
raise HTTPException(
status_code=500,
detail=str(e)
)