MrUtakata commited on
Commit
2434a2a
·
verified ·
1 Parent(s): 6cf0498

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -38
app.py CHANGED
@@ -61,52 +61,57 @@ def preprocess_input(df, features_to_drop, category_encodings):
61
  return df
62
 
63
  ##############################################
64
- # Streamlit User Interface - Manual Row Input
65
  ##############################################
66
 
67
  st.set_page_config(page_title="Intrusion Detection System - Test", layout="wide")
68
- st.title("Intrusion Detection System (IDS)")
69
 
70
- st.markdown(
71
- """
72
- Enter a single row of network traffic data below to test for possible intrusions.
73
- """
74
- )
75
 
76
- with st.form("manual_input_form"):
77
- Stime = st.number_input("Stime (Start Time)", min_value=0.0, step=1.0)
78
- Ltime = st.number_input("Ltime (End Time)", min_value=0.0, step=1.0)
79
- sbytes = st.number_input("sbytes (Source Bytes)", min_value=0, step=1)
80
- dbytes = st.number_input("dbytes (Destination Bytes)", min_value=0, step=1)
81
- Spkts = st.number_input("Spkts (Source Packets)", min_value=0, step=1)
82
- Dpkts = st.number_input("Dpkts (Destination Packets)", min_value=0, step=1)
83
- # Add more input fields as needed
84
 
 
 
 
 
 
85
  submitted = st.form_submit_button("Run IDS Prediction")
86
 
87
  if submitted:
88
- # Construct a DataFrame from input
89
- user_input = pd.DataFrame([{
90
- "Stime": Stime,
91
- "Ltime": Ltime,
92
- "sbytes": sbytes,
93
- "dbytes": dbytes,
94
- "Spkts": Spkts,
95
- "Dpkts": Dpkts
96
- }])
97
-
98
- # Load model artifacts
99
- features_to_drop, category_encodings, model = load_model_artifacts()
 
 
 
 
100
 
101
- # Preprocess and predict
102
- processed_input = preprocess_input(user_input, features_to_drop, category_encodings)
103
 
104
- if processed_input is not None:
105
- prediction = model.predict(processed_input)[0]
106
- st.success(f"Prediction: {prediction}")
107
- st.markdown("""
108
- - **13** → Normal Traffic
109
- - **Other values** → Intrusion Category (refer to model documentation for exact mappings)
110
- """)
111
- else:
112
- st.error("Preprocessing failed. Please check your inputs.")
 
 
 
61
  return df
62
 
63
  ##############################################
64
+ # Streamlit Interface - TextArea Input
65
  ##############################################
66
 
67
  st.set_page_config(page_title="Intrusion Detection System - Test", layout="wide")
68
+ st.title("Intrusion Detection System (IDS) - Single Row Input")
69
 
70
+ st.markdown("""
71
+ Paste a **single row of comma-separated values** below. Include only the relevant features required for prediction.
 
 
 
72
 
73
+ **Expected columns (in order):**
74
+ `Stime, Ltime, sbytes, dbytes, Spkts, Dpkts`
75
+
76
+ You may include additional columns (like `label`) if desired, but they will be ignored.
77
+ """)
 
 
 
78
 
79
+ with st.form("manual_input_form"):
80
+ text_input = st.text_area(
81
+ "Paste a single row of data (comma-separated values):",
82
+ placeholder="e.g. 1425579984.0,1425579990.0,275,423,10,8"
83
+ )
84
  submitted = st.form_submit_button("Run IDS Prediction")
85
 
86
  if submitted:
87
+ try:
88
+ # Split and parse the user input into a list of values
89
+ input_values = [x.strip() for x in text_input.split(',') if x.strip() != '']
90
+
91
+ # Define the column names expected by the model
92
+ expected_columns = ["Stime", "Ltime", "sbytes", "dbytes", "Spkts", "Dpkts"]
93
+
94
+ if len(input_values) < len(expected_columns):
95
+ st.error(f"Not enough values provided. Expected at least {len(expected_columns)} values.")
96
+ else:
97
+ # Take only the first N values needed
98
+ input_data = dict(zip(expected_columns, input_values[:len(expected_columns)]))
99
+ user_input = pd.DataFrame([input_data])
100
+
101
+ # Load model artifacts
102
+ features_to_drop, category_encodings, model = load_model_artifacts()
103
 
104
+ # Preprocess and predict
105
+ processed_input = preprocess_input(user_input, features_to_drop, category_encodings)
106
 
107
+ if processed_input is not None:
108
+ prediction = model.predict(processed_input)[0]
109
+ st.success(f"Prediction: {prediction}")
110
+ st.markdown("""
111
+ - **13** → Normal Traffic
112
+ - **Other values** → Intrusion Category (refer to model documentation for exact mappings)
113
+ """)
114
+ else:
115
+ st.error("Preprocessing failed. Please check your input values.")
116
+ except Exception as e:
117
+ st.error(f"An error occurred while processing your input: {e}")