{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "2c1a0cda-918c-4bcb-abb1-3afeb823add8", "metadata": {}, "outputs": [], "source": [ "#| default_exp app" ] }, { "cell_type": "code", "execution_count": 21, "id": "baaff5aa-0845-4184-90de-533fec9fc366", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "#|export\n", "from fastbook import search_images_ddg\n", "from fastdownload import download_url\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "import torch" ] }, { "cell_type": "code", "execution_count": 22, "id": "0bfbf2a5-a46a-4a47-b982-4e36d8418c0c", "metadata": {}, "outputs": [], "source": [ "#def greet(name):\n", "# return \"Hello \" + name + \"!!\"" ] }, { "cell_type": "code", "execution_count": 23, "id": "c725868e-a654-4ea7-9a2f-78bee1491e72", "metadata": {}, "outputs": [], "source": [ "#iface = gr.Interface(fn=greet, inputs=\"text\", outputs=\"text\")\n", "#iface.launch(share=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "b908abcb-2973-48d5-89ec-0f02b75b9ed3", "metadata": {}, "outputs": [], "source": [ "#|export\n", "img = PILImage.create('grizzly.jpg')\n", "img.thumbnail((192,192))\n", "img" ] }, { "cell_type": "code", "execution_count": 32, "id": "9db1bbb9-b033-472f-b971-416b7756568c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'model.pkl'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "path = Path()\n", "filename = path.ls(file_exts='.pkl')\n", "filename[0].name" ] }, { "cell_type": "code", "execution_count": null, "id": "40e4ac20-e52f-4e4a-b6a8-6ad2816a3415", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "#|export\n", "learn = load_learner(filename[0].name)" ] }, { "cell_type": "code", "execution_count": null, "id": "7dba9619-034f-476d-a7c4-5f4511ed18ce", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "%time learn.predict(img)" ] }, { "cell_type": "code", "execution_count": 37, "id": "74f92818-e648-4198-a061-308aebf61989", "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "searches = (\"grizzly bears\",\"black bears\",\"teddy bears\")\n", "\n", "def classify_image(img):\n", " pred,idx,probs = learn.predict(img)\n", " return dict(zip(searches, map(float,probs)))" ] }, { "cell_type": "code", "execution_count": null, "id": "634686dd-4306-4888-8792-699d95f2782c", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [ "%time classify_image(img)" ] }, { "cell_type": "code", "execution_count": 39, "id": "33d12cfc-68eb-4666-8bd7-b7617eec65cd", "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "title = \"FastAi demo\"\n", "description = \"This demo is the original\"\n", "examples = [\"grizzly.jpg\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "5dfdfd51-32bd-4364-a41a-a391962e72c6", "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "\n", "demo = gr.Interface(\n", " fn=classify_image,\n", " inputs=\"image\",\n", " outputs=\"label\",\n", " title=title,\n", " description=description,\n", " examples = examples)\n", "demo.launch()" ] }, { "cell_type": "markdown", "id": "ac141816-1577-41bb-bd91-dc79682896de", "metadata": {}, "source": [ "## export" ] }, { "cell_type": "code", "execution_count": 43, "id": "5f14dab3-e6c3-4768-a8e0-8b7355c50929", "metadata": {}, "outputs": [], "source": [ "from nbdev.export import *" ] }, { "cell_type": "code", "execution_count": 44, "id": "1998fe76-7137-4f3a-b1b2-69f202bc0f49", "metadata": {}, "outputs": [], "source": [ "nb_export('app.ipynb',lib_path=Path())" ] }, { "cell_type": "code", "execution_count": null, "id": "0cfad669-4502-49f8-8dca-3fc7c5ea0525", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [], "source": [] } ], "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.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }