File size: 3,364 Bytes
814ade9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0b5e6d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|default_exp app"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "894a6707",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|export\n",
    "from fastai.vision.all import *\n",
    "import gradio as gr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d9019a15",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|export \n",
    "\n",
    "categories = ('ocean', 'space', 'money')\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": 3,
   "id": "d04d8882",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'learn' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[3], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m im \u001b[38;5;241m=\u001b[39m PILImage\u001b[38;5;241m.\u001b[39mcreate(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mocean.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mclassify_image\u001b[49m\u001b[43m(\u001b[49m\u001b[43mim\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[2], line 6\u001b[0m, in \u001b[0;36mclassify_image\u001b[0;34m(img)\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mclassify_image\u001b[39m(img):\n\u001b[0;32m----> 6\u001b[0m     pred, idx, probs \u001b[38;5;241m=\u001b[39m \u001b[43mlearn\u001b[49m\u001b[38;5;241m.\u001b[39mpredict(img)\n\u001b[1;32m      7\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mdict\u001b[39m(\u001b[38;5;28mzip\u001b[39m(categories, \u001b[38;5;28mmap\u001b[39m(\u001b[38;5;28mfloat\u001b[39m, probs)))\n",
      "\u001b[0;31mNameError\u001b[0m: name 'learn' is not defined"
     ]
    }
   ],
   "source": [
    "im = PILImage.create('ocean.jpg')\n",
    "classify_image(im)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbef2af6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#|export\n",
    "image = gr.inputs.Image(shape=(192,192))\n",
    "label = gr.outputs.Label()\n",
    "examples = ['ocean.jpg', 'space.jpg', 'money.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": null,
   "id": "c0c533db",
   "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.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}