{ "cells": [ { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *\n", "import gradio as gr\n", "\n", "def is_man(x): return x[0].isupper()" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [], "source": [ "girl1 = PILImage.create('/Users/francostratta/Desktop/learning/girl1.jpeg')\n", "girl2 = PILImage.create('/Users/francostratta/Desktop/learning/girl2.jpg')\n", "girl3 = PILImage.create('/Users/francostratta/Desktop/learning/girl3.jpg')\n", "\n", "man1 = PILImage.create('/Users/francostratta/Desktop/learning/man1.jpeg')\n", "man2 = PILImage.create('/Users/francostratta/Desktop/learning/man2.jpg')\n", "man3 = PILImage.create('/Users/francostratta/Desktop/learning/man3.jpg')" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('sex.pkl')" ] }, { "cell_type": "code", "execution_count": 96, "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": [ "('women', tensor(1), tensor([6.9461e-05, 9.9993e-01]))" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.predict(girl1)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "#|export\n", "categories = (\"man\", \"girl\")\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": 98, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "girl1: {'man': 6.946067151147872e-05, 'girl': 0.9999305009841919}\n" ] }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "man1: {'man': 0.9999998807907104, 'girl': 1.692434494771078e-07}\n" ] } ], "source": [ "print('girl1: ',classify_image(girl1))\n", "print('man1: ',classify_image(man1))" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7869\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#|export\n", "Image = gr.Image()\n", "label = gr.Label()\n", "examples = ['girl1.jpeg', 'man1.jpeg']\n", "\n", "interface = gr.Interface(\n", " fn=classify_image,\n", " inputs=Image,\n", " outputs=label,\n", " examples=examples)\n", "\n", "interface.launch()" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "!nbdev_migrate " ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "import nbdev\n", "nbdev.export.nb_export('app.ipynb', '.')\n" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.8" } }, "nbformat": 4, "nbformat_minor": 2 }