Ravishankarsharma commited on
Commit
45b6850
·
verified ·
1 Parent(s): e23f7a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -1,44 +1,46 @@
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__":
 
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__":