Spaces:
Running
Running
| 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 SentiRiskEngine | |
| app = FastAPI( | |
| title="SentiRisk SML API", | |
| description="Independent Small Machine Learning (SML) service for institutional risk assessment.", | |
| version="1.0.0" | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| WEIGHTS_PATH = os.path.join(BASE_DIR, "weights", "risk_model.pt") | |
| engine = SentiRiskEngine(WEIGHTS_PATH) | |
| class RequestBody(BaseModel): | |
| text: str | |
| async def root(): | |
| return {"status": "active", "service": "SentiRisk", "port": 9205} | |
| async def health(): | |
| return {"status": "ok"} | |
| async def risk_score(body: RequestBody): | |
| try: | |
| return engine.predict(body.text) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def risk_assess(body: RequestBody): | |
| try: | |
| return engine.predict(body.text) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| 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": "sentirisk"} | |
| 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=9205) | |