Update app.py
Browse files
app.py
CHANGED
|
@@ -4,57 +4,71 @@ import joblib
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
# =====================================================
|
| 7 |
-
# Load CICIDS2018 Model
|
| 8 |
# =====================================================
|
| 9 |
rf_model_path = hf_hub_download(
|
| 10 |
repo_id="CodebaseAi/netraids-ml-models",
|
| 11 |
filename="rf_pipeline.joblib"
|
| 12 |
)
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
CLASS_MAPPING =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# =====================================================
|
| 26 |
-
# Load BCC-Darknet Model (5
|
| 27 |
# =====================================================
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
filename="realtime_model.pkl"
|
| 31 |
)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
filename="realtime_scaler.pkl"
|
| 35 |
)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
filename="realtime_encoder.pkl"
|
| 39 |
)
|
| 40 |
|
| 41 |
-
darknet_model = joblib.load(darknet_model_path)
|
| 42 |
-
darknet_scaler = joblib.load(darknet_scaler_path)
|
| 43 |
-
darknet_encoder = joblib.load(darknet_encoder_path)
|
| 44 |
-
|
| 45 |
# =====================================================
|
| 46 |
# Prediction Router
|
| 47 |
# =====================================================
|
| 48 |
def predict(model_choice, features: dict):
|
| 49 |
-
|
| 50 |
df = pd.DataFrame([features])
|
| 51 |
|
| 52 |
if model_choice == "CICIDS2018 (13 Classes)":
|
| 53 |
-
# Enforce feature order
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
pred_idx = rf_model.predict(df)[0]
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
return {
|
| 60 |
"dataset": "CICIDS2018",
|
|
@@ -62,7 +76,6 @@ def predict(model_choice, features: dict):
|
|
| 62 |
}
|
| 63 |
|
| 64 |
else:
|
| 65 |
-
# BCC-Darknet pipeline
|
| 66 |
X_scaled = darknet_scaler.transform(df)
|
| 67 |
pred_encoded = darknet_model.predict(X_scaled)[0]
|
| 68 |
pred_label = darknet_encoder.inverse_transform([pred_encoded])[0]
|
|
@@ -73,7 +86,7 @@ def predict(model_choice, features: dict):
|
|
| 73 |
}
|
| 74 |
|
| 75 |
# =====================================================
|
| 76 |
-
# Gradio
|
| 77 |
# =====================================================
|
| 78 |
app = gr.Interface(
|
| 79 |
fn=predict,
|
|
@@ -89,7 +102,8 @@ app = gr.Interface(
|
|
| 89 |
],
|
| 90 |
outputs=gr.JSON(label="Detection Result"),
|
| 91 |
title="NetraIDS – Dual-Model Network Intrusion Detection",
|
| 92 |
-
description="
|
| 93 |
)
|
| 94 |
|
| 95 |
app.launch()
|
|
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
# =====================================================
|
| 7 |
+
# Load CICIDS2018 Model
|
| 8 |
# =====================================================
|
| 9 |
rf_model_path = hf_hub_download(
|
| 10 |
repo_id="CodebaseAi/netraids-ml-models",
|
| 11 |
filename="rf_pipeline.joblib"
|
| 12 |
)
|
| 13 |
+
rf_model = joblib.load(rf_model_path)
|
| 14 |
|
| 15 |
+
# =====================================================
|
| 16 |
+
# Load Training Artifacts (SAFE)
|
| 17 |
+
# =====================================================
|
| 18 |
+
artifacts = {}
|
| 19 |
+
try:
|
| 20 |
+
artifacts_path = hf_hub_download(
|
| 21 |
+
repo_id="CodebaseAi/netraids-ml-models",
|
| 22 |
+
filename="training_artifacts.joblib"
|
| 23 |
+
)
|
| 24 |
+
artifacts = joblib.load(artifacts_path)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print("Artifacts not loaded:", e)
|
| 27 |
|
| 28 |
+
# Try to infer feature columns safely
|
| 29 |
+
FEATURE_COLUMNS = None
|
| 30 |
+
for key in ["feature_columns", "features", "columns", "X_columns"]:
|
| 31 |
+
if key in artifacts:
|
| 32 |
+
FEATURE_COLUMNS = artifacts[key]
|
| 33 |
+
break
|
| 34 |
|
| 35 |
+
# Try to infer class mapping safely
|
| 36 |
+
CLASS_MAPPING = None
|
| 37 |
+
for key in ["class_mapping", "label_mapping", "classes", "target_mapping"]:
|
| 38 |
+
if key in artifacts:
|
| 39 |
+
CLASS_MAPPING = artifacts[key]
|
| 40 |
+
break
|
| 41 |
|
| 42 |
# =====================================================
|
| 43 |
+
# Load BCC-Darknet Model (5-class)
|
| 44 |
# =====================================================
|
| 45 |
+
darknet_model = joblib.load(
|
| 46 |
+
hf_hub_download("CodebaseAi/netraids-ml-models", "realtime_model.pkl")
|
|
|
|
| 47 |
)
|
| 48 |
+
darknet_scaler = joblib.load(
|
| 49 |
+
hf_hub_download("CodebaseAi/netraids-ml-models", "realtime_scaler.pkl")
|
|
|
|
| 50 |
)
|
| 51 |
+
darknet_encoder = joblib.load(
|
| 52 |
+
hf_hub_download("CodebaseAi/netraids-ml-models", "realtime_encoder.pkl")
|
|
|
|
| 53 |
)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# =====================================================
|
| 56 |
# Prediction Router
|
| 57 |
# =====================================================
|
| 58 |
def predict(model_choice, features: dict):
|
|
|
|
| 59 |
df = pd.DataFrame([features])
|
| 60 |
|
| 61 |
if model_choice == "CICIDS2018 (13 Classes)":
|
| 62 |
+
# Enforce feature order ONLY if available
|
| 63 |
+
if FEATURE_COLUMNS is not None:
|
| 64 |
+
df = df[FEATURE_COLUMNS]
|
| 65 |
|
| 66 |
pred_idx = rf_model.predict(df)[0]
|
| 67 |
+
|
| 68 |
+
if CLASS_MAPPING is not None:
|
| 69 |
+
pred_label = CLASS_MAPPING.get(pred_idx, str(pred_idx))
|
| 70 |
+
else:
|
| 71 |
+
pred_label = str(pred_idx)
|
| 72 |
|
| 73 |
return {
|
| 74 |
"dataset": "CICIDS2018",
|
|
|
|
| 76 |
}
|
| 77 |
|
| 78 |
else:
|
|
|
|
| 79 |
X_scaled = darknet_scaler.transform(df)
|
| 80 |
pred_encoded = darknet_model.predict(X_scaled)[0]
|
| 81 |
pred_label = darknet_encoder.inverse_transform([pred_encoded])[0]
|
|
|
|
| 86 |
}
|
| 87 |
|
| 88 |
# =====================================================
|
| 89 |
+
# Gradio UI
|
| 90 |
# =====================================================
|
| 91 |
app = gr.Interface(
|
| 92 |
fn=predict,
|
|
|
|
| 102 |
],
|
| 103 |
outputs=gr.JSON(label="Detection Result"),
|
| 104 |
title="NetraIDS – Dual-Model Network Intrusion Detection",
|
| 105 |
+
description="Robust cloud-deployed ML inference with dataset-specific pipelines"
|
| 106 |
)
|
| 107 |
|
| 108 |
app.launch()
|
| 109 |
+
|