{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "b0b5e6d7", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": null, "id": "894a6707", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 2, "id": "d9019a15", "metadata": {}, "outputs": [], "source": [ "#|export \n", "\n", "categories = ('ocean', 'space', 'money')\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": 3, "id": "d04d8882", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'learn' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[3], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m im \u001b[38;5;241m=\u001b[39m PILImage\u001b[38;5;241m.\u001b[39mcreate(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mocean.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mclassify_image\u001b[49m\u001b[43m(\u001b[49m\u001b[43mim\u001b[49m\u001b[43m)\u001b[49m\n", "Cell \u001b[0;32mIn[2], line 6\u001b[0m, in \u001b[0;36mclassify_image\u001b[0;34m(img)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mclassify_image\u001b[39m(img):\n\u001b[0;32m----> 6\u001b[0m pred, idx, probs \u001b[38;5;241m=\u001b[39m \u001b[43mlearn\u001b[49m\u001b[38;5;241m.\u001b[39mpredict(img)\n\u001b[1;32m 7\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mdict\u001b[39m(\u001b[38;5;28mzip\u001b[39m(categories, \u001b[38;5;28mmap\u001b[39m(\u001b[38;5;28mfloat\u001b[39m, probs)))\n", "\u001b[0;31mNameError\u001b[0m: name 'learn' is not defined" ] } ], "source": [ "im = PILImage.create('ocean.jpg')\n", "classify_image(im)" ] }, { "cell_type": "code", "execution_count": null, "id": "bbef2af6", "metadata": {}, "outputs": [], "source": [ "#|export\n", "image = gr.inputs.Image(shape=(192,192))\n", "label = gr.outputs.Label()\n", "examples = ['ocean.jpg', 'space.jpg', 'money.jpg']\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "c0c533db", "metadata": {}, "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.8.13" } }, "nbformat": 4, "nbformat_minor": 5 }