mg643 commited on
Commit
83a4e77
Β·
1 Parent(s): 6ea5be4

updated requirements, created docker file for HF deployment, modified lifespan to load model, stopped tracking joblib files

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. Dockerfile +12 -0
  3. README.md +7 -0
  4. app/main.py +25 -0
  5. requirements.txt +2 -0
.gitignore CHANGED
@@ -5,6 +5,7 @@ data/outputs/
5
 
6
  # models
7
  models/distilbert_trace/
 
8
 
9
  # python
10
  __pycache__/
 
5
 
6
  # models
7
  models/distilbert_trace/
8
+ models/*.joblib
9
 
10
  # python
11
  __pycache__/
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ EXPOSE 7860
11
+
12
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,3 +1,10 @@
 
 
 
 
 
 
 
1
  # Agent Trace Anomaly Detection
2
 
3
  Detect when AI agents "go off the rails" β€” unnecessary tool calls, circular reasoning, and goal drift β€” by framing multi-step execution traces as a **sequence anomaly detection** problem.
 
1
+ ---
2
+ title: OffRails API
3
+ sdk: docker
4
+ app_port: 7860
5
+ pinned: false
6
+ ---
7
+
8
  # Agent Trace Anomaly Detection
9
 
10
  Detect when AI agents "go off the rails" β€” unnecessary tool calls, circular reasoning, and goal drift β€” by framing multi-step execution traces as a **sequence anomaly detection** problem.
app/main.py CHANGED
@@ -21,6 +21,9 @@ import logging
21
  from fastapi import FastAPI
22
  from fastapi.middleware.cors import CORSMiddleware
23
 
 
 
 
24
  # ── Make partner's scripts/ importable ───────────────────────────────────────
25
  # inference.py does `from model import ...` and `from build_features import ...`
26
  # so we need scripts/ on sys.path.
@@ -38,10 +41,32 @@ logging.basicConfig(
38
  datefmt="%H:%M:%S",
39
  )
40
 
 
 
41
  # ── App ──────────────────────────────────────────────────────────────────────
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  app = FastAPI(
44
  title="Agent Trace Anomaly Detection API",
 
45
  description=(
46
  "Detects anomalous agent execution traces β€” unnecessary tool calls, "
47
  "circular reasoning, and goal drift.\n\n"
 
21
  from fastapi import FastAPI
22
  from fastapi.middleware.cors import CORSMiddleware
23
 
24
+ from contextlib import asynccontextmanager
25
+ from app.api.routes import _state
26
+
27
  # ── Make partner's scripts/ importable ───────────────────────────────────────
28
  # inference.py does `from model import ...` and `from build_features import ...`
29
  # so we need scripts/ on sys.path.
 
41
  datefmt="%H:%M:%S",
42
  )
43
 
44
+ logger = logging.getLogger(__name__)
45
+
46
  # ── App ──────────────────────────────────────────────────────────────────────
47
 
48
+ @asynccontextmanager
49
+ async def lifespan(app: FastAPI):
50
+ from huggingface_hub import hf_hub_download
51
+ from scripts.inference import TraceAnomalyDetector
52
+ try:
53
+ model_path = hf_hub_download(
54
+ repo_id="mg643/offrails-models",
55
+ filename="xgboost_model.joblib",
56
+ )
57
+ _state["detector"] = TraceAnomalyDetector(
58
+ model_dir=os.path.dirname(model_path),
59
+ model_type="xgboost"
60
+ )
61
+ _state["model_type"] = "xgboost"
62
+ logger.info("XGBoost model loaded from HF Hub")
63
+ except Exception as e:
64
+ logger.warning(f"Could not load model: {e}")
65
+ yield
66
+
67
  app = FastAPI(
68
  title="Agent Trace Anomaly Detection API",
69
+ lifespan=lifespan,
70
  description=(
71
  "Detects anomalous agent execution traces β€” unnecessary tool calls, "
72
  "circular reasoning, and goal drift.\n\n"
requirements.txt CHANGED
@@ -5,6 +5,8 @@ torch
5
  scikit-learn
6
  xgboost
7
  joblib
 
 
8
 
9
  # evaluation + viz
10
  matplotlib
 
5
  scikit-learn
6
  xgboost
7
  joblib
8
+ uvicorn
9
+ fastapi
10
 
11
  # evaluation + viz
12
  matplotlib