Spaces:
Runtime error
Runtime error
Commit
•
b3a8c59
1
Parent(s):
84de915
create app.py file
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
22 |
+
benign_dataset = benign_dataset[benign_dataset.duplicated(keep=False) == False]
|
23 |
+
# Combining both datasets together
|
24 |
+
all_flows = pd.concat([malicious_dataset, benign_dataset])
|
25 |
+
# Reducing the size of the dataset to reduce the amount of time taken in training models
|
26 |
+
reduced_dataset = all_flows.sample(38000)
|
27 |
+
#dataset with columns with nan values dropped
|
28 |
+
df = reduced_dataset.drop(reduced_dataset.columns[np.isnan(reduced_dataset).any()], axis=1)
|
29 |
+
#### Isolating independent and dependent variables for training dataset
|
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([
|
43 |
+
tf.keras.layers.Dense(32, activation ='relu', input_shape=(373,)),
|
44 |
+
tf.keras.layers.Dense(32, activation = 'relu'),
|
45 |
+
tf.keras.layers.Dense(1, activation = 'sigmoid'),
|
46 |
+
])
|
47 |
+
|
48 |
+
|
49 |
+
model.compile(optimizer ='adam',
|
50 |
+
loss = 'binary_crossentropy',
|
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 = 150,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
|
59 |
+
plt.plot(history.history['accuracy'])
|
60 |
+
plt.plot(history.history['val_accuracy'])
|
61 |
+
plt.title('model accuracy')
|
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'])
|
69 |
+
plt.plot(history.history['val_loss'])
|
70 |
+
plt.title('model loss')
|
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()
|
87 |
+
|