File size: 2,796 Bytes
021e065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 SentiCommunityEngine, SentiCommunityRLM

app = FastAPI(
    title="SentiCommunity RLM API",
    description="Service for SACCO dividend management, public finance explanation, and Swahili spend coaching.",
    version="2.0.0"
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

WEIGHTS_PATH = os.path.join(BASE_DIR, "weights", "community_model.pt")
engine = SentiCommunityEngine(WEIGHTS_PATH)
rlm_engine = SentiCommunityRLM()

class RequestBody(BaseModel):
    text: str
    tier: str = "A"

@app.get("/")
async def root():
    return {"status": "active", "service": "senticommunity", "port": 9216}

@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/community/sacco")
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/community/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/community/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/community/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": "senticommunity"}
    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=9216)