Ravishankarsharma commited on
Commit
89c0e77
Β·
verified Β·
1 Parent(s): 45b6850

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -29
app.py CHANGED
@@ -1,47 +1,53 @@
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__":
47
- iface.launch()
 
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()