MrUtakata commited on
Commit
edd1c24
·
verified ·
1 Parent(s): f6e4e35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -16,36 +16,48 @@ 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}")
 
16
  category_encodings = pickle.load(f)
17
 
18
  # ---------------------------
19
+ # Define column names (excluding 'attack_cat')
20
+ # ---------------------------
21
+ columns = [
22
+ 'srcip', 'sport', 'dstip', 'dsport', 'proto', 'state', 'dur', 'sbytes', 'dbytes',
23
+ 'sttl', 'dttl', 'sloss', 'dloss', 'service', 'Sload', 'Dload', 'Spkts', 'Dpkts',
24
+ 'swin', 'dwin', 'stcpb', 'dtcpb', 'smeansz', 'dmeansz', 'trans_depth', 'res_bdy_len',
25
+ 'Sjit', 'Djit', 'Stime', 'Ltime', 'Sintpkt', 'Dintpkt', 'tcprtt', 'synack', 'ackdat',
26
+ 'is_sm_ips_ports', 'ct_state_ttl', 'ct_flw_http_mthd', 'is_ftp_login', 'ct_ftp_cmd',
27
+ 'ct_srv_src', 'ct_srv_dst', 'ct_dst_ltm', 'ct_src_ ltm', 'ct_src_dport_ltm',
28
+ 'ct_dst_sport_ltm', 'ct_dst_src_ltm', 'Label', 'duration', 'byte_ratio', 'pkt_ratio'
29
+ ]
30
+
31
+ # ---------------------------
32
+ # Streamlit UI
33
  # ---------------------------
34
  st.title("🚨 Intrusion Detection In Networks")
35
+ st.markdown("""
36
+ Paste a single preprocessed row of NB15 features (comma-separated) below.
37
+ Make sure it includes exactly **{}** comma-separated values matching the model's input format.
38
+ """.format(len(columns)))
39
 
40
+ user_input = st.text_area("📥 Input a row:", height=150)
41
 
42
  if st.button("Predict"):
43
  try:
 
44
  row = [x.strip() for x in user_input.strip().split(",")]
45
+
 
 
 
 
 
 
46
  if len(row) != len(columns):
47
+ st.error(f"Expected {len(columns)} values, but got {len(row)}.")
48
  else:
49
+ # Convert to DataFrame
50
+ input_df = pd.DataFrame([row], columns=columns)
51
+
52
+ # Convert to numeric
53
  input_df = input_df.apply(pd.to_numeric, errors='coerce')
54
 
55
+ # Drop the high-correlation features
56
  input_df = input_df.drop(columns=features_to_drop, errors='ignore')
57
 
58
+ # Prediction
59
  prediction = model.predict(input_df)[0]
60
  st.success(f"🛡️ Predicted Intrusion Category: **{prediction}**")
61
+
62
  except Exception as e:
63
+ st.error(f"⚠️ Error during prediction: {e}")