joseph njoroge kariuki
Deploy Senti AI to Hugging Face Spaces
021e065
Raw
History Blame Contribute Delete
2.73 kB
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 SentiPlanEngine, SentiPlanRLM
app = FastAPI(
title="SentiPlan RLM API",
description="Service for personal financial planning, budgeting, and cash flow health analysis.",
version="2.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
WEIGHTS_PATH = os.path.join(BASE_DIR, "weights", "plan_model.pt")
engine = SentiPlanEngine(WEIGHTS_PATH)
rlm_engine = SentiPlanRLM()
class RequestBody(BaseModel):
text: str
tier: str = "A"
@app.get("/")
async def root():
return {"status": "active", "service": "sentiplan", "port": 9212}
@app.get("/health")
async def health():
rlm_health = await rlm_engine.engine.health_check()
return {
"status": "ok",
"rlm_health": rlm_health
}
@app.post("/api/v1/plan/health")
async def predict_endpoint(body: RequestBody, deep: bool = False):
try:
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/plan/score")
async def score_endpoint(body: RequestBody, deep: bool = False):
try:
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/plan/reason")
async def reason_endpoint(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():
from senti.core.engines.superpacks.rlm_engine import RLMEngine
await RLMEngine.shutdown()
@app.post("/api/v1/plan/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": "sentiplan"}
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=9212)