Sellibro commited on
Commit
9d7b5d7
·
1 Parent(s): 546ca6b

Create app.py

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