{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "27c92395-9f07-4383-9526-58363a090f46", "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 2, "id": "a8969a2b-13c0-496d-918b-179bcd1ad7b8", "metadata": {}, "outputs": [], "source": [ "def is_cat(x): return x[0].isupper()" ] }, { "cell_type": "code", "execution_count": 3, "id": "407054be-12ef-40f0-860f-856e8a835a5e", "metadata": {}, "outputs": [], "source": [ "learn = load_learner('model.pkl')" ] }, { "cell_type": "code", "execution_count": 4, "id": "51f3903e-55c2-476e-96f9-ece20e49915c", "metadata": {}, "outputs": [], "source": [ "categories = ('Dog', 'Cat')" ] }, { "cell_type": "code", "execution_count": 5, "id": "3067fd5b-4872-4a0b-94bb-3a245129f340", "metadata": {}, "outputs": [], "source": [ "def classify_image(img):\n", " pred, idx, probs = learn.predict(img)\n", " return dict(zip(categories, map(float, probs)))" ] }, { "cell_type": "code", "execution_count": 6, "id": "f285720c-a8d3-46ba-80e9-8e5ef79ec4ec", "metadata": {}, "outputs": [], "source": [ "image = gr.Image(height=192, width=192)\n", "label = gr.Label()\n", "examples = ['dog.jpg', 'cat.jpg']" ] }, { "cell_type": "code", "execution_count": 7, "id": "9ae63726-4b8b-4d9c-9a95-a18e9ffe61ab", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/plain": [] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] } ], "metadata": { "kernelspec": { "display_name": "Python (base)", "language": "python", "name": "base" }, "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.2" } }, "nbformat": 4, "nbformat_minor": 5 }