{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0eee85c0", "metadata": {}, "outputs": [], "source": [ "#| default_exp app" ] }, { "cell_type": "code", "execution_count": 2, "id": "598c11f4", "metadata": {}, "outputs": [], "source": [ "!pip install gradio > /dev/null" ] }, { "cell_type": "markdown", "id": "1f836217", "metadata": {}, "source": [ "### Ecommerece Image classification app" ] }, { "cell_type": "code", "execution_count": 3, "id": "76333c52", "metadata": {}, "outputs": [], "source": [ "#| export\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "# ['jeans', 'sofa', 'tshirt', 'tv']\n", "\n", "def is_jeans(x): return x[0].isupper()" ] }, { "cell_type": "code", "execution_count": 4, "id": "09b9fe0b", "metadata": {}, "outputs": [], "source": [ "im = PILImage.create('jeans.jpeg')\n", "im.thumbnail((192,192))\n", "im" ] }, { "cell_type": "code", "execution_count": 5, "id": "ab0e0e6f", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('export.pkl')" ] }, { "cell_type": "code", "execution_count": 6, "id": "97559e61", "metadata": {}, "outputs": [], "source": [ "%time\n", "learn.predict(im)" ] }, { "cell_type": "code", "execution_count": 7, "id": "4e112978", "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = ('Jeans', 'Sofa', 'Tshirt', 'Tv')\n", "\n", "def classify_images(img):\n", " pred, idx, probs = learn.predict(img)\n", " return dict(zip(categories, map(float, probs)))" ] }, { "cell_type": "code", "execution_count": 8, "id": "d8d09015", "metadata": {}, "outputs": [], "source": [ "classify_images(im)" ] }, { "cell_type": "code", "execution_count": 9, "id": "8530628e", "metadata": {}, "outputs": [], "source": [ "#|export\n", "image = gr.components.Image(shape=(192,192))\n", "label = gr.components.Label()\n", "examples = ['jeans.jpeg', 'sofa.jpeg', 'tshirt.jpg', 'tv.jpeg']\n", "\n", "intf = gr.Interface(fn=classify_images, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": 10, "id": "9487e868", "metadata": {}, "outputs": [], "source": [ "import nbdev\n", "nbdev.export.nb_export('app.ipynb', './')\n", "print('Export successful')" ] }, { "cell_type": "code", "execution_count": null, "id": "a97c4bb5", "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.11.3" } }, "nbformat": 4, "nbformat_minor": 5 }