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