{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "c7070975-920c-426e-8f2d-19f9e70fec81", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "markdown", "id": "fc3cd6f5-7bb2-4ef3-bb8f-5c05043949d9", "metadata": {}, "source": [ "# Bear Classifier App\n", "\n", "This notebook creates uses an exported model `export.pkl` for a bear classifier, to create a python script which can run the model on HuggingFace. " ] }, { "cell_type": "code", "execution_count": 2, "id": "4defcbc0-6c1d-413e-a858-5aaa7c73d994", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr" ] }, { "cell_type": "markdown", "id": "a9ff5538-2fa7-428e-8464-9a5e26debc09", "metadata": {}, "source": [ "Let's take a look at an example picture:" ] }, { "cell_type": "code", "execution_count": 3, "id": "209f8ab6-3fa8-458a-bcfe-ed60e35fd480", "metadata": {}, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'blackbear.jpg'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[3], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m#im = PILImage.create('teddybear.jpg')\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m#im = PILImage.create('grizzly.jpg')\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m im \u001b[38;5;241m=\u001b[39m PILImage\u001b[38;5;241m.\u001b[39mcreate(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mblackbear.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 5\u001b[0m im\u001b[38;5;241m.\u001b[39mthumbnail((\u001b[38;5;241m192\u001b[39m, \u001b[38;5;241m192\u001b[39m))\n\u001b[0;32m 6\u001b[0m im\n", "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\fastai\\vision\\core.py:125\u001b[0m, in \u001b[0;36mPILBase.create\u001b[1;34m(cls, fn, **kwargs)\u001b[0m\n\u001b[0;32m 123\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(fn,\u001b[38;5;28mbytes\u001b[39m): fn \u001b[38;5;241m=\u001b[39m io\u001b[38;5;241m.\u001b[39mBytesIO(fn)\n\u001b[0;32m 124\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(fn,Image\u001b[38;5;241m.\u001b[39mImage): \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m(fn)\n\u001b[1;32m--> 125\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m(load_image(fn, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mmerge(\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_open_args, kwargs)))\n", "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\fastai\\vision\\core.py:98\u001b[0m, in \u001b[0;36mload_image\u001b[1;34m(fn, mode)\u001b[0m\n\u001b[0;32m 96\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mload_image\u001b[39m(fn, mode\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m 97\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOpen and load a `PIL.Image` and convert to `mode`\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m---> 98\u001b[0m im \u001b[38;5;241m=\u001b[39m Image\u001b[38;5;241m.\u001b[39mopen(fn)\n\u001b[0;32m 99\u001b[0m im\u001b[38;5;241m.\u001b[39mload()\n\u001b[0;32m 100\u001b[0m im \u001b[38;5;241m=\u001b[39m im\u001b[38;5;241m.\u001b[39m_new(im\u001b[38;5;241m.\u001b[39mim)\n", "File \u001b[1;32m~\\anaconda3\\Lib\\site-packages\\PIL\\Image.py:3227\u001b[0m, in \u001b[0;36mopen\u001b[1;34m(fp, mode, formats)\u001b[0m\n\u001b[0;32m 3224\u001b[0m filename \u001b[38;5;241m=\u001b[39m fp\n\u001b[0;32m 3226\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m filename:\n\u001b[1;32m-> 3227\u001b[0m fp \u001b[38;5;241m=\u001b[39m builtins\u001b[38;5;241m.\u001b[39mopen(filename, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrb\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 3228\u001b[0m exclusive_fp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m 3230\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'blackbear.jpg'" ] } ], "source": [ "#im = PILImage.create('teddybear.jpg')\n", "#im = PILImage.create('grizzly.jpg')\n", "im = PILImage.create('blackbear.jpg')\n", "\n", "im.thumbnail((192, 192))\n", "im" ] }, { "cell_type": "markdown", "id": "8373d8af-73cb-4d72-8a9d-c19228a03355", "metadata": {}, "source": [ "Let's import the model and create the learner:" ] }, { "cell_type": "code", "execution_count": 4, "id": "6e9106b7-9c6c-4b1a-8d15-777b0e44ad60", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('export.pkl')" ] }, { "cell_type": "markdown", "id": "bad6e35f-536a-4de4-b914-1ca4e3ec3fd5", "metadata": {}, "source": [ "With the learner we can to the predictions (inference):" ] }, { "cell_type": "code", "execution_count": 5, "id": "52a0bb5a-e5a9-4905-a45f-4f613701c207", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "('black', TensorBase(0), TensorBase([9.9979e-01, 1.9130e-04, 1.6887e-05]))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.predict(im)" ] }, { "cell_type": "markdown", "id": "c9519ddb-f6dd-4e8c-ba67-3ba748593911", "metadata": {}, "source": [ "The available categories are contained in the vocab:" ] }, { "cell_type": "code", "execution_count": 6, "id": "82cd137c-b375-4902-a572-95a5d723fb3f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['black', 'grizzly', 'teddy']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.dls.vocab" ] }, { "cell_type": "markdown", "id": "49c774b1-96c0-4a3a-82e9-8453de8b26cd", "metadata": {}, "source": [ "This is the function to classify the images:" ] }, { "cell_type": "code", "execution_count": 7, "id": "1f479df5-838d-4f33-909c-703d39912d9c", "metadata": {}, "outputs": [], "source": [ "#|export\n", "def classify_image(img):\n", " pred,pred_idx,probs = learn.predict(img)\n", " return dict(zip(learn.dls.vocab, map(float, probs)))" ] }, { "cell_type": "markdown", "id": "15b2bff4-0a3e-4f6d-b628-341759c6508e", "metadata": {}, "source": [ "Testing the function:" ] }, { "cell_type": "code", "execution_count": 8, "id": "9049900f-0cd1-4a33-9a5c-e5bc08062dbb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "{'black': 0.9997918009757996,\n", " 'grizzly': 0.0001913038140628487,\n", " 'teddy': 1.688681004452519e-05}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classify_image(im)" ] }, { "cell_type": "markdown", "id": "16d81693-3786-4dee-8edf-92fad120bc91", "metadata": {}, "source": [ "## Gradio App\n", "\n", "Now it is time to create the gradio app:" ] }, { "cell_type": "code", "execution_count": 1, "id": "c95ee1ca-5feb-4011-b99c-3241468d3e3a", "metadata": {}, "outputs": [], "source": [ "# commented, because it produced warnings\n", "\n", "#image = gr.inputs.Image(shape=(192,192))\n", "#label = gr.outputs.Label()" ] }, { "cell_type": "code", "execution_count": 10, "id": "5ca3c2b5-1691-434f-acf8-f0cb91bf32b7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/plain": [ "(, 'http://127.0.0.1:7860/', None)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "image = gr.components.Image(shape=(192,192))\n", "label = gr.components.Label()\n", "examples = ['teddybear.jpg', 'grizzly.jpg', 'blackbear.jpg']\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": 10, "id": "63b8cbb0-40f8-495d-995e-9f946e28ae98", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7860\n" ] } ], "source": [ "intf.close()" ] }, { "cell_type": "markdown", "id": "585bbd25-7895-40df-b15c-790f1ca058a2", "metadata": {}, "source": [ "## Export\n", "\n", "Finally, we export the code in the cells which are marked with `#|export`:" ] }, { "cell_type": "code", "execution_count": 12, "id": "dcc8edc2-d153-4221-92bd-3e32277a65b7", "metadata": {}, "outputs": [], "source": [ "# commented, because it does not work\n", "\n", "# import notebook2script from nbdev.export\n", "# notebook2script('app.ipynb')" ] }, { "cell_type": "code", "execution_count": 13, "id": "5031b82c-67fa-4cac-b1a4-9c52646795b1", "metadata": {}, "outputs": [], "source": [ "from nbdev import nbdev_export" ] }, { "cell_type": "code", "execution_count": 13, "id": "85bbbb28-b48d-4531-9179-f98ebb751508", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Export successful\n" ] } ], "source": [ "nbdev_export('app.ipynb')\n", "print('Export successful')" ] } ], "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.11.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "vscode": { "interpreter": { "hash": "d9da906b64701e68312bc07fbc15a3a13814f930718c2c6b0e41a29d035806a3" } } }, "nbformat": 4, "nbformat_minor": 5 }