{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "2904c251", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "markdown", "id": "8f566fe5", "metadata": {}, "source": [ "# Export instructions" ] }, { "cell_type": "markdown", "id": "a8bd8621-65ae-4cb2-bdac-12c3b97a0de5", "metadata": {}, "source": [ "As of 24.3.18 the way to export cells to a python script is:\n", "1) pip install nbdev\n", "2) Add \"#|default_exp [file_name]\" to the top of your notebook\n", "3) Add \"#|export\" to the top of each cell you want to export\n", "4) run:\n", "```python\n", " import nbdev\n", " notebook_name = \"lesson_2.ipynb\"\n", " export_destination = \".\" # the root directory\n", " nbdev.export.nb_export(notebook_name, export_destination)\n", "```" ] }, { "cell_type": "markdown", "id": "6f087d39-106b-46a2-8623-f4429e2885f1", "metadata": {}, "source": [ "# Code" ] }, { "cell_type": "code", "execution_count": 2, "id": "746c335b-5825-4032-9ee9-834d947ba01d", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages/torchvision/io/image.py:13: UserWarning: Failed to load image Python extension: 'dlopen(/Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages/torchvision/image.so, 0x0006): Symbol not found: __ZN3c1017RegisterOperatorsD1Ev\n", " Referenced from: /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages/torchvision/image.so\n", " Expected in: /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages/torch/lib/libtorch_cpu.dylib'If you don't plan on using image functionality from `torchvision.io`, you can ignore this warning. Otherwise, there might be something wrong with your environment. Did you have `libjpeg` or `libpng` installed before building `torchvision` from source?\n", " warn(\n" ] } ], "source": [ "#|export\n", "from fastai.vision.all import *\n", "from fastai.collab import *\n", "\n", "if os.path.isfile('export.pkl'):\n", " learn: Learner = load_learner('export.pkl')\n", "else:\n", " path = untar_data(URLs.PETS)\n", " dls = ImageDataLoaders.from_name_re(path, get_image_files(path/'images'), pat='(.+)_\\d+.jpg', item_tfms=Resize(460), batch_tfms=aug_transforms(size=224, min_scale=0.75))\n", " learn = vision_learner(dls, models.resnet50, metrics=accuracy)\n", " learn.fine_tune(1)\n", " learn.path = Path('.')\n", " learn.export()" ] }, { "cell_type": "code", "execution_count": 3, "id": "d670bc7e", "metadata": {}, "outputs": [], "source": [ "#|export\n", "dls: DataLoaders = learn.dls\n", "labels = dls.vocab\n", "\n", "def predict(image):\n", " image = PILImage.create(image)\n", " prediction, prediction_index, probabilities = learn.predict(image)\n", " return { labels[i]: float(probabilities[i]) for i in range(len(labels))}\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "5be95314", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: gradio in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (4.22.0)\n", "Requirement already satisfied: aiofiles<24.0,>=22.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (23.2.1)\n", "Requirement already satisfied: altair<6.0,>=4.2.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (5.2.0)\n", "Requirement already satisfied: fastapi in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.110.0)\n", "Requirement already satisfied: ffmpy in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.3.2)\n", "Requirement already satisfied: gradio-client==0.13.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.13.0)\n", "Requirement already satisfied: httpx>=0.24.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.27.0)\n", "Requirement already satisfied: huggingface-hub>=0.19.3 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.21.4)\n", "Requirement already satisfied: importlib-resources<7.0,>=1.3 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (6.1.1)\n", "Requirement already satisfied: jinja2<4.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (3.1.3)\n", "Requirement already satisfied: markupsafe~=2.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (2.1.3)\n", "Requirement already satisfied: matplotlib~=3.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (3.8.0)\n", "Requirement already satisfied: numpy~=1.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (1.26.4)\n", "Requirement already satisfied: orjson~=3.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (3.9.15)\n", "Requirement already satisfied: packaging in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (23.2)\n", "Requirement already satisfied: pandas<3.0,>=1.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (2.2.1)\n", "Requirement already satisfied: pillow<11.0,>=8.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (10.2.0)\n", "Requirement already satisfied: pydantic>=2.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (2.5.3)\n", "Requirement already satisfied: pydub in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.25.1)\n", "Requirement already satisfied: python-multipart>=0.0.9 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.0.9)\n", "Requirement already satisfied: pyyaml<7.0,>=5.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (6.0.1)\n", "Requirement already satisfied: ruff>=0.2.2 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.3.4)\n", "Requirement already satisfied: semantic-version~=2.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (2.10.0)\n", "Requirement already satisfied: tomlkit==0.12.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.12.0)\n", "Requirement already satisfied: typer<1.0,>=0.9 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from typer[all]<1.0,>=0.9->gradio) (0.9.0)\n", "Requirement already satisfied: typing-extensions~=4.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (4.9.0)\n", "Requirement already satisfied: uvicorn>=0.14.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio) (0.29.0)\n", "Requirement already satisfied: fsspec in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio-client==0.13.0->gradio) (2023.10.0)\n", "Requirement already satisfied: websockets<12.0,>=10.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from gradio-client==0.13.0->gradio) (11.0.3)\n", "Requirement already satisfied: jsonschema>=3.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from altair<6.0,>=4.2.0->gradio) (4.21.1)\n", "Requirement already satisfied: toolz in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from altair<6.0,>=4.2.0->gradio) (0.12.1)\n", "Requirement already satisfied: anyio in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from httpx>=0.24.1->gradio) (4.3.0)\n", "Requirement already satisfied: certifi in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from httpx>=0.24.1->gradio) (2024.2.2)\n", "Requirement already satisfied: httpcore==1.* in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from httpx>=0.24.1->gradio) (1.0.4)\n", "Requirement already satisfied: idna in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from httpx>=0.24.1->gradio) (3.4)\n", "Requirement already satisfied: sniffio in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from httpx>=0.24.1->gradio) (1.3.1)\n", "Requirement already satisfied: h11<0.15,>=0.13 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n", "Requirement already satisfied: filelock in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from huggingface-hub>=0.19.3->gradio) (3.13.1)\n", "Requirement already satisfied: requests in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from huggingface-hub>=0.19.3->gradio) (2.31.0)\n", "Requirement already satisfied: tqdm>=4.42.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from huggingface-hub>=0.19.3->gradio) (4.65.0)\n", "Requirement already satisfied: zipp>=3.1.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from importlib-resources<7.0,>=1.3->gradio) (3.17.0)\n", "Requirement already satisfied: contourpy>=1.0.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from matplotlib~=3.0->gradio) (1.2.0)\n", "Requirement already satisfied: cycler>=0.10 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from matplotlib~=3.0->gradio) (0.11.0)\n", "Requirement already satisfied: fonttools>=4.22.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from matplotlib~=3.0->gradio) (4.25.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from matplotlib~=3.0->gradio) (1.4.4)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from matplotlib~=3.0->gradio) (3.0.9)\n", "Requirement already satisfied: python-dateutil>=2.7 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from matplotlib~=3.0->gradio) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from pandas<3.0,>=1.0->gradio) (2023.3.post1)\n", "Requirement already satisfied: tzdata>=2022.7 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from pandas<3.0,>=1.0->gradio) (2023.3)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from pydantic>=2.0->gradio) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.14.6 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from pydantic>=2.0->gradio) (2.14.6)\n", "Requirement already satisfied: click<9.0.0,>=7.1.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from typer<1.0,>=0.9->typer[all]<1.0,>=0.9->gradio) (8.1.7)\n", "Requirement already satisfied: colorama<0.5.0,>=0.4.3 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from typer[all]<1.0,>=0.9->gradio) (0.4.6)\n", "Requirement already satisfied: shellingham<2.0.0,>=1.3.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from typer[all]<1.0,>=0.9->gradio) (1.5.0)\n", "Requirement already satisfied: rich<14.0.0,>=10.11.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from typer[all]<1.0,>=0.9->gradio) (13.3.5)\n", "Requirement already satisfied: starlette<0.37.0,>=0.36.3 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from fastapi->gradio) (0.36.3)\n", "Requirement already satisfied: attrs>=22.2.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (23.2.0)\n", "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (2023.12.1)\n", "Requirement already satisfied: referencing>=0.28.4 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.34.0)\n", "Requirement already satisfied: rpds-py>=0.7.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.18.0)\n", "Requirement already satisfied: six>=1.5 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n", "Requirement already satisfied: markdown-it-py<3.0.0,>=2.2.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from rich<14.0.0,>=10.11.0->typer[all]<1.0,>=0.9->gradio) (2.2.0)\n", "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from rich<14.0.0,>=10.11.0->typer[all]<1.0,>=0.9->gradio) (2.15.1)\n", "Requirement already satisfied: exceptiongroup>=1.0.2 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from anyio->httpx>=0.24.1->gradio) (1.2.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from requests->huggingface-hub>=0.19.3->gradio) (2.0.4)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from requests->huggingface-hub>=0.19.3->gradio) (2.1.0)\n", "Requirement already satisfied: mdurl~=0.1 in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (from markdown-it-py<3.0.0,>=2.2.0->rich<14.0.0,>=10.11.0->typer[all]<1.0,>=0.9->gradio) (0.1.0)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install gradio" ] }, { "cell_type": "code", "execution_count": 5, "id": "e7e4c80c", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages/tqdm/auto.py:21: 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" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "Running on public URL: https://95de38d55a9ae84b0c.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "import gradio\n", "interface = gradio.Interface(\n", " fn=predict, \n", " inputs=gradio.Image(height=512, width=512), \n", " outputs=gradio.Label(num_top_classes=3),\n", " title=\"Dog Breed Classifier\",\n", " description=\"Upload a photo of a dog and we'll tell you the likely breeds\",\n", " article=\"This is Roman, he's a Red Nosed Pitbull (aka. American Pit-Bull Terrier) from New Orleans.\\n\\nTo try out the classifier, tap on his image to load him into the input box above and then press submit.\",\n", " examples=['roman.png']\n", ")\n", "interface.launch(share=True)" ] }, { "cell_type": "code", "execution_count": 6, "id": "807e0708", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: jupyterlab-quarto in /Users/charlesfeinn/miniforge-pypy3/envs/default/lib/python3.9/site-packages (0.3.3)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install jupyterlab-quarto" ] }, { "cell_type": "code", "execution_count": 7, "id": "342a044e", "metadata": {}, "outputs": [], "source": [ "import nbdev\n", "\n", "notebook_name = \"lesson_2.ipynb\"\n", "export_destination = \".\" # the root directory\n", "nbdev.export.nb_export(notebook_name, export_destination)" ] } ], "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.18" }, "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 } }, "nbformat": 4, "nbformat_minor": 5 }