{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "143fb102", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 2, "id": "d5bcdf28", "metadata": {}, "outputs": [], "source": [ "#|export\n", "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": 3, "id": "6b2565d5", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/florian/miniconda3/envs/env_transformers/lib/python3.11/site-packages/fastai/learner.py:455: UserWarning: load_learner` uses Python's insecure pickle module, which can execute malicious arbitrary code when loading. Only load files you trust.\n", "If you only need to load model weights and optimizer state, use the safe `Learner.load` instead.\n", " warn(\"load_learner` uses Python's insecure pickle module, which can execute malicious arbitrary code when loading. Only load files you trust.\\nIf you only need to load model weights and optimizer state, use the safe `Learner.load` instead.\")\n" ] } ], "source": [ "#|export\n", "learn = load_learner('modelchampi.pkl')" ] }, { "cell_type": "code", "execution_count": 4, "id": "e24a470c", "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = ('amanite','bolet', 'morille', 'truffe')\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": 5, "id": "ff8bb0b5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "{'amanite': 0.9999909400939941,\n", " 'bolet': 4.352936571194732e-07,\n", " 'morille': 3.9385918171319645e-06,\n", " 'truffe': 4.598049144988181e-06}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "classify_image('amanite.jpg')" ] }, { "cell_type": "code", "execution_count": 6, "id": "a5b4b573", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7860\n", "* To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/plain": [] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "image = gr.Image(height=192, width=192)\n", "label = gr.Label()\n", "examples = ['amanite.jpg', 'truffe.jpg']\n", "\n", "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n", "intf.launch(inline=False)" ] } ], "metadata": { "kernelspec": { "display_name": "env_transformers", "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.11" } }, "nbformat": 4, "nbformat_minor": 5 }