Commit
·
174bdeb
1
Parent(s):
fed206b
Update Sniffer_AI(GPS Tracker Dataset).py
Browse files
Sniffer_AI(GPS Tracker Dataset).py
CHANGED
|
@@ -8,12 +8,12 @@ import tempfile
|
|
| 8 |
# Set a custom directory for Gradio's temporary files
|
| 9 |
os.environ["GRADIO_TEMP"] = tempfile.mkdtemp()
|
| 10 |
|
| 11 |
-
# Load the
|
| 12 |
-
|
| 13 |
|
| 14 |
# Define required numeric features
|
| 15 |
numeric_features = [
|
| 16 |
-
"date_numeric", "time_numeric", "
|
| 17 |
]
|
| 18 |
|
| 19 |
# Class labels for attack types
|
|
@@ -31,18 +31,20 @@ class_labels = {
|
|
| 31 |
def convert_datetime_features(log_data):
|
| 32 |
"""Convert date and time into numeric values."""
|
| 33 |
try:
|
| 34 |
-
log_data['date'] = pd.to_datetime(log_data['date'], format='%d-%
|
| 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['time_numeric'] = (
|
|
|
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error processing date/time: {str(e)}"
|
| 41 |
-
|
| 42 |
return log_data
|
| 43 |
|
| 44 |
def detect_intrusion(file):
|
| 45 |
-
"""Process log file and predict attack type."""
|
| 46 |
try:
|
| 47 |
log_data = pd.read_csv(file.name)
|
| 48 |
except Exception as e:
|
|
@@ -55,11 +57,8 @@ def detect_intrusion(file):
|
|
| 55 |
return f"Missing features in file: {', '.join(missing_features)}"
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
log_data['door_state'] = log_data['door_state'].astype(str).str.strip().replace({'closed': 0, 'open': 1})
|
| 59 |
-
log_data['sphone_signal'] = pd.to_numeric(log_data['sphone_signal'], errors='coerce')
|
| 60 |
-
|
| 61 |
feature_values = log_data[numeric_features].astype(float).values
|
| 62 |
-
predictions =
|
| 63 |
except Exception as e:
|
| 64 |
return f"Error during prediction: {str(e)}"
|
| 65 |
|
|
@@ -70,10 +69,10 @@ def detect_intrusion(file):
|
|
| 70 |
log_data['date'] = log_data['date'].dt.strftime('%Y-%m-%d')
|
| 71 |
|
| 72 |
# Select final output columns
|
| 73 |
-
output_df = log_data[['date', 'time', 'Prediction']]
|
| 74 |
|
| 75 |
# Save the output to a CSV file for download
|
| 76 |
-
output_file = "
|
| 77 |
output_df.to_csv(output_file, index=False)
|
| 78 |
|
| 79 |
return output_df, output_file
|
|
@@ -81,15 +80,18 @@ def detect_intrusion(file):
|
|
| 81 |
# Create Gradio interface
|
| 82 |
iface = gr.Interface(
|
| 83 |
fn=detect_intrusion,
|
| 84 |
-
inputs=[gr.File(label="Upload Log File (CSV format)")],
|
| 85 |
-
outputs=[
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
description=(
|
| 88 |
"""
|
| 89 |
-
Upload a
|
| 90 |
-
date,time,
|
| 91 |
Example:
|
| 92 |
-
|
| 93 |
"""
|
| 94 |
)
|
| 95 |
)
|
|
|
|
| 8 |
# Set a custom directory for Gradio's temporary files
|
| 9 |
os.environ["GRADIO_TEMP"] = tempfile.mkdtemp()
|
| 10 |
|
| 11 |
+
# Load the trained Decision Tree model
|
| 12 |
+
decision_tree_model = joblib.load('decision_tree_model.pkl') # Update path if necessary
|
| 13 |
|
| 14 |
# Define required numeric features
|
| 15 |
numeric_features = [
|
| 16 |
+
"date_numeric", "time_numeric", "latitude", "longitude", "label"
|
| 17 |
]
|
| 18 |
|
| 19 |
# Class labels for attack types
|
|
|
|
| 31 |
def convert_datetime_features(log_data):
|
| 32 |
"""Convert date and time into numeric values."""
|
| 33 |
try:
|
| 34 |
+
log_data['date'] = pd.to_datetime(log_data['date'], format='%d-%b-%y', errors='coerce')
|
| 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['time_numeric'] = (
|
| 39 |
+
time_parsed.dt.hour * 3600 + time_parsed.dt.minute * 60 + time_parsed.dt.second
|
| 40 |
+
)
|
| 41 |
except Exception as e:
|
| 42 |
return f"Error processing date/time: {str(e)}"
|
| 43 |
+
|
| 44 |
return log_data
|
| 45 |
|
| 46 |
def detect_intrusion(file):
|
| 47 |
+
"""Process GPS tracker log file and predict attack type."""
|
| 48 |
try:
|
| 49 |
log_data = pd.read_csv(file.name)
|
| 50 |
except Exception as e:
|
|
|
|
| 57 |
return f"Missing features in file: {', '.join(missing_features)}"
|
| 58 |
|
| 59 |
try:
|
|
|
|
|
|
|
|
|
|
| 60 |
feature_values = log_data[numeric_features].astype(float).values
|
| 61 |
+
predictions = model.predict(feature_values)
|
| 62 |
except Exception as e:
|
| 63 |
return f"Error during prediction: {str(e)}"
|
| 64 |
|
|
|
|
| 69 |
log_data['date'] = log_data['date'].dt.strftime('%Y-%m-%d')
|
| 70 |
|
| 71 |
# Select final output columns
|
| 72 |
+
output_df = log_data[['date', 'time', 'latitude', 'longitude', 'Prediction']]
|
| 73 |
|
| 74 |
# Save the output to a CSV file for download
|
| 75 |
+
output_file = "gps_intrusion_results.csv"
|
| 76 |
output_df.to_csv(output_file, index=False)
|
| 77 |
|
| 78 |
return output_df, output_file
|
|
|
|
| 80 |
# Create Gradio interface
|
| 81 |
iface = gr.Interface(
|
| 82 |
fn=detect_intrusion,
|
| 83 |
+
inputs=[gr.File(label="Upload GPS Tracker Log File (CSV format)")],
|
| 84 |
+
outputs=[
|
| 85 |
+
gr.Dataframe(label="Intrusion Detection Results"),
|
| 86 |
+
gr.File(label="Download Predictions CSV")
|
| 87 |
+
],
|
| 88 |
+
title="GPS Tracker Intrusion Detection System",
|
| 89 |
description=(
|
| 90 |
"""
|
| 91 |
+
Upload a GPS log file with the following features:
|
| 92 |
+
date,time,latitude,longitude,label,type
|
| 93 |
Example:
|
| 94 |
+
25-Apr-19,18:31:39,116.521704,132.162504,1,ddos
|
| 95 |
"""
|
| 96 |
)
|
| 97 |
)
|