santanupoddar commited on
Commit
34841fa
1 Parent(s): 63208a3

new file ipynb

Browse files
Malware_Detection_with_Gradio-Final.ipynb ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 4,
6
+ "id": "complete-wealth",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "ename": "ContextualVersionConflict",
11
+ "evalue": "(anyio 2.2.0 (/opt/anaconda3/lib/python3.9/site-packages), Requirement.parse('anyio<4,>=3.0.0'), {'starlette'})",
12
+ "output_type": "error",
13
+ "traceback": [
14
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
15
+ "\u001b[0;31mContextualVersionConflict\u001b[0m Traceback (most recent call last)",
16
+ "\u001b[0;32m/var/folders/01/vtqqk20n4gq6wxn80d0ly7v80000gn/T/ipykernel_18714/2264163249.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mgradio\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mgr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpandas\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mmath\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msqrt\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
17
+ "\u001b[0;32m/opt/anaconda3/lib/python3.9/site-packages/gradio/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgradio\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatic\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mButton\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mMarkdown\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0mcurrent_pkg_version\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpkg_resources\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequire\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"gradio\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mversion\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0m__version__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcurrent_pkg_version\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
18
+ "\u001b[0;32m/opt/anaconda3/lib/python3.9/site-packages/pkg_resources/__init__.py\u001b[0m in \u001b[0;36mrequire\u001b[0;34m(self, *requirements)\u001b[0m\n\u001b[1;32m 884\u001b[0m \u001b[0mincluded\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meven\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mthey\u001b[0m \u001b[0mwere\u001b[0m \u001b[0malready\u001b[0m \u001b[0mactivated\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mthis\u001b[0m \u001b[0mworking\u001b[0m \u001b[0mset\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 885\u001b[0m \"\"\"\n\u001b[0;32m--> 886\u001b[0;31m \u001b[0mneeded\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresolve\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparse_requirements\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrequirements\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 887\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 888\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mdist\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mneeded\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
19
+ "\u001b[0;32m/opt/anaconda3/lib/python3.9/site-packages/pkg_resources/__init__.py\u001b[0m in \u001b[0;36mresolve\u001b[0;34m(self, requirements, env, installer, replace_conflicting, extras)\u001b[0m\n\u001b[1;32m 775\u001b[0m \u001b[0;31m# Oops, the \"best\" so far conflicts with a dependency\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 776\u001b[0m \u001b[0mdependent_req\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrequired_by\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mreq\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 777\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mVersionConflict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreq\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwith_context\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdependent_req\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 778\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 779\u001b[0m \u001b[0;31m# push the new requirements onto the stack\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
20
+ "\u001b[0;31mContextualVersionConflict\u001b[0m: (anyio 2.2.0 (/opt/anaconda3/lib/python3.9/site-packages), Requirement.parse('anyio<4,>=3.0.0'), {'starlette'})"
21
+ ]
22
+ }
23
+ ],
24
+ "source": [
25
+ "import gradio as gr\n",
26
+ "\n",
27
+ "\n",
28
+ "import pandas as pd\n",
29
+ "from math import sqrt;\n",
30
+ "from sklearn import preprocessing\n",
31
+ "from sklearn.ensemble import RandomForestClassifier\n",
32
+ "from sklearn.linear_model import LogisticRegression;\n",
33
+ "from sklearn.metrics import accuracy_score, r2_score, confusion_matrix, mean_absolute_error, mean_squared_error, f1_score, log_loss\n",
34
+ "from sklearn.model_selection import train_test_split\n",
35
+ "import numpy as np\n",
36
+ "import matplotlib.pyplot as plt\n",
37
+ "import seaborn as sns \n",
38
+ "import joblib\n",
39
+ " #load packages for ANN\n",
40
+ "import tensorflow as tf\n",
41
+ " \n",
42
+ "def malware_detection_DL (results, malicious_traffic, benign_traffic):\n",
43
+ " malicious_dataset = pd.read_csv(malicious_traffic) #Importing Datasets \n",
44
+ " benign_dataset = pd.read_csv(benign_traffic)\n",
45
+ " # Removing duplicated rows from benign_dataset (5380 rows removed)\n",
46
+ " benign_dataset = benign_dataset[benign_dataset.duplicated(keep=False) == False]\n",
47
+ " # Combining both datasets together\n",
48
+ " all_flows = pd.concat([malicious_dataset, benign_dataset])\n",
49
+ " # Reducing the size of the dataset to reduce the amount of time taken in training models\n",
50
+ " reduced_dataset = all_flows.sample(38000)\n",
51
+ " #dataset with columns with nan values dropped\n",
52
+ " df = reduced_dataset.drop(reduced_dataset.columns[np.isnan(reduced_dataset).any()], axis=1)\n",
53
+ " #### Isolating independent and dependent variables for training dataset\n",
54
+ " reduced_y = df['isMalware']\n",
55
+ " reduced_x = df.drop(['isMalware'], axis=1);\n",
56
+ " # Splitting datasets into training and test data\n",
57
+ " x_train, x_test, y_train, y_test = train_test_split(reduced_x, reduced_y, test_size=0.2, random_state=42)\n",
58
+ " \n",
59
+ " #scale data between 0 and 1\n",
60
+ " min_max_scaler = preprocessing.MinMaxScaler()\n",
61
+ " x_scale = min_max_scaler.fit_transform(reduced_x)\n",
62
+ " # Splitting datasets into training and test data\n",
63
+ " x_train, x_test, y_train, y_test = train_test_split(x_scale, reduced_y, test_size=0.2, random_state=42)\n",
64
+ " #type of layers in ann model is sequential, dense and uses relu activation \n",
65
+ " ann = tf.keras.models.Sequential()\n",
66
+ " model = tf.keras.Sequential([\n",
67
+ " tf.keras.layers.Dense(32, activation ='relu', input_shape=(373,)),\n",
68
+ " tf.keras.layers.Dense(32, activation = 'relu'),\n",
69
+ " tf.keras.layers.Dense(1, activation = 'sigmoid'),\n",
70
+ " ])\n",
71
+ " \n",
72
+ " \n",
73
+ " model.compile(optimizer ='adam', \n",
74
+ " loss = 'binary_crossentropy',\n",
75
+ " metrics = ['accuracy'])\n",
76
+ " #model.fit(x_train, y_train, batch_size=32, epochs = 150, validation_data=(x_test, y_test))\n",
77
+ " #does not output epochs and gives evalutaion of validation data and history of losses and accuracy\n",
78
+ " history = model.fit(x_train, y_train, batch_size=32, epochs = 150,verbose=0, validation_data=(x_test, y_test))\n",
79
+ " _, accuracy = model.evaluate(x_train, y_train)\n",
80
+ " #return history.history\n",
81
+ " if results==\"Accuracy\":\n",
82
+ " #summarize history for accuracy\n",
83
+ " plt.plot(history.history['accuracy'])\n",
84
+ " plt.plot(history.history['val_accuracy'])\n",
85
+ " plt.title('model accuracy')\n",
86
+ " plt.ylabel('accuracy')\n",
87
+ " plt.xlabel('epoch')\n",
88
+ " plt.legend(['train', 'test'], loc='upper left')\n",
89
+ " return plt.show()\n",
90
+ " else:\n",
91
+ " # summarize history for loss\n",
92
+ " plt.plot(history.history['loss'])\n",
93
+ " plt.plot(history.history['val_loss'])\n",
94
+ " plt.title('model loss')\n",
95
+ " plt.ylabel('loss')\n",
96
+ " plt.xlabel('epoch')\n",
97
+ " plt.legend(['train', 'test'], loc='upper left')\n",
98
+ " return plt.show()\n",
99
+ " \n",
100
+ " \n",
101
+ " \n",
102
+ "iface = gr.Interface(\n",
103
+ " malware_detection_DL, [gr.inputs.Dropdown([\"Accuracy\",\"Loss\"], label=\"Result Type\"),\n",
104
+ " gr.inputs.Dropdown([\"malicious_flows.csv\"], label = \"Malicious traffic in .csv\"), gr.inputs.Dropdown([\"sample_benign_flows.csv\"], label=\"Benign Traffic in .csv\")\n",
105
+ " ], \"plot\",\n",
106
+ " \n",
107
+ " \n",
108
+ ")\n",
109
+ "\n",
110
+ "iface.launch()\n",
111
+ "\n"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type": "code",
116
+ "execution_count": null,
117
+ "id": "curious-detector",
118
+ "metadata": {},
119
+ "outputs": [],
120
+ "source": []
121
+ },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": null,
125
+ "id": "2b934bff",
126
+ "metadata": {},
127
+ "outputs": [],
128
+ "source": []
129
+ }
130
+ ],
131
+ "metadata": {
132
+ "kernelspec": {
133
+ "display_name": "Python 3 (ipykernel)",
134
+ "language": "python",
135
+ "name": "python3"
136
+ },
137
+ "language_info": {
138
+ "codemirror_mode": {
139
+ "name": "ipython",
140
+ "version": 3
141
+ },
142
+ "file_extension": ".py",
143
+ "mimetype": "text/x-python",
144
+ "name": "python",
145
+ "nbconvert_exporter": "python",
146
+ "pygments_lexer": "ipython3",
147
+ "version": "3.9.7"
148
+ }
149
+ },
150
+ "nbformat": 4,
151
+ "nbformat_minor": 5
152
+ }