Update app.py
Browse files
app.py
CHANGED
|
@@ -61,52 +61,57 @@ def preprocess_input(df, features_to_drop, category_encodings):
|
|
| 61 |
return df
|
| 62 |
|
| 63 |
##############################################
|
| 64 |
-
# Streamlit
|
| 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 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 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 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
"
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
| 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}")
|