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 SentiWealthEngine, SentiWealthRLM
app = FastAPI(
title="SentiWealth RLM API",
description="SML and RLM service for portfolio optimization and asset allocation scoring.",
version="2.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
WEIGHTS_PATH = os.path.join(BASE_DIR, "weights", "wealth_model.pt")
engine = SentiWealthEngine(WEIGHTS_PATH)
rlm_engine = SentiWealthRLM()
class RequestBody(BaseModel):
text: str
tier: str = "A"
@app.get("/")
async def root():
return {"status": "active", "service": "sentiwealth", "port": 9210}
@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/wealth/allocate")
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/wealth/score")
async def score_endpoint(body: RequestBody, deep: bool = False):
# Support legacy /score path mapped in sml_client.py
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/wealth/reason")
async def wealth_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():
from senti.core.engines.superpacks.rlm_engine import RLMEngine
await RLMEngine.shutdown()
@app.post("/api/v1/wealth/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": "sentiwealth"}
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=9210)