Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +63 -0
- category_encodings.pkl +3 -0
- ensemble_model.pkl +3 -0
- features_to_drop.pkl +3 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import pickle
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Load model and preprocessing artifacts
|
| 8 |
+
model = joblib.load("ensemble_model.pkl")
|
| 9 |
+
with open("features_to_drop.pkl", "rb") as f:
|
| 10 |
+
features_to_drop = pickle.load(f)
|
| 11 |
+
|
| 12 |
+
# Column names from the raw 49-column dataset (before feature engineering)
|
| 13 |
+
raw_columns = [
|
| 14 |
+
'srcip', 'sport', 'dstip', 'dsport', 'proto', 'state', 'dur', 'sbytes', 'dbytes',
|
| 15 |
+
'sttl', 'dttl', 'sloss', 'dloss', 'service', 'Sload', 'Dload', 'Spkts', 'Dpkts',
|
| 16 |
+
'swin', 'dwin', 'stcpb', 'dtcpb', 'smeansz', 'dmeansz', 'trans_depth', 'res_bdy_len',
|
| 17 |
+
'Sjit', 'Djit', 'Stime', 'Ltime', 'Sintpkt', 'Dintpkt', 'tcprtt', 'synack', 'ackdat',
|
| 18 |
+
'is_sm_ips_ports', 'ct_state_ttl', 'ct_flw_http_mthd', 'is_ftp_login', 'ct_ftp_cmd',
|
| 19 |
+
'ct_srv_src', 'ct_srv_dst', 'ct_dst_ltm', 'ct_src_ ltm', 'ct_src_dport_ltm',
|
| 20 |
+
'ct_dst_sport_ltm', 'ct_dst_src_ltm', 'attack_cat', 'Label'
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
# Function to preprocess a single input row
|
| 24 |
+
def preprocess_input(row_values):
|
| 25 |
+
if len(row_values) != 49:
|
| 26 |
+
raise ValueError(f"❌ Expected 49 values, but got {len(row_values)}.")
|
| 27 |
+
|
| 28 |
+
# Create DataFrame from input
|
| 29 |
+
input_df = pd.DataFrame([row_values], columns=raw_columns)
|
| 30 |
+
|
| 31 |
+
# Convert all columns to numeric
|
| 32 |
+
input_df = input_df.apply(pd.to_numeric, errors='coerce')
|
| 33 |
+
|
| 34 |
+
# Feature engineering
|
| 35 |
+
input_df['duration'] = input_df['Ltime'] - input_df['Stime']
|
| 36 |
+
input_df['byte_ratio'] = input_df['sbytes'] / (input_df['dbytes'] + 1)
|
| 37 |
+
input_df['pkt_ratio'] = input_df['Spkts'] / (input_df['Dpkts'] + 1)
|
| 38 |
+
|
| 39 |
+
# ✅ Fix: convert features_to_drop to list before adding with another list
|
| 40 |
+
input_df = input_df.drop(columns=list(features_to_drop) + ['attack_cat', 'Label'], errors='ignore')
|
| 41 |
+
|
| 42 |
+
return input_df
|
| 43 |
+
|
| 44 |
+
# Streamlit UI
|
| 45 |
+
st.title("🔍 Anomaly Detection In Network Traffic")
|
| 46 |
+
st.markdown("Paste a **single row** of raw features from the dataset (49 values, tab-separated):")
|
| 47 |
+
|
| 48 |
+
user_input = st.text_area("Input Row", height=150)
|
| 49 |
+
|
| 50 |
+
if st.button("Predict"):
|
| 51 |
+
try:
|
| 52 |
+
# Parse the input
|
| 53 |
+
values = user_input.strip().split("\t")
|
| 54 |
+
|
| 55 |
+
# Preprocess the input row
|
| 56 |
+
processed_df = preprocess_input(values)
|
| 57 |
+
|
| 58 |
+
# Predict using the preprocessed data
|
| 59 |
+
prediction = model.predict(processed_df)[0]
|
| 60 |
+
st.success(f"✅ Predicted Attack Category: **{prediction}**")
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
st.error(f"❌ Error processing input: {e}")
|
category_encodings.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:39d3adee7f8b02aaa7a418edcf40a7323190ff3f341aca38190ae791846556fa
|
| 3 |
+
size 388170
|
ensemble_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d1c52325a456b3c3aef4d23e7bdcebe47dbf9fb7a0c8c368df86a7e810b4f0be
|
| 3 |
+
size 3542608
|
features_to_drop.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:63b1f550232da8f121220bebf07112cf4a933e24a3b219b0e31557f567903387
|
| 3 |
+
size 314
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
xgboost==2.1.4
|
| 5 |
+
joblib
|
| 6 |
+
scikit-learn
|
| 7 |
+
gdown
|