santanupoddar commited on
Commit
bd57e00
1 Parent(s): 38a82a5

updated app py file

Browse files
Files changed (1) hide show
  1. app.py +29 -20
app.py CHANGED
@@ -1,21 +1,28 @@
1
- import gradio as gr
 
 
 
2
 
 
3
 
 
4
  import pandas as pd
5
  from math import sqrt;
6
- from sklearn import preprocessing
7
- from sklearn.ensemble import RandomForestClassifier
8
- from sklearn.linear_model import LogisticRegression;
9
- from sklearn.metrics import accuracy_score, r2_score, confusion_matrix, mean_absolute_error, mean_squared_error, f1_score, log_loss
10
- from sklearn.model_selection import train_test_split
11
  import numpy as np
12
  import matplotlib.pyplot as plt
13
- import seaborn as sns
14
- import joblib
15
  #load packages for ANN
16
  import tensorflow as tf
17
 
18
  def malware_detection_DL (results, malicious_traffic, benign_traffic):
 
 
 
 
 
 
 
 
 
19
  malicious_dataset = pd.read_csv(malicious_traffic) #Importing Datasets
20
  benign_dataset = pd.read_csv(benign_traffic)
21
  # Removing duplicated rows from benign_dataset (5380 rows removed)
@@ -30,13 +37,13 @@ def malware_detection_DL (results, malicious_traffic, benign_traffic):
30
  reduced_y = df['isMalware']
31
  reduced_x = df.drop(['isMalware'], axis=1);
32
  # Splitting datasets into training and test data
33
- x_train, x_test, y_train, y_test = train_test_split(reduced_x, reduced_y, test_size=0.2, random_state=42)
34
 
35
  #scale data between 0 and 1
36
- min_max_scaler = preprocessing.MinMaxScaler()
37
- x_scale = min_max_scaler.fit_transform(reduced_x)
38
  # Splitting datasets into training and test data
39
- x_train, x_test, y_train, y_test = train_test_split(x_scale, reduced_y, test_size=0.2, random_state=42)
40
  #type of layers in ann model is sequential, dense and uses relu activation
41
  ann = tf.keras.models.Sequential()
42
  model = tf.keras.Sequential([
@@ -51,8 +58,8 @@ def malware_detection_DL (results, malicious_traffic, benign_traffic):
51
  metrics = ['accuracy'])
52
  #model.fit(x_train, y_train, batch_size=32, epochs = 150, validation_data=(x_test, y_test))
53
  #does not output epochs and gives evalutaion of validation data and history of losses and accuracy
54
- history = model.fit(x_train, y_train, batch_size=32, epochs = 10,verbose=0, validation_data=(x_test, y_test))
55
- _, accuracy = model.evaluate(x_train, y_train)
56
  #return history.history
57
  if results=="Accuracy":
58
  #summarize history for accuracy
@@ -62,7 +69,8 @@ def malware_detection_DL (results, malicious_traffic, benign_traffic):
62
  plt.ylabel('accuracy')
63
  plt.xlabel('epoch')
64
  plt.legend(['train', 'test'], loc='upper left')
65
- return plt.show()
 
66
  else:
67
  # summarize history for loss
68
  plt.plot(history.history['loss'])
@@ -71,17 +79,18 @@ def malware_detection_DL (results, malicious_traffic, benign_traffic):
71
  plt.ylabel('loss')
72
  plt.xlabel('epoch')
73
  plt.legend(['train', 'test'], loc='upper left')
74
- return plt.show()
 
75
 
76
 
77
 
78
  iface = gr.Interface(
79
  malware_detection_DL, [gr.inputs.Dropdown(["Accuracy","Loss"], label="Result Type"),
80
- gr.inputs.Dropdown(["malicious_flows.csv"], label = "Malicious traffic in .csv"), gr.inputs.Dropdown(["sample_benign_flows.csv"], label="Benign Traffic in .csv")
81
- ], "plot",
 
82
 
83
 
84
  )
85
 
86
- iface.launch(inline=True, enable_queue=True, show_error=True)
87
-
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[2]:
5
 
6
+ import gradio as gr
7
 
8
+ import os
9
  import pandas as pd
10
  from math import sqrt;
 
 
 
 
 
11
  import numpy as np
12
  import matplotlib.pyplot as plt
 
 
13
  #load packages for ANN
14
  import tensorflow as tf
15
 
16
  def malware_detection_DL (results, malicious_traffic, benign_traffic):
17
+ plt.clf()
18
+ if os.path.exists("accplot.png"):
19
+ os.remove("accplot.png")
20
+ else:
21
+ pass
22
+ if os.path.exists("lossplot.png"):
23
+ os.remove("lossplot.png")
24
+ else:
25
+ pass
26
  malicious_dataset = pd.read_csv(malicious_traffic) #Importing Datasets
27
  benign_dataset = pd.read_csv(benign_traffic)
28
  # Removing duplicated rows from benign_dataset (5380 rows removed)
 
37
  reduced_y = df['isMalware']
38
  reduced_x = df.drop(['isMalware'], axis=1);
39
  # Splitting datasets into training and test data
40
+ #x_train, x_test, y_train, y_test = train_test_split(reduced_x, reduced_y, test_size=0.2, random_state=42)
41
 
42
  #scale data between 0 and 1
43
+ #min_max_scaler = preprocessing.MinMaxScaler()
44
+ #x_scale = min_max_scaler.fit_transform(reduced_x)
45
  # Splitting datasets into training and test data
46
+ #x_train, x_test, y_train, y_test = train_test_split(x_scale, reduced_y, test_size=0.2, random_state=42)
47
  #type of layers in ann model is sequential, dense and uses relu activation
48
  ann = tf.keras.models.Sequential()
49
  model = tf.keras.Sequential([
 
58
  metrics = ['accuracy'])
59
  #model.fit(x_train, y_train, batch_size=32, epochs = 150, validation_data=(x_test, y_test))
60
  #does not output epochs and gives evalutaion of validation data and history of losses and accuracy
61
+ history = model.fit(reduced_x, reduced_y,validation_split=0.33, batch_size=32, epochs = 10,verbose=0)
62
+ _, accuracy = model.evaluate(reduced_x, reduced_y)
63
  #return history.history
64
  if results=="Accuracy":
65
  #summarize history for accuracy
 
69
  plt.ylabel('accuracy')
70
  plt.xlabel('epoch')
71
  plt.legend(['train', 'test'], loc='upper left')
72
+ plt.savefig('accplot.png')
73
+ return "accplot.png",accuracy
74
  else:
75
  # summarize history for loss
76
  plt.plot(history.history['loss'])
 
79
  plt.ylabel('loss')
80
  plt.xlabel('epoch')
81
  plt.legend(['train', 'test'], loc='upper left')
82
+ plt.savefig('lossplot.png')
83
+ return 'lossplot.png',accuracy
84
 
85
 
86
 
87
  iface = gr.Interface(
88
  malware_detection_DL, [gr.inputs.Dropdown(["Accuracy","Loss"], label="Result Type"),
89
+ gr.inputs.Dropdown(["malicious_flows.csv"], label = "Malicious traffic in .csv"),
90
+ gr.inputs.Dropdown(["sample_benign_flows.csv"], label="Benign Traffic in .csv")
91
+ ],["image","text"], theme="grass"
92
 
93
 
94
  )
95
 
96
+ iface.launch(enable_queue = True)