{
"cells": [
{
"cell_type": "code",
"execution_count": 64,
"id": "30364f49-507a-474b-ac8c-bc60668a83d5",
"metadata": {},
"outputs": [],
"source": [
"#|default_exp app"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "22c0675e-0de0-4e5f-a92b-a903b2f6874a",
"metadata": {},
"outputs": [],
"source": [
"!pip install -Uqq fastai"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "63fa0ea9-142e-44ed-96fa-751e8dacc3ba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: gradio in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (3.16.1)\n",
"Requirement already satisfied: requests in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (2.28.1)\n",
"Requirement already satisfied: aiohttp in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.8.3)\n",
"Requirement already satisfied: pillow in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (9.3.0)\n",
"Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (1.23.5)\n",
"Requirement already satisfied: fastapi in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.89.1)\n",
"Requirement already satisfied: fsspec in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (2022.11.0)\n",
"Requirement already satisfied: pydub in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.25.1)\n",
"Requirement already satisfied: ffmpy in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.3.0)\n",
"Requirement already satisfied: websockets>=10.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (10.4)\n",
"Requirement already satisfied: python-multipart in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.0.5)\n",
"Requirement already satisfied: jinja2 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.1.2)\n",
"Requirement already satisfied: pycryptodome in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.16.0)\n",
"Requirement already satisfied: matplotlib in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.6.2)\n",
"Requirement already satisfied: markdown-it-py[linkify,plugins] in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (2.1.0)\n",
"Requirement already satisfied: httpx in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.23.3)\n",
"Requirement already satisfied: markupsafe in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (2.1.1)\n",
"Requirement already satisfied: typing-extensions in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (4.4.0)\n",
"Requirement already satisfied: pandas in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (1.5.2)\n",
"Requirement already satisfied: uvicorn in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.20.0)\n",
"Requirement already satisfied: pydantic in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (1.8.2)\n",
"Requirement already satisfied: altair>=4.2.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (4.2.0)\n",
"Requirement already satisfied: pyyaml in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (6.0)\n",
"Requirement already satisfied: orjson in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.8.5)\n",
"Requirement already satisfied: jsonschema>=3.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from altair>=4.2.0->gradio) (4.16.0)\n",
"Requirement already satisfied: entrypoints in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from altair>=4.2.0->gradio) (0.4)\n",
"Requirement already satisfied: toolz in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from altair>=4.2.0->gradio) (0.12.0)\n",
"Requirement already satisfied: python-dateutil>=2.8.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from pandas->gradio) (2.8.2)\n",
"Requirement already satisfied: pytz>=2020.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from pandas->gradio) (2022.7)\n",
"Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (2.0.4)\n",
"Requirement already satisfied: attrs>=17.3.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (22.1.0)\n",
"Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (4.0.2)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (6.0.4)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (1.3.1)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (1.3.3)\n",
"Requirement already satisfied: yarl<2.0,>=1.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (1.8.2)\n",
"Requirement already satisfied: starlette==0.22.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from fastapi->gradio) (0.22.0)\n",
"Requirement already satisfied: anyio<5,>=3.4.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from starlette==0.22.0->fastapi->gradio) (3.5.0)\n",
"Requirement already satisfied: httpcore<0.17.0,>=0.15.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from httpx->gradio) (0.16.3)\n",
"Requirement already satisfied: certifi in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from httpx->gradio) (2022.12.7)\n",
"Requirement already satisfied: sniffio in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from httpx->gradio) (1.2.0)\n",
"Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from httpx->gradio) (1.5.0)\n",
"Requirement already satisfied: mdurl~=0.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from markdown-it-py[linkify,plugins]->gradio) (0.1.2)\n",
"Requirement already satisfied: linkify-it-py~=1.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from markdown-it-py[linkify,plugins]->gradio) (1.0.3)\n",
"Requirement already satisfied: mdit-py-plugins in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from markdown-it-py[linkify,plugins]->gradio) (0.3.3)\n",
"Requirement already satisfied: cycler>=0.10 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (0.11.0)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (1.4.4)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (4.25.0)\n",
"Requirement already satisfied: packaging>=20.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (22.0)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (3.0.9)\n",
"Requirement already satisfied: contourpy>=1.0.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (1.0.5)\n",
"Requirement already satisfied: six>=1.4.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from python-multipart->gradio) (1.16.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from requests->gradio) (3.4)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from requests->gradio) (1.26.13)\n",
"Requirement already satisfied: h11>=0.8 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from uvicorn->gradio) (0.14.0)\n",
"Requirement already satisfied: click>=7.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from uvicorn->gradio) (8.0.4)\n",
"Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from jsonschema>=3.0->altair>=4.2.0->gradio) (0.18.0)\n",
"Requirement already satisfied: uc-micro-py in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from linkify-it-py~=1.0->markdown-it-py[linkify,plugins]->gradio) (1.0.1)\n"
]
}
],
"source": [
"!pip install gradio"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "7658cf93-38a8-4f3c-a773-ec7b7777652d",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"from fastai.vision.all import *\n",
"import gradio as gr\n",
"\n",
"def is_cat(x): return x[0].isupper()"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "9c6c2b95-2f0d-4ac5-8d9c-59b420ba8d8e",
"metadata": {},
"outputs": [],
"source": [
"path = untar_data(URLs.PETS)/'images'"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "a892c11a-33bc-4947-a7b2-04237df332d8",
"metadata": {},
"outputs": [],
"source": [
"dls = ImageDataLoaders.from_name_func('.',\n",
" get_image_files(path), valid_pct=0.2, seed=42,\n",
" label_func=is_cat,\n",
" item_tfms=Resize(192))"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "1a83ccd5-ec38-4f03-b937-fabe5f9c63b1",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"learn = load_learner('model.pkl')"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "9a4418cc-4bb4-4e9c-9ae8-0ba1f42e7af7",
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"categories = ('Dog', 'Cat')\n",
"\n",
"def classify(img):\n",
" pred,idx,probs = learn.predict(img)\n",
" return dict(zip(categories, map(float, probs)))"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "79085d0b-8db7-43d4-9da3-0d764c302888",
"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": [
"{'Dog': 3.103109945650634e-14, 'Cat': 1.0}"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"classify('cat.jpg')"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "b1155832-5e29-4f88-aea4-85a6261bedfd",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages/gradio/inputs.py:257: 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",
"/opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
" warnings.warn(value)\n",
"/opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages/gradio/outputs.py:197: 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",
"/opt/homebrew/Caskroom/miniforge/base/envs/fastai/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:7865\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/plain": []
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#|export\n",
"image = gr.inputs.Image(shape=(192, 192))\n",
"labels = gr.outputs.Label()\n",
"examples = ['dog.jpg', 'cat.jpg']\n",
"\n",
"intf = gr.Interface(fn=classify, inputs=image, outputs=labels, examples=examples)\n",
"intf.launch(inline=False)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "419e727b-5fc0-4a19-94e3-7c9c9c8c22ad",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Export successful\n"
]
}
],
"source": [
"import nbdev\n",
"nbdev.export.nb_export('main.ipynb', '.')\n",
"print('Export successful')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b864b212-17a0-4737-a541-1ead7e3c762a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "fastai",
"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.8"
},
"vscode": {
"interpreter": {
"hash": "4b6371f9b180b180e3c0fc2dc539c52593b72b3e75aed3593bd0a5845eb81bd2"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}