{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2d619f07",
   "metadata": {},
   "source": [
    "# The Super Duper Bear Classification Algorithm\n",
    "\n",
    "Here I am implementing the bear detector model as a binder notebook\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c7456be4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# What you need\n",
    "from fastai.vision.all import *\n",
    "from fastai.vision.widgets import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f894690c",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = Path()\n",
    "learn_inf = load_learner(path/'export.pkl', cpu=True)\n",
    "btn_upload = widgets.FileUpload()\n",
    "out_pl = widgets.Output()\n",
    "lbl_pred = widgets.Label()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "24149467",
   "metadata": {},
   "outputs": [],
   "source": [
    "def on_data_change(change):\n",
    "    lbl_pred.value = ''\n",
    "    img = PILImage.create(btn_upload.data[-1])\n",
    "    out_pl.clear_output()\n",
    "    with out_pl: display(img.to_thumb(128,128))\n",
    "    pred, pred_idx, probs = learn_inf.predict(img)\n",
    "    lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4cc87a9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "btn_upload.observe(on_data_change, names=['data'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e17eae68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0e6fc3444b0644afb064b0361c13797e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Label(value='Select your bear!'), FileUpload(value={}, description='Upload'), Output(), Label(v…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(VBox([widgets.Label('Select your bear!'), btn_upload, out_pl, lbl_pred]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8c8aa55",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}