{ "cells": [ { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 72, "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/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "# import gradio as gr\n", "# # from fastai.vision.all import vision_learner\n", "\n", "# def greet(val1, val2):\n", "# return int(val1) + int(val2)\n", "\n", "# iface = gr.Interface(fn=greet, inputs=[\"text\", \"text\"], outputs=\"text\")\n", "# iface.launch(share=False)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7860\n" ] } ], "source": [ "iface.close()" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "\n", "def label_func(fname):\n", " return fname.name.split('_')[1].split('.')[0].strip()\n", "\n", "learner = load_learner('fruits.pkl')" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = learner.dls.vocab\n", "\n", "def classify_image(img):\n", " pred, pred_idx, probs = learner.predict(img)\n", " return dict(zip(categories, map(float, probs)))" ] }, { "cell_type": "code", "execution_count": 93, "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/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 93, "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" } ], "source": [ "#|export\n", "from re import I\n", "import gradio as gr\n", "\n", "ifc = gr.Interface(fn=classify_image,\n", " inputs=gr.Image(type='pil', label='Input Image'),\n", " outputs=gr.Label(num_top_classes=6)\n", " )\n", "ifc.launch()" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7860\n" ] } ], "source": [ "ifc.close()" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "# from doctest import debug\n", "from doctest import debug\n", "from os import name\n", "import nbdev\n", "# ?nbdev.export.nb_export\n", "!rm app.py\n", "val = nbdev.export.nb_export('./app.ipynb', lib_path='.')\n" ] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 2 }