{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "YbyU8YKP5KOh"
   },
   "outputs": [],
   "source": [
    "# Capture to supress the download ouput\n",
    "%%capture\n",
    "!pip install datasets evaluate transformers;\n",
    "!pip install huggingface_hub;\n",
    "!pip install pandas;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "hzlMD2hyVrtD",
    "outputId": "53ad9ba2-a64b-4bd8-eeca-4449035b0595"
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Mounted at /content/drive\n"
     ]
    }
   ],
   "source": [
    "# Load dataset with google drive\n",
    "# We downloaded the dataset from kaggle and uploaded it to google drive, then used google colab to load\n",
    "# It is possible to download it directly using the kaggle api\n",
    "\n",
    "# Link to dataset: https://www.kaggle.com/datasets/engraqeel/iot23preprocesseddata?resource=download\n",
    "# Link to kaggle api docs: https://www.kaggle.com/docs/api#interacting-with-datasets\n",
    "\n",
    "from google.colab import drive\n",
    "drive.mount('/content/drive')\n",
    "reduced_iot_path =  \"/content/drive/MyDrive/PATH/TO/FILE/iot23_combined_new.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "id": "9fLFeygkITnn"
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Define Features\n",
    "# ts\tuid\tid.orig_h\tid.orig_p\tid.resp_h\tid.resp_p\tproto\tservice\tduration\torig_bytes\tresp_bytes\tconn_state\tlocal_orig\tlocal_resp\tmissed_bytes\thistory\torig_pkts\torig_ip_bytes\tresp_pkts\tresp_ip_bytes\tlabel\n",
    "# https://docs.zeek.org/en/master/scripts/base/protocols/conn/main.zeek.html#type-Conn::Info\n",
    "\n",
    "pandas_features = {\n",
    "    'id.orig_p': int,\n",
    "    'id.resp_p': int,\n",
    "    'proto': str,\n",
    "    'service': str,\n",
    "    'duration': float,\n",
    "    'orig_bytes': pd.Int64Dtype(),\n",
    "    'resp_bytes': pd.Int64Dtype(),\n",
    "    'conn_state': str,\n",
    "    'missed_bytes': pd.Int64Dtype(),\n",
    "    'history': str,\n",
    "    'orig_pkts': pd.Int64Dtype(),\n",
    "    'orig_ip_bytes': pd.Int64Dtype(),\n",
    "    'resp_pkts': pd.Int64Dtype(),\n",
    "    'resp_ip_bytes': pd.Int64Dtype(),\n",
    "    'label': str\n",
    "}\n",
    "\n",
    "all_column_names = ['ts', 'uid', 'id.orig_h', 'id.orig_p', 'id.resp_h', 'id.resp_p', 'proto', 'service', 'duration', 'orig_bytes', 'resp_bytes', 'conn_state', 'local_orig', 'local_resp', 'missed_bytes', 'history', 'orig_pkts', 'orig_ip_bytes', 'resp_pkts', 'resp_ip_bytes', 'label'];\n",
    "important_column_names = ['id.resp_p', 'proto', 'conn_state', 'orig_pkts', 'orig_ip_bytes', 'resp_ip_bytes', 'label'];\n",
    "exclude_column_names = ['ts','uid','id.orig_h', 'id.resp_h', 'local_orig', 'local_resp']\n",
    "\n",
    "column_names = all_columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "id": "Zll2DOeT9yv2"
   },
   "outputs": [],
   "source": [
    "# Load dataset with Pandas\n",
    "from datasets import Dataset\n",
    "import pandas as pd\n",
    "reduced_iot_dataset_pandas = pd.read_csv(reduced_iot_path, usecols=column_names, na_values=['-'], dtype=pandas_features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "id": "SUb01Eg7I7wS"
   },
   "outputs": [],
   "source": [
    "# Remove Duplicates\n",
    "reduced_iot_dataset_pandas = reduced_iot_dataset_pandas.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# Make label Benign / Malicious\n",
    "reduced_iot_dataset_pandas['label'] = reduced_iot_dataset_pandas['label'].apply(lambda x: \"Benign\" if x == \"Benign\" else \"Malicious\")"
   ],
   "metadata": {
    "id": "M06Rb8fj2fzk"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "xamjYrWbgxkf",
    "outputId": "1ddb22b6-98ab-44b4-83e1-b995e8c1ea4a"
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "orig_bytes\n",
       "0       564771\n",
       "<NA>    241092\n",
       "48        2121\n",
       "29        1463\n",
       "45        1348\n",
       "         ...  \n",
       "1088         1\n",
       "1093         1\n",
       "1094         1\n",
       "1104         1\n",
       "770          1\n",
       "Length: 431, dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "# Test distribution of data\n",
    "reduced_iot_dataset_pandas.value_counts('orig_bytes', dropna=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "Js3KG0xNvwpf"
   },
   "outputs": [],
   "source": [
    "# Final step: convert to hugging face dataset\n",
    "reduced_iot_dataset = Dataset.from_pandas(reduced_iot_dataset_pandas).remove_columns(\"__index_level_0__\")"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# Test distribution of data again\n",
    "reduced_iot_dataset.to_pandas().value_counts('orig_bytes', dropna=False)"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "pOxVs7H-0-3k",
    "outputId": "dcd084dd-3369-4d8e-91ca-c9fbfc1636f0"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "orig_bytes\n",
       "0.0       564771\n",
       "NaN       241092\n",
       "48.0        2121\n",
       "29.0        1463\n",
       "45.0        1348\n",
       "           ...  \n",
       "1088.0         1\n",
       "1093.0         1\n",
       "1094.0         1\n",
       "1104.0         1\n",
       "770.0          1\n",
       "Length: 431, dtype: int64"
      ]
     },
     "metadata": {},
     "execution_count": 12
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# Authenticate hugging face\n",
    "!huggingface-cli login"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "pq6DF3Z8wdmF",
    "outputId": "aaf5e476-96ce-4edc-d65c-858a7e4e52ec"
   },
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "\n",
      "    _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|\n",
      "    _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|\n",
      "    _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|\n",
      "    _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|\n",
      "    _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|\n",
      "    \n",
      "    A token is already saved on your machine. Run `huggingface-cli whoami` to get more information or `huggingface-cli logout` if you want to log out.\n",
      "    Setting a new token will erase the existing one.\n",
      "    To login, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .\n",
      "Token: \n",
      "Add token as git credential? (Y/n) Y\n",
      "Token is valid (permission: write).\n",
      "Your token has been saved in your configured git credential helpers (store).\n",
      "Your token has been saved to /root/.cache/huggingface/token\n",
      "Login successful\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "# Push to the hugging face hub\n",
    "reduced_iot_dataset.push_to_hub(\"yashika0998/iot-23-preprocessed-allcolumns\")"
   ],
   "metadata": {
    "id": "Nz5RwnjxwnwY"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "# Test loading the data set\n",
    "from datasets import load_dataset\n",
    "pulledDataSet= load_dataset(\"yashika0998/iot-23-preprocessed\", download_mode=\"force_redownload\")"
   ],
   "metadata": {
    "id": "6rMEA58Pzlyx"
   },
   "execution_count": null,
   "outputs": []
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}