{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting gradio\n", " Downloading gradio-3.2-py3-none-any.whl (6.1 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.1/6.1 MB\u001b[0m \u001b[31m16.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting pycryptodome\n", " Downloading pycryptodome-3.15.0.tar.gz (4.5 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.5/4.5 MB\u001b[0m \u001b[31m28.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25ldone\n", "\u001b[?25hCollecting ffmpy\n", " Downloading ffmpy-0.3.0.tar.gz (4.8 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25ldone\n", "\u001b[?25hCollecting pydub\n", " Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n", "Collecting orjson\n", " Downloading orjson-3.8.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (483 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m483.2/483.2 kB\u001b[0m \u001b[31m17.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pillow in /Users/rudra/mambaforge/lib/python3.10/site-packages (from gradio) (9.2.0)\n", "Collecting fastapi\n", " Downloading fastapi-0.82.0-py3-none-any.whl (55 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m55.3/55.3 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: Jinja2 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from gradio) (3.1.2)\n", "Collecting analytics-python\n", " Downloading analytics_python-1.4.0-py2.py3-none-any.whl (15 kB)\n", "Collecting websockets\n", " Downloading websockets-10.3-cp310-cp310-macosx_11_0_arm64.whl (97 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m97.3/97.3 kB\u001b[0m \u001b[31m4.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: matplotlib in /Users/rudra/mambaforge/lib/python3.10/site-packages (from gradio) (3.5.3)\n", "Collecting aiohttp\n", " Downloading aiohttp-3.8.1-cp310-cp310-macosx_11_0_arm64.whl (552 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m552.5/552.5 kB\u001b[0m \u001b[31m15.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: numpy in /Users/rudra/mambaforge/lib/python3.10/site-packages (from gradio) (1.23.2)\n", "Requirement already satisfied: requests in /Users/rudra/mambaforge/lib/python3.10/site-packages (from gradio) (2.28.1)\n", "Collecting markdown-it-py[linkify,plugins]\n", " Downloading markdown_it_py-2.1.0-py3-none-any.whl (84 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.5/84.5 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting fsspec\n", " Downloading fsspec-2022.8.2-py3-none-any.whl (140 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m140.8/140.8 kB\u001b[0m \u001b[31m5.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pydantic in /Users/rudra/mambaforge/lib/python3.10/site-packages (from gradio) (1.9.2)\n", "Collecting paramiko\n", " Downloading paramiko-2.11.0-py2.py3-none-any.whl (212 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m212.9/212.9 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hCollecting python-multipart\n", " Downloading python-multipart-0.0.5.tar.gz (32 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25ldone\n", "\u001b[?25hCollecting h11<0.13,>=0.11\n", " Downloading h11-0.12.0-py3-none-any.whl (54 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m54.9/54.9 kB\u001b[0m \u001b[31m2.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting httpx\n", " Downloading httpx-0.23.0-py3-none-any.whl (84 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.8/84.8 kB\u001b[0m \u001b[31m3.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: pandas in /Users/rudra/mambaforge/lib/python3.10/site-packages (from gradio) (1.4.4)\n", "Collecting uvicorn\n", " Downloading uvicorn-0.18.3-py3-none-any.whl (57 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.4/57.4 kB\u001b[0m \u001b[31m1.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting multidict<7.0,>=4.5\n", " Downloading multidict-6.0.2-cp310-cp310-macosx_11_0_arm64.whl (29 kB)\n", "Collecting aiosignal>=1.1.2\n", " Downloading aiosignal-1.2.0-py3-none-any.whl (8.2 kB)\n", "Collecting yarl<2.0,>=1.0\n", " Downloading yarl-1.8.1-cp310-cp310-macosx_11_0_arm64.whl (57 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m57.7/57.7 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting frozenlist>=1.1.1\n", " Downloading frozenlist-1.3.1-cp310-cp310-macosx_11_0_arm64.whl (34 kB)\n", "Requirement already satisfied: attrs>=17.3.0 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from aiohttp->gradio) (22.1.0)\n", "Collecting async-timeout<5.0,>=4.0.0a3\n", " Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)\n", "Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from aiohttp->gradio) (2.1.1)\n", "Requirement already satisfied: python-dateutil>2.1 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from analytics-python->gradio) (2.8.2)\n", "Requirement already satisfied: six>=1.5 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from analytics-python->gradio) (1.16.0)\n", "Collecting backoff==1.10.0\n", " Downloading backoff-1.10.0-py2.py3-none-any.whl (31 kB)\n", "Collecting monotonic>=1.5\n", " Downloading monotonic-1.6-py2.py3-none-any.whl (8.2 kB)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from requests->gradio) (1.26.11)\n", "Requirement already satisfied: idna<4,>=2.5 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from requests->gradio) (3.3)\n", "Requirement already satisfied: certifi>=2017.4.17 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from requests->gradio) (2022.6.15)\n", "Collecting starlette==0.19.1\n", " Downloading starlette-0.19.1-py3-none-any.whl (63 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m63.3/63.3 kB\u001b[0m \u001b[31m2.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting anyio<5,>=3.4.0\n", " Downloading anyio-3.6.1-py3-none-any.whl (80 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m80.6/80.6 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: typing-extensions>=3.7.4.3 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from pydantic->gradio) (4.3.0)\n", "Collecting sniffio\n", " Downloading sniffio-1.3.0-py3-none-any.whl (10 kB)\n", "Collecting httpcore<0.16.0,>=0.15.0\n", " Downloading httpcore-0.15.0-py3-none-any.whl (68 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m68.4/68.4 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting rfc3986[idna2008]<2,>=1.3\n", " Downloading rfc3986-1.5.0-py2.py3-none-any.whl (31 kB)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from Jinja2->gradio) (2.1.1)\n", "Collecting mdurl~=0.1\n", " Downloading mdurl-0.1.2-py3-none-any.whl (10.0 kB)\n", "Collecting mdit-py-plugins\n", " Downloading mdit_py_plugins-0.3.0-py3-none-any.whl (43 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m43.7/43.7 kB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting linkify-it-py~=1.0\n", " Downloading linkify_it_py-1.0.3-py3-none-any.whl (19 kB)\n", "Requirement already satisfied: packaging>=20.0 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from matplotlib->gradio) (21.3)\n", "Requirement already satisfied: cycler>=0.10 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from matplotlib->gradio) (0.11.0)\n", "Requirement already satisfied: pyparsing>=2.2.1 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from matplotlib->gradio) (3.0.9)\n", "Requirement already satisfied: fonttools>=4.22.0 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from matplotlib->gradio) (4.37.1)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from matplotlib->gradio) (1.4.4)\n", "Requirement already satisfied: pytz>=2020.1 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from pandas->gradio) (2022.2.1)\n", "Collecting pynacl>=1.0.1\n", " Downloading PyNaCl-1.5.0-cp36-abi3-macosx_10_10_universal2.whl (349 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m349.9/349.9 kB\u001b[0m \u001b[31m13.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: cryptography>=2.5 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from paramiko->gradio) (37.0.4)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting bcrypt>=3.1.3\n", " Downloading bcrypt-4.0.0-cp36-abi3-macosx_10_10_universal2.whl (475 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m475.0/475.0 kB\u001b[0m \u001b[31m16.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: click>=7.0 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from uvicorn->gradio) (8.1.3)\n", "Requirement already satisfied: cffi>=1.12 in /Users/rudra/mambaforge/lib/python3.10/site-packages (from cryptography>=2.5->paramiko->gradio) (1.15.1)\n", "Collecting uc-micro-py\n", " Downloading uc_micro_py-1.0.1-py3-none-any.whl (6.2 kB)\n", "Requirement already satisfied: pycparser in /Users/rudra/mambaforge/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=2.5->paramiko->gradio) (2.21)\n", "Building wheels for collected packages: ffmpy, pycryptodome, python-multipart\n", " Building wheel for ffmpy (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Created wheel for ffmpy: filename=ffmpy-0.3.0-py3-none-any.whl size=4693 sha256=cca6ffaeb95611efa462dd6db688783feed1689cfaf3dc0f4fdadff6b795d6f1\n", " Stored in directory: /Users/rudra/Library/Caches/pip/wheels/0c/c2/0e/3b9c6845c6a4e35beb90910cc70d9ac9ab5d47402bd62af0df\n", " Building wheel for pycryptodome (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Created wheel for pycryptodome: filename=pycryptodome-3.15.0-cp35-abi3-macosx_11_0_arm64.whl size=1531792 sha256=4d20b21a578e519808e9790f3165eaa567e7948a7efb8ad2a95167b503b66c97\n", " Stored in directory: /Users/rudra/Library/Caches/pip/wheels/cc/09/af/5da58a51ca5bf4bd4a37747a0658984c196aa2badf61236a35\n", " Building wheel for python-multipart (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Created wheel for python-multipart: filename=python_multipart-0.0.5-py3-none-any.whl size=31671 sha256=74cbcfb82ca96c926b3312fdff0ee48229ecef1fc306cf1b79eca31112851db6\n", " Stored in directory: /Users/rudra/Library/Caches/pip/wheels/ae/3f/03/fa4bd98cd7f4a25e63b6a0b61a7a8352e0f874cd9de1f3390d\n", "Successfully built ffmpy pycryptodome python-multipart\n", "Installing collected packages: rfc3986, pydub, monotonic, ffmpy, websockets, uc-micro-py, sniffio, python-multipart, pycryptodome, orjson, multidict, mdurl, h11, fsspec, frozenlist, bcrypt, backoff, async-timeout, yarl, uvicorn, pynacl, markdown-it-py, linkify-it-py, anyio, analytics-python, aiosignal, starlette, paramiko, mdit-py-plugins, httpcore, aiohttp, httpx, fastapi, gradio\n", "Successfully installed aiohttp-3.8.1 aiosignal-1.2.0 analytics-python-1.4.0 anyio-3.6.1 async-timeout-4.0.2 backoff-1.10.0 bcrypt-4.0.0 fastapi-0.82.0 ffmpy-0.3.0 frozenlist-1.3.1 fsspec-2022.8.2 gradio-3.2 h11-0.12.0 httpcore-0.15.0 httpx-0.23.0 linkify-it-py-1.0.3 markdown-it-py-2.1.0 mdit-py-plugins-0.3.0 mdurl-0.1.2 monotonic-1.6 multidict-6.0.2 orjson-3.8.0 paramiko-2.11.0 pycryptodome-3.15.0 pydub-0.25.1 pynacl-1.5.0 python-multipart-0.0.5 rfc3986-1.5.0 sniffio-1.3.0 starlette-0.19.1 uc-micro-py-1.0.1 uvicorn-0.18.3 websockets-10.3 yarl-1.8.1\n" ] } ], "source": [ "#!pip install gradio" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lamborghini vs Mercedes" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/rudra/mambaforge/lib/python3.10/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "#|export\n", "\n", "from fastai.vision.all import *\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('lamborghini_vs_mercedes_model.pkl')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = ('lamborghini', 'mercedes')\n", "\n", "def predict(img):\n", " pred, idx, prob = learn.predict(img)\n", " return dict(zip(categories, map(float, prob)))" ] }, { "cell_type": "code", "execution_count": 9, "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": [ "{'lamborghini': 1.0, 'mercedes': 6.128332508126277e-09}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img = PILImage.create('lambo1.jpg')\n", "predict(img)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/rudra/mambaforge/lib/python3.10/site-packages/gradio/inputs.py:256: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n", " warnings.warn(\n", "/Users/rudra/mambaforge/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n", "/Users/rudra/mambaforge/lib/python3.10/site-packages/gradio/outputs.py:196: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n", " warnings.warn(\n", "/Users/rudra/mambaforge/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n", " warnings.warn(value)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861/\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/plain": [ "(, 'http://127.0.0.1:7861/', None)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#|export\n", "image = gr.inputs.Image(shape=(192,192))\n", "label = gr.outputs.Label()\n", "\n", "interface = gr.Interface(fn=predict, inputs=image, outputs=label, examples=['lambo1.jpg', 'mercedes1.jpg'])\n", "interface.launch(inline=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Export" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "from nbdev.export import nb_export\n", "nb_export('app.ipynb', '.')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "a03200ae361ba04b33dfbb7697ae27a1e19c2e9be6094bfaf79232177e8f3033" }, "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }