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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"#/default_exp app"
|
10 |
+
]
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cell_type": "code",
|
14 |
+
"execution_count": 2,
|
15 |
+
"metadata": {},
|
16 |
+
"outputs": [],
|
17 |
+
"source": [
|
18 |
+
"#/export\n",
|
19 |
+
"from fastai.vision.all import *\n",
|
20 |
+
"import gradio as gr\n",
|
21 |
+
"\n",
|
22 |
+
"def is_cat(x): return x[0].isupper()"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 3,
|
28 |
+
"metadata": {},
|
29 |
+
"outputs": [],
|
30 |
+
"source": [
|
31 |
+
"#/export\n",
|
32 |
+
"learn = load_learner('model.pkl')"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "code",
|
37 |
+
"execution_count": 7,
|
38 |
+
"metadata": {},
|
39 |
+
"outputs": [],
|
40 |
+
"source": [
|
41 |
+
"#/export\n",
|
42 |
+
"categories = ('Dog','Cat')\n",
|
43 |
+
"\n",
|
44 |
+
"def classify_image(img):\n",
|
45 |
+
" pred,idx,probs = learn.predict(img)\n",
|
46 |
+
" return dict(zip(categories,map(float,probs)))"
|
47 |
+
]
|
48 |
+
},
|
49 |
+
{
|
50 |
+
"cell_type": "code",
|
51 |
+
"execution_count": 8,
|
52 |
+
"metadata": {},
|
53 |
+
"outputs": [
|
54 |
+
{
|
55 |
+
"data": {
|
56 |
+
"text/html": [
|
57 |
+
"\n",
|
58 |
+
"<style>\n",
|
59 |
+
" /* Turns off some styling */\n",
|
60 |
+
" progress {\n",
|
61 |
+
" /* gets rid of default border in Firefox and Opera. */\n",
|
62 |
+
" border: none;\n",
|
63 |
+
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
|
64 |
+
" background-size: auto;\n",
|
65 |
+
" }\n",
|
66 |
+
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
|
67 |
+
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
|
68 |
+
" }\n",
|
69 |
+
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
|
70 |
+
" background: #F44336;\n",
|
71 |
+
" }\n",
|
72 |
+
"</style>\n"
|
73 |
+
],
|
74 |
+
"text/plain": [
|
75 |
+
"<IPython.core.display.HTML object>"
|
76 |
+
]
|
77 |
+
},
|
78 |
+
"metadata": {},
|
79 |
+
"output_type": "display_data"
|
80 |
+
},
|
81 |
+
{
|
82 |
+
"data": {
|
83 |
+
"text/html": [],
|
84 |
+
"text/plain": [
|
85 |
+
"<IPython.core.display.HTML object>"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
"metadata": {},
|
89 |
+
"output_type": "display_data"
|
90 |
+
},
|
91 |
+
{
|
92 |
+
"data": {
|
93 |
+
"text/plain": [
|
94 |
+
"{'Dog': 0.9999974966049194, 'Cat': 2.5427661967114545e-06}"
|
95 |
+
]
|
96 |
+
},
|
97 |
+
"execution_count": 8,
|
98 |
+
"metadata": {},
|
99 |
+
"output_type": "execute_result"
|
100 |
+
}
|
101 |
+
],
|
102 |
+
"source": [
|
103 |
+
"classify_image('dog.jpeg')"
|
104 |
+
]
|
105 |
+
},
|
106 |
+
{
|
107 |
+
"cell_type": "code",
|
108 |
+
"execution_count": 9,
|
109 |
+
"metadata": {},
|
110 |
+
"outputs": [
|
111 |
+
{
|
112 |
+
"name": "stderr",
|
113 |
+
"output_type": "stream",
|
114 |
+
"text": [
|
115 |
+
"/Users/katetran/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",
|
116 |
+
" warnings.warn(\n",
|
117 |
+
"/Users/katetran/mambaforge/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
|
118 |
+
" warnings.warn(value)\n",
|
119 |
+
"/Users/katetran/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",
|
120 |
+
" warnings.warn(\n",
|
121 |
+
"/Users/katetran/mambaforge/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",
|
122 |
+
" warnings.warn(value)\n"
|
123 |
+
]
|
124 |
+
},
|
125 |
+
{
|
126 |
+
"name": "stdout",
|
127 |
+
"output_type": "stream",
|
128 |
+
"text": [
|
129 |
+
"Running on local URL: http://127.0.0.1:7861\n",
|
130 |
+
"\n",
|
131 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
132 |
+
]
|
133 |
+
},
|
134 |
+
{
|
135 |
+
"data": {
|
136 |
+
"text/plain": [
|
137 |
+
"(<gradio.routes.App at 0x295dc81c0>, 'http://127.0.0.1:7861/', None)"
|
138 |
+
]
|
139 |
+
},
|
140 |
+
"execution_count": 9,
|
141 |
+
"metadata": {},
|
142 |
+
"output_type": "execute_result"
|
143 |
+
},
|
144 |
+
{
|
145 |
+
"data": {
|
146 |
+
"text/html": [
|
147 |
+
"\n",
|
148 |
+
"<style>\n",
|
149 |
+
" /* Turns off some styling */\n",
|
150 |
+
" progress {\n",
|
151 |
+
" /* gets rid of default border in Firefox and Opera. */\n",
|
152 |
+
" border: none;\n",
|
153 |
+
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
|
154 |
+
" background-size: auto;\n",
|
155 |
+
" }\n",
|
156 |
+
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
|
157 |
+
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
|
158 |
+
" }\n",
|
159 |
+
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
|
160 |
+
" background: #F44336;\n",
|
161 |
+
" }\n",
|
162 |
+
"</style>\n"
|
163 |
+
],
|
164 |
+
"text/plain": [
|
165 |
+
"<IPython.core.display.HTML object>"
|
166 |
+
]
|
167 |
+
},
|
168 |
+
"metadata": {},
|
169 |
+
"output_type": "display_data"
|
170 |
+
},
|
171 |
+
{
|
172 |
+
"data": {
|
173 |
+
"text/html": [],
|
174 |
+
"text/plain": [
|
175 |
+
"<IPython.core.display.HTML object>"
|
176 |
+
]
|
177 |
+
},
|
178 |
+
"metadata": {},
|
179 |
+
"output_type": "display_data"
|
180 |
+
},
|
181 |
+
{
|
182 |
+
"data": {
|
183 |
+
"text/html": [
|
184 |
+
"\n",
|
185 |
+
"<style>\n",
|
186 |
+
" /* Turns off some styling */\n",
|
187 |
+
" progress {\n",
|
188 |
+
" /* gets rid of default border in Firefox and Opera. */\n",
|
189 |
+
" border: none;\n",
|
190 |
+
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
|
191 |
+
" background-size: auto;\n",
|
192 |
+
" }\n",
|
193 |
+
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
|
194 |
+
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
|
195 |
+
" }\n",
|
196 |
+
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
|
197 |
+
" background: #F44336;\n",
|
198 |
+
" }\n",
|
199 |
+
"</style>\n"
|
200 |
+
],
|
201 |
+
"text/plain": [
|
202 |
+
"<IPython.core.display.HTML object>"
|
203 |
+
]
|
204 |
+
},
|
205 |
+
"metadata": {},
|
206 |
+
"output_type": "display_data"
|
207 |
+
},
|
208 |
+
{
|
209 |
+
"data": {
|
210 |
+
"text/html": [],
|
211 |
+
"text/plain": [
|
212 |
+
"<IPython.core.display.HTML object>"
|
213 |
+
]
|
214 |
+
},
|
215 |
+
"metadata": {},
|
216 |
+
"output_type": "display_data"
|
217 |
+
},
|
218 |
+
{
|
219 |
+
"data": {
|
220 |
+
"text/html": [
|
221 |
+
"\n",
|
222 |
+
"<style>\n",
|
223 |
+
" /* Turns off some styling */\n",
|
224 |
+
" progress {\n",
|
225 |
+
" /* gets rid of default border in Firefox and Opera. */\n",
|
226 |
+
" border: none;\n",
|
227 |
+
" /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
|
228 |
+
" background-size: auto;\n",
|
229 |
+
" }\n",
|
230 |
+
" progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
|
231 |
+
" background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
|
232 |
+
" }\n",
|
233 |
+
" .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
|
234 |
+
" background: #F44336;\n",
|
235 |
+
" }\n",
|
236 |
+
"</style>\n"
|
237 |
+
],
|
238 |
+
"text/plain": [
|
239 |
+
"<IPython.core.display.HTML object>"
|
240 |
+
]
|
241 |
+
},
|
242 |
+
"metadata": {},
|
243 |
+
"output_type": "display_data"
|
244 |
+
},
|
245 |
+
{
|
246 |
+
"data": {
|
247 |
+
"text/html": [],
|
248 |
+
"text/plain": [
|
249 |
+
"<IPython.core.display.HTML object>"
|
250 |
+
]
|
251 |
+
},
|
252 |
+
"metadata": {},
|
253 |
+
"output_type": "display_data"
|
254 |
+
}
|
255 |
+
],
|
256 |
+
"source": [
|
257 |
+
"#/export\n",
|
258 |
+
"image = gr.inputs.Image(shape=(192,192))\n",
|
259 |
+
"label = gr.outputs.Label()\n",
|
260 |
+
"examples = ['dog.jpeg','cat.jpeg', 'dunno.jpeg']\n",
|
261 |
+
"\n",
|
262 |
+
"intf = gr.Interface(fn=classify_image, inputs=image, outputs=label,examples=examples)\n",
|
263 |
+
"intf.launch(inline=False)"
|
264 |
+
]
|
265 |
+
}
|
266 |
+
],
|
267 |
+
"metadata": {
|
268 |
+
"kernelspec": {
|
269 |
+
"display_name": "Python 3.10.6 ('base')",
|
270 |
+
"language": "python",
|
271 |
+
"name": "python3"
|
272 |
+
},
|
273 |
+
"language_info": {
|
274 |
+
"codemirror_mode": {
|
275 |
+
"name": "ipython",
|
276 |
+
"version": 3
|
277 |
+
},
|
278 |
+
"file_extension": ".py",
|
279 |
+
"mimetype": "text/x-python",
|
280 |
+
"name": "python",
|
281 |
+
"nbconvert_exporter": "python",
|
282 |
+
"pygments_lexer": "ipython3",
|
283 |
+
"version": "3.10.6"
|
284 |
+
},
|
285 |
+
"orig_nbformat": 4,
|
286 |
+
"vscode": {
|
287 |
+
"interpreter": {
|
288 |
+
"hash": "de7cb0b1218c3fc8713775238ab21314d73326b1c691e0df13913a336de394b8"
|
289 |
+
}
|
290 |
+
}
|
291 |
+
},
|
292 |
+
"nbformat": 4,
|
293 |
+
"nbformat_minor": 2
|
294 |
+
}
|
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# %%
|
2 |
+
#/default_exp app
|
3 |
+
|
4 |
+
# %%
|
5 |
+
#/export
|
6 |
+
from fastai.vision.all import *
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
def is_cat(x): return x[0].isupper()
|
10 |
+
|
11 |
+
# %%
|
12 |
+
#/export
|
13 |
+
learn = load_learner('model.pkl')
|
14 |
+
|
15 |
+
# %%
|
16 |
+
#/export
|
17 |
+
categories = ('Dog','Cat')
|
18 |
+
|
19 |
+
def classify_image(img):
|
20 |
+
pred,idx,probs = learn.predict(img)
|
21 |
+
return dict(zip(categories,map(float,probs)))
|
22 |
+
|
23 |
+
# %%
|
24 |
+
classify_image('dog.jpeg')
|
25 |
+
|
26 |
+
# %%
|
27 |
+
#/export
|
28 |
+
image = gr.inputs.Image(shape=(192,192))
|
29 |
+
label = gr.outputs.Label()
|
30 |
+
examples = ['dog.jpeg','cat.jpeg', 'dunno.jpeg']
|
31 |
+
|
32 |
+
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label,examples=examples)
|
33 |
+
intf.launch(inline=False)
|
34 |
+
|
35 |
+
|
cat.jpeg
ADDED
dog.jpeg
ADDED
dunno.jpeg
ADDED
requirement.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
fastai
|