patricksaxton commited on
Commit
0086095
·
1 Parent(s): 9d11237

create gradio with cat model

Browse files
Files changed (7) hide show
  1. app.py +26 -4
  2. app/app.py +17 -0
  3. cat.jpeg +0 -0
  4. ch2/app.py +17 -0
  5. dog.jpeg +0 -0
  6. dunno.jpeg +0 -0
  7. test.ipynb +194 -0
app.py CHANGED
@@ -1,9 +1,31 @@
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
 
4
- def greet(name):
5
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
6
 
7
 
8
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
9
- iface.launch()
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: test.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['learn', 'categories', 'image', 'label', 'examples', 'intf', 'is_cat', 'classify_image']
5
+
6
+ # %% test.ipynb 1
7
+ from fastai.vision.all import *
8
  import gradio as gr
9
 
10
 
11
+ def is_cat(x): return x[0].isupper()
12
+
13
+
14
+ # %% test.ipynb 3
15
+ learn = load_learner('model.pkl')
16
+
17
+ # %% test.ipynb 5
18
+ categories = 'Dog', 'Cat'
19
+
20
+
21
+ def classify_image(img):
22
+ pred, idx, probs = learn.predict(img)
23
+ return dict(zip(categories, map(float, probs)))
24
 
25
 
26
+ # %% test.ipynb 7
27
+ image = gr.components.Image(shape=(192, 192))
28
+ label = gr.components.Label()
29
+ examples = ['dog.jpeg', 'cat.jpeg', 'dunno.jpeg']
30
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
31
+ intf.launch(inline=False)
app/app.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../test.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['categories', 'image', 'label', 'examples', 'intf', 'classify_image']
5
+
6
+ # %% ../test.ipynb 5
7
+ categories = 'Dog', 'Cat'
8
+ def classify_image(img):
9
+ pred,idx,probs = learn.predict(img)
10
+ return dict(zip(categories, map(float, probs)))
11
+
12
+ # %% ../test.ipynb 7
13
+ image = gr.components.Image(shape=(192,192))
14
+ label = gr.components.Label()
15
+ examples = ['dog.jpeg', 'cat.jpeg', 'dunno.jpeg']
16
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
17
+ intf.launch(inline=False)
cat.jpeg ADDED
ch2/app.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../test.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['categories', 'image', 'label', 'examples', 'intf', 'classify_image']
5
+
6
+ # %% ../test.ipynb 5
7
+ categories = 'Dog', 'Cat'
8
+ def classify_image(img):
9
+ pred,idx,probs = learn.predict(img)
10
+ return dict(zip(categories, map(float, probs)))
11
+
12
+ # %% ../test.ipynb 7
13
+ image = gr.components.Image(shape=(192,192))
14
+ label = gr.components.Label()
15
+ examples = ['dog.jpeg', 'cat.jpeg', 'dunno.jpeg']
16
+ intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
17
+ intf.launch(inline=False)
dog.jpeg ADDED
dunno.jpeg ADDED
test.ipynb ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 5,
6
+ "outputs": [],
7
+ "source": [
8
+ "#|default_exp app"
9
+ ],
10
+ "metadata": {
11
+ "collapsed": false,
12
+ "ExecuteTime": {
13
+ "start_time": "2023-04-24T10:37:08.913579Z",
14
+ "end_time": "2023-04-24T10:37:08.916434Z"
15
+ }
16
+ }
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "execution_count": null,
21
+ "outputs": [],
22
+ "source": [
23
+ "#|export\n",
24
+ "from fastai.vision.all import *\n",
25
+ "import gradio as gr\n",
26
+ "\n",
27
+ "def is_cat(x): return x[0].isupper()\n"
28
+ ],
29
+ "metadata": {
30
+ "collapsed": false
31
+ }
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": null,
36
+ "outputs": [],
37
+ "source": [
38
+ "im = PILImage.create('dog.jpeg')\n",
39
+ "im.thumbnail((192,192))\n",
40
+ "im\n"
41
+ ],
42
+ "metadata": {
43
+ "collapsed": false,
44
+ "ExecuteTime": {
45
+ "start_time": "2023-04-24T10:17:24.640902Z",
46
+ "end_time": "2023-04-24T10:17:24.720177Z"
47
+ }
48
+ }
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": 15,
53
+ "outputs": [
54
+ {
55
+ "ename": "NameError",
56
+ "evalue": "name 'load_learner' is not defined",
57
+ "output_type": "error",
58
+ "traceback": [
59
+ "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
60
+ "\u001B[0;31mNameError\u001B[0m Traceback (most recent call last)",
61
+ "Cell \u001B[0;32mIn[15], line 2\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;66;03m#|export\u001B[39;00m\n\u001B[0;32m----> 2\u001B[0m learn \u001B[38;5;241m=\u001B[39m \u001B[43mload_learner\u001B[49m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mmodel.pkl\u001B[39m\u001B[38;5;124m'\u001B[39m)\n",
62
+ "\u001B[0;31mNameError\u001B[0m: name 'load_learner' is not defined"
63
+ ]
64
+ }
65
+ ],
66
+ "source": [
67
+ "#|export\n",
68
+ "learn = load_learner('model.pkl')"
69
+ ],
70
+ "metadata": {
71
+ "collapsed": false,
72
+ "ExecuteTime": {
73
+ "start_time": "2023-04-24T10:17:40.853831Z",
74
+ "end_time": "2023-04-24T10:17:40.916389Z"
75
+ }
76
+ }
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "execution_count": null,
81
+ "outputs": [],
82
+ "source": [
83
+ "%time learn.predict(im)"
84
+ ],
85
+ "metadata": {
86
+ "collapsed": false,
87
+ "ExecuteTime": {
88
+ "start_time": "2023-04-24T10:18:41.130215Z",
89
+ "end_time": "2023-04-24T10:18:41.263864Z"
90
+ }
91
+ }
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "execution_count": null,
96
+ "outputs": [],
97
+ "source": [
98
+ "#|export\n",
99
+ "categories = 'Dog', 'Cat'\n",
100
+ "def classify_image(img):\n",
101
+ " pred,idx,probs = learn.predict(img)\n",
102
+ " return dict(zip(categories, map(float, probs)))"
103
+ ],
104
+ "metadata": {
105
+ "collapsed": false,
106
+ "ExecuteTime": {
107
+ "start_time": "2023-04-24T10:24:12.712802Z",
108
+ "end_time": "2023-04-24T10:24:12.716475Z"
109
+ }
110
+ }
111
+ },
112
+ {
113
+ "cell_type": "code",
114
+ "execution_count": null,
115
+ "outputs": [],
116
+ "source": [
117
+ "classify_image(im)"
118
+ ],
119
+ "metadata": {
120
+ "collapsed": false,
121
+ "ExecuteTime": {
122
+ "start_time": "2023-04-24T10:24:13.986686Z",
123
+ "end_time": "2023-04-24T10:24:14.038054Z"
124
+ }
125
+ }
126
+ },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": null,
130
+ "outputs": [],
131
+ "source": [
132
+ "#|export\n",
133
+ "image = gr.components.Image(shape=(192,192))\n",
134
+ "label = gr.components.Label()\n",
135
+ "examples = ['dog.jpeg', 'cat.jpeg', 'dunno.jpeg']\n",
136
+ "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)\n",
137
+ "intf.launch(inline=False)"
138
+ ],
139
+ "metadata": {
140
+ "collapsed": false,
141
+ "ExecuteTime": {
142
+ "start_time": "2023-04-24T10:29:07.009173Z",
143
+ "end_time": "2023-04-24T10:29:07.451301Z"
144
+ }
145
+ }
146
+ },
147
+ {
148
+ "cell_type": "code",
149
+ "execution_count": 16,
150
+ "outputs": [],
151
+ "source": [
152
+ "import nbdev\n",
153
+ "nbdev.export.nb_export('test.ipynb', '')"
154
+ ],
155
+ "metadata": {
156
+ "collapsed": false,
157
+ "ExecuteTime": {
158
+ "start_time": "2023-04-24T10:40:21.436209Z",
159
+ "end_time": "2023-04-24T10:40:21.493462Z"
160
+ }
161
+ }
162
+ },
163
+ {
164
+ "cell_type": "code",
165
+ "execution_count": null,
166
+ "outputs": [],
167
+ "source": [],
168
+ "metadata": {
169
+ "collapsed": false
170
+ }
171
+ }
172
+ ],
173
+ "metadata": {
174
+ "kernelspec": {
175
+ "display_name": "Python 3",
176
+ "language": "python",
177
+ "name": "python3"
178
+ },
179
+ "language_info": {
180
+ "codemirror_mode": {
181
+ "name": "ipython",
182
+ "version": 2
183
+ },
184
+ "file_extension": ".py",
185
+ "mimetype": "text/x-python",
186
+ "name": "python",
187
+ "nbconvert_exporter": "python",
188
+ "pygments_lexer": "ipython2",
189
+ "version": "2.7.6"
190
+ }
191
+ },
192
+ "nbformat": 4,
193
+ "nbformat_minor": 0
194
+ }