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
# Ensure current directory is in path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
from model import SentiBankingEngine, SentiBankingRLM
app = FastAPI(
title="SentiBanking RLM API",
description="Service for transaction risk analysis and reconciliation supporting SML and RLM deep reasoning.",
version="2.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
WEIGHTS_PATH = os.path.join(BASE_DIR, "weights", "banking_model.pt")
engine = SentiBankingEngine(WEIGHTS_PATH)
rlm_engine = SentiBankingRLM()
class RequestBody(BaseModel):
text: str
tier: str = "A" # Default tier is A (System 1 SML)
@app.get("/")
async def root():
return {"status": "active", "service": "sentibanking", "port": 9209}
@app.get("/health")
async def health():
# Basic check on the RLM model health
rlm_health = await rlm_engine.engine.health_check()
return {
"status": "ok",
"rlm_health": rlm_health
}
@app.post("/api/v1/banking/score")
async def predict_endpoint(body: RequestBody, deep: bool = False):
try:
# Escalate to RLM if deep=True or if the tier is C or D
if deep or body.tier in ("C", "D"):
return await rlm_engine.predict_deep(body.text, body.tier)
return engine.predict(body.text)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/v1/banking/reason")
async def banking_reason(body: RequestBody):
try:
return await rlm_engine.predict_deep(body.text, body.tier)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.on_event("shutdown")
async def shutdown_event():
# Gracefully close connection-pooled HTTP clients
from senti.core.engines.superpacks.rlm_engine import RLMEngine
await RLMEngine.shutdown()
@app.post("/api/v1/banking/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": "sentibanking"}
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=9209)