{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Turning a Jupyter notebook into a web app\n",
    "\n",
    "---\n",
    "\n",
    "### This is the completed image classifier notebook to be used as reference for Anvil's [turning a Jupyter notebook into a web app tutorial](https://anvil.works/learn/tutorials/jupyter-notebook-to-web-app).\n",
    "\n",
    "The following code was added so you can connect this notebook to your Anvil app.\n",
    "\n",
    "1. Install the `anvil-uplink` library\n",
    "2. Import the `anvil.server` package\n",
    "3. Connect the notebook using your apps Uplink key\n",
    "5. Create a function to call from your app that includes the `anvil.server.callable` decorator\n",
    "6. Add `anvil.server.wait_forever()` to the end of the notebook\n",
    "\n",
    "### See below for more detail\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Install the `anvil-uplink` library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install anvil-uplink"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -U transformers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text"
   },
   "source": [
    "### Local Inference on GPU \n",
    "Model page: https://huggingface.co/google/vit-base-patch16-224\n",
    "\n",
    "⚠️ If the generated code snippets do not work, please open an issue on either the [model repo](https://huggingface.co/google/vit-base-patch16-224)\n",
    "\t\t\tand/or on [huggingface.js](https://github.com/huggingface/huggingface.js/blob/main/packages/tasks/src/model-libraries-snippets.ts) 🙏"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab_type": "code"
   },
   "outputs": [],
   "source": [
    "# Use a pipeline as a high-level helper\n",
    "from transformers import pipeline\n",
    "\n",
    "pipe = pipeline(\"image-classification\", model=\"google/vit-base-patch16-224\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import the Anvil server package and connect this notebook to your app\n",
    "\n",
    "For information on how to get your app's Uplink key, see [Step 4 - Enable the Uplink](https://anvil.works/learn/tutorials/jupyter-notebook-to-web-app#step-3-enable-the-uplink)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import anvil.server\n",
    "\n",
    "anvil.server.connect(\"your-uplink-key\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create the `classify_image()` function with a `@anvil.server.callable` decorator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import anvil.media\n",
    "\n",
    "@anvil.server.callable\n",
    "def classify_image(file):\n",
    "    with anvil.media.TempFile(file) as filename:\n",
    "        result = pipe(filename)\n",
    "    top_result = result[0]\n",
    "    label = top_result['label'].split(',')[0].capitalize()\n",
    "    confidence = round(top_result['score'] * 100)\n",
    "    \n",
    "    return {\n",
    "        \"label\": label,\n",
    "        \"confidence\": confidence,\n",
    "        \"message\": f\"Looks like a {label} to me! I'm {confidence}% sure!\"\n",
    "    }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Call `anvil.server.wait_forever()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "anvil.server.wait_forever()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## That's it, 5 simple steps to connect your notebook to your Anvil app!"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "gpuType": "T4",
   "machine_shape": "hm"
  },
  "kaggle": {
   "accelerator": "nvidiaTeslaT4",
   "dataSources": [],
   "isGpuEnabled": true,
   "isInternetEnabled": true,
   "language": "python",
   "sourceType": "notebook"
  },
  "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.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
