{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using LRE to Deploy your Encrypted Model\n",
    "\n",
    "This notebook describes deployment of a LEIP Optimize encrypted model. Encryption of the model has to be done in your host environment described in [the LEIP Optimize encryption tutorial](https://docs.latentai.io/leip/optimize/latest/content/notebooks/EncryptionwithForgeTutorial/).\n",
    "\n",
    "You should have produced a bundle which has a `modelLibrary.so` and a `modelKey.bin`. You will also have used a password during the encryption. You will need these 3 artifacts to decrypt your model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, we will locate these artifacts for deployment. \n",
    "- path to `modelLibrary.so` is set to `model_path`\n",
    "- path to `modelKey.bin` is set to `key_path`\n",
    "- password string is set to `password`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pylre import LatentRuntimeEngine as LRE\n",
    "encrypted_output_dir = \"encrypted_output\"\n",
    "model_path = f\"{encrypted_output_dir}/modelLibrary.so\"\n",
    "key_path = f\"{encrypted_output_dir}/modelKey.bin\"\n",
    "password = \"test_password\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will first check if the model can be loaded, just like we did in the [Deploy a Model tutorial](https://docs.latentai.io/leip/deploy/latest/content/tutorials/deploy-model-library/). You will run into an error saying this is an encrypted model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    lre_llvm_insecure = LRE(model_path)\n",
    "except Exception as e:\n",
    "    print(f\"Error initializing LRE: {e}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's load the model with the proper credentials."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lre_llvm_secure = LRE(model_path, password=\"test_password\", key_path=key_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now you can use the `lre_llvm_secure` LRE object like we did in the [Deploy a Model tutorial](https://docs.latentai.io/leip/deploy/latest/content/tutorials/deploy-model-library/). However, please note that once decrypted, the `lre_llvm_secure` is no longer secure. Please ensure your deployment environment is sandboxed before decrypting to maintain your model security.  "
   ]
  }
 ],
 "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.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
