{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "id": "UySFk1vPKxb_" }, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "markdown", "metadata": { "id": "gT0wxrhGKIxL" }, "source": [ "# Bearify" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "Fg2er2rQLApV" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\utkar\\prod_apps\\Bearify\\bear_env\\lib\\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" ] } ], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "\n", "def which_bear(x): pass" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 209 }, "id": "vBBjPghILOjq", "outputId": "caa4c037-3d1e-43ae-a8e2-0f9c79198a2d" }, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'C:\\\\content\\\\teddy.jpg'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m im \u001b[38;5;241m=\u001b[39m \u001b[43mPILImage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m/content/teddy.jpg\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2\u001b[0m im\u001b[38;5;241m.\u001b[39mthumbnail((\u001b[38;5;241m192\u001b[39m,\u001b[38;5;241m192\u001b[39m))\n\u001b[0;32m 3\u001b[0m im\n", "File \u001b[1;32m~\\prod_apps\\Bearify\\bear_env\\lib\\site-packages\\fastai\\vision\\core.py:125\u001b[0m, in \u001b[0;36mPILBase.create\u001b[1;34m(cls, fn, **kwargs)\u001b[0m\n\u001b[0;32m 123\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(fn,\u001b[38;5;28mbytes\u001b[39m): fn \u001b[38;5;241m=\u001b[39m io\u001b[38;5;241m.\u001b[39mBytesIO(fn)\n\u001b[0;32m 124\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(fn,Image\u001b[38;5;241m.\u001b[39mImage): \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m(fn)\n\u001b[1;32m--> 125\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m(load_image(fn, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mmerge(\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_open_args, kwargs)))\n", "File \u001b[1;32m~\\prod_apps\\Bearify\\bear_env\\lib\\site-packages\\fastai\\vision\\core.py:98\u001b[0m, in \u001b[0;36mload_image\u001b[1;34m(fn, mode)\u001b[0m\n\u001b[0;32m 96\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mload_image\u001b[39m(fn, mode\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m 97\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOpen and load a `PIL.Image` and convert to `mode`\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m---> 98\u001b[0m im \u001b[38;5;241m=\u001b[39m \u001b[43mImage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 99\u001b[0m im\u001b[38;5;241m.\u001b[39mload()\n\u001b[0;32m 100\u001b[0m im \u001b[38;5;241m=\u001b[39m im\u001b[38;5;241m.\u001b[39m_new(im\u001b[38;5;241m.\u001b[39mim)\n", "File \u001b[1;32m~\\prod_apps\\Bearify\\bear_env\\lib\\site-packages\\PIL\\Image.py:3277\u001b[0m, in \u001b[0;36mopen\u001b[1;34m(fp, mode, formats)\u001b[0m\n\u001b[0;32m 3274\u001b[0m filename \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mrealpath(os\u001b[38;5;241m.\u001b[39mfspath(fp))\n\u001b[0;32m 3276\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m filename:\n\u001b[1;32m-> 3277\u001b[0m fp \u001b[38;5;241m=\u001b[39m \u001b[43mbuiltins\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfilename\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrb\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 3278\u001b[0m exclusive_fp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m 3280\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n", "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'C:\\\\content\\\\teddy.jpg'" ] } ], "source": [ "im = PILImage.create('/content/teddy.jpg')\n", "im.thumbnail((192,192))\n", "im" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "Ko1vxtuzACNo" }, "outputs": [], "source": [ "learn = load_learner('/content/bear_model.pkl')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "id": "N4lUOFyom35W", "outputId": "d363cb16-e67f-4829-a776-8af408671170" }, "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": [ "('teddy', tensor(2), tensor([4.8331e-05, 7.1999e-05, 9.9988e-01]))" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.predict(im)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "k8MzL29fm5wO" }, "outputs": [], "source": [ "categories = ('Teddy', 'Black', 'Grizzly')\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": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "id": "R_dNtPRtoPER", "outputId": "95b072b8-736f-424d-98dd-2a99e5078bef" }, "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": [ "{'Teddy': 4.833127968595363e-05,\n", " 'Black': 7.199876563390717e-05,\n", " 'Grizzly': 0.9998795986175537}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classify_image(im)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 211 }, "id": "Uc2M0zOEoR6b", "outputId": "08c190d2-b5ad-43d1-aa00-f4c452152024" }, "outputs": [ { "ename": "AttributeError", "evalue": "module 'gradio' has no attribute 'inputs'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mImage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshape\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m192\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m192\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mlabels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mLabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mintf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mInterface\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mclassify_image\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlabels\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mintf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlaunch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minline\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: module 'gradio' has no attribute 'inputs'" ] } ], "source": [ "image = gr.inputs.Image(shape = (192,192))\n", "labels = gr.outputs.Label()\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=labels)\n", "intf.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "bqK_vxTfpqBj" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": "bear_env", "language": "python", "name": "bear_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.10.9" } }, "nbformat": 4, "nbformat_minor": 4 }