File size: 10,283 Bytes
6ea5be4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
FastAPI routes for Agent Trace Anomaly Detection.

All ML logic lives in scripts/inference.py (partner's code).
This file only handles HTTP ↔ inference translation.

Endpoints
---------
GET  /health                 – service health & loaded model info
POST /models/load            – load a model (xgboost or distilbert)
POST /predict                – predict anomaly for a single trace
POST /predict/batch          – predict anomalies for multiple traces
POST /predict/compare        – run both models on same trace, compare
POST /pipeline/train         – trigger the full training pipeline
GET  /pipeline/status        – check if models dir has trained models
"""

from __future__ import annotations

import json
import os
import subprocess
import logging
from typing import Any

from fastapi import APIRouter, HTTPException, Query

from app.schemas import (
    PredictRequest,
    PredictBatchRequest,
    PredictResponse,
    PredictBatchResponse,
    CompareResponse,
    ModelLoadRequest,
    HealthResponse,
)

logger = logging.getLogger(__name__)
router = APIRouter()


# ── In-memory state ──────────────────────────────────────────────────────────

_state: dict[str, Any] = {
    "detector": None,          # TraceAnomalyDetector instance
    "model_type": None,        # "xgboost" or "distilbert"
    "model_dir": "models",     # path to saved models
}


def _get_detector():
    """Return the loaded detector or raise 409."""
    if _state["detector"] is None:
        raise HTTPException(
            status_code=409,
            detail="No model loaded. POST /models/load first.",
        )
    return _state["detector"]


def _check_available_models(model_dir: str) -> list[str]:
    """Check which trained models exist on disk."""
    available = []
    if os.path.exists(os.path.join(model_dir, "xgboost_model.joblib")):
        available.append("xgboost")
    if os.path.exists(os.path.join(model_dir, "distilbert_trace", "trace_config.json")):
        available.append("distilbert")
    if os.path.exists(os.path.join(model_dir, "naive_baseline.joblib")):
        available.append("naive_baseline")
    return available


# ── Health ───────────────────────────────────────────────────────────────────

@router.get("/health", response_model=HealthResponse, tags=["System"])
def health_check():
    model_dir = _state["model_dir"]
    return HealthResponse(
        status="ok",
        loaded_model=_state["model_type"],
        available_models=_check_available_models(model_dir),
        model_dir=model_dir,
    )


# ── Model Loading ───────────────────────────────────────────────────────────

@router.post("/models/load", tags=["Models"])
def load_model(req: ModelLoadRequest):
    """
    Load a trained model into memory for inference.
    Models must be trained first via the pipeline (setup.py).
    """
    from scripts.inference import TraceAnomalyDetector

    available = _check_available_models(req.model_dir)
    if req.model_type not in available:
        raise HTTPException(
            status_code=404,
            detail=(
                f"Model '{req.model_type}' not found in '{req.model_dir}/'. "
                f"Available models: {available}. "
                f"Run the training pipeline first: python setup.py"
            ),
        )

    try:
        detector = TraceAnomalyDetector(
            model_dir=req.model_dir,
            model_type=req.model_type,
        )
        _state["detector"] = detector
        _state["model_type"] = req.model_type
        _state["model_dir"] = req.model_dir

        return {
            "message": f"Model '{req.model_type}' loaded successfully",
            "model_type": req.model_type,
            "model_dir": req.model_dir,
        }
    except Exception as exc:
        logger.exception("Failed to load model")
        raise HTTPException(status_code=500, detail=f"Failed to load model: {exc}")


# ── Prediction ───────────────────────────────────────────────────────────────

@router.post("/predict", response_model=PredictResponse, tags=["Prediction"])
def predict(req: PredictRequest):
    """
    Predict whether a single agent trace is anomalous.

    Pass conversations in ShareGPT/ToolBench format:
    [{"from": "user", "value": "..."}, {"from": "assistant", "value": "..."}, ...]
    """
    detector = _get_detector()

    try:
        result = detector.predict(req.conversations)
    except Exception as exc:
        logger.exception("Prediction failed")
        raise HTTPException(status_code=500, detail=f"Prediction error: {exc}")

    return PredictResponse(
        is_anomalous=result["is_anomalous"],
        confidence=result["confidence"],
        label=result["label"],
        anomaly_signals=result.get("anomaly_signals", []),
        model_used=_state["model_type"],
        features=result.get("features"),
    )


@router.post("/predict/batch", response_model=PredictBatchResponse, tags=["Prediction"])
def predict_batch(req: PredictBatchRequest):
    """Predict anomalies for multiple traces at once."""
    detector = _get_detector()

    try:
        results = detector.predict_batch(req.traces)
    except Exception as exc:
        logger.exception("Batch prediction failed")
        raise HTTPException(status_code=500, detail=f"Batch prediction error: {exc}")

    predictions = [
        PredictResponse(
            is_anomalous=r["is_anomalous"],
            confidence=r["confidence"],
            label=r["label"],
            anomaly_signals=r.get("anomaly_signals", []),
            model_used=_state["model_type"],
            features=r.get("features"),
        )
        for r in results
    ]

    return PredictBatchResponse(
        predictions=predictions,
        anomaly_count=sum(1 for p in predictions if p.is_anomalous),
        total=len(predictions),
    )


@router.post("/predict/compare", response_model=CompareResponse, tags=["Prediction"])
def predict_compare(req: PredictRequest):
    """
    Run both XGBoost and DistilBERT on the same trace and compare results.
    Both models must be trained and available in the models directory.
    """
    from scripts.inference import TraceAnomalyDetector

    model_dir = _state["model_dir"]
    available = _check_available_models(model_dir)
    results = {}

    for model_type in ["xgboost", "distilbert"]:
        if model_type in available:
            try:
                det = TraceAnomalyDetector(model_dir=model_dir, model_type=model_type)
                r = det.predict(req.conversations)
                results[model_type] = PredictResponse(
                    is_anomalous=r["is_anomalous"],
                    confidence=r["confidence"],
                    label=r["label"],
                    anomaly_signals=r.get("anomaly_signals", []),
                    model_used=model_type,
                    features=r.get("features"),
                )
            except Exception as exc:
                logger.warning("Compare: %s failed: %s", model_type, exc)

    if not results:
        raise HTTPException(
            status_code=404,
            detail=f"No trained models found in '{model_dir}/'. Run the training pipeline first.",
        )

    xgb = results.get("xgboost")
    bert = results.get("distilbert")
    agreement = True
    if xgb and bert:
        agreement = xgb.label == bert.label

    return CompareResponse(
        xgboost=xgb,
        distilbert=bert,
        agreement=agreement,
    )


# ── Training Pipeline ────────────────────────────────────────────────────────

@router.post("/pipeline/train", tags=["Pipeline"])
def trigger_training(
    max_samples: int | None = Query(None, ge=100, description="Cap on dataset rows"),
    model: str = Query("all", description="Which model to train: all, naive, classical, deep"),
):
    """
    Trigger the full training pipeline (setup.py).

    This runs data download β†’ feature extraction β†’ model training.
    May take several minutes depending on dataset size and model choice.
    """
    cmd = ["python", "setup.py"]
    if max_samples:
        cmd.extend(["--max_samples", str(max_samples)])
    if model != "all":
        cmd.extend(["--step", "train", "--model", model])

    try:
        result = subprocess.run(
            cmd, capture_output=True, text=True, timeout=1800,  # 30 min max
        )
        return {
            "message": "Training pipeline completed" if result.returncode == 0 else "Pipeline failed",
            "returncode": result.returncode,
            "stdout": result.stdout[-3000:] if result.stdout else "",  # last 3000 chars
            "stderr": result.stderr[-1000:] if result.stderr else "",
        }
    except subprocess.TimeoutExpired:
        raise HTTPException(status_code=504, detail="Training pipeline timed out (30 min limit)")
    except FileNotFoundError:
        raise HTTPException(
            status_code=500,
            detail="setup.py not found. Make sure you're running from the OffRails project root.",
        )


@router.get("/pipeline/status", tags=["Pipeline"])
def pipeline_status():
    """Check what trained models and data files are available."""
    model_dir = _state["model_dir"]
    data_dir = "data/processed"

    data_files = {}
    if os.path.isdir(data_dir):
        for f in os.listdir(data_dir):
            path = os.path.join(data_dir, f)
            data_files[f] = {
                "size_mb": round(os.path.getsize(path) / 1_048_576, 2),
            }

    return {
        "available_models": _check_available_models(model_dir),
        "loaded_model": _state["model_type"],
        "model_dir": model_dir,
        "data_files": data_files,
    }