Commit
·
de02c0f
1
Parent(s):
c0ea06e
Update app.py
Browse files
app.py
CHANGED
@@ -11,9 +11,10 @@ os.environ["GRADIO_TEMP"] = tempfile.mkdtemp()
|
|
11 |
# Load the saved Random Forest model
|
12 |
rf_model = joblib.load('rf_model.pkl') # Ensure the correct model path
|
13 |
|
14 |
-
# Define numeric features
|
15 |
numeric_features = [
|
16 |
-
"date_numeric", "
|
|
|
17 |
]
|
18 |
|
19 |
# Class labels for attack types
|
@@ -35,7 +36,8 @@ def convert_datetime_features(log_data):
|
|
35 |
log_data['date_numeric'] = log_data['date'].astype(np.int64) // 10**9
|
36 |
|
37 |
time_parsed = pd.to_datetime(log_data['time'], format='%H:%M:%S', errors='coerce')
|
38 |
-
log_data['
|
|
|
39 |
except Exception as e:
|
40 |
return f"Error processing date/time: {str(e)}"
|
41 |
|
@@ -50,18 +52,14 @@ def detect_intrusion(file):
|
|
50 |
|
51 |
log_data = convert_datetime_features(log_data)
|
52 |
|
53 |
-
# Ensure required features exist
|
54 |
missing_features = [feature for feature in numeric_features if feature not in log_data.columns]
|
55 |
if missing_features:
|
56 |
return f"Missing features in file: {', '.join(missing_features)}"
|
57 |
|
58 |
try:
|
59 |
-
# Convert categorical and numeric values
|
60 |
log_data['door_state'] = log_data['door_state'].astype(str).str.strip().replace({'closed': 0, 'open': 1})
|
61 |
log_data['sphone_signal'] = pd.to_numeric(log_data['sphone_signal'], errors='coerce')
|
62 |
-
log_data['label'] = pd.to_numeric(log_data['label'], errors='coerce')
|
63 |
|
64 |
-
# Extract only the required numeric features
|
65 |
feature_values = log_data[numeric_features].astype(float).values
|
66 |
predictions = rf_model.predict(feature_values)
|
67 |
except Exception as e:
|
@@ -88,12 +86,14 @@ iface = gr.Interface(
|
|
88 |
inputs=[gr.File(label="Upload Log File (CSV format)")],
|
89 |
outputs=[gr.Dataframe(label="Intrusion Detection Results"), gr.File(label="Download Predictions CSV")],
|
90 |
title="Intrusion Detection System",
|
91 |
-
description=(
|
|
|
92 |
Upload a CSV log file with the following features:
|
93 |
date,time,door_state,sphone_signal,label
|
94 |
Example:
|
95 |
-
26-04-19,13:59:20,1,-85,
|
96 |
-
|
|
|
97 |
)
|
98 |
|
99 |
iface.launch()
|
|
|
11 |
# Load the saved Random Forest model
|
12 |
rf_model = joblib.load('rf_model.pkl') # Ensure the correct model path
|
13 |
|
14 |
+
# Define required numeric features
|
15 |
numeric_features = [
|
16 |
+
"date_numeric", "total_minutes", "seconds",
|
17 |
+
"door_state", "sphone_signal", "label"
|
18 |
]
|
19 |
|
20 |
# Class labels for attack types
|
|
|
36 |
log_data['date_numeric'] = log_data['date'].astype(np.int64) // 10**9
|
37 |
|
38 |
time_parsed = pd.to_datetime(log_data['time'], format='%H:%M:%S', errors='coerce')
|
39 |
+
log_data['total_minutes'] = (time_parsed.dt.hour * 60) + time_parsed.dt.minute
|
40 |
+
log_data['seconds'] = time_parsed.dt.second
|
41 |
except Exception as e:
|
42 |
return f"Error processing date/time: {str(e)}"
|
43 |
|
|
|
52 |
|
53 |
log_data = convert_datetime_features(log_data)
|
54 |
|
|
|
55 |
missing_features = [feature for feature in numeric_features if feature not in log_data.columns]
|
56 |
if missing_features:
|
57 |
return f"Missing features in file: {', '.join(missing_features)}"
|
58 |
|
59 |
try:
|
|
|
60 |
log_data['door_state'] = log_data['door_state'].astype(str).str.strip().replace({'closed': 0, 'open': 1})
|
61 |
log_data['sphone_signal'] = pd.to_numeric(log_data['sphone_signal'], errors='coerce')
|
|
|
62 |
|
|
|
63 |
feature_values = log_data[numeric_features].astype(float).values
|
64 |
predictions = rf_model.predict(feature_values)
|
65 |
except Exception as e:
|
|
|
86 |
inputs=[gr.File(label="Upload Log File (CSV format)")],
|
87 |
outputs=[gr.Dataframe(label="Intrusion Detection Results"), gr.File(label="Download Predictions CSV")],
|
88 |
title="Intrusion Detection System",
|
89 |
+
description=(
|
90 |
+
"""
|
91 |
Upload a CSV log file with the following features:
|
92 |
date,time,door_state,sphone_signal,label
|
93 |
Example:
|
94 |
+
26-04-19,13:59:20,1,-85,normal
|
95 |
+
"""
|
96 |
+
)
|
97 |
)
|
98 |
|
99 |
iface.launch()
|