|
|
import streamlit as st |
|
|
import pandas as pd |
|
|
import joblib |
|
|
import pickle |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
model = joblib.load("ensemble_voting_model.pkl") |
|
|
with open("features_to_drop.pkl", "rb") as f: |
|
|
features_to_drop = pickle.load(f) |
|
|
|
|
|
|
|
|
raw_columns = [ |
|
|
'srcip', 'sport', 'dstip', 'dsport', 'proto', 'state', 'dur', 'sbytes', 'dbytes', |
|
|
'sttl', 'dttl', 'sloss', 'dloss', 'service', 'Sload', 'Dload', 'Spkts', 'Dpkts', |
|
|
'swin', 'dwin', 'stcpb', 'dtcpb', 'smeansz', 'dmeansz', 'trans_depth', 'res_bdy_len', |
|
|
'Sjit', 'Djit', 'Stime', 'Ltime', 'Sintpkt', 'Dintpkt', 'tcprtt', 'synack', 'ackdat', |
|
|
'is_sm_ips_ports', 'ct_state_ttl', 'ct_flw_http_mthd', 'is_ftp_login', 'ct_ftp_cmd', |
|
|
'ct_srv_src', 'ct_srv_dst', 'ct_dst_ltm', 'ct_src_ ltm', 'ct_src_dport_ltm', |
|
|
'ct_dst_sport_ltm', 'ct_dst_src_ltm', 'attack_cat', 'Label' |
|
|
] |
|
|
|
|
|
|
|
|
def preprocess_input(row_values): |
|
|
if len(row_values) != 49: |
|
|
raise ValueError(f"β Expected 49 values, but got {len(row_values)}.") |
|
|
|
|
|
|
|
|
input_df = pd.DataFrame([row_values], columns=raw_columns) |
|
|
|
|
|
|
|
|
input_df = input_df.apply(pd.to_numeric, errors='coerce') |
|
|
|
|
|
|
|
|
input_df['duration'] = input_df['Ltime'] - input_df['Stime'] |
|
|
input_df['byte_ratio'] = input_df['sbytes'] / (input_df['dbytes'] + 1) |
|
|
input_df['pkt_ratio'] = input_df['Spkts'] / (input_df['Dpkts'] + 1) |
|
|
|
|
|
|
|
|
input_df = input_df.drop(columns=list(features_to_drop) + ['attack_cat', 'Label'], errors='ignore') |
|
|
|
|
|
return input_df |
|
|
|
|
|
|
|
|
st.title("π Intrusion Detection In Networks") |
|
|
st.markdown("Paste a **single row** of raw features from the dataset (49 values, tab-separated):") |
|
|
|
|
|
user_input = st.text_area("Input Row", height=150) |
|
|
|
|
|
if st.button("Predict"): |
|
|
try: |
|
|
|
|
|
values = user_input.strip().split("\t") |
|
|
|
|
|
|
|
|
processed_df = preprocess_input(values) |
|
|
|
|
|
|
|
|
prediction = model.predict(processed_df)[0] |
|
|
st.success(f"β
Predicted Attack Category: **{prediction}**") |
|
|
|
|
|
except Exception as e: |
|
|
st.error(f"β Error processing input: {e}") |
|
|
|