Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
import joblib
|
|
|
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
model = joblib.load("cybersecurity_model.joblib")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
'A_frequency',
|
| 11 |
-
'PTR_frequency',
|
| 12 |
-
'SRV_frequency',
|
| 13 |
-
'
|
| 14 |
-
'
|
| 15 |
-
'
|
| 16 |
-
'
|
| 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
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
df[
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
-
return f"β Error: {str(e)}"
|
| 36 |
|
| 37 |
-
# Gradio
|
| 38 |
-
|
| 39 |
fn=predict_from_csv,
|
| 40 |
inputs=gr.File(label="π Upload CSV File with Network Traffic Features"),
|
| 41 |
-
outputs="
|
| 42 |
title="π¨ Cybersecurity Attack Detector",
|
| 43 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
|
| 5 |
+
# Load trained model
|
| 6 |
model = joblib.load("cybersecurity_model.joblib")
|
| 7 |
|
| 8 |
+
# List of features your model expects (update as per your actual features)
|
| 9 |
+
FEATURE_NAMES = [
|
| 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','rr_name_length',
|
| 13 |
+
'distinct_ns','distinct_ip','unique_country','unique_asn','distinct_domains','reverse_dns',
|
| 14 |
+
'a_records','unique_ttl','ttl_mean','ttl_variance','FQDN_count','subdomain_length','upper',
|
| 15 |
+
'lower','numeric','entropy','special','labels','labels_max','labels_average','longest_word',
|
| 16 |
+
'sld','len','subdomain'
|
|
|
|
| 17 |
]
|
| 18 |
|
| 19 |
+
# Inference function
|
| 20 |
def predict_from_csv(file):
|
| 21 |
try:
|
| 22 |
df = pd.read_csv(file)
|
| 23 |
+
|
| 24 |
+
# Ensure required columns are present
|
| 25 |
+
missing_cols = set(FEATURE_NAMES) - set(df.columns)
|
| 26 |
+
if missing_cols:
|
| 27 |
+
return f"β Missing columns in CSV: {', '.join(missing_cols)}"
|
| 28 |
+
|
| 29 |
+
# Select and predict
|
| 30 |
+
X = df[FEATURE_NAMES]
|
| 31 |
+
preds = model.predict(X)
|
| 32 |
+
|
| 33 |
+
# Format output nicely
|
| 34 |
+
result = pd.DataFrame({"Prediction": preds})
|
| 35 |
+
return result
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"β Error reading file: {str(e)}"
|
| 38 |
|
| 39 |
+
# Gradio UI
|
| 40 |
+
interface = gr.Interface(
|
| 41 |
fn=predict_from_csv,
|
| 42 |
inputs=gr.File(label="π Upload CSV File with Network Traffic Features"),
|
| 43 |
+
outputs=gr.Dataframe(label="π Predictions"),
|
| 44 |
title="π¨ Cybersecurity Attack Detector",
|
| 45 |
+
description=(
|
| 46 |
+
"AI-powered model to detect attacks from DNS/network traffic data. "
|
| 47 |
+
"Upload a CSV file with preprocessed features to get predictions."
|
| 48 |
+
),
|
| 49 |
+
allow_flagging="never"
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
+
interface.launch()
|