{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "cde69a49-7ab1-4bf5-bb0f-30746827dedb", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 2, "id": "e3e7300c-9b84-4687-8c72-7914240e9397", "metadata": {}, "outputs": [], "source": [ "# Suppress only UserWarning\n", "import warnings\n", "\n", "warnings.filterwarnings('ignore', category=UserWarning)" ] }, { "cell_type": "code", "execution_count": 3, "id": "5026e71e-78b2-451c-b5f8-20421387a480", "metadata": {}, "outputs": [], "source": [ "!pip install gradio > /dev/null" ] }, { "cell_type": "code", "execution_count": 12, "id": "39544176-83a1-4bcb-bdc3-c8acb8a2ba84", "metadata": { "scrolled": true }, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "\n", "import pathlib\n", "import os" ] }, { "cell_type": "code", "execution_count": 13, "id": "38301e4e-11fc-4f1e-909f-58759b8d9e8d", "metadata": { "scrolled": true }, "outputs": [], "source": [ "#|export\n", "\n", "path = pathlib.Path('.')\n", "\n", "try: \n", " path = pathlib.Path(__file__).parent.resolve()\n", "except NameError: \n", " if 'SPACE_ID' in os.environ: \n", " path = pathlib.Path('nbs') \n", "\n", "model_path = path / 'model.pkl'\n", "\n", "learn = load_learner(model_path)" ] }, { "cell_type": "code", "execution_count": 14, "id": "905035bc-6dd2-45ba-aee8-b77a5b519a04", "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = learn.dls.vocab\n", "\n", "def classify_image(img):\n", " pred,idx,probs = learn.predict(img)\n", " return dict(zip(categories, map(float,probs)))" ] }, { "cell_type": "code", "execution_count": 19, "id": "e77f8a97-e73f-4c3b-a73d-a958689ff1a6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7863\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/plain": [] }, "execution_count": 19, "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" } ], "source": [ "#|export\n", "image = gr.Image(type=\"pil\")\n", "label = gr.Label()\n", "\n", "examples = [\n", " path / 'black_bear.jpg',\n", " path / 'grizzly_bear.jpg',\n", " path / 'teddy_bear.jpg'\n", " ]\n", "\n", "intf = gr.Interface(\n", " fn=classify_image, \n", " inputs=image, \n", " outputs=label, \n", " examples=examples\n", ")\n", "\n", "intf.launch(inline=False)" ] }, { "cell_type": "markdown", "id": "3a379ae5-65ff-476a-ba6d-3c1edc6878b3", "metadata": {}, "source": [ "## export python file from the notebook" ] }, { "cell_type": "code", "execution_count": 21, "id": "af0ec285-9e53-49f0-be57-12d4e1fc1a3c", "metadata": {}, "outputs": [], "source": [ "import nbdev\n", "\n", "nbdev.export.nb_export('app.ipynb', '.')" ] } ], "metadata": { "kernelspec": { "display_name": "Python (fastai_env)", "language": "python", "name": "fastai_env" }, "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.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }