-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserve_model.py
More file actions
47 lines (37 loc) · 1.18 KB
/
serve_model.py
File metadata and controls
47 lines (37 loc) · 1.18 KB
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import joblib
import numpy as np
from pathlib import Path
app = FastAPI()
MODEL_PATH = Path("models/isolation_forest.pkl")
# Load model
try:
loaded = joblib.load(MODEL_PATH)
if isinstance(loaded, tuple) and len(loaded) == 2:
scaler, model = loaded
else:
raise ValueError("Expected a (scaler, model) tuple in model file.")
except Exception as e:
print(f"[ERROR] Failed to load model: {e}")
model = None
scaler = None
# Define request schema
class FeatureInput(BaseModel):
features: list[float]
# Health check
@app.get("/")
def health_check():
return {"message": "GitHub Anomaly Detection API is live."}
# Prediction endpoint
@app.post("/predict")
def predict(request: FeatureInput):
try:
if model is None or scaler is None:
raise RuntimeError("Model or scaler not loaded.")
X = np.array(request.features).reshape(1, -1)
X_scaled = scaler.transform(X)
score = model.decision_function(X_scaled)[0]
return {"anomaly_score": float(score)}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))