senti-beta / sentilaw /api.py
joseph njoroge kariuki
Deploy Senti AI to Hugging Face Spaces
021e065
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import os
import sys
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
from model import SentiLawEngine
app = FastAPI(
title="SentiLaw SML API",
description="Independent Small Machine Learning (SML) service for tax & compliance audit checks.",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
WEIGHTS_PATH = os.path.join(BASE_DIR, "weights", "compliance_model.pt")
engine = SentiLawEngine(WEIGHTS_PATH)
class RequestBody(BaseModel):
text: str
@app.get("/")
async def root():
return {"status": "active", "service": "SentiLaw", "port": 9204}
@app.get("/health")
async def health():
return {"status": "ok"}
@app.post("/api/v1/law/compliance")
async def law_compliance(body: RequestBody):
try:
return engine.predict(body.text)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/v1/law/predict")
async def predict_endpoint_legacy_alias(body: RequestBody):
try:
# Try different possible engines
if 'engine' in globals():
return engine.predict(body.text)
elif 'rlm_engine' in globals():
return await rlm_engine.predict_deep(body.text, "A")
else:
return {"status": "ok", "service": "sentilaw"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=9204)