Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import pandas as pd
|
|
| 3 |
import pickle
|
| 4 |
import joblib
|
| 5 |
|
| 6 |
-
#
|
| 7 |
@st.cache_resource
|
| 8 |
def load_model_artifacts():
|
| 9 |
with open('features_to_drop.pkl', 'rb') as f:
|
|
@@ -13,75 +13,70 @@ def load_model_artifacts():
|
|
| 13 |
model = joblib.load('xgb_model.pkl')
|
| 14 |
return features_to_drop, category_encodings, model
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def preprocess_input(df, features_to_drop, category_encodings):
|
| 18 |
df = df.copy()
|
| 19 |
-
|
| 20 |
-
# Ensure numeric conversion
|
| 21 |
-
for col in ["Stime", "Ltime", "sbytes", "dbytes", "Spkts", "Dpkts"]:
|
| 22 |
-
df[col] = pd.to_numeric(df[col], errors='coerce')
|
| 23 |
-
|
| 24 |
-
# Create engineered features
|
| 25 |
-
df["duration"] = df["Ltime"] - df["Stime"]
|
| 26 |
-
df["byte_ratio"] = df["sbytes"] / (df["dbytes"] + 1)
|
| 27 |
-
df["pkt_ratio"] = df["Spkts"] / (df["Dpkts"] + 1)
|
| 28 |
-
|
| 29 |
-
# Drop features removed during training
|
| 30 |
-
drop_cols = list(features_to_drop.intersection(set(df.columns)))
|
| 31 |
df.drop(columns=drop_cols, inplace=True, errors='ignore')
|
| 32 |
-
|
| 33 |
-
# Encode categoricals
|
| 34 |
for col, cats in category_encodings.items():
|
| 35 |
if col in df.columns:
|
| 36 |
df[col] = df[col].astype(str)
|
| 37 |
df[col] = pd.Categorical(df[col], categories=cats)
|
| 38 |
df[col] = df[col].cat.codes
|
| 39 |
-
|
| 40 |
df = df.fillna(0)
|
| 41 |
return df
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
st.set_page_config("
|
| 45 |
-
st.title("Intrusion Detection System (IDS) - Manual
|
| 46 |
|
| 47 |
-
st.markdown("Enter a single row of network
|
| 48 |
|
| 49 |
-
with st.form("
|
| 50 |
col1, col2 = st.columns(2)
|
| 51 |
with col1:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
with col2:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
submitted = st.form_submit_button("Run IDS Prediction")
|
| 65 |
|
| 66 |
-
# ------------------- Prediction Logic -------------------
|
| 67 |
if submitted:
|
| 68 |
user_input = pd.DataFrame([{
|
| 69 |
-
"
|
| 70 |
-
"Ltime": Ltime,
|
| 71 |
-
"sbytes": sbytes,
|
| 72 |
-
"dbytes": dbytes,
|
| 73 |
-
"Spkts": Spkts,
|
| 74 |
-
"Dpkts": Dpkts,
|
| 75 |
"proto": proto,
|
| 76 |
"service": service,
|
| 77 |
"state": state,
|
| 78 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
}])
|
| 80 |
|
| 81 |
-
# Load model artifacts
|
| 82 |
features_to_drop, category_encodings, model = load_model_artifacts()
|
| 83 |
-
|
| 84 |
-
# Preprocess input
|
| 85 |
processed_input = preprocess_input(user_input, features_to_drop, category_encodings)
|
| 86 |
|
| 87 |
if processed_input is not None:
|
|
@@ -89,7 +84,7 @@ if submitted:
|
|
| 89 |
st.success(f"Prediction: {prediction}")
|
| 90 |
st.markdown("""
|
| 91 |
- **13** → Normal Traffic
|
| 92 |
-
- **Other values** → Intrusion Category
|
| 93 |
""")
|
| 94 |
else:
|
| 95 |
-
st.error("Preprocessing failed. Please check your input
|
|
|
|
| 3 |
import pickle
|
| 4 |
import joblib
|
| 5 |
|
| 6 |
+
# Load model artifacts
|
| 7 |
@st.cache_resource
|
| 8 |
def load_model_artifacts():
|
| 9 |
with open('features_to_drop.pkl', 'rb') as f:
|
|
|
|
| 13 |
model = joblib.load('xgb_model.pkl')
|
| 14 |
return features_to_drop, category_encodings, model
|
| 15 |
|
| 16 |
+
# Preprocessing function
|
| 17 |
def preprocess_input(df, features_to_drop, category_encodings):
|
| 18 |
df = df.copy()
|
| 19 |
+
drop_cols = list(features_to_drop.intersection(df.columns))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
df.drop(columns=drop_cols, inplace=True, errors='ignore')
|
|
|
|
|
|
|
| 21 |
for col, cats in category_encodings.items():
|
| 22 |
if col in df.columns:
|
| 23 |
df[col] = df[col].astype(str)
|
| 24 |
df[col] = pd.Categorical(df[col], categories=cats)
|
| 25 |
df[col] = df[col].cat.codes
|
|
|
|
| 26 |
df = df.fillna(0)
|
| 27 |
return df
|
| 28 |
|
| 29 |
+
# UI layout
|
| 30 |
+
st.set_page_config("IDS - Selected Features Input", layout="wide")
|
| 31 |
+
st.title("Intrusion Detection System (IDS) - Manual Test Input")
|
| 32 |
|
| 33 |
+
st.markdown("Enter selected features for a single row of network flow data:")
|
| 34 |
|
| 35 |
+
with st.form("selected_input_form"):
|
| 36 |
col1, col2 = st.columns(2)
|
| 37 |
with col1:
|
| 38 |
+
dur = st.number_input("Duration of Flow (`dur`)", min_value=0.0, step=0.001)
|
| 39 |
+
proto = st.selectbox("Protocol (`proto`)", ["tcp", "udp", "icmp", "-"])
|
| 40 |
+
service = st.text_input("Service (`service`)", value="-")
|
| 41 |
+
state = st.selectbox("State (`state`)", ["CON", "INT", "FIN", "REQ", "-"])
|
| 42 |
+
sbytes = st.number_input("Source Bytes (`sbytes`)", min_value=0)
|
| 43 |
+
spkts = st.number_input("Source Packets (`spkts`)", min_value=0)
|
| 44 |
+
sttl = st.number_input("Source TTL (`sttl`)", min_value=0)
|
| 45 |
+
sload = st.number_input("Source Load (`sload`)", min_value=0.0)
|
| 46 |
+
|
| 47 |
with col2:
|
| 48 |
+
dbytes = st.number_input("Destination Bytes (`dbytes`)", min_value=0)
|
| 49 |
+
dpkts = st.number_input("Destination Packets (`dpkts`)", min_value=0)
|
| 50 |
+
sloss = st.number_input("Source Packet Loss (`sloss`)", min_value=0)
|
| 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:
|
|
|
|
| 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.")
|