{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Models\n",
    "\n",
    "A _model_ is generally used to identify and solve a high-level business problem\n",
    "via identification or prediction.\n",
    "\n",
    "Examples include \"identify fraudulent transactions\", \"predict when equipment\n",
    "will next need maintenance\", and \"forecast data center costs next quarter\".\n",
    "\n",
    "Model design is often an iterative process. As such, many _variants_ (also called a \n",
    "version) of a given model might be evaluated before arriving at the best solution \n",
    "to the problem given business constraints.\n",
    "\n",
    "Each _variant_ is a fully specified artifact that contains hyperparameters and\n",
    "the trained state of the model, along with a complete operational definition of\n",
    "how to execute the model. Every _variant_ of the model must be given a unique\n",
    "and descriptive name.\n",
    "\n",
    "Once defined, each variant can then be _deployed_ independently to generate\n",
    "fully reproducible inferences. This can be used, for example, to run comparisons\n",
    "necessary to evaluate different variants. These _deployments_ can have their\n",
    "resources independently tuned as necessary.\n",
    "\n",
    "## Example\n",
    "\n",
    "Let's take a quick look at how we can define and deploy a model.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import wallaroo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, let's initialize a reference to the Wallaroo system."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "wl = wallaroo.Client()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we can create a _deployment_ for our model using default resource\n",
    "allocations. The `upload_model` function uploads the file and `deploy()` creates a single-model pipeline to make predictions, in a running state and ready to perform inference."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipeline = wl.upload_model('ccfraud' , \"./keras_ccfraud.onnx\").deploy('ccfraud-deployment')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once deployed, we can run inference:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[InferenceResult({'check_failures': None,\n",
       "  'elapsed': 163619,\n",
       "  'model_id': 'ccfraud',\n",
       "  'model_version': '2021-05-01',\n",
       "  'original_data': {'tensor': [[1.0678324729342086,\n",
       "                                0.21778102664937624,\n",
       "                                -1.7115145261843976,\n",
       "                                0.6822857209662413,\n",
       "                                1.0138553066742804,\n",
       "                                -0.43350000129006655,\n",
       "                                0.7395859436561657,\n",
       "                                -0.28828395953577357,\n",
       "                                -0.44726268795990787,\n",
       "                                0.5146124987725894,\n",
       "                                0.3791316964287545,\n",
       "                                0.5190619748123175,\n",
       "                                -0.4904593221655364,\n",
       "                                1.1656456468728569,\n",
       "                                -0.9776307444180006,\n",
       "                                -0.6322198962519854,\n",
       "                                -0.6891477694494687,\n",
       "                                0.17833178574255615,\n",
       "                                0.1397992467197424,\n",
       "                                -0.35542206494183326,\n",
       "                                0.4394217876939808,\n",
       "                                1.4588397511627804,\n",
       "                                -0.3886829614721505,\n",
       "                                0.4353492889350186,\n",
       "                                1.7420053483337177,\n",
       "                                -0.4434654615252943,\n",
       "                                -0.15157478906219238,\n",
       "                                -0.26684517248765616,\n",
       "                                -1.454961775612449]]},\n",
       "  'outputs': [{'Float': {'data': [0.001497417688369751],\n",
       "                         'dim': [1, 1],\n",
       "                         'v': 1}}],\n",
       "  'pipeline_name': 'ccfraud-deployment',\n",
       "  'time': 1630599510450})]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filename = './dev_smoke_test.json'\n",
    "pipeline.infer_from_file(filename)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And then shut down the deployment:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'ccfraud-deployment', 'create_time': datetime.datetime(2021, 5, 21, 4, 11, 11, 816565, tzinfo=tzutc()), 'definition': \"[{'ModelInference': {'models': [{'name': 'ccfraud', 'version': '76cacac7-1dc7-41fc-8b7a-1e5dc17a093c', 'sha': '4dc88d159249ccce83942ada69b919cb91455d5fd0e4bfc287de3f21d1aafb1b'}]}}]\"}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipeline.undeploy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## API Reference\n",
    "\n",
    "See the [Model](sdk.rst#wallaroo.model.Model) section."
   ]
  }
 ],
 "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.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
