Spaces:
Sleeping
Sleeping
File size: 1,891 Bytes
3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 5b0fc4d 3dec487 | 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 | from pathlib import Path
import joblib
import numpy as np
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
app = FastAPI(title="Web Attack Detection API")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
EXPECTED_FEATURES = 35
BASE_DIR = Path(__file__).resolve().parent
MODEL_PATH = BASE_DIR / "traffic_model.pkl"
SCALER_PATH = BASE_DIR / "traffic_scaler.pkl"
THRESHOLD_PATH = BASE_DIR / "traffic_threshold.pkl"
class InputData(BaseModel):
features: list[float] = Field(
...,
min_length=EXPECTED_FEATURES,
max_length=EXPECTED_FEATURES,
)
try:
model = joblib.load(MODEL_PATH)
scaler = joblib.load(SCALER_PATH)
threshold = float(joblib.load(THRESHOLD_PATH))
except Exception as ex:
raise RuntimeError(f"Startup load failed: {ex}") from ex
@app.get("/")
def home() -> dict[str, str]:
return {"message": "Web attack detection model running"}
@app.get("/health")
def health() -> dict[str, str]:
return {"status": "ok"}
@app.post("/predict")
def predict(data: InputData) -> dict[str, float | int | str]:
x = np.asarray(data.features, dtype=np.float64).reshape(1, -1)
if not np.isfinite(x).all():
raise HTTPException(status_code=400, detail="Features contain NaN or Inf")
x_scaled = scaler.transform(x)
class_index = list(model.classes_).index(-1)
attack_probability = float(model.predict_proba(x_scaled)[0][class_index])
raw_prediction = -1 if attack_probability >= threshold else 1
return {
"prediction": "attack detected" if raw_prediction == -1 else "normal request",
"raw_prediction": raw_prediction,
"attack_probability": attack_probability,
"threshold": threshold,
}
|