Ravishankarsharma commited on
Commit
cad9e4e
·
verified ·
1 Parent(s): 5aaf6e7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import numpy as np
4
+
5
+ # Load your saved model
6
+ model = joblib.load('cybersecurity_model.joblib')
7
+
8
+ # Replace this list with your actual feature names in the order your model expects
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',
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', 'timestamp', 'FQDN_count', 'subdomain_length', 'upper',
16
+ 'lower', 'numeric', 'entropy'
17
+ ]
18
+
19
+ def predict(*inputs):
20
+ # Convert inputs to numpy array, reshape for prediction
21
+ data = np.array(inputs).reshape(1, -1)
22
+
23
+ # Predict class label
24
+ prediction = model.predict(data)[0]
25
+
26
+ # Optionally, return class probabilities:
27
+ # proba = model.predict_proba(data)[0]
28
+ # return prediction, proba
29
+
30
+ return prediction
31
+
32
+ # Create Gradio inputs dynamically based on feature names
33
+ inputs = [gr.Number(label=feat) for feat in FEATURE_NAMES]
34
+
35
+ # Gradio Interface setup
36
+ iface = gr.Interface(
37
+ fn=predict,
38
+ inputs=inputs,
39
+ outputs="text",
40
+ title="Cybersecurity Attack Detection",
41
+ description="Input the feature values and predict whether the network activity is benign or an attack."
42
+ )
43
+
44
+ if __name__ == "__main__":
45
+ iface.launch()