forked from zhadyz/AI_SOC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference_api.py
More file actions
359 lines (288 loc) · 9.93 KB
/
Copy pathinference_api.py
File metadata and controls
359 lines (288 loc) · 9.93 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
CICIDS2017 IDS - Real-Time Inference API
FastAPI endpoint for real-time network intrusion detection using trained ML models.
Provides prediction endpoint with confidence scores and model selection.
Integration: Alert-Triage Service Architecture
Endpoint: POST /predict
Target Latency: <100ms per prediction
Author: HOLLOWED_EYES
Mission: OPERATION ML-BASELINE
Date: 2025-10-13
"""
import os
import pickle
import time
from pathlib import Path
from typing import Dict, List, Optional
import numpy as np
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
import uvicorn
# Constants
# Support both local and Docker paths
MODEL_PATH_ENV = os.getenv("MODEL_PATH", "/app/models")
MODEL_PATH = Path(MODEL_PATH_ENV)
# Initialize FastAPI
app = FastAPI(
title="CICIDS2017 Intrusion Detection API",
description="Real-time network intrusion detection using ML models",
version="1.0.0"
)
# Global model storage
models = {}
scaler = None
label_encoder = None
feature_names = None
class NetworkFlow(BaseModel):
"""Network flow features for prediction"""
features: List[float] = Field(
...,
description="List of 77 network flow features (after dropping non-predictive columns)",
min_length=77,
max_length=77
)
model_name: Optional[str] = Field(
default="random_forest",
description="Model to use for prediction: random_forest, xgboost, or decision_tree"
)
class Config:
json_schema_extra = {
"example": {
"features": [0.0] * 77, # Example placeholder
"model_name": "random_forest"
}
}
class NetworkFlowDict(BaseModel):
"""Network flow features as dictionary (feature_name: value)"""
flow_duration: float = 0.0
total_fwd_packet: float = 0.0
total_bwd_packets: float = 0.0
# Add more fields as needed - this is a simplified example
model_name: Optional[str] = "random_forest"
class PredictionResponse(BaseModel):
"""Prediction response with confidence and metadata"""
prediction: str = Field(..., description="Predicted class: BENIGN or ATTACK")
confidence: float = Field(..., description="Confidence score (0-1)")
probabilities: Dict[str, float] = Field(..., description="Class probabilities")
model_used: str = Field(..., description="Model used for prediction")
inference_time_ms: float = Field(..., description="Inference time in milliseconds")
timestamp: str = Field(..., description="Prediction timestamp")
def load_models():
"""Load all trained models and preprocessing objects"""
global models, scaler, label_encoder, feature_names
print("Loading models and artifacts...")
artifacts = {
'random_forest': MODEL_PATH / 'random_forest_ids.pkl',
'xgboost': MODEL_PATH / 'xgboost_ids.pkl',
'decision_tree': MODEL_PATH / 'decision_tree_ids.pkl',
'scaler': MODEL_PATH / 'scaler.pkl',
'label_encoder': MODEL_PATH / 'label_encoder.pkl',
'feature_names': MODEL_PATH / 'feature_names.pkl'
}
for name, path in artifacts.items():
if not path.exists():
print(f"WARNING: {name} not found at {path}")
continue
try:
with open(path, 'rb') as f:
obj = pickle.load(f)
if name in ['random_forest', 'xgboost', 'decision_tree']:
models[name] = obj
print(f" Loaded model: {name}")
elif name == 'scaler':
scaler = obj
print(f" Loaded scaler")
elif name == 'label_encoder':
label_encoder = obj
print(f" Loaded label encoder: {label_encoder.classes_}")
elif name == 'feature_names':
feature_names = obj
print(f" Loaded feature names: {len(obj)} features")
except Exception as e:
print(f"ERROR loading {name}: {e}")
if not models:
raise RuntimeError("No models loaded successfully")
print(f"Models loaded: {list(models.keys())}")
return True
@app.on_event("startup")
async def startup_event():
"""Load models on startup"""
load_models()
@app.get("/")
async def root():
"""API root endpoint"""
return {
"service": "CICIDS2017 Intrusion Detection API",
"version": "1.0.0",
"status": "operational",
"models_loaded": list(models.keys()),
"endpoints": {
"predict": "/predict",
"health": "/health",
"models": "/models"
}
}
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"models_loaded": len(models),
"available_models": list(models.keys())
}
@app.get("/models")
async def list_models():
"""List available models and their info"""
model_info = {}
for name, model in models.items():
try:
model_bytes = len(pickle.dumps(model))
size_mb = model_bytes / (1024 * 1024)
except:
size_mb = None
model_info[name] = {
"name": name,
"type": type(model).__name__,
"size_mb": round(size_mb, 2) if size_mb else "unknown",
"loaded": True
}
return {
"total_models": len(models),
"models": model_info,
"feature_count": len(feature_names) if feature_names else "unknown",
"label_classes": label_encoder.classes_.tolist() if label_encoder else []
}
@app.post("/models/reload")
async def reload_models():
"""
Hot-reload models from disk without restart.
Called by the retraining pipeline after promoting new models.
"""
try:
load_models()
return {
"status": "success",
"models_loaded": list(models.keys()),
"message": "Models reloaded successfully"
}
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Failed to reload models: {str(e)}"
)
@app.post("/predict", response_model=PredictionResponse)
async def predict(flow: NetworkFlow):
"""
Predict intrusion detection for a network flow
Args:
flow: NetworkFlow object with 78 features and optional model selection
Returns:
PredictionResponse with prediction, confidence, and metadata
"""
from datetime import datetime
start_time = time.time()
# Validate model selection
model_name = flow.model_name.lower()
if model_name not in models:
raise HTTPException(
status_code=400,
detail=f"Model '{model_name}' not available. Choose from: {list(models.keys())}"
)
# Validate feature count
if len(flow.features) != len(feature_names):
raise HTTPException(
status_code=400,
detail=f"Expected {len(feature_names)} features, got {len(flow.features)}"
)
try:
# Prepare features
X = np.array(flow.features).reshape(1, -1)
# Scale features
if scaler:
X_scaled = scaler.transform(X)
else:
X_scaled = X
# Get model
model = models[model_name]
# Make prediction
y_pred = model.predict(X_scaled)[0]
y_pred_proba = model.predict_proba(X_scaled)[0]
# Decode prediction
predicted_class = label_encoder.inverse_transform([y_pred])[0]
confidence = float(np.max(y_pred_proba))
# Build probability dictionary
probabilities = {
label_encoder.classes_[i]: float(y_pred_proba[i])
for i in range(len(label_encoder.classes_))
}
inference_time_ms = (time.time() - start_time) * 1000
return PredictionResponse(
prediction=predicted_class,
confidence=confidence,
probabilities=probabilities,
model_used=model_name,
inference_time_ms=round(inference_time_ms, 4),
timestamp=datetime.now().isoformat()
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Prediction error: {str(e)}"
)
@app.post("/predict/batch")
async def predict_batch(flows: List[NetworkFlow]):
"""
Batch prediction for multiple network flows
Args:
flows: List of NetworkFlow objects
Returns:
List of PredictionResponse objects
"""
from datetime import datetime
if len(flows) > 1000:
raise HTTPException(
status_code=400,
detail="Batch size limited to 1000 flows"
)
start_time = time.time()
results = []
for i, flow in enumerate(flows):
try:
# Use the single prediction endpoint
result = await predict(flow)
results.append(result.dict())
except Exception as e:
results.append({
"error": str(e),
"flow_index": i
})
total_time_ms = (time.time() - start_time) * 1000
avg_time_ms = total_time_ms / len(flows)
return {
"total_predictions": len(results),
"total_time_ms": round(total_time_ms, 2),
"avg_time_per_prediction_ms": round(avg_time_ms, 4),
"results": results
}
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
"""Global exception handler"""
return JSONResponse(
status_code=500,
content={
"error": "Internal server error",
"detail": str(exc),
"path": str(request.url)
}
)
def run_server(host: str = "0.0.0.0", port: int = 8000):
"""Run the FastAPI server"""
print(f"\nStarting CICIDS2017 IDS Inference API...")
print(f"Host: {host}")
print(f"Port: {port}")
print(f"Docs: http://{host}:{port}/docs")
print(f"Redoc: http://{host}:{port}/redoc")
uvicorn.run(app, host=host, port=port)
if __name__ == "__main__":
run_server()