{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from tensorflow.keras.layers import Dense, Embedding, LSTM, SpatialDropout1D\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.callbacks import EarlyStopping\n",
    "from tensorflow.keras import metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "maxLen = 128\n",
    "epochs = 50\n",
    "dim_embedding = 50\n",
    "batch_size = 256\n",
    "dropout_rate = 0.25\n",
    "num_LSTM_cell = 64\n",
    "trainDataFileName = 's3://smle-experiments/datasets/phishing_email/train.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "xTrain = []\n",
    "yTrain = []\n",
    "df = pd.read_json(trainDataFileName,  lines = True)\n",
    "for idx, row in df.iterrows():\n",
    "    label = np.zeros(1)\n",
    "    if row['isPhishing'] == 'True':\n",
    "        label[0] = 1\n",
    "    yTrain.append(label)\n",
    "    aMessage = row['From'] + ' ' + row['Subject'] + ' ' + row['Content']\n",
    "    anEvent = np.array([32]*maxLen)\n",
    "    p = 0\n",
    "    for c in aMessage:\n",
    "        v = ord(c)\n",
    "        if v < 32 or v > 126:\n",
    "            continue\n",
    "        anEvent[p] = v\n",
    "        p += 1\n",
    "        if p >= maxLen:\n",
    "            break\n",
    "    xTrain.append(anEvent)\n",
    "xTrain = np.array(xTrain)\n",
    "yTrain = np.array(yTrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/initializers.py:119: calling RandomUniform.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Call initializer instance with the dtype argument instead of passing it to the constructor\n",
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "If using Keras pass *_constraint arguments to layers.\n",
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/ops/nn_impl.py:183: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
      "Train on 40000 samples, validate on 10000 samples\n",
      "Epoch 1/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.1514 - acc: 0.9740 - val_loss: 0.0912 - val_acc: 0.9817\n",
      "Epoch 2/50\n",
      "40000/40000 [==============================] - 44s 1ms/sample - loss: 0.0990 - acc: 0.9796 - val_loss: 0.0904 - val_acc: 0.9817\n",
      "Epoch 3/50\n",
      "40000/40000 [==============================] - 43s 1ms/sample - loss: 0.0884 - acc: 0.9796 - val_loss: 0.0726 - val_acc: 0.9828\n",
      "Epoch 4/50\n",
      "40000/40000 [==============================] - 44s 1ms/sample - loss: 0.0726 - acc: 0.9798 - val_loss: 0.0640 - val_acc: 0.9830\n",
      "Epoch 5/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0667 - acc: 0.9812 - val_loss: 0.0621 - val_acc: 0.9855\n",
      "Epoch 6/50\n",
      "40000/40000 [==============================] - 44s 1ms/sample - loss: 0.0665 - acc: 0.9813 - val_loss: 0.0598 - val_acc: 0.9849\n",
      "Epoch 7/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0651 - acc: 0.9816 - val_loss: 0.0529 - val_acc: 0.9824\n",
      "Epoch 8/50\n",
      "40000/40000 [==============================] - 44s 1ms/sample - loss: 0.0532 - acc: 0.9830 - val_loss: 0.0393 - val_acc: 0.9889\n",
      "Epoch 9/50\n",
      "40000/40000 [==============================] - 44s 1ms/sample - loss: 0.0474 - acc: 0.9844 - val_loss: 0.0428 - val_acc: 0.9891\n",
      "Epoch 10/50\n",
      "40000/40000 [==============================] - 44s 1ms/sample - loss: 0.0419 - acc: 0.9891 - val_loss: 0.0359 - val_acc: 0.9912\n",
      "Epoch 11/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0385 - acc: 0.9886 - val_loss: 0.0271 - val_acc: 0.9922\n",
      "Epoch 12/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0369 - acc: 0.9894 - val_loss: 0.0251 - val_acc: 0.9937\n",
      "Epoch 13/50\n",
      "40000/40000 [==============================] - 46s 1ms/sample - loss: 0.0299 - acc: 0.9921 - val_loss: 0.0255 - val_acc: 0.9933\n",
      "Epoch 14/50\n",
      "40000/40000 [==============================] - 46s 1ms/sample - loss: 0.0242 - acc: 0.9942 - val_loss: 0.0181 - val_acc: 0.9948\n",
      "Epoch 15/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0210 - acc: 0.9958 - val_loss: 0.0195 - val_acc: 0.9957\n",
      "Epoch 16/50\n",
      "40000/40000 [==============================] - 44s 1ms/sample - loss: 0.0179 - acc: 0.9961 - val_loss: 0.0146 - val_acc: 0.9965\n",
      "Epoch 17/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0168 - acc: 0.9964 - val_loss: 0.0147 - val_acc: 0.9969\n",
      "Epoch 18/50\n",
      "40000/40000 [==============================] - 43s 1ms/sample - loss: 0.0179 - acc: 0.9959 - val_loss: 0.0125 - val_acc: 0.9968\n",
      "Epoch 19/50\n",
      "40000/40000 [==============================] - 46s 1ms/sample - loss: 0.0262 - acc: 0.9909 - val_loss: 0.0168 - val_acc: 0.9971\n",
      "Epoch 20/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0169 - acc: 0.9961 - val_loss: 0.0128 - val_acc: 0.9971\n",
      "Epoch 21/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0163 - acc: 0.9962 - val_loss: 0.0134 - val_acc: 0.9966\n",
      "Epoch 22/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0165 - acc: 0.9966 - val_loss: 0.0121 - val_acc: 0.9971\n",
      "Epoch 23/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0143 - acc: 0.9967 - val_loss: 0.0115 - val_acc: 0.9970\n",
      "Epoch 24/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0266 - acc: 0.9909 - val_loss: 0.0170 - val_acc: 0.9971\n",
      "Epoch 25/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0158 - acc: 0.9964 - val_loss: 0.0119 - val_acc: 0.9971\n",
      "Epoch 26/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0190 - acc: 0.9951 - val_loss: 0.0146 - val_acc: 0.9959\n",
      "Epoch 27/50\n",
      "40000/40000 [==============================] - 46s 1ms/sample - loss: 0.0173 - acc: 0.9956 - val_loss: 0.0134 - val_acc: 0.9973\n",
      "Epoch 28/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0120 - acc: 0.9970 - val_loss: 0.0130 - val_acc: 0.9972\n",
      "Epoch 29/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0125 - acc: 0.9970 - val_loss: 0.0123 - val_acc: 0.9973\n",
      "Epoch 30/50\n",
      "40000/40000 [==============================] - 45s 1ms/sample - loss: 0.0121 - acc: 0.9970 - val_loss: 0.0127 - val_acc: 0.9969\n"
     ]
    }
   ],
   "source": [
    "model = Sequential()\n",
    "model.add(Embedding(128, dim_embedding, input_length=maxLen))\n",
    "model.add(SpatialDropout1D(dropout_rate))\n",
    "model.add(LSTM(num_LSTM_cell, dropout=dropout_rate, recurrent_dropout=dropout_rate))\n",
    "model.add(Dense(1, activation='sigmoid'))\n",
    "model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
    "history = model.fit(xTrain, yTrain, epochs=epochs, batch_size=batch_size, validation_split=0.2, \n",
    "                    callbacks=[EarlyStopping(monitor='val_loss',patience=7, min_delta=0.00001)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Publish Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import smle\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "from smle.context import Context\n",
    "cwd = os.getcwd()\n",
    "\n",
    "config = {  \n",
    "    'username': '',\n",
    "    \n",
    "    'model_storage_type': 's3', \n",
    "    'model_storage_address': \"s3.us-west-2.amazonaws.com\",\n",
    "    'model_storage_bucket': 'smle-experiments',\n",
    "    'model_storage_access_key': '',\n",
    "    'model_storage_secret_key': '',\n",
    "    'model_storage_secure': False,\n",
    "}\n",
    "smle_context = Context(config)\n",
    "\n",
    "%load_ext spl2_kernel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/smle/onnx.py:88: export_saved_model (from tensorflow.python.keras.saving.saved_model_experimental) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use `model.save(..., save_format=\"tf\")` or `tf.keras.models.save_model(..., save_format=\"tf\")`.\n",
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/ops/init_ops.py:97: calling GlorotUniform.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Call initializer instance with the dtype argument instead of passing it to the constructor\n",
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/ops/init_ops.py:97: calling Orthogonal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Call initializer instance with the dtype argument instead of passing it to the constructor\n",
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/ops/init_ops.py:97: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Call initializer instance with the dtype argument instead of passing it to the constructor\n",
      "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow_core/python/saved_model/signature_def_utils_impl.py:253: build_tensor_info (from tensorflow.python.saved_model.utils_impl) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.utils.build_tensor_info or tf.compat.v1.saved_model.build_tensor_info.\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Classify: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Regress: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Predict: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Train: ['train']\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Eval: None\n",
      "WARNING:tensorflow:Export includes no default signature!\n",
      "INFO:tensorflow:No assets to save.\n",
      "INFO:tensorflow:No assets to write.\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Classify: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Regress: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Predict: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Train: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Eval: ['eval']\n",
      "WARNING:tensorflow:Export includes no default signature!\n",
      "INFO:tensorflow:No assets to save.\n",
      "INFO:tensorflow:No assets to write.\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Classify: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Regress: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Predict: ['serving_default']\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Train: None\n",
      "INFO:tensorflow:Signatures INCLUDED in export for Eval: None\n",
      "INFO:tensorflow:No assets to save.\n",
      "INFO:tensorflow:No assets to write.\n",
      "INFO:tensorflow:SavedModel written to: /tmp/phishing_email/saved_model.pb\n"
     ]
    }
   ],
   "source": [
    "model_path = \"models/phishing_email\"\n",
    "model_name = \"phishing_email\"\n",
    "\n",
    "!rm -rf /tmp/{model_name}\n",
    "sample_data = pd.read_csv('s3://smle-experiments/datasets/phishing_email/sample_file.csv')\n",
    "smle_context.publish(model, model_name=model_name, path = model_path, sample= sample_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Metadata:\n",
      "{'inputFields': [{'name': 'embedding_input:0',\n",
      "                  'size': 128,\n",
      "                  'type': 'floatTensor'}],\n",
      " 'modelName': 'phishing_email',\n",
      " 'outputFields': [{'name': 'dense/Sigmoid:0',\n",
      "                   'size': 1,\n",
      "                   'type': 'floatTensor'}]}\n",
      "\n",
      "ONNX model specs:\n",
      "{'inputs': [{'name': 'embedding_input:0',\n",
      "             'shape': ['unk__236', 128],\n",
      "             'type': 'tensor(float)'}],\n",
      " 'outputs': [{'name': 'dense/Sigmoid:0',\n",
      "              'shape': ['unk__237', 1],\n",
      "              'type': 'tensor(float)'}]}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import onnxruntime as rt\n",
    "from pprint import pprint\n",
    "\n",
    "\n",
    "def inspect_model(dir_path, model_name):\n",
    "\n",
    "    metadata = json.load(open(dir_path + \"metadata.json\"))\n",
    "    \n",
    "    print(\"Metadata:\")\n",
    "    pprint(metadata)\n",
    "    print()\n",
    "    \n",
    "    model_path = dir_path + model_name + \".onnx\"\n",
    "    sess = rt.InferenceSession(model_path)\n",
    "\n",
    "    onnx_inputs = sess.get_inputs()\n",
    "    onnx_outputs = sess.get_outputs()\n",
    "\n",
    "    inputs = [{\"name\": node.name, \"type\": node.type, \"shape\": node.shape} for node in onnx_inputs]\n",
    "    outputs = [{\"name\": node.name, \"type\": node.type, \"shape\": node.shape} for node in onnx_outputs]\n",
    "    onnx_model_specs = {\"inputs\": inputs, \"outputs\": outputs}\n",
    "    print(\"ONNX model specs:\")\n",
    "    \n",
    "    pprint(onnx_model_specs)\n",
    "\n",
    "\n",
    "inspect_model(\"/tmp/\", model_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%spl2_add_params from_python\n",
    "phishing_email = dict(\n",
    "    model_path = \"s3://smle-experiments/models/phishing_email\",\n",
    "    model_name = \"phishing_email\",\n",
    "    input_field = \"embedding_input:0\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6aa0cfe6342a4e59b0ff9a68f1de5596",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=5.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Finished.                     "
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>eventLine</th>\n",
       "      <th>probability</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>karem ahmed  &lt;karemahmed-18@hotmail.fr&gt;  PLEA...</td>\n",
       "      <td>[0.9720325]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>nkomo robert  &lt;nkomo002@5fm.za.com&gt;  FAMILY R...</td>\n",
       "      <td>[0.9768889]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Mrs  Mariam   Taylor  &lt;familyboxjanetfamilybo...</td>\n",
       "      <td>[0.9780622999999999]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Mr.Fred   Chima  &lt;fredi@fastermail.com&gt;  busi...</td>\n",
       "      <td>[0.97669697]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Alexander Afadia &lt;eeaesq@123.com&gt;  Please repl...</td>\n",
       "      <td>[0.9775347999999999]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>Comfort Somba.  &lt;comfort_somba11@yahoo.fr&gt;  F...</td>\n",
       "      <td>[0.97327405]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>bintu pat  &lt;pat_bintu004@hotmail.com&gt;  TRUSTI...</td>\n",
       "      <td>[0.96824765]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>FR RICHARD DAVID  &lt;unofice@katamail.com&gt;  FRO...</td>\n",
       "      <td>[0.96306074]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>85</th>\n",
       "      <td>&lt;joseph_m333@micasilla.net&gt;  Farmers Greetings...</td>\n",
       "      <td>[0.97275084]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>MRS. LARISA SOSNITSKAYA  &lt;larisasosnkayapawou...</td>\n",
       "      <td>[0.9751048999999999]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>87 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                            eventLine           probability\n",
       "0    karem ahmed  <karemahmed-18@hotmail.fr>  PLEA...           [0.9720325]\n",
       "1    nkomo robert  <nkomo002@5fm.za.com>  FAMILY R...           [0.9768889]\n",
       "2    Mrs  Mariam   Taylor  <familyboxjanetfamilybo...  [0.9780622999999999]\n",
       "3    Mr.Fred   Chima  <fredi@fastermail.com>  busi...          [0.97669697]\n",
       "4   Alexander Afadia <eeaesq@123.com>  Please repl...  [0.9775347999999999]\n",
       "..                                                ...                   ...\n",
       "82   Comfort Somba.  <comfort_somba11@yahoo.fr>  F...          [0.97327405]\n",
       "83   bintu pat  <pat_bintu004@hotmail.com>  TRUSTI...          [0.96824765]\n",
       "84   FR RICHARD DAVID  <unofice@katamail.com>  FRO...          [0.96306074]\n",
       "85  <joseph_m333@micasilla.net>  Farmers Greetings...          [0.97275084]\n",
       "86   MRS. LARISA SOSNITSKAYA  <larisasosnkayapawou...  [0.9751048999999999]\n",
       "\n",
       "[87 rows x 2 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<spl2_kernel.spl2_runner.SPL2Job at 0x7f4c14e9a2d0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%spl2 -q phishing_email\n",
    "| from read_json(\"s3://smle-experiments/datasets/phishing_email/test.json\")\n",
    "| eval eventLine=concat(From, \" \", Subject, \" \", Content, \" \", \"                                                                                                                                \")\n",
    "| eval mapC = {\" \":32,\"!\":33,\"\\\"\":34,\"#\":35,\"$$\":36,\"%\":37,\"&\":38,\"'\":39,\"(\":40,\")\":41,\"*\":42,\"+\":43,\",\":44,\"-\":45,\".\":46,\"/\":47,\"0\":48,\"1\":49,\"2\":50,\"3\":51,\"4\":52,\"5\":53,\"6\":54,\"7\":55,\"8\":56,\"9\":57,\":\":58,\";\":59,\"<\":60,\"=\":61,\">\":62,\"?\":63,\"@\":64,\"A\":65,\"B\":66,\"C\":67,\"D\":68,\"E\":69,\"F\":70,\"G\":71,\"H\":72,\"I\":73,\"J\":74,\"K\":75,\"L\":76,\"M\":77,\"N\":78,\"O\":79,\"P\":80,\"Q\":81,\"R\":82,\"S\":83,\"T\":84,\"U\":85,\"V\":86,\"W\":87,\"X\":88,\"Y\":89,\"Z\":90,\"[\":91,\"\\\\\":92,\"]\":93,\"^\":94,\"_\":95,\"`\":96,\"a\":97,\"b\":98,\"c\":99,\"d\":100,\"e\":101,\"f\":102,\"g\":103,\"h\":104,\"i\":105,\"j\":106,\"k\":107,\"l\":108,\"m\":109,\"n\":110,\"o\":111,\"p\":112,\"q\":113,\"r\":114,\"s\":115,\"t\":116,\"u\":117,\"v\":118,\"w\":119,\"x\":120,\"y\":121,\"z\":122,\"{\":123,\"|\":124,\"}\":125,\"~\":126}\n",
    "| eval 'embedding_input:0' = for_each(\n",
    "        iterator(mvrange(1,129), \"i\"),\n",
    "        cast(map_get(mapC, substr(eventLine, i, 1)), \"float\") )\n",
    "| apply_model connection_id=\"\" path=\"$model_path\" name=\"$model_name\" \n",
    "| rename 'dense/Sigmoid:0' AS probability \n",
    "| where mvindex(probability, 0) > 0.5 \n",
    "| select eventLine, probability \n",
    ";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = _.df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\" karem ahmed  <karemahmed-18@hotmail.fr>  PLEASE VERY URGENT. FROM THE DESK OF Mr,KAREM AHMED.BILL AND EXCHANGE MANAGER,BANK OF AFRICA (B.O.A) OUAGADOUGOU,BURKINA FASO WEST AFRICA.PHONE CONTACT  00226.78.89.77.34DEAR FRIEND,I know you may be surprise to recieve this e-mail ; I got your contact address from the internet while I was searching for my friend that has similar name with you.I am the manager of bill and exchange BANK OF AFRICA (B.O.A) at foreign remittance department.There is a business I would want you to champion for me, in my department I discovered an abandoned sum of ($31.500.000 U.S) thirty one million five hundred thousands US dollars)In an account that belongs to one of our foreign customer who died along with his entire family on 25TH JULY, 2000 CONCORDE PLANE CRASH [Flight AF4590] with the whole passengers aboard. The name of the deceased man was(MR.ANDREAS SCHRANNER from Munich Germany)N.B. In other for you to believe me honestly, visit the web site to enable you know whether we can work together, below is the website.                    http://news.bbc.co.uk/1/hi/world/europe/859479.stmYou have to understand that I come crossed this huge amount of money when I was arranging the departmental customers file to submit to the bank management for the annual audit of the year.Since we got information about his death, we have been expecting his next of kin to come over and claim his money because we cannot release it unless somebody applies for it as next of kin or relation to the deceased as indicated in our banking guidelines, but unfortunately we learnt that all his supposed next of kin's or relation died alongside with him at the plane crash leaving nobody behind for the claim.It is therefore upon this discovery that I now decided to make this business proposal to you and release the money to you as the next of kin or relation to the deceased for safety and subsequent disbursement since nobody is coming for it and I dont want this money to go into the Bank treasury as unclaimed Bill.The Banking law and guideline here stipulates that if such money remained unclaimed after some years, the money will be transferred into the Bank treasury as unclaimed fund.The request of foreigner as next of kin in this business is occasioned by the fact that the customer was a foreigner and a Burkina citizen cannot stand as next of kin to a foreigner.In fact I could have done this deal alone but because of my position in this country as a civil servant (A Banker), we are not allowed to operate any  foreign account and would eventually raise an eye brow on my side during the time of transfer because I work in the same bank. This is the actual reason why it will require a second party or fellow who will forward claims as the next of kin to the Bank and also present a foreign account where he will need the money to be re-transferred into on his request as it may be after due verification and clarification by the correspondent branch of the bank where the whole money will be remitted from to your own designation bank account.I dont want this money to go into the Bank treasury as unclaimed Bill, I agree that 30 % of this money will be for you as foreign partner, in respect to the provision of a foreign account, 10 % will be set aside for expenses incurred during the business and 60 % would be for me. There after I will visit your country for disbursement according to the percentages indicated. Therefore to enable immediate transfer of this fund to you as arranged, you must apply first to the bank as relations or next of kin of the deceased indicating your bank name, your bank account number, your private telephone and fax number for easy and effective communication and location where in the money will be remitted.All modalities of this transaction have been carefully worked out and once started will not take more than fourteen (14) working days, with your full support.This transaction is 100% risk free. it involve no any implication and no any precaution there after.Upon receipt of your reply, I will send to you by fax or email the text of the application. I will not fail to bring to your notice that this transaction is hitch free and that you should not entertain any atom of fear as all required arrangements have been made for the transfer.You should contact me immediately as soon as you receive this letterreply to thi box. at;Trusting to hear from you immediately.Yours faithfully,Mr,KAREM AHMEDBill and exchange manager,BANK OF AFRICA_________________________________________________________________Personnalisez votre Messenger avec Live.com http://www.windowslive.fr/livecom/                                                                                                                                 \""
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[0]['eventLine']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
