Ajit Singh commited on
Commit
44c4e71
1 Parent(s): e6aa4d5

Add gradio interface

Browse files
.gitattributes CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ *.jpg filter=lfs diff=lfs merge=lfs -text
app.ipynb ADDED
@@ -0,0 +1,750 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 4,
6
+ "id": "2ccea5e4",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "#|default_exp app\n"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "id": "1d47b7c1",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "#|export\n",
21
+ "# Make sure we've got the latest version of fastai:\n",
22
+ "!pip install -Uqq fastai gradio"
23
+ ]
24
+ },
25
+ {
26
+ "cell_type": "code",
27
+ "execution_count": 12,
28
+ "id": "553b0061",
29
+ "metadata": {},
30
+ "outputs": [],
31
+ "source": [
32
+ "#|export\n",
33
+ "!pip install -Uqq fastbook\n",
34
+ "import fastbook\n",
35
+ "fastbook.setup_book()"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": 32,
41
+ "id": "efc0850a",
42
+ "metadata": {},
43
+ "outputs": [],
44
+ "source": [
45
+ "#|export\n",
46
+ "from fastbook import *\n",
47
+ "from fastai.vision.widgets import *\n",
48
+ "from fastai.vision.all import *\n",
49
+ "from urllib.request import urlopen"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "markdown",
54
+ "id": "c73e94bb",
55
+ "metadata": {},
56
+ "source": [
57
+ "# Dogs vs Cats"
58
+ ]
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "execution_count": 5,
63
+ "id": "15421cd2",
64
+ "metadata": {
65
+ "scrolled": true
66
+ },
67
+ "outputs": [],
68
+ "source": [
69
+ "#|export\n",
70
+ "import gradio as gr\n",
71
+ "\n",
72
+ "def is_cat(x): return x[0].issuper()"
73
+ ]
74
+ },
75
+ {
76
+ "cell_type": "code",
77
+ "execution_count": 64,
78
+ "id": "f8743ab2",
79
+ "metadata": {},
80
+ "outputs": [],
81
+ "source": [
82
+ "ims = search_images_ddg('dog', max_images=3)\n",
83
+ "dest = 'images/'\n",
84
+ "download_images(dest, urls=ims)"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "code",
89
+ "execution_count": 38,
90
+ "id": "91067f42",
91
+ "metadata": {},
92
+ "outputs": [
93
+ {
94
+ "data": {
95
+ "text/plain": [
96
+ "(#9) [Path('images/3cf5e7be-7aa0-4f7d-b585-ab7bd97d6d95.jpg'),Path('images/4d3b88ed-d0dc-43ec-bbaf-6babf6f310a7.jpg'),Path('images/5c283719-addd-4dbf-9847-eda74d0af8e7.jpg'),Path('images/6c9ec27e-0dbf-4e92-8059-12ec91bfa37b.jpg'),Path('images/a8ceb639-1fbf-447f-856e-7cf9bc796475.jpg'),Path('images/cc15e76a-5cd9-471e-9620-ae608e1d6be6.jpg'),Path('images/d53d92d8-7958-4fb6-ba9d-5f1e27fe9ceb.jpg'),Path('images/dog.jpg'),Path('images/edca2fd3-9ef6-40a5-a091-446b1abdfb55.jpg')]"
97
+ ]
98
+ },
99
+ "execution_count": 38,
100
+ "metadata": {},
101
+ "output_type": "execute_result"
102
+ }
103
+ ],
104
+ "source": [
105
+ "fns = get_image_files(dest)\n",
106
+ "fns"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": 39,
112
+ "id": "ba9916e9",
113
+ "metadata": {},
114
+ "outputs": [
115
+ {
116
+ "data": {
117
+ "text/plain": [
118
+ "(#0) []"
119
+ ]
120
+ },
121
+ "execution_count": 39,
122
+ "metadata": {},
123
+ "output_type": "execute_result"
124
+ }
125
+ ],
126
+ "source": [
127
+ "failed = verify_images(fns)\n",
128
+ "failed"
129
+ ]
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "execution_count": 40,
134
+ "id": "cf2964df",
135
+ "metadata": {},
136
+ "outputs": [],
137
+ "source": [
138
+ "failed.map(Path.unlink);"
139
+ ]
140
+ },
141
+ {
142
+ "cell_type": "markdown",
143
+ "id": "a5deae21",
144
+ "metadata": {},
145
+ "source": [
146
+ "# Load Learner"
147
+ ]
148
+ },
149
+ {
150
+ "cell_type": "code",
151
+ "execution_count": 44,
152
+ "id": "13b84738",
153
+ "metadata": {},
154
+ "outputs": [],
155
+ "source": [
156
+ "#|export\n",
157
+ "learn = load_learner('model.pkl')"
158
+ ]
159
+ },
160
+ {
161
+ "cell_type": "code",
162
+ "execution_count": 48,
163
+ "id": "ad82eadc",
164
+ "metadata": {},
165
+ "outputs": [
166
+ {
167
+ "data": {
168
+ "text/html": [
169
+ "\n",
170
+ "<style>\n",
171
+ " /* Turns off some styling */\n",
172
+ " progress {\n",
173
+ " /* gets rid of default border in Firefox and Opera. */\n",
174
+ " border: none;\n",
175
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
176
+ " background-size: auto;\n",
177
+ " }\n",
178
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
179
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
180
+ " }\n",
181
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
182
+ " background: #F44336;\n",
183
+ " }\n",
184
+ "</style>\n"
185
+ ],
186
+ "text/plain": [
187
+ "<IPython.core.display.HTML object>"
188
+ ]
189
+ },
190
+ "metadata": {},
191
+ "output_type": "display_data"
192
+ },
193
+ {
194
+ "data": {
195
+ "text/html": [],
196
+ "text/plain": [
197
+ "<IPython.core.display.HTML object>"
198
+ ]
199
+ },
200
+ "metadata": {},
201
+ "output_type": "display_data"
202
+ },
203
+ {
204
+ "name": "stdout",
205
+ "output_type": "stream",
206
+ "text": [
207
+ "('False', tensor(0), tensor([9.9998e-01, 1.5045e-05]))\n"
208
+ ]
209
+ },
210
+ {
211
+ "data": {
212
+ "text/html": [
213
+ "\n",
214
+ "<style>\n",
215
+ " /* Turns off some styling */\n",
216
+ " progress {\n",
217
+ " /* gets rid of default border in Firefox and Opera. */\n",
218
+ " border: none;\n",
219
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
220
+ " background-size: auto;\n",
221
+ " }\n",
222
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
223
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
224
+ " }\n",
225
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
226
+ " background: #F44336;\n",
227
+ " }\n",
228
+ "</style>\n"
229
+ ],
230
+ "text/plain": [
231
+ "<IPython.core.display.HTML object>"
232
+ ]
233
+ },
234
+ "metadata": {},
235
+ "output_type": "display_data"
236
+ },
237
+ {
238
+ "data": {
239
+ "text/html": [],
240
+ "text/plain": [
241
+ "<IPython.core.display.HTML object>"
242
+ ]
243
+ },
244
+ "metadata": {},
245
+ "output_type": "display_data"
246
+ },
247
+ {
248
+ "name": "stdout",
249
+ "output_type": "stream",
250
+ "text": [
251
+ "('False', tensor(0), tensor([1.0000e+00, 2.4750e-06]))\n"
252
+ ]
253
+ },
254
+ {
255
+ "data": {
256
+ "text/html": [
257
+ "\n",
258
+ "<style>\n",
259
+ " /* Turns off some styling */\n",
260
+ " progress {\n",
261
+ " /* gets rid of default border in Firefox and Opera. */\n",
262
+ " border: none;\n",
263
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
264
+ " background-size: auto;\n",
265
+ " }\n",
266
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
267
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
268
+ " }\n",
269
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
270
+ " background: #F44336;\n",
271
+ " }\n",
272
+ "</style>\n"
273
+ ],
274
+ "text/plain": [
275
+ "<IPython.core.display.HTML object>"
276
+ ]
277
+ },
278
+ "metadata": {},
279
+ "output_type": "display_data"
280
+ },
281
+ {
282
+ "data": {
283
+ "text/html": [],
284
+ "text/plain": [
285
+ "<IPython.core.display.HTML object>"
286
+ ]
287
+ },
288
+ "metadata": {},
289
+ "output_type": "display_data"
290
+ },
291
+ {
292
+ "name": "stdout",
293
+ "output_type": "stream",
294
+ "text": [
295
+ "('False', tensor(0), tensor([1.0000e+00, 2.2275e-06]))\n"
296
+ ]
297
+ },
298
+ {
299
+ "data": {
300
+ "text/html": [
301
+ "\n",
302
+ "<style>\n",
303
+ " /* Turns off some styling */\n",
304
+ " progress {\n",
305
+ " /* gets rid of default border in Firefox and Opera. */\n",
306
+ " border: none;\n",
307
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
308
+ " background-size: auto;\n",
309
+ " }\n",
310
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
311
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
312
+ " }\n",
313
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
314
+ " background: #F44336;\n",
315
+ " }\n",
316
+ "</style>\n"
317
+ ],
318
+ "text/plain": [
319
+ "<IPython.core.display.HTML object>"
320
+ ]
321
+ },
322
+ "metadata": {},
323
+ "output_type": "display_data"
324
+ },
325
+ {
326
+ "data": {
327
+ "text/html": [],
328
+ "text/plain": [
329
+ "<IPython.core.display.HTML object>"
330
+ ]
331
+ },
332
+ "metadata": {},
333
+ "output_type": "display_data"
334
+ },
335
+ {
336
+ "name": "stdout",
337
+ "output_type": "stream",
338
+ "text": [
339
+ "('False', tensor(0), tensor([1.0000e+00, 1.0369e-07]))\n"
340
+ ]
341
+ },
342
+ {
343
+ "data": {
344
+ "text/html": [
345
+ "\n",
346
+ "<style>\n",
347
+ " /* Turns off some styling */\n",
348
+ " progress {\n",
349
+ " /* gets rid of default border in Firefox and Opera. */\n",
350
+ " border: none;\n",
351
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
352
+ " background-size: auto;\n",
353
+ " }\n",
354
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
355
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
356
+ " }\n",
357
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
358
+ " background: #F44336;\n",
359
+ " }\n",
360
+ "</style>\n"
361
+ ],
362
+ "text/plain": [
363
+ "<IPython.core.display.HTML object>"
364
+ ]
365
+ },
366
+ "metadata": {},
367
+ "output_type": "display_data"
368
+ },
369
+ {
370
+ "data": {
371
+ "text/html": [],
372
+ "text/plain": [
373
+ "<IPython.core.display.HTML object>"
374
+ ]
375
+ },
376
+ "metadata": {},
377
+ "output_type": "display_data"
378
+ },
379
+ {
380
+ "name": "stdout",
381
+ "output_type": "stream",
382
+ "text": [
383
+ "('False', tensor(0), tensor([9.9997e-01, 2.7372e-05]))\n"
384
+ ]
385
+ },
386
+ {
387
+ "data": {
388
+ "text/html": [
389
+ "\n",
390
+ "<style>\n",
391
+ " /* Turns off some styling */\n",
392
+ " progress {\n",
393
+ " /* gets rid of default border in Firefox and Opera. */\n",
394
+ " border: none;\n",
395
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
396
+ " background-size: auto;\n",
397
+ " }\n",
398
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
399
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
400
+ " }\n",
401
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
402
+ " background: #F44336;\n",
403
+ " }\n",
404
+ "</style>\n"
405
+ ],
406
+ "text/plain": [
407
+ "<IPython.core.display.HTML object>"
408
+ ]
409
+ },
410
+ "metadata": {},
411
+ "output_type": "display_data"
412
+ },
413
+ {
414
+ "data": {
415
+ "text/html": [],
416
+ "text/plain": [
417
+ "<IPython.core.display.HTML object>"
418
+ ]
419
+ },
420
+ "metadata": {},
421
+ "output_type": "display_data"
422
+ },
423
+ {
424
+ "name": "stdout",
425
+ "output_type": "stream",
426
+ "text": [
427
+ "('False', tensor(0), tensor([0.9984, 0.0016]))\n"
428
+ ]
429
+ },
430
+ {
431
+ "data": {
432
+ "text/html": [
433
+ "\n",
434
+ "<style>\n",
435
+ " /* Turns off some styling */\n",
436
+ " progress {\n",
437
+ " /* gets rid of default border in Firefox and Opera. */\n",
438
+ " border: none;\n",
439
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
440
+ " background-size: auto;\n",
441
+ " }\n",
442
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
443
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
444
+ " }\n",
445
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
446
+ " background: #F44336;\n",
447
+ " }\n",
448
+ "</style>\n"
449
+ ],
450
+ "text/plain": [
451
+ "<IPython.core.display.HTML object>"
452
+ ]
453
+ },
454
+ "metadata": {},
455
+ "output_type": "display_data"
456
+ },
457
+ {
458
+ "data": {
459
+ "text/html": [],
460
+ "text/plain": [
461
+ "<IPython.core.display.HTML object>"
462
+ ]
463
+ },
464
+ "metadata": {},
465
+ "output_type": "display_data"
466
+ },
467
+ {
468
+ "name": "stdout",
469
+ "output_type": "stream",
470
+ "text": [
471
+ "('False', tensor(0), tensor([9.9941e-01, 5.8600e-04]))\n"
472
+ ]
473
+ },
474
+ {
475
+ "data": {
476
+ "text/html": [
477
+ "\n",
478
+ "<style>\n",
479
+ " /* Turns off some styling */\n",
480
+ " progress {\n",
481
+ " /* gets rid of default border in Firefox and Opera. */\n",
482
+ " border: none;\n",
483
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
484
+ " background-size: auto;\n",
485
+ " }\n",
486
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
487
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
488
+ " }\n",
489
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
490
+ " background: #F44336;\n",
491
+ " }\n",
492
+ "</style>\n"
493
+ ],
494
+ "text/plain": [
495
+ "<IPython.core.display.HTML object>"
496
+ ]
497
+ },
498
+ "metadata": {},
499
+ "output_type": "display_data"
500
+ },
501
+ {
502
+ "data": {
503
+ "text/html": [],
504
+ "text/plain": [
505
+ "<IPython.core.display.HTML object>"
506
+ ]
507
+ },
508
+ "metadata": {},
509
+ "output_type": "display_data"
510
+ },
511
+ {
512
+ "name": "stdout",
513
+ "output_type": "stream",
514
+ "text": [
515
+ "('False', tensor(0), tensor([9.9920e-01, 7.9651e-04]))\n"
516
+ ]
517
+ },
518
+ {
519
+ "data": {
520
+ "text/html": [
521
+ "\n",
522
+ "<style>\n",
523
+ " /* Turns off some styling */\n",
524
+ " progress {\n",
525
+ " /* gets rid of default border in Firefox and Opera. */\n",
526
+ " border: none;\n",
527
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
528
+ " background-size: auto;\n",
529
+ " }\n",
530
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
531
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
532
+ " }\n",
533
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
534
+ " background: #F44336;\n",
535
+ " }\n",
536
+ "</style>\n"
537
+ ],
538
+ "text/plain": [
539
+ "<IPython.core.display.HTML object>"
540
+ ]
541
+ },
542
+ "metadata": {},
543
+ "output_type": "display_data"
544
+ },
545
+ {
546
+ "data": {
547
+ "text/html": [],
548
+ "text/plain": [
549
+ "<IPython.core.display.HTML object>"
550
+ ]
551
+ },
552
+ "metadata": {},
553
+ "output_type": "display_data"
554
+ },
555
+ {
556
+ "name": "stdout",
557
+ "output_type": "stream",
558
+ "text": [
559
+ "('False', tensor(0), tensor([9.9999e-01, 9.5192e-06]))\n"
560
+ ]
561
+ }
562
+ ],
563
+ "source": [
564
+ "for path in fns:\n",
565
+ " im = PILImage.create(path)\n",
566
+ " im.thumbnail((192,192))\n",
567
+ " print(learn.predict(im)) "
568
+ ]
569
+ },
570
+ {
571
+ "cell_type": "code",
572
+ "execution_count": 49,
573
+ "id": "011cccba",
574
+ "metadata": {},
575
+ "outputs": [],
576
+ "source": [
577
+ "categories = ('Dog', 'Cat')\n",
578
+ "def classify_image(img):\n",
579
+ " pref,idx,probs = learn.predict(img)\n",
580
+ " return dict(zip(categories, map(float, probs)))"
581
+ ]
582
+ },
583
+ {
584
+ "cell_type": "code",
585
+ "execution_count": 50,
586
+ "id": "16881a1b",
587
+ "metadata": {},
588
+ "outputs": [
589
+ {
590
+ "data": {
591
+ "text/html": [
592
+ "\n",
593
+ "<style>\n",
594
+ " /* Turns off some styling */\n",
595
+ " progress {\n",
596
+ " /* gets rid of default border in Firefox and Opera. */\n",
597
+ " border: none;\n",
598
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
599
+ " background-size: auto;\n",
600
+ " }\n",
601
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
602
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
603
+ " }\n",
604
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
605
+ " background: #F44336;\n",
606
+ " }\n",
607
+ "</style>\n"
608
+ ],
609
+ "text/plain": [
610
+ "<IPython.core.display.HTML object>"
611
+ ]
612
+ },
613
+ "metadata": {},
614
+ "output_type": "display_data"
615
+ },
616
+ {
617
+ "data": {
618
+ "text/html": [],
619
+ "text/plain": [
620
+ "<IPython.core.display.HTML object>"
621
+ ]
622
+ },
623
+ "metadata": {},
624
+ "output_type": "display_data"
625
+ },
626
+ {
627
+ "data": {
628
+ "text/plain": [
629
+ "{'Dog': 0.999957799911499, 'Cat': 4.219008405925706e-05}"
630
+ ]
631
+ },
632
+ "execution_count": 50,
633
+ "metadata": {},
634
+ "output_type": "execute_result"
635
+ }
636
+ ],
637
+ "source": [
638
+ "base_image = PILImage.create(fns[0])\n",
639
+ "classify_image(base_image)"
640
+ ]
641
+ },
642
+ {
643
+ "cell_type": "markdown",
644
+ "id": "4c11585c",
645
+ "metadata": {},
646
+ "source": [
647
+ "# Gradio"
648
+ ]
649
+ },
650
+ {
651
+ "cell_type": "code",
652
+ "execution_count": 74,
653
+ "id": "69406102",
654
+ "metadata": {},
655
+ "outputs": [
656
+ {
657
+ "name": "stderr",
658
+ "output_type": "stream",
659
+ "text": [
660
+ "/media/data/programming/ml/anaconda3/lib/python3.9/site-packages/gradio/inputs.py:257: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n",
661
+ " warnings.warn(\n",
662
+ "/media/data/programming/ml/anaconda3/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
663
+ " warnings.warn(value)\n",
664
+ "/media/data/programming/ml/anaconda3/lib/python3.9/site-packages/gradio/outputs.py:197: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n",
665
+ " warnings.warn(\n",
666
+ "/media/data/programming/ml/anaconda3/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",
667
+ " warnings.warn(value)\n"
668
+ ]
669
+ },
670
+ {
671
+ "name": "stdout",
672
+ "output_type": "stream",
673
+ "text": [
674
+ "Running on local URL: http://127.0.0.1:7861\n",
675
+ "\n",
676
+ "To create a public link, set `share=True` in `launch()`.\n"
677
+ ]
678
+ },
679
+ {
680
+ "data": {
681
+ "text/plain": []
682
+ },
683
+ "execution_count": 74,
684
+ "metadata": {},
685
+ "output_type": "execute_result"
686
+ }
687
+ ],
688
+ "source": [
689
+ "#|export\n",
690
+ "image = gr.inputs.Image(shape=(192,192))\n",
691
+ "label = gr.outputs.Label()\n",
692
+ "examples = ['images/4d3b88ed-d0dc-43ec-bbaf-6babf6f310a7.jpg',\n",
693
+ " 'images/5c283719-addd-4dbf-9847-eda74d0af8e7.jpg']\n",
694
+ "\n",
695
+ "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n",
696
+ "intf.launch(inline=False)"
697
+ ]
698
+ },
699
+ {
700
+ "cell_type": "markdown",
701
+ "id": "38ae565f",
702
+ "metadata": {},
703
+ "source": [
704
+ "# Exporting"
705
+ ]
706
+ },
707
+ {
708
+ "cell_type": "code",
709
+ "execution_count": 75,
710
+ "id": "ef61159a",
711
+ "metadata": {},
712
+ "outputs": [],
713
+ "source": [
714
+ "!pip install -Uqq nbdev\n",
715
+ "import nbdev"
716
+ ]
717
+ },
718
+ {
719
+ "cell_type": "code",
720
+ "execution_count": 76,
721
+ "id": "5e35eaab",
722
+ "metadata": {},
723
+ "outputs": [],
724
+ "source": [
725
+ "nbdev.export.nb_export('app.ipynb', '.')"
726
+ ]
727
+ }
728
+ ],
729
+ "metadata": {
730
+ "kernelspec": {
731
+ "display_name": "Python 3 (ipykernel)",
732
+ "language": "python",
733
+ "name": "python3"
734
+ },
735
+ "language_info": {
736
+ "codemirror_mode": {
737
+ "name": "ipython",
738
+ "version": 3
739
+ },
740
+ "file_extension": ".py",
741
+ "mimetype": "text/x-python",
742
+ "name": "python",
743
+ "nbconvert_exporter": "python",
744
+ "pygments_lexer": "ipython3",
745
+ "version": "3.9.13"
746
+ }
747
+ },
748
+ "nbformat": 4,
749
+ "nbformat_minor": 5
750
+ }
app.py CHANGED
@@ -1,7 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['learn', 'image', 'label', 'examples', 'intf', 'is_cat']
5
+
6
+ # %% app.ipynb 1
7
+ # Make sure we've got the latest version of fastai:
8
+ !pip install -Uqq fastai gradio
9
+
10
+ # %% app.ipynb 2
11
+ !pip install -Uqq fastbook
12
+ import fastbook
13
+ fastbook.setup_book()
14
+
15
+ # %% app.ipynb 3
16
+ from fastbook import *
17
+ from fastai.vision.widgets import *
18
+ from fastai.vision.all import *
19
+ from urllib.request import urlopen
20
+
21
+ # %% app.ipynb 5
22
  import gradio as gr
23
 
24
+ def is_cat(x): return x[0].issuper()
25
+
26
+ # %% app.ipynb 11
27
+ learn = load_learner('model.pkl')
28
+
29
+ # %% app.ipynb 16
30
+ image = gr.inputs.Image(shape=(192,192))
31
+ label = gr.outputs.Label()
32
+ examples = ['images/4d3b88ed-d0dc-43ec-bbaf-6babf6f310a7.jpg',
33
+ 'images/5c283719-addd-4dbf-9847-eda74d0af8e7.jpg']
34
 
35
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
36
+ intf.launch(inline=False)
images/130c33cc-1251-41d6-87bb-b53ffd4d4a39.jpg ADDED

Git LFS Details

  • SHA256: 005ec4e30919c46389455ed24fc98038e84884af7ee8da249e28d21c88a60a75
  • Pointer size: 131 Bytes
  • Size of remote file: 116 kB
images/4d3b88ed-d0dc-43ec-bbaf-6babf6f310a7.jpg ADDED

Git LFS Details

  • SHA256: 005ec4e30919c46389455ed24fc98038e84884af7ee8da249e28d21c88a60a75
  • Pointer size: 131 Bytes
  • Size of remote file: 116 kB
images/53ddc328-9b4a-432c-895e-ce1ee86765b0.jpg ADDED

Git LFS Details

  • SHA256: 7f2c0188549a1d36355fa40bcda2f2f16859652f55edfc2b13ee5045c2f258e7
  • Pointer size: 131 Bytes
  • Size of remote file: 313 kB
images/5c283719-addd-4dbf-9847-eda74d0af8e7.jpg ADDED

Git LFS Details

  • SHA256: 4809930d2aa8f1d503564024bfe48193e36ec5ae764cf5cea25462745527278d
  • Pointer size: 131 Bytes
  • Size of remote file: 311 kB
images/6e42305a-7a26-4dad-a9a1-bb0ef95be82d.jpg ADDED

Git LFS Details

  • SHA256: d69272b30dee2e83dff86907a55f11a546f62315f2db09af8d48f3181163b885
  • Pointer size: 132 Bytes
  • Size of remote file: 1.35 MB
images/a8ceb639-1fbf-447f-856e-7cf9bc796475.jpg ADDED

Git LFS Details

  • SHA256: 35bef33a0f4a84c2c1063330af2b422719c2bd99b67b64880fb90fffbb228bbd
  • Pointer size: 131 Bytes
  • Size of remote file: 353 kB
images/cc15e76a-5cd9-471e-9620-ae608e1d6be6.jpg ADDED

Git LFS Details

  • SHA256: 7f2c0188549a1d36355fa40bcda2f2f16859652f55edfc2b13ee5045c2f258e7
  • Pointer size: 131 Bytes
  • Size of remote file: 313 kB
images/d53d92d8-7958-4fb6-ba9d-5f1e27fe9ceb.jpg ADDED

Git LFS Details

  • SHA256: 5e3d7a91fc3e289f0f0f8e8286ab727438c0b4cf6821ec40e4f5452083bea161
  • Pointer size: 130 Bytes
  • Size of remote file: 88.5 kB
images/dog.jpg ADDED

Git LFS Details

  • SHA256: dee5f344382bad4de9c76c2109e56bb343a34607a6af264c28a9b5268465c217
  • Pointer size: 131 Bytes
  • Size of remote file: 536 kB
images/edca2fd3-9ef6-40a5-a091-446b1abdfb55.jpg ADDED

Git LFS Details

  • SHA256: 0d528b56157c6f26fe11beebdc79858652a10a0c02171f93fd53be8ce4473c89
  • Pointer size: 131 Bytes
  • Size of remote file: 121 kB