{ "cells": [ { "cell_type": "code", "execution_count": 7, "id": "db4fccaf", "metadata": {}, "outputs": [], "source": [ "#|default_exp app\n", "\n", "!pip install -Uqq fastai\n", "!pip install -Uqq gradio" ] }, { "cell_type": "code", "execution_count": 8, "id": "7f067622", "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "\n", "def is_cat(x): return x[0].isupper() " ] }, { "cell_type": "code", "execution_count": 9, "id": "2a2f5376", "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "learn = load_learner('model.pkl')" ] }, { "cell_type": "code", "execution_count": 10, "id": "2ee57395", "metadata": {}, "outputs": [], "source": [ "#|export\n", "\n", "labels = learn.dls.vocab\n", "def predict(img):\n", " img = PILImage.create(img)\n", " pred,pred_idx,probs = learn.predict(img)\n", " return {labels[i]: float(probs[i]) for i in range(len(labels))}" ] }, { "cell_type": "code", "execution_count": 17, "id": "7e0abb03", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7866\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" }, { "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": [ "#|export\n", "\n", "examples = [\"example1.webp\",\"example2.webp\",\"example3.webp\"]\n", " \n", "gr.Interface(fn=predict, inputs=\"image\", examples=examples, outputs=\"label\").launch(share=False)" ] }, { "cell_type": "code", "execution_count": 18, "id": "22dc9b3a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Export successful\n" ] } ], "source": [ "import nbdev\n", "nbdev.export.nb_export('is_a_cat.ipynb', './')\n", "print('Export successful')" ] }, { "cell_type": "code", "execution_count": null, "id": "baca5791", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }