Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117import streamlit as stimport pandas as pdimport gdownimport pickleimport joblibimport xgboost as xgb############################################### Helper Functions##############################################@st.cache_resourcedef load_model_artifacts():"""Loads and returns the artifacts needed for testing the IDS:- features_to_drop: The set of features dropped during training.- category_encodings: The mapping for encoding categorical variables.- xgb_model: The pre-trained XGBoost classifier."""with open('features_to_drop.pkl', 'rb') as f:features_to_drop = pickle.load(f)with open('category_encodings.pkl', 'rb') as f:category_encodings = pickle.load(f)xgb_model = joblib.load('xgb_model.pkl')return features_to_drop, category_encodings, xgb_modeldef preprocess_input(df, features_to_drop, category_encodings):"""Preprocesses the incoming test DataFrame:- Converts required columns to numeric.- Computes engineered features: duration, byte_ratio, and pkt_ratio.- Drops columns that were removed during model training.- Encodes categorical variables using pre-saved mappings."""df = df.copy()expected_cols = ["Stime", "Ltime", "sbytes", "dbytes", "Spkts", "Dpkts"]for col in expected_cols:if col in df.columns:df[col] = pd.to_numeric(df[col], errors='coerce')else:st.error(f"Missing required column: {col}")return None# Create engineered featuresdf['duration'] = df['Ltime'] - df['Stime']df['byte_ratio'] = df['sbytes'] / (df['dbytes'] + 1)df['pkt_ratio'] = df['Spkts'] / (df['Dpkts'] + 1)# Drop features (if present in the input) that were filtered during trainingdrop_cols = list(features_to_drop.intersection(set(df.columns)))if drop_cols:df = df.drop(columns=drop_cols)# Encode categorical variablesfor col, categories in category_encodings.items():if col in df.columns:df[col] = df[col].astype(str)df[col] = pd.Categorical(df[col], categories=categories)df[col] = df[col].cat.codesdf = df.fillna(0)return df############################################### Streamlit Interface - TextArea Input##############################################st.set_page_config(page_title="Intrusion Detection System - Test", layout="wide")st.title("Intrusion Detection System (IDS) - Single Row Input")st.markdown("""Paste a **single row of comma-separated values** below. Include only the relevant features required for prediction.**Expected columns (in order):**`Stime, Ltime, sbytes, dbytes, Spkts, Dpkts`You may include additional columns (like `label`) if desired, but they will be ignored.""")with st.form("manual_input_form"):text_input = st.text_area("Paste a single row of data (comma-separated values):",placeholder="e.g. 1425579984.0,1425579990.0,275,423,10,8")submitted = st.form_submit_button("Run IDS Prediction")if submitted:try:# Split and parse the user input into a list of valuesinput_values = [x.strip() for x in text_input.split(',') if x.strip() != '']# Define the column names expected by the modelexpected_columns = ["Stime", "Ltime", "sbytes", "dbytes", "Spkts", "Dpkts"]if len(input_values) < len(expected_columns):st.error(f"Not enough values provided. Expected at least {len(expected_columns)} values.")else:# Take only the first N values neededinput_data = dict(zip(expected_columns, input_values[:len(expected_columns)]))user_input = pd.DataFrame([input_data])# Load model artifactsfeatures_to_drop, category_encodings, model = load_model_artifacts()# Preprocess and predictprocessed_input = preprocess_input(user_input, features_to_drop, category_encodings)if processed_input is not None:prediction = model.predict(processed_input)[0]st.success(f"Prediction: {prediction}")st.markdown("""- **13** → Normal Traffic- **Other values** → Intrusion Category (refer to model documentation for exact mappings)""")else:st.error("Preprocessing failed. Please check your input values.")except Exception as e:st.error(f"An error occurred while processing your input: {e}")1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import streamlit as stimport pandas as pdimport pickleimport joblib# ------------------- Load Artifacts -------------------@st.cache_resourcedef load_model_artifacts():with open('features_to_drop.pkl', 'rb') as f:features_to_drop = pickle.load(f)with open('category_encodings.pkl', 'rb') as f:category_encodings = pickle.load(f)model = joblib.load('xgb_model.pkl')return features_to_drop, category_encodings, model# ------------------- Preprocess Function -------------------def preprocess_input(df, features_to_drop, category_encodings):df = df.copy()# Ensure numeric conversionfor col in ["Stime", "Ltime", "sbytes", "dbytes", "Spkts", "Dpkts"]:df[col] = pd.to_numeric(df[col], errors='coerce')# Create engineered featuresdf["duration"] = df["Ltime"] - df["Stime"]df["byte_ratio"] = df["sbytes"] / (df["dbytes"] + 1)df["pkt_ratio"] = df["Spkts"] / (df["Dpkts"] + 1)# Drop features removed during trainingdrop_cols = list(features_to_drop.intersection(set(df.columns)))df.drop(columns=drop_cols, inplace=True, errors='ignore')# Encode categoricalsfor col, cats in category_encodings.items():if col in df.columns:df[col] = df[col].astype(str)df[col] = pd.Categorical(df[col], categories=cats)df[col] = df[col].cat.codesdf = df.fillna(0)return df# ------------------- Streamlit UI -------------------st.set_page_config("Intrusion Detection - Manual Test", layout="wide")st.title("Intrusion Detection System (IDS) - Manual Row Test")st.markdown("Enter a single row of network traffic data below for intrusion detection:")with st.form("manual_input_form"):col1, col2 = st.columns(2)with col1:Stime = st.number_input("Stime (Start Time)", min_value=0.0, step=1.0)sbytes = st.number_input("sbytes (Source Bytes)", min_value=0, step=1)Spkts = st.number_input("Spkts (Source Packets)", min_value=0, step=1)proto = st.selectbox("Protocol (proto)", ["tcp", "udp", "icmp", "-"])service = st.text_input("Service (e.g., http, dns, -)", value="-")with col2:Ltime = st.number_input("Ltime (End Time)", min_value=0.0, step=1.0)dbytes = st.number_input("dbytes (Destination Bytes)", min_value=0, step=1)Dpkts = st.number_input("Dpkts (Destination Packets)", min_value=0, step=1)state = st.selectbox("State", ["CON", "INT", "FIN", "RST", "REQ", "-"])attack_cat = st.text_input("Optional: Known Attack Category", value="")submitted = st.form_submit_button("Run IDS Prediction")# ------------------- Prediction Logic -------------------if submitted:user_input = pd.DataFrame([{"Stime": Stime,"Ltime": Ltime,"sbytes": sbytes,"dbytes": dbytes,"Spkts": Spkts,"Dpkts": Dpkts,"proto": proto,"service": service,"state": state,"attack_cat": attack_cat # optional}])# Load model artifactsfeatures_to_drop, category_encodings, model = load_model_artifacts()# Preprocess inputprocessed_input = preprocess_input(user_input, features_to_drop, category_encodings)if processed_input is not None:prediction = model.predict(processed_input)[0]st.success(f"Prediction: {prediction}")st.markdown("""- **13** → Normal Traffic- **Other values** → Intrusion Category (refer to model documentation)""")else:st.error("Preprocessing failed. Please check your input fields.")
app.py+64-86