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 SentiCoachEngine app = FastAPI( title="SentiCoach SML API", description="Independent Small Machine Learning (SML) service for budgeting and spend archetyping.", version="1.0.0" ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) WEIGHTS_PATH = os.path.join(BASE_DIR, "weights", "coach_model.pt") engine = SentiCoachEngine(WEIGHTS_PATH) class RequestBody(BaseModel): text: str @app.get("/") async def root(): return {"status": "active", "service": "SentiCoach", "port": 9202} @app.get("/health") async def health(): return {"status": "ok"} @app.post("/api/v1/coach/archetype") async def coach_archetype(body: RequestBody): try: return engine.predict(body.text) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.post("/api/v1/coach/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": "senticoach"} 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=9202)