{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "\n", "def is_cat(x: str): return x[0].isupper()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "im = PILImage.create(\"dog.jpg\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner(\"model.pkl\")" ] }, { "cell_type": "code", "execution_count": 4, "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": [ "('True', TensorBase(1), TensorBase([2.0341e-14, 1.0000e+00]))" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.predict(\"cat.jpg\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## this is how we will get the answer" ] }, { "cell_type": "code", "execution_count": 5, "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': 2.0341067629640712e-14, 'cat': 1.0}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pred, idx, probs = learn.predict(\"cat.jpg\")\n", "a = [\"dog\", \"cat\"]\n", "dict(zip(a,map(float, probs)))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = (\"Dog\", \"Cat\")\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": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/danielcodex/.local/lib/python3.9/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", "/home/danielcodex/.local/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n", "/home/danielcodex/.local/lib/python3.9/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", "/home/danielcodex/.local/lib/python3.9/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": [] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "image = gr.inputs.Image(shape=(192, 192))\n", "label = gr.outputs.Label()\n", "examples = [\"dog.jpg\", \"cat.jpg\"]\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "export successful\n" ] }, { "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" }, { "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" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import nbdev\n", "nbdev.export.nb_export(\"learning.ipynb\", \"app\")\n", "print(\"export successful\")\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from fastdownload import download_url\n", "from duckduckgo_search import ddg_images\n", "\n", "test = L(ddg_images(\"dog\", max_results=1)).itemgot(\"image\")\n", "# download_url(test[0], \"dog.jpg\", show_progress=False)\n", "\n", "cat = L(ddg_images(\"cat\", max_results=1)).itemgot(\"image\")\n", "# download_url(cat[0], \"cat.jpg\", show_progress=False)\n", "\n", "# dunno = L(ddg_images(\"dunno\", max_results=20)).itemgot(\"image\")\n", "# download_url(dunno[3], \"dunno.jpg\", show_progress=False)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "36cf16204b8548560b1c020c4e8fb5b57f0e4c58016f52f2d4be01e192833930" } } }, "nbformat": 4, "nbformat_minor": 2 }