{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "4b40cb3a-544a-4b23-8c00-431cb7133130", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python 3.11.5\n" ] } ], "source": [ "%%bash\n", "python --version" ] }, { "cell_type": "code", "execution_count": 2, "id": "93e1afb8-f78c-4862-9d56-a06a3559b4d1", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 3, "id": "2adf2fa8-199b-48e4-a91c-9a093032480c", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "import timm" ] }, { "cell_type": "code", "execution_count": 4, "id": "f0218bf1-1836-4d7a-8d47-33584471f28b", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('cycle-model.pkl')" ] }, { "cell_type": "code", "execution_count": 5, "id": "168ac2e4-f83b-4ce0-8f23-00999eb5d556", "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = learn.dls.vocab\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": 6, "id": "d343a0d3-40fd-4502-a86b-cb3bac9fdf7f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "images/unicycle.jpeg is a tricycle\n" ] }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "images/bicycle.jpeg is a bicycle\n" ] }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "images/tricycle.png is a tricycle\n" ] } ], "source": [ "# Upload your own images and link them\n", "examples = ['images/unicycle.jpeg', 'images/bicycle.jpeg', 'images/tricycle.png']\n", "\n", "for example in examples:\n", " image = PILImage.create(example)\n", " res_dict = classify_image(image)\n", " top = max(res_dict, key=res_dict.get)\n", "\n", " print(example + ' is a '+ top)" ] }, { "cell_type": "code", "execution_count": 7, "id": "156a1fa0-e124-4a18-b411-367e7926afa4", "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": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "\n", "image = gr.Image()\n", "label = gr.Label()\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch()" ] }, { "cell_type": "code", "execution_count": 8, "id": "2894f2be-e453-4795-8a16-2aa4770aa16d", "metadata": {}, "outputs": [], "source": [ "import nbdev" ] }, { "cell_type": "code", "execution_count": 10, "id": "10568397-2167-4c39-8120-436e577b452d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Export successful\n" ] } ], "source": [ "nbdev.export.nb_export('notebook.ipynb', '')\n", "print('Export successful')" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }