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