{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "30ef7d2a-a96e-4f09-b836-e313d40de266",
   "metadata": {},
   "source": [
    "# MLSteam Model SDK\n",
    "\n",
    "💡This example demonstrates how to use *MLSteam Model SDK* to download an encrypted model for prediction. It has been verified in an *MLSteam* lab running from a `ubuntu:20.04` image.\n",
    "\n",
    "## Create a new model version\n",
    "\n",
    "1. Create a new *folder* (previously known as *dataset*) in *MLSteam*.\n",
    "1. Upload the `model.zip` file into folder and extract it.\n",
    "1. Create a new model named `stock` in *MLSteam*.\n",
    "1. Within the `stock` model, create a new *packaged* or *encrypted* version named `v1`.\n",
    "   - models: `models/` (*folder*)\n",
    "   - hooks: `hooks/` (*folder*)\n",
    "   - manifest: `manifest.json`\n",
    "\n",
    "## Install required SDK packages\n",
    "\n",
    "If the last step of *install-themisdev* fails in *JupyterLab* or if you are not using *Ubuntu*, you may install the package according to the official [instructions](https://docs.cossacklabs.com/themis/languages/python/installation/) instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48c7a68b-c5b3-4023-ad70-438ea935546b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "%pip install -U pip\n",
    "%pip install -U setuptools\n",
    "%pip install -U mlsteam-model-sdk\n",
    "!mlsteam-model-cli install-themisdev -p ubuntu"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "952d97b2-8c59-4548-8a9f-6dedaf6c7249",
   "metadata": {},
   "source": [
    "## Install required model packages\n",
    "\n",
    "1. Replace the following *pip-requirements* path by the actual file location."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e638bd6-ff47-492d-9674-7893599efb23",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install -U -r examples/tensorflow_stockpred/requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b44c9f43-e28e-4538-9d99-9b10f5c50c9d",
   "metadata": {},
   "source": [
    "## Initialize SDK\n",
    "\n",
    "1. Replace the value of `--default_project_val` by the actual project name (format: `PROJECT_OWNER_NAME/PROJECT_NAME`).\n",
    "1. Change `--default_model_val` also if you use another model name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e099f35d-d46c-454c-9c2e-0c91c98fe125",
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -rf ~/.mlsteam-model-sdk\n",
    "!mlsteam-model-cli init \\\n",
    "    --default_project_type name \\\n",
    "    --default_project_val \"admin/test\" \\\n",
    "    --default_model_type name \\\n",
    "    --default_model_val \"stock\"\n",
    "!cat ~/.mlsteam-model-sdk/cfg.ini"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32de185d-42b8-4c16-a4a4-1789b289376a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from mlsteam_model_sdk.sdk.model import Model\n",
    "\n",
    "sdk_model = Model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59e0b7d7-851a-4b77-a856-5e81862eb9a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "sdk_model.download_model_version(version_name='v1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b0384c6-24c2-435e-b503-10a65543b49b",
   "metadata": {},
   "outputs": [],
   "source": [
    "mv = sdk_model.load_model_version(version_name='v1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6802b114-00b8-4622-8af2-32372beb40a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "inputs = [50.0]*80\n",
    "outputs = mv.predict(inputs=inputs)\n",
    "print(outputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82a8e3f3-4501-45a5-a5c1-a1f6d6d9c612",
   "metadata": {},
   "outputs": [],
   "source": [
    "mv.models"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (myenv_tensorflow)",
   "language": "python",
   "name": "myenv_tensorflow"
  },
  "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
