Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,51 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
import joblib
|
|
|
|
| 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 |
-
swin = st.number_input("Source TCP Window Size (`swin`)", min_value=0)
|
| 52 |
-
trans_depth = st.number_input("Transaction Depth (`trans_depth`)", min_value=0)
|
| 53 |
-
ct_flw_http_mthd = st.text_input("HTTP Method (`ct_flw_http_mthd`)", value="-")
|
| 54 |
-
is_ftp_login = st.selectbox("Is FTP Login (`is_ftp_login`)", [0, 1])
|
| 55 |
-
attack_cat = st.text_input("(Optional) Ground Truth Label (`attack_cat`)", value="")
|
| 56 |
-
|
| 57 |
-
submitted = st.form_submit_button("Run IDS Prediction")
|
| 58 |
-
|
| 59 |
-
if submitted:
|
| 60 |
-
user_input = pd.DataFrame([{
|
| 61 |
-
"dur": dur,
|
| 62 |
-
"proto": proto,
|
| 63 |
-
"service": service,
|
| 64 |
-
"state": state,
|
| 65 |
-
"sbytes": sbytes,
|
| 66 |
-
"dbytes": dbytes,
|
| 67 |
-
"spkts": spkts,
|
| 68 |
-
"dpkts": dpkts,
|
| 69 |
-
"sttl": sttl,
|
| 70 |
-
"sload": sload,
|
| 71 |
-
"sloss": sloss,
|
| 72 |
-
"swin": swin,
|
| 73 |
-
"trans_depth": trans_depth,
|
| 74 |
-
"ct_flw_http_mthd": ct_flw_http_mthd,
|
| 75 |
-
"is_ftp_login": is_ftp_login,
|
| 76 |
-
"attack_cat": attack_cat
|
| 77 |
-
}])
|
| 78 |
-
|
| 79 |
-
features_to_drop, category_encodings, model = load_model_artifacts()
|
| 80 |
-
processed_input = preprocess_input(user_input, features_to_drop, category_encodings)
|
| 81 |
-
|
| 82 |
-
if processed_input is not None:
|
| 83 |
-
prediction = model.predict(processed_input)[0]
|
| 84 |
-
st.success(f"Prediction: {prediction}")
|
| 85 |
-
st.markdown("""
|
| 86 |
-
- **13** → Normal Traffic
|
| 87 |
-
- **Other values** → Intrusion Category
|
| 88 |
-
""")
|
| 89 |
-
else:
|
| 90 |
-
st.error("Preprocessing failed. Please check your input.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
import joblib
|
| 5 |
+
import pickle
|
| 6 |
|
| 7 |
+
# ---------------------------
|
| 8 |
+
# Load trained model and metadata
|
| 9 |
+
# ---------------------------
|
| 10 |
+
model = joblib.load('xgb_model.pkl')
|
| 11 |
+
|
| 12 |
+
with open('features_to_drop.pkl', 'rb') as f:
|
| 13 |
+
features_to_drop = pickle.load(f)
|
| 14 |
+
|
| 15 |
+
with open('category_encodings.pkl', 'rb') as f:
|
| 16 |
+
category_encodings = pickle.load(f)
|
| 17 |
+
|
| 18 |
+
# ---------------------------
|
| 19 |
+
# App UI
|
| 20 |
+
# ---------------------------
|
| 21 |
+
st.title("🚨 Intrusion Detection In Networks")
|
| 22 |
+
st.markdown("Paste a single row of preprocessed NB15 features (comma-separated) below to predict the intrusion type.")
|
| 23 |
+
|
| 24 |
+
user_input = st.text_area("Input (1 row from NB15_combined_preprocessed.csv):", height=150)
|
| 25 |
+
|
| 26 |
+
if st.button("Predict"):
|
| 27 |
+
try:
|
| 28 |
+
# Convert user input string into a DataFrame
|
| 29 |
+
row = [x.strip() for x in user_input.strip().split(",")]
|
| 30 |
+
input_df = pd.DataFrame([row])
|
| 31 |
+
|
| 32 |
+
# Load a sample row to get correct column names and data types
|
| 33 |
+
sample_df = pd.read_csv('NB15_combined_preprocessed.csv', nrows=1)
|
| 34 |
+
columns = sample_df.drop('attack_cat', axis=1).columns.tolist()
|
| 35 |
+
|
| 36 |
+
# Ensure correct number of columns
|
| 37 |
+
if len(row) != len(columns):
|
| 38 |
+
st.error(f"Expected {len(columns)} values, but got {len(row)}.")
|
| 39 |
+
else:
|
| 40 |
+
input_df.columns = columns
|
| 41 |
+
# Convert to appropriate types
|
| 42 |
+
input_df = input_df.apply(pd.to_numeric, errors='coerce')
|
| 43 |
+
|
| 44 |
+
# Drop high-correlation features
|
| 45 |
+
input_df = input_df.drop(columns=features_to_drop, errors='ignore')
|
| 46 |
+
|
| 47 |
+
# Predict
|
| 48 |
+
prediction = model.predict(input_df)[0]
|
| 49 |
+
st.success(f"🛡️ Predicted Intrusion Category: **{prediction}**")
|
| 50 |
+
except Exception as e:
|
| 51 |
+
st.error(f"⚠️ Error processing input: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|