{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#|default_exp demo" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Basic setup, once again..." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import pathlib\n", "import gradio as gr\n", "plt = platform.system()\n", "if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath\n", "\n", "def is_albani(path):\n", " return parent_label(path) == \"albani\"" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Test with own images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "albani = PILImage.create('albani2.jpg')\n", "\n", "albani.thumbnail((192, 192))\n", "albani" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "learn = load_learner(Path('./resnet18-albani.pkl'))\n", "\n", "learn.predict(albani)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Gradio demo" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7873\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/plain": [] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "\n", "learn = load_learner(Path('./resnet18-albani.pkl'))\n", "categories = ('Dårlig Øl', 'God Øl')\n", "\n", "def classify_image(img):\n", " pred,idx,probs = learn.predict(img)\n", " return dict(zip(categories, map(float, probs)))\n", "\n", "title = \"\"\"\n", " # Er du i tvivl om at den øl du sidder med i hånden lige, nu er god?\n", " ### Tvivl ej 68 års invotation inden for machine learning skal nok fortælle dig om øllen er god eller ej\n", "\"\"\"\n", "image = gr.Image(shape=(192, 192))\n", "label = gr.Label()\n", "examples = ['albani.jpg', 'albani2.jpg', 'heineken.jpg', 'carlsberg.jpg']\n", "\n", "iface = gr.Interface(\n", " fn=classify_image, \n", " inputs=image, \n", " outputs=label, \n", " examples=examples, \n", " description=title,\n", " title=\"Er du i tvivl om at den øl du sidder med i hånden lige nu er god? \"\n", ")\n", "\n", "iface.launch(inline=False)\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Build for huggingface" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO: Successfully saved requirements file in ./requirements.txt\n" ] } ], "source": [ "from nbdev.export import nb_export\n", "\n", "nb_export('model-test.ipynb', '.')\n", "! pipreqs . --force\n" ] } ], "metadata": { "kernelspec": { "display_name": "fastai", "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.8" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "b977fc7f635931fb2de4a5e9c6503b6f91cf50b058799eb3f69da04d3aca2546" } } }, "nbformat": 4, "nbformat_minor": 2 }