{ "cells": [ { "cell_type": "code", "execution_count": 149, "id": "4fbf20c3", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 150, "id": "5cd0900b", "metadata": {}, "outputs": [], "source": [ "!pip install -Uqq fastai" ] }, { "cell_type": "code", "execution_count": 151, "id": "e9fdfb5c", "metadata": {}, "outputs": [], "source": [ "from fastai.vision.all import *\n", "import gradio as gr\n", "\n", "def is_cat(x): return x[0].isupper() " ] }, { "cell_type": "code", "execution_count": 152, "id": "9a9003fb", "metadata": { "scrolled": true }, "outputs": [], "source": [ "im = PILImage.create('dog.jpg')\n", "im.thumbnail((192,192))\n", "im" ] }, { "cell_type": "markdown", "id": "7683e0bb", "metadata": {}, "source": [ "Wrote #|export to know what you would need to include in your python script" ] }, { "cell_type": "code", "execution_count": 153, "id": "b6eafa98", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('model.pkl')" ] }, { "cell_type": "code", "execution_count": 154, "id": "c58e6bec", "metadata": { "scrolled": false }, "outputs": [], "source": [ "%time learn.predict(im)" ] }, { "cell_type": "markdown", "id": "89895bea", "metadata": {}, "source": [ "### Gradio doesn't offer numpy arrays. It returns tensors. It changes it into a normal float\n", "\n", "zip(categories, map(float,probs)): The zip() function takes two iterables as its arguments and returns an iterator of pairs where the first element of each passed iterable is paired together, the second element of each passed iterable is paired together, and so on. In this case, the categories tuple and the iterable of floating-point numbers are passed, resulting in an iterable of pairs with a category (e.g., 'Dog' or 'Cat') as the first element and the corresponding probability as the second element.\n", "\n", "dict(zip(categories, map(float,probs))): The dict() function takes the iterable of pairs and converts it into a dictionary. The first element of each pair becomes a key in the dictionary, and the second element becomes the corresponding value.\n", "\n" ] }, { "cell_type": "code", "execution_count": 155, "id": "60943f1e", "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = ('Dog', 'Cat')\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": 156, "id": "faa912af", "metadata": {}, "outputs": [], "source": [ "classify_image(im)" ] }, { "cell_type": "code", "execution_count": 157, "id": "b2cc2ec2", "metadata": { "scrolled": true }, "outputs": [], "source": [ "#|export\n", "image = gr.inputs.Image(shape=(192,192))\n", "label = gr.outputs.Label()\n", "examples = ['dog.jpg', 'cat.jpg', 'dunno.jpg']\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] }, { "cell_type": "code", "execution_count": 164, "id": "933e4d13", "metadata": {}, "outputs": [], "source": [ "m = learn.model" ] }, { "cell_type": "code", "execution_count": 165, "id": "a95e8301", "metadata": {}, "outputs": [], "source": [ "ps = list(m.parameters())" ] }, { "cell_type": "code", "execution_count": 166, "id": "dba2620d", "metadata": { "scrolled": true }, "outputs": [], "source": [ "ps[1]" ] }, { "cell_type": "code", "execution_count": 167, "id": "eb9bac04", "metadata": {}, "outputs": [], "source": [ "ps[0].shape" ] }, { "cell_type": "code", "execution_count": 168, "id": "9c5971f4", "metadata": {}, "outputs": [], "source": [ "ps[0]" ] }, { "cell_type": "markdown", "id": "3a97f529", "metadata": {}, "source": [ "### export -" ] }, { "cell_type": "code", "execution_count": 169, "id": "0db010b4", "metadata": {}, "outputs": [], "source": [ "from nbdev.export import notebook2script\n", "import os\n", "print(os.listdir())\n", "print('hi')" ] }, { "cell_type": "code", "execution_count": 170, "id": "d7c9f03c", "metadata": {}, "outputs": [], "source": [ "notebook2script('app.ipynb')|" ] }, { "cell_type": "code", "execution_count": null, "id": "cb79a018", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "9a55ccd6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "14ca061f", "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.2" } }, "nbformat": 4, "nbformat_minor": 5 }