torch360/app
0
1{2 "cells": [3 {4 "cell_type": "markdown",5 "id": "d71d474e",6 "metadata": {},7 "source": [8 "## Car Classifier\n",9 "Choose a PNG file of a Lamborghini, Mclaren, Pagani, Ferrari or a Koenigsegg to classify."10 ]11 },12 {13 "cell_type": "code",14 "execution_count": 1,15 "id": "4a6153d5-f58d-4e26-ad26-1d3af0c25633",16 "metadata": {},17 "outputs": [],18 "source": [19 "from fastai.vision.all import *\n",20 "from fastai.vision.widgets import *\n",21 "import gradio as gr"22 ]23 },24 {25 "cell_type": "code",26 "execution_count": 2,27 "id": "09eaf79b",28 "metadata": {},29 "outputs": [],30 "source": [31 "learn = load_learner('export.pkl')"32 ]33 },34 {35 "cell_type": "code",36 "execution_count": 3,37 "id": "24b9e084",38 "metadata": {},39 "outputs": [],40 "source": [41 "import warnings\n",42 "warnings.filterwarnings(\"ignore\")"43 ]44 },45 {46 "cell_type": "code",47 "execution_count": 4,48 "id": "2c2b5f96-9b5d-49ab-8ea6-01e4b745630b",49 "metadata": {},50 "outputs": [],51 "source": [52 "car_types = ('Ferrari','Koenigsegg', 'Lamborghini', 'Mclaren', 'Pagani')"53 ]54 },55 {56 "cell_type": "code",57 "execution_count": 5,58 "id": "5556a071-667c-4dca-9567-e85f1dcdfe6a",59 "metadata": {},60 "outputs": [],61 "source": [62 "def image_classifier(im):\n",63 " pred = learn.predict(im)\n",64 " print(pred)\n",65 " return {car_types[i]: pred[2][i].item() for i in range(0,5)}"66 ]67 },68 {69 "cell_type": "code",70 "execution_count": 6,71 "id": "b0aa9c4c-fe7d-43a0-ae41-22f7def186e0",72 "metadata": {},73 "outputs": [],74 "source": [75 "iface = gr.Interface(\n",76 " image_classifier,\n",77 " gr.inputs.Image(shape=(224, 224)),\n",78 " gr.outputs.Label(num_top_classes=5),\n",79 " interpretation=\"default\")"80 ]81 },82 {83 "cell_type": "code",84 "execution_count": 7,85 "id": "1d1a3e2b-c8f1-407a-b0ce-57d07b566bde",86 "metadata": {87 "scrolled": true88 },89 "outputs": [90 {91 "name": "stdout",92 "output_type": "stream",93 "text": [94 "Running on local URL: http://127.0.0.1:7860/\n",95 "Running on public URL: https://49110.gradio.app\n",96 "\n",97 "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"98 ]99 },100 {101 "data": {102 "text/html": [103 "<div><iframe src=\"https://49110.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"104 ],105 "text/plain": [106 "<IPython.core.display.HTML object>"107 ]108 },109 "metadata": {},110 "output_type": "display_data"111 },112 {113 "data": {114 "text/plain": [115 "(<gradio.routes.App at 0x28a1360e0>,\n",116 " 'http://127.0.0.1:7860/',\n",117 " 'https://49110.gradio.app')"118 ]119 },120 "execution_count": 7,121 "metadata": {},122 "output_type": "execute_result"123 }124 ],125 "source": [126 "iface.launch(share=True)"127 ]128 }129 ],130 "metadata": {131 "kernelspec": {132 "display_name": "Python 3 (ipykernel)",133 "language": "python",134 "name": "python3"135 },136 "language_info": {137 "codemirror_mode": {138 "name": "ipython",139 "version": 3140 },141 "file_extension": ".py",142 "mimetype": "text/x-python",143 "name": "python",144 "nbconvert_exporter": "python",145 "pygments_lexer": "ipython3",146 "version": "3.10.1"147 }148 },149 "nbformat": 4,150 "nbformat_minor": 5151}152 