toufikzitouni commited on
Commit
8a8f85d
·
1 Parent(s): 44822ed

first app built for huggingface

Browse files
Files changed (11) hide show
  1. .DS_Store +0 -0
  2. .gitattributes +1 -0
  3. .gitconfig +11 -0
  4. app.py +0 -7
  5. cat.jpg +0 -0
  6. dog.jpg +0 -0
  7. dunno.jpg +0 -0
  8. main.ipynb +322 -0
  9. main.py +28 -0
  10. model.pkl +3 -0
  11. saving-a-basic-fastai-model.ipynb +1 -0
.DS_Store ADDED
Binary file (8.2 kB). View file
 
.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
+ *.ipynb merge=nbdev-merge
.gitconfig ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Generated by nbdev_install_hooks
2
+ #
3
+ # If you need to disable this instrumentation do:
4
+ # git config --local --unset include.path
5
+ #
6
+ # To restore:
7
+ # git config --local include.path ../.gitconfig
8
+ #
9
+ [merge "nbdev-merge"]
10
+ name = resolve conflicts with nbdev_fix
11
+ driver = nbdev_merge %O %A %B %P
app.py DELETED
@@ -1,7 +0,0 @@
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()
 
 
 
 
 
 
 
 
cat.jpg ADDED
dog.jpg ADDED
dunno.jpg ADDED
main.ipynb ADDED
@@ -0,0 +1,322 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 20,
6
+ "id": "30364f49-507a-474b-ac8c-bc60668a83d5",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "#|default_exp main"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 21,
16
+ "id": "22c0675e-0de0-4e5f-a92b-a903b2f6874a",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "!pip install -Uqq fastai"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 22,
26
+ "id": "63fa0ea9-142e-44ed-96fa-751e8dacc3ba",
27
+ "metadata": {},
28
+ "outputs": [
29
+ {
30
+ "name": "stdout",
31
+ "output_type": "stream",
32
+ "text": [
33
+ "Requirement already satisfied: gradio in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (3.16.1)\n",
34
+ "Requirement already satisfied: pydantic in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (1.8.2)\n",
35
+ "Requirement already satisfied: websockets>=10.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (10.4)\n",
36
+ "Requirement already satisfied: fsspec in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (2022.11.0)\n",
37
+ "Requirement already satisfied: matplotlib in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.6.2)\n",
38
+ "Requirement already satisfied: pyyaml in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (6.0)\n",
39
+ "Requirement already satisfied: requests in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (2.28.1)\n",
40
+ "Requirement already satisfied: uvicorn in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.20.0)\n",
41
+ "Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (1.23.5)\n",
42
+ "Requirement already satisfied: pydub in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.25.1)\n",
43
+ "Requirement already satisfied: altair>=4.2.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (4.2.0)\n",
44
+ "Requirement already satisfied: typing-extensions in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (4.4.0)\n",
45
+ "Requirement already satisfied: pillow in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (9.3.0)\n",
46
+ "Requirement already satisfied: jinja2 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.1.2)\n",
47
+ "Requirement already satisfied: python-multipart in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.0.5)\n",
48
+ "Requirement already satisfied: orjson in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.8.5)\n",
49
+ "Requirement already satisfied: ffmpy in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.3.0)\n",
50
+ "Requirement already satisfied: pycryptodome in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.16.0)\n",
51
+ "Requirement already satisfied: httpx in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.23.3)\n",
52
+ "Requirement already satisfied: aiohttp in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (3.8.3)\n",
53
+ "Requirement already satisfied: markupsafe in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (2.1.1)\n",
54
+ "Requirement already satisfied: markdown-it-py[linkify,plugins] in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (2.1.0)\n",
55
+ "Requirement already satisfied: fastapi in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (0.89.1)\n",
56
+ "Requirement already satisfied: pandas in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from gradio) (1.5.2)\n",
57
+ "Requirement already satisfied: toolz in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from altair>=4.2.0->gradio) (0.12.0)\n",
58
+ "Requirement already satisfied: entrypoints in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from altair>=4.2.0->gradio) (0.4)\n",
59
+ "Requirement already satisfied: jsonschema>=3.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from altair>=4.2.0->gradio) (4.16.0)\n",
60
+ "Requirement already satisfied: python-dateutil>=2.8.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from pandas->gradio) (2.8.2)\n",
61
+ "Requirement already satisfied: pytz>=2020.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from pandas->gradio) (2022.7)\n",
62
+ "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (4.0.2)\n",
63
+ "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (1.8.2)\n",
64
+ "Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (1.3.3)\n",
65
+ "Requirement already satisfied: attrs>=17.3.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (22.1.0)\n",
66
+ "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (6.0.4)\n",
67
+ "Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (1.3.1)\n",
68
+ "Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from aiohttp->gradio) (2.0.4)\n",
69
+ "Requirement already satisfied: starlette==0.22.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from fastapi->gradio) (0.22.0)\n",
70
+ "Requirement already satisfied: anyio<5,>=3.4.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from starlette==0.22.0->fastapi->gradio) (3.5.0)\n",
71
+ "Requirement already satisfied: httpcore<0.17.0,>=0.15.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from httpx->gradio) (0.16.3)\n",
72
+ "Requirement already satisfied: certifi in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from httpx->gradio) (2022.12.7)\n",
73
+ "Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from httpx->gradio) (1.5.0)\n",
74
+ "Requirement already satisfied: sniffio in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from httpx->gradio) (1.2.0)\n",
75
+ "Requirement already satisfied: mdurl~=0.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from markdown-it-py[linkify,plugins]->gradio) (0.1.2)\n",
76
+ "Requirement already satisfied: mdit-py-plugins in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from markdown-it-py[linkify,plugins]->gradio) (0.3.3)\n",
77
+ "Requirement already satisfied: linkify-it-py~=1.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from markdown-it-py[linkify,plugins]->gradio) (1.0.3)\n",
78
+ "Requirement already satisfied: cycler>=0.10 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (0.11.0)\n",
79
+ "Requirement already satisfied: kiwisolver>=1.0.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (1.4.4)\n",
80
+ "Requirement already satisfied: contourpy>=1.0.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (1.0.5)\n",
81
+ "Requirement already satisfied: pyparsing>=2.2.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (3.0.9)\n",
82
+ "Requirement already satisfied: packaging>=20.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (22.0)\n",
83
+ "Requirement already satisfied: fonttools>=4.22.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from matplotlib->gradio) (4.25.0)\n",
84
+ "Requirement already satisfied: six>=1.4.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from python-multipart->gradio) (1.16.0)\n",
85
+ "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from requests->gradio) (3.4)\n",
86
+ "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from requests->gradio) (1.26.13)\n",
87
+ "Requirement already satisfied: h11>=0.8 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from uvicorn->gradio) (0.14.0)\n",
88
+ "Requirement already satisfied: click>=7.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from uvicorn->gradio) (8.0.4)\n",
89
+ "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from jsonschema>=3.0->altair>=4.2.0->gradio) (0.18.0)\n",
90
+ "Requirement already satisfied: uc-micro-py in /opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages (from linkify-it-py~=1.0->markdown-it-py[linkify,plugins]->gradio) (1.0.1)\n"
91
+ ]
92
+ }
93
+ ],
94
+ "source": [
95
+ "!pip install gradio"
96
+ ]
97
+ },
98
+ {
99
+ "cell_type": "code",
100
+ "execution_count": 23,
101
+ "id": "7658cf93-38a8-4f3c-a773-ec7b7777652d",
102
+ "metadata": {},
103
+ "outputs": [],
104
+ "source": [
105
+ "#|export\n",
106
+ "from fastai.vision.all import *\n",
107
+ "import gradio as gr\n",
108
+ "\n",
109
+ "def is_cat(x): return x[0].isupper()"
110
+ ]
111
+ },
112
+ {
113
+ "cell_type": "code",
114
+ "execution_count": 24,
115
+ "id": "9c6c2b95-2f0d-4ac5-8d9c-59b420ba8d8e",
116
+ "metadata": {},
117
+ "outputs": [],
118
+ "source": [
119
+ "path = untar_data(URLs.PETS)/'images'"
120
+ ]
121
+ },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": 25,
125
+ "id": "a892c11a-33bc-4947-a7b2-04237df332d8",
126
+ "metadata": {},
127
+ "outputs": [],
128
+ "source": [
129
+ "dls = ImageDataLoaders.from_name_func('.',\n",
130
+ " get_image_files(path), valid_pct=0.2, seed=42,\n",
131
+ " label_func=is_cat,\n",
132
+ " item_tfms=Resize(192))"
133
+ ]
134
+ },
135
+ {
136
+ "cell_type": "code",
137
+ "execution_count": 26,
138
+ "id": "1a83ccd5-ec38-4f03-b937-fabe5f9c63b1",
139
+ "metadata": {},
140
+ "outputs": [],
141
+ "source": [
142
+ "#|export\n",
143
+ "learn = load_learner('model.pkl')"
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "code",
148
+ "execution_count": 27,
149
+ "id": "9a4418cc-4bb4-4e9c-9ae8-0ba1f42e7af7",
150
+ "metadata": {},
151
+ "outputs": [],
152
+ "source": [
153
+ "#|export\n",
154
+ "categories = ('Dog', 'Cat')\n",
155
+ "\n",
156
+ "def classify(img):\n",
157
+ " pred,idx,probs = learn.predict(img)\n",
158
+ " return dict(zip(categories, map(float, probs)))"
159
+ ]
160
+ },
161
+ {
162
+ "cell_type": "code",
163
+ "execution_count": 28,
164
+ "id": "79085d0b-8db7-43d4-9da3-0d764c302888",
165
+ "metadata": {},
166
+ "outputs": [
167
+ {
168
+ "data": {
169
+ "text/html": [
170
+ "\n",
171
+ "<style>\n",
172
+ " /* Turns off some styling */\n",
173
+ " progress {\n",
174
+ " /* gets rid of default border in Firefox and Opera. */\n",
175
+ " border: none;\n",
176
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
177
+ " background-size: auto;\n",
178
+ " }\n",
179
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
180
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
181
+ " }\n",
182
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
183
+ " background: #F44336;\n",
184
+ " }\n",
185
+ "</style>\n"
186
+ ],
187
+ "text/plain": [
188
+ "<IPython.core.display.HTML object>"
189
+ ]
190
+ },
191
+ "metadata": {},
192
+ "output_type": "display_data"
193
+ },
194
+ {
195
+ "data": {
196
+ "text/html": [],
197
+ "text/plain": [
198
+ "<IPython.core.display.HTML object>"
199
+ ]
200
+ },
201
+ "metadata": {},
202
+ "output_type": "display_data"
203
+ },
204
+ {
205
+ "data": {
206
+ "text/plain": [
207
+ "{'Dog': 3.103109945650634e-14, 'Cat': 1.0}"
208
+ ]
209
+ },
210
+ "execution_count": 28,
211
+ "metadata": {},
212
+ "output_type": "execute_result"
213
+ }
214
+ ],
215
+ "source": [
216
+ "classify('cat.jpg')"
217
+ ]
218
+ },
219
+ {
220
+ "cell_type": "code",
221
+ "execution_count": 29,
222
+ "id": "b1155832-5e29-4f88-aea4-85a6261bedfd",
223
+ "metadata": {},
224
+ "outputs": [
225
+ {
226
+ "name": "stderr",
227
+ "output_type": "stream",
228
+ "text": [
229
+ "/opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/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",
230
+ " warnings.warn(\n",
231
+ "/opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
232
+ " warnings.warn(value)\n",
233
+ "/opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/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",
234
+ " warnings.warn(\n",
235
+ "/opt/homebrew/Caskroom/miniforge/base/envs/fastai/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",
236
+ " warnings.warn(value)\n"
237
+ ]
238
+ },
239
+ {
240
+ "name": "stdout",
241
+ "output_type": "stream",
242
+ "text": [
243
+ "Running on local URL: http://127.0.0.1:7861\n",
244
+ "\n",
245
+ "To create a public link, set `share=True` in `launch()`.\n"
246
+ ]
247
+ },
248
+ {
249
+ "data": {
250
+ "text/plain": []
251
+ },
252
+ "execution_count": 29,
253
+ "metadata": {},
254
+ "output_type": "execute_result"
255
+ }
256
+ ],
257
+ "source": [
258
+ "#|export\n",
259
+ "image = gr.inputs.Image(shape=(192, 192))\n",
260
+ "labels = gr.outputs.Label()\n",
261
+ "examples = ['dog.jpg', 'cat.jpg']\n",
262
+ "\n",
263
+ "intf = gr.Interface(fn=classify, inputs=image, outputs=labels, examples=examples)\n",
264
+ "intf.launch(inline=False)"
265
+ ]
266
+ },
267
+ {
268
+ "cell_type": "code",
269
+ "execution_count": 30,
270
+ "id": "419e727b-5fc0-4a19-94e3-7c9c9c8c22ad",
271
+ "metadata": {},
272
+ "outputs": [
273
+ {
274
+ "name": "stdout",
275
+ "output_type": "stream",
276
+ "text": [
277
+ "Export successful\n"
278
+ ]
279
+ }
280
+ ],
281
+ "source": [
282
+ "import nbdev\n",
283
+ "nbdev.export.nb_export('main.ipynb', '.')\n",
284
+ "print('Export successful')"
285
+ ]
286
+ },
287
+ {
288
+ "cell_type": "code",
289
+ "execution_count": null,
290
+ "id": "b864b212-17a0-4737-a541-1ead7e3c762a",
291
+ "metadata": {},
292
+ "outputs": [],
293
+ "source": []
294
+ }
295
+ ],
296
+ "metadata": {
297
+ "kernelspec": {
298
+ "display_name": "fastai",
299
+ "language": "python",
300
+ "name": "python3"
301
+ },
302
+ "language_info": {
303
+ "codemirror_mode": {
304
+ "name": "ipython",
305
+ "version": 3
306
+ },
307
+ "file_extension": ".py",
308
+ "mimetype": "text/x-python",
309
+ "name": "python",
310
+ "nbconvert_exporter": "python",
311
+ "pygments_lexer": "ipython3",
312
+ "version": "3.10.8"
313
+ },
314
+ "vscode": {
315
+ "interpreter": {
316
+ "hash": "4b6371f9b180b180e3c0fc2dc539c52593b72b3e75aed3593bd0a5845eb81bd2"
317
+ }
318
+ }
319
+ },
320
+ "nbformat": 4,
321
+ "nbformat_minor": 5
322
+ }
main.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: main.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['learn', 'categories', 'image', 'labels', 'examples', 'intf', 'is_cat', 'classify']
5
+
6
+ # %% main.ipynb 3
7
+ from fastai.vision.all import *
8
+ import gradio as gr
9
+
10
+ def is_cat(x): return x[0].isupper()
11
+
12
+ # %% main.ipynb 6
13
+ learn = load_learner('model.pkl')
14
+
15
+ # %% main.ipynb 7
16
+ categories = ('Dog', 'Cat')
17
+
18
+ def classify(img):
19
+ pred,idx,probs = learn.predict(img)
20
+ return dict(zip(categories, map(float, probs)))
21
+
22
+ # %% main.ipynb 9
23
+ image = gr.inputs.Image(shape=(192, 192))
24
+ labels = gr.outputs.Label()
25
+ examples = ['dog.jpg', 'cat.jpg']
26
+
27
+ intf = gr.Interface(fn=classify, inputs=image, outputs=labels, examples=examples)
28
+ intf.launch(inline=False)
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7824a16eaea51baa955cc628d777947501d66dce80f11f115b12c40df00a9b92
3
+ size 47056529
saving-a-basic-fastai-model.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Saving a Cats v Dogs Model","metadata":{"id":"98d53c05"}},{"cell_type":"markdown","source":"This is a minimal example showing how to train a fastai model on Kaggle, and save it so you can use it in your app.","metadata":{}},{"cell_type":"code","source":"# Make sure we've got the latest version of fastai:\n!pip install -Uqq fastai","metadata":{"id":"evvA0fqvSblq","outputId":"ba21b811-767c-459a-ccdf-044758720a55","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-03T05:51:37.948558Z","iopub.execute_input":"2022-05-03T05:51:37.949032Z","iopub.status.idle":"2022-05-03T05:51:59.531217Z","shell.execute_reply.started":"2022-05-03T05:51:37.948947Z","shell.execute_reply":"2022-05-03T05:51:59.530294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"First, import all the stuff we need from fastai:","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"id":"44eb0ad3","execution":{"iopub.status.busy":"2022-05-03T05:51:59.533878Z","iopub.execute_input":"2022-05-03T05:51:59.534478Z","iopub.status.idle":"2022-05-03T05:52:02.177975Z","shell.execute_reply.started":"2022-05-03T05:51:59.534432Z","shell.execute_reply":"2022-05-03T05:52:02.177267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Download and decompress our dataset, which is pictures of dogs and cats:","metadata":{}},{"cell_type":"code","source":"path = untar_data(URLs.PETS)/'images'","metadata":{"execution":{"iopub.status.busy":"2022-05-03T05:52:02.180192Z","iopub.execute_input":"2022-05-03T05:52:02.180691Z","iopub.status.idle":"2022-05-03T05:53:02.465242Z","shell.execute_reply.started":"2022-05-03T05:52:02.180651Z","shell.execute_reply":"2022-05-03T05:53:02.464516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We need a way to label our images as dogs or cats. In this dataset, pictures of cats are given a filename that starts with a capital letter:","metadata":{}},{"cell_type":"code","source":"def is_cat(x): return x[0].isupper() ","metadata":{"id":"44eb0ad3","execution":{"iopub.status.busy":"2022-05-03T05:53:02.467289Z","iopub.execute_input":"2022-05-03T05:53:02.467572Z","iopub.status.idle":"2022-05-03T05:53:02.474701Z","shell.execute_reply.started":"2022-05-03T05:53:02.467536Z","shell.execute_reply":"2022-05-03T05:53:02.474109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we can create our `DataLoaders`:","metadata":{}},{"cell_type":"code","source":"dls = ImageDataLoaders.from_name_func('.',\n get_image_files(path), valid_pct=0.2, seed=42,\n label_func=is_cat,\n item_tfms=Resize(192))","metadata":{"id":"44eb0ad3","execution":{"iopub.status.busy":"2022-05-03T05:53:02.475754Z","iopub.execute_input":"2022-05-03T05:53:02.476084Z","iopub.status.idle":"2022-05-03T05:53:06.703777Z","shell.execute_reply.started":"2022-05-03T05:53:02.476052Z","shell.execute_reply":"2022-05-03T05:53:06.703023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"... and train our model, a resnet18 (to keep it small and fast):","metadata":{}},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(3)","metadata":{"id":"c107f724","outputId":"fcc1de68-7c8b-43f5-b9eb-fcdb0773ef07","execution":{"iopub.status.busy":"2022-05-03T05:53:28.092381Z","iopub.execute_input":"2022-05-03T05:53:28.093059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we can export our trained `Learner`. This contains all the information needed to run the model:","metadata":{}},{"cell_type":"code","source":"learn.export('model.pkl')","metadata":{"id":"ae2bc6ac","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Finally, open the Kaggle sidebar on the right if it's not already, and find the section marked \"Output\". Open the `/kaggle/working` folder, and you'll see `model.pkl`. Click on it, then click on the menu on the right that appears, and choose \"Download\". After a few seconds, your model will be downloaded to your computer, where you can then create your app that uses the model.","metadata":{"id":"Q2HTrQKTf3BV"}}]}