{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "5e13806a-fd9c-4685-adc2-4cf72cb36720", "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "from fastai.vision.all import *" ] }, { "cell_type": "code", "execution_count": 3, "id": "accabd51-74ca-4852-b0ef-8b896d4e90d3", "metadata": {}, "outputs": [], "source": [ "def breed_name(x):\n", " return ''.join([char for char in x if not char.isdigit()][:-5])\n", "learn = load_learner('pet_breeds.pkl')" ] }, { "cell_type": "code", "execution_count": 4, "id": "da5e1ef5-bbeb-4a2b-a85e-6ea89acce255", "metadata": {}, "outputs": [], "source": [ "labels = learn.dls.vocab\n", "def predict(img):\n", " img = PILImage.create(img)\n", " pred,pred_idx,probs = learn.predict(img)\n", " return {labels[i]: float(probs[i]) for i in range(len(labels))}" ] }, { "cell_type": "code", "execution_count": 5, "id": "fed109a8-1988-4acf-b59c-4e485bf84eab", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "Running on public URL: https://cfc020d066f0b2c918.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "gr.Interface(fn=predict,\n", " inputs=gr.components.Image(height=512, width=512),\n", " outputs=gr.components.Label(num_top_classes=3),\n", " title='What breed is it ?',\n", " description='A pet breeds classifier',\n", " article=\"

Reference

\",\n", " examples=['keeshond.jpeg', 'maine_coon.jpeg']\n", " ).launch(share=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "5ac9ad24-503e-46ee-a24f-d0c41af63453", "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.9" } }, "nbformat": 4, "nbformat_minor": 5 }