-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
153 lines (124 loc) · 5.87 KB
/
Copy pathapi.py
File metadata and controls
153 lines (124 loc) · 5.87 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
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
from __future__ import annotations
import json
import logging
import os
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any
import joblib
import numpy as np
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, ConfigDict, Field, field_validator
BASE_DIR = Path(__file__).resolve().parent
MODEL_DIR = BASE_DIR / "models"
STATIC_DIR = BASE_DIR / "static"
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
THRESHOLD = float(os.getenv("FRAUD_THRESHOLD", "0.5"))
logging.basicConfig(level=LOG_LEVEL, format="%(asctime)s %(levelname)s %(name)s %(message)s")
logger = logging.getLogger("fraudguard")
def load_registry() -> tuple[dict[str, Any], dict[str, Any]]:
config_path = MODEL_DIR / "model_config.json"
if not config_path.exists():
raise FileNotFoundError(f"Missing model configuration: {config_path}")
config = json.loads(config_path.read_text(encoding="utf-8"))
models: dict[str, Any] = {}
for model_name in config:
model_path = MODEL_DIR / f"{model_name}.pkl"
if model_path.exists():
try:
models[model_name] = joblib.load(model_path)
except Exception:
logger.exception("Unable to load model %s", model_name)
return config, models
@asynccontextmanager
async def lifespan(app: FastAPI):
try:
app.state.model_config, app.state.models = load_registry()
logger.info("Loaded %d model artifacts", len(app.state.models))
except Exception as exc:
app.state.model_config, app.state.models = {}, {}
app.state.startup_error = str(exc)
logger.error("Model registry unavailable: %s", exc)
yield
app = FastAPI(
title="FraudGuard API",
version="1.0.0",
description="Production application API for the credit-card fraud detection models.",
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=os.getenv("CORS_ORIGINS", "*").split(","),
allow_credentials=False,
allow_methods=["GET", "POST"],
allow_headers=["*"],
)
class Transaction(BaseModel):
model_config = ConfigDict(extra="allow")
features: dict[str, float] = Field(..., description="Transaction feature values keyed by model feature name")
threshold: float = Field(default=THRESHOLD, ge=0.0, le=1.0)
@field_validator("features")
@classmethod
def validate_features(cls, value: dict[str, float]) -> dict[str, float]:
if not value:
raise ValueError("At least one feature is required")
if not all(np.isfinite(v) for v in value.values()):
raise ValueError("Feature values must be finite numbers")
return value
class Prediction(BaseModel):
model: str
predicted_class: int
fraud_probability: float
threshold: float
interpretation: str
class PredictionResponse(BaseModel):
predictions: list[Prediction]
available_models: list[str]
required_features: list[str]
def required_features(config: dict[str, Any]) -> list[str]:
ordered: list[str] = []
for definition in config.values():
for feature in definition.get("features", []):
if feature not in ordered:
ordered.append(feature)
return ordered
@app.get("/health")
def health(request: Request) -> dict[str, Any]:
models = getattr(request.app.state, "models", {})
return {"status": "ok" if models else "degraded", "models_loaded": len(models), "service": "fraudguard-api"}
@app.get("/api/models")
def models(request: Request) -> dict[str, Any]:
config = getattr(request.app.state, "model_config", {})
loaded = getattr(request.app.state, "models", {})
return {"models": [{"name": name, "description": item.get("description", ""), "loaded": name in loaded} for name, item in config.items()], "features": required_features(config), "threshold": THRESHOLD}
@app.post("/api/predict", response_model=PredictionResponse)
def predict(payload: Transaction, request: Request) -> PredictionResponse:
config = getattr(request.app.state, "model_config", {})
loaded = getattr(request.app.state, "models", {})
if not loaded:
raise HTTPException(status_code=503, detail="Prediction service is temporarily unavailable because no model artifacts are loaded.")
results: list[Prediction] = []
for name, model in loaded.items():
features = config.get(name, {}).get("features", [])
missing = [feature for feature in features if feature not in payload.features]
if missing:
raise HTTPException(status_code=422, detail=f"Missing required transaction features: {', '.join(missing)}")
vector = np.asarray([[payload.features[feature] for feature in features]], dtype=float)
try:
probability = float(model.predict_proba(vector)[0][1]) if hasattr(model, "predict_proba") else float(model.decision_function(vector)[0])
if not 0 <= probability <= 1:
probability = 1 / (1 + np.exp(-probability))
fraud = int(probability >= payload.threshold)
except Exception as exc:
logger.exception("Prediction failed for %s", name)
raise HTTPException(status_code=500, detail=f"Model inference failed for {name}.") from exc
results.append(Prediction(model=name, predicted_class=fraud, fraud_probability=round(probability, 6), threshold=payload.threshold, interpretation="Review transaction" if fraud else "Likely legitimate"))
return PredictionResponse(predictions=results, available_models=list(loaded), required_features=required_features(config))
if STATIC_DIR.exists():
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
@app.get("/", include_in_schema=False)
def dashboard() -> FileResponse:
return FileResponse(STATIC_DIR / "index.html")