Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,46 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import joblib
|
| 3 |
-
import numpy as np
|
| 4 |
|
| 5 |
-
# Load your
|
| 6 |
-
model = joblib.load(
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
'A_frequency', 'NS_frequency', 'CNAME_frequency', 'SOA_frequency', 'NULL_frequency',
|
| 11 |
'PTR_frequency', 'HINFO_frequency', 'MX_frequency', 'TXT_frequency', 'AAAA_frequency',
|
| 12 |
'SRV_frequency', 'OPT_frequency', 'rr_type', 'rr_count', 'rr_name_entropy',
|
| 13 |
'rr_name_length', 'distinct_ns', 'distinct_ip', 'unique_country', 'unique_asn',
|
| 14 |
'distinct_domains', 'reverse_dns', 'a_records', 'unique_ttl', 'ttl_mean',
|
| 15 |
-
'ttl_variance', '
|
| 16 |
-
'
|
|
|
|
| 17 |
]
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
iface = gr.Interface(
|
| 37 |
-
fn=
|
| 38 |
-
inputs=
|
| 39 |
-
outputs="
|
| 40 |
-
title="Cybersecurity Attack
|
| 41 |
-
description="
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
import joblib
|
|
|
|
| 4 |
|
| 5 |
+
# Load your trained model
|
| 6 |
+
model = joblib.load("cybersecurity_model.joblib")
|
| 7 |
|
| 8 |
+
# Exact features the model was trained on
|
| 9 |
+
FEATURES = [
|
| 10 |
'A_frequency', 'NS_frequency', 'CNAME_frequency', 'SOA_frequency', 'NULL_frequency',
|
| 11 |
'PTR_frequency', 'HINFO_frequency', 'MX_frequency', 'TXT_frequency', 'AAAA_frequency',
|
| 12 |
'SRV_frequency', 'OPT_frequency', 'rr_type', 'rr_count', 'rr_name_entropy',
|
| 13 |
'rr_name_length', 'distinct_ns', 'distinct_ip', 'unique_country', 'unique_asn',
|
| 14 |
'distinct_domains', 'reverse_dns', 'a_records', 'unique_ttl', 'ttl_mean',
|
| 15 |
+
'ttl_variance', 'FQDN_count', 'subdomain_length', 'upper', 'lower',
|
| 16 |
+
'numeric', 'entropy', 'special', 'labels', 'labels_max', 'labels_average',
|
| 17 |
+
'longest_word', 'sld', 'len', 'subdomain'
|
| 18 |
]
|
| 19 |
|
| 20 |
+
def predict_from_csv(file):
|
| 21 |
+
try:
|
| 22 |
+
df = pd.read_csv(file)
|
| 23 |
+
|
| 24 |
+
# Ensure only expected features are used
|
| 25 |
+
data = df[FEATURES]
|
| 26 |
+
|
| 27 |
+
# Make predictions
|
| 28 |
+
predictions = model.predict(data)
|
| 29 |
+
|
| 30 |
+
df['Prediction'] = predictions
|
| 31 |
+
|
| 32 |
+
return df[['Prediction']]
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"❌ Error: {str(e)}"
|
| 36 |
+
|
| 37 |
+
# Gradio Interface
|
| 38 |
iface = gr.Interface(
|
| 39 |
+
fn=predict_from_csv,
|
| 40 |
+
inputs=gr.File(label="📁 Upload CSV File with Network Traffic Features"),
|
| 41 |
+
outputs="dataframe",
|
| 42 |
+
title="🚨 Cybersecurity Attack Detector",
|
| 43 |
+
description="AI-powered model to detect attacks from DNS/network traffic data. Upload a CSV file with preprocessed features to get predictions."
|
| 44 |
)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|