Brian Sigafoos commited on
Commit
b8dfde0
1 Parent(s): 835040b

Add gradio interface

Browse files
Files changed (2) hide show
  1. app.ipynb +71 -13
  2. app.py +16 -2
app.ipynb CHANGED
@@ -22,9 +22,9 @@
22
  "metadata": {},
23
  "outputs": [],
24
  "source": [
25
- "#|export\n",
26
  "from fastai.vision.all import *\n",
27
- "import gradio as gr"
28
  ]
29
  },
30
  {
@@ -33,8 +33,8 @@
33
  "metadata": {},
34
  "outputs": [],
35
  "source": [
36
- "#|export\n",
37
- "learn = load_learner('model.pkl')"
38
  ]
39
  },
40
  {
@@ -56,8 +56,8 @@
56
  ],
57
  "source": [
58
  "im = PILImage.create('images/ash.jpg')\n",
59
- "im.thumbnail((224,224))\n",
60
- "im"
61
  ]
62
  },
63
  {
@@ -122,7 +122,7 @@
122
  }
123
  ],
124
  "source": [
125
- "%time learn.predict(im)"
126
  ]
127
  },
128
  {
@@ -142,7 +142,7 @@
142
  }
143
  ],
144
  "source": [
145
- "learn.dls.vocab"
146
  ]
147
  },
148
  {
@@ -151,14 +151,15 @@
151
  "metadata": {},
152
  "outputs": [],
153
  "source": [
154
- "#|export\n",
155
- "# NOTE: Put in alphabetical order\n",
156
  "categories = ('ash', 'chestnut', 'ginkgo biloba', 'silver maple', 'willow oak')\n",
157
  "\n",
 
158
  "def classify_image(img):\n",
159
  " pred, idx, probs = learn.predict(img)\n",
160
  " # Change each probability to a float, since Gradio doesn't support Tensors or NumPy\n",
161
- " return dict(zip(categories, map(float, probs)))"
162
  ]
163
  },
164
  {
@@ -222,6 +223,63 @@
222
  "classify_image(im)"
223
  ]
224
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
  {
226
  "cell_type": "markdown",
227
  "metadata": {},
@@ -231,7 +289,7 @@
231
  },
232
  {
233
  "cell_type": "code",
234
- "execution_count": 43,
235
  "metadata": {},
236
  "outputs": [
237
  {
@@ -246,7 +304,7 @@
246
  "from nbdev.export import nb_export\n",
247
  "\n",
248
  "nb_export('app.ipynb', './')\n",
249
- "print('Export successful')"
250
  ]
251
  },
252
  {
 
22
  "metadata": {},
23
  "outputs": [],
24
  "source": [
25
+ "# |export\n",
26
  "from fastai.vision.all import *\n",
27
+ "import gradio as gr\n"
28
  ]
29
  },
30
  {
 
33
  "metadata": {},
34
  "outputs": [],
35
  "source": [
36
+ "# |export\n",
37
+ "learn = load_learner('model.pkl')\n"
38
  ]
39
  },
40
  {
 
56
  ],
57
  "source": [
58
  "im = PILImage.create('images/ash.jpg')\n",
59
+ "im.thumbnail((224, 224))\n",
60
+ "im\n"
61
  ]
62
  },
63
  {
 
122
  }
123
  ],
124
  "source": [
125
+ "%time learn.predict(im)\n"
126
  ]
127
  },
128
  {
 
142
  }
143
  ],
144
  "source": [
145
+ "learn.dls.vocab\n"
146
  ]
147
  },
148
  {
 
151
  "metadata": {},
152
  "outputs": [],
153
  "source": [
154
+ "# |export\n",
155
+ "\n",
156
  "categories = ('ash', 'chestnut', 'ginkgo biloba', 'silver maple', 'willow oak')\n",
157
  "\n",
158
+ "\n",
159
  "def classify_image(img):\n",
160
  " pred, idx, probs = learn.predict(img)\n",
161
  " # Change each probability to a float, since Gradio doesn't support Tensors or NumPy\n",
162
+ " return dict(zip(categories, map(float, probs)))\n"
163
  ]
164
  },
165
  {
 
223
  "classify_image(im)"
224
  ]
225
  },
226
+ {
227
+ "attachments": {},
228
+ "cell_type": "markdown",
229
+ "metadata": {},
230
+ "source": [
231
+ "# gradio interface"
232
+ ]
233
+ },
234
+ {
235
+ "cell_type": "code",
236
+ "execution_count": 46,
237
+ "metadata": {},
238
+ "outputs": [
239
+ {
240
+ "name": "stderr",
241
+ "output_type": "stream",
242
+ "text": [
243
+ "/Users/briansigafoos/mambaforge/lib/python3.10/site-packages/gradio/inputs.py:256: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
244
+ " warnings.warn(\n",
245
+ "/Users/briansigafoos/mambaforge/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
246
+ " warnings.warn(value)\n",
247
+ "/Users/briansigafoos/mambaforge/lib/python3.10/site-packages/gradio/outputs.py:196: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n",
248
+ " warnings.warn(\n",
249
+ "/Users/briansigafoos/mambaforge/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",
250
+ " warnings.warn(value)\n"
251
+ ]
252
+ },
253
+ {
254
+ "name": "stdout",
255
+ "output_type": "stream",
256
+ "text": [
257
+ "Running on local URL: http://127.0.0.1:7860\n",
258
+ "\n",
259
+ "To create a public link, set `share=True` in `launch()`.\n"
260
+ ]
261
+ },
262
+ {
263
+ "data": {
264
+ "text/plain": []
265
+ },
266
+ "execution_count": 46,
267
+ "metadata": {},
268
+ "output_type": "execute_result"
269
+ }
270
+ ],
271
+ "source": [
272
+ "# |export\n",
273
+ "image = gr.inputs.Image(shape=(192, 192))\n",
274
+ "label = gr.outputs.Label()\n",
275
+ "examples = ['images/ash.jpg', 'images/chestnut.jpg', 'images/ginkgo_biloba.jpg',\n",
276
+ " 'images/silver_maple.jpg', 'images/willow_oak.jpg']\n",
277
+ "\n",
278
+ "interface = gr.Interface(fn=classify_image, inputs=image,\n",
279
+ " outputs=label, examples=examples)\n",
280
+ "interface.launch(inline=False)\n"
281
+ ]
282
+ },
283
  {
284
  "cell_type": "markdown",
285
  "metadata": {},
 
289
  },
290
  {
291
  "cell_type": "code",
292
+ "execution_count": 47,
293
  "metadata": {},
294
  "outputs": [
295
  {
 
304
  "from nbdev.export import nb_export\n",
305
  "\n",
306
  "nb_export('app.ipynb', './')\n",
307
+ "print('Export successful')\n"
308
  ]
309
  },
310
  {
app.py CHANGED
@@ -1,20 +1,34 @@
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
2
 
3
  # %% auto 0
4
- __all__ = ['learn', 'categories', 'classify_image']
5
 
6
  # %% app.ipynb 2
7
  from fastai.vision.all import *
8
  import gradio as gr
9
 
 
10
  # %% app.ipynb 3
11
  learn = load_learner('model.pkl')
12
 
 
13
  # %% app.ipynb 7
14
- # NOTE: Put in alphabetical order
15
  categories = ('ash', 'chestnut', 'ginkgo biloba', 'silver maple', 'willow oak')
16
 
 
17
  def classify_image(img):
18
  pred, idx, probs = learn.predict(img)
19
  # Change each probability to a float, since Gradio doesn't support Tensors or NumPy
20
  return dict(zip(categories, map(float, probs)))
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
2
 
3
  # %% auto 0
4
+ __all__ = ['learn', 'categories', 'image', 'label', 'examples', 'interface', 'classify_image']
5
 
6
  # %% app.ipynb 2
7
  from fastai.vision.all import *
8
  import gradio as gr
9
 
10
+
11
  # %% app.ipynb 3
12
  learn = load_learner('model.pkl')
13
 
14
+
15
  # %% app.ipynb 7
 
16
  categories = ('ash', 'chestnut', 'ginkgo biloba', 'silver maple', 'willow oak')
17
 
18
+
19
  def classify_image(img):
20
  pred, idx, probs = learn.predict(img)
21
  # Change each probability to a float, since Gradio doesn't support Tensors or NumPy
22
  return dict(zip(categories, map(float, probs)))
23
+
24
+
25
+ # %% app.ipynb 10
26
+ image = gr.inputs.Image(shape=(192, 192))
27
+ label = gr.outputs.Label()
28
+ examples = ['images/ash.jpg', 'images/chestnut.jpg', 'images/ginkgo_biloba.jpg',
29
+ 'images/silver_maple.jpg', 'images/willow_oak.jpg']
30
+
31
+ interface = gr.Interface(fn=classify_image, inputs=image,
32
+ outputs=label, examples=examples)
33
+ interface.launch(inline=False)
34
+