{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "!pip install -Uqq fastai gradio nbdev" ], "metadata": { "id": "LyRA7KSuOt6f" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "P9petL8cIBXd" }, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr" ], "metadata": { "id": "6olANYYvILjT" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "im = PILImage.create('garamond.png')\n", "im.thumbnail((192, 192))\n", "im" ], "metadata": { "id": "JNb8CfVCRM3C" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#|export\n", "learn = load_learner('model.pkl')" ], "metadata": { "id": "FFxZjXWrP-Fg" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "learn.predict(im)" ], "metadata": { "id": "gA5vqoTISPVL" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#|export\n", "categories = ('Garamond', 'Helvetica')\n", "\n", "def classify_image(img):\n", " pred, idx, probs = learn.predict(img)\n", " return dict(zip(categories, map(float, probs)))" ], "metadata": { "id": "bScV4g0eQUgh" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "classify_image(im)" ], "metadata": { "id": "_yMhyT_hSXt0" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#|export\n", "image = gr.inputs.Image(shape=(192, 192))\n", "label = gr.outputs.Label()\n", "examples = ['garamond.png', 'helvetica.png']\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ], "metadata": { "id": "BTRZvR-3SdRp" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Export" ], "metadata": { "id": "BtgbVjRtTTqB" } }, { "cell_type": "code", "source": [ "import nbdev" ], "metadata": { "id": "_wNIvDXSTUwC" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "nbdev.export.nb_export('app.ipynb', 'app')" ], "metadata": { "id": "CMlwAQz0Taj0" }, "execution_count": null, "outputs": [] } ] }