tiaona commited on
Commit
0a7212a
·
1 Parent(s): 75e1bca

fruit recognizer

Browse files
Files changed (4) hide show
  1. app.ipynb +243 -0
  2. app.py +28 -11
  3. fruits.pkl +3 -0
  4. requirements.txt +1 -0
app.ipynb ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 56,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "#|default_exp app"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 72,
15
+ "metadata": {},
16
+ "outputs": [
17
+ {
18
+ "name": "stdout",
19
+ "output_type": "stream",
20
+ "text": [
21
+ "Running on local URL: http://127.0.0.1:7860\n",
22
+ "\n",
23
+ "To create a public link, set `share=True` in `launch()`.\n"
24
+ ]
25
+ },
26
+ {
27
+ "data": {
28
+ "text/html": [
29
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
30
+ ],
31
+ "text/plain": [
32
+ "<IPython.core.display.HTML object>"
33
+ ]
34
+ },
35
+ "metadata": {},
36
+ "output_type": "display_data"
37
+ },
38
+ {
39
+ "data": {
40
+ "text/plain": []
41
+ },
42
+ "execution_count": 72,
43
+ "metadata": {},
44
+ "output_type": "execute_result"
45
+ }
46
+ ],
47
+ "source": [
48
+ "#|export\n",
49
+ "# import gradio as gr\n",
50
+ "# # from fastai.vision.all import vision_learner\n",
51
+ "\n",
52
+ "# def greet(val1, val2):\n",
53
+ "# return int(val1) + int(val2)\n",
54
+ "\n",
55
+ "# iface = gr.Interface(fn=greet, inputs=[\"text\", \"text\"], outputs=\"text\")\n",
56
+ "# iface.launch(share=False)"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": 73,
62
+ "metadata": {},
63
+ "outputs": [
64
+ {
65
+ "name": "stdout",
66
+ "output_type": "stream",
67
+ "text": [
68
+ "Closing server running on port: 7860\n"
69
+ ]
70
+ }
71
+ ],
72
+ "source": [
73
+ "iface.close()"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "execution_count": 96,
79
+ "metadata": {},
80
+ "outputs": [],
81
+ "source": [
82
+ "#|export\n",
83
+ "from fastai.vision.all import *\n",
84
+ "\n",
85
+ "def label_func(fname):\n",
86
+ " return fname.name.split('_')[1].split('.')[0].strip()\n",
87
+ "\n",
88
+ "learner = load_learner('fruits.pkl')"
89
+ ]
90
+ },
91
+ {
92
+ "cell_type": "code",
93
+ "execution_count": 78,
94
+ "metadata": {},
95
+ "outputs": [],
96
+ "source": [
97
+ "#|export\n",
98
+ "categories = learner.dls.vocab\n",
99
+ "\n",
100
+ "def classify_image(img):\n",
101
+ " pred, pred_idx, probs = learner.predict(img)\n",
102
+ " return dict(zip(categories, map(float, probs)))"
103
+ ]
104
+ },
105
+ {
106
+ "cell_type": "code",
107
+ "execution_count": 93,
108
+ "metadata": {},
109
+ "outputs": [
110
+ {
111
+ "name": "stdout",
112
+ "output_type": "stream",
113
+ "text": [
114
+ "Running on local URL: http://127.0.0.1:7860\n",
115
+ "\n",
116
+ "To create a public link, set `share=True` in `launch()`.\n"
117
+ ]
118
+ },
119
+ {
120
+ "data": {
121
+ "text/html": [
122
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
123
+ ],
124
+ "text/plain": [
125
+ "<IPython.core.display.HTML object>"
126
+ ]
127
+ },
128
+ "metadata": {},
129
+ "output_type": "display_data"
130
+ },
131
+ {
132
+ "data": {
133
+ "text/plain": []
134
+ },
135
+ "execution_count": 93,
136
+ "metadata": {},
137
+ "output_type": "execute_result"
138
+ },
139
+ {
140
+ "data": {
141
+ "text/html": [
142
+ "\n",
143
+ "<style>\n",
144
+ " /* Turns off some styling */\n",
145
+ " progress {\n",
146
+ " /* gets rid of default border in Firefox and Opera. */\n",
147
+ " border: none;\n",
148
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
149
+ " background-size: auto;\n",
150
+ " }\n",
151
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
152
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
153
+ " }\n",
154
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
155
+ " background: #F44336;\n",
156
+ " }\n",
157
+ "</style>\n"
158
+ ],
159
+ "text/plain": [
160
+ "<IPython.core.display.HTML object>"
161
+ ]
162
+ },
163
+ "metadata": {},
164
+ "output_type": "display_data"
165
+ },
166
+ {
167
+ "data": {
168
+ "text/html": [],
169
+ "text/plain": [
170
+ "<IPython.core.display.HTML object>"
171
+ ]
172
+ },
173
+ "metadata": {},
174
+ "output_type": "display_data"
175
+ }
176
+ ],
177
+ "source": [
178
+ "#|export\n",
179
+ "from re import I\n",
180
+ "import gradio as gr\n",
181
+ "\n",
182
+ "ifc = gr.Interface(fn=classify_image,\n",
183
+ " inputs=gr.Image(type='pil', label='Input Image'),\n",
184
+ " outputs=gr.Label(num_top_classes=6)\n",
185
+ " )\n",
186
+ "ifc.launch()"
187
+ ]
188
+ },
189
+ {
190
+ "cell_type": "code",
191
+ "execution_count": 91,
192
+ "metadata": {},
193
+ "outputs": [
194
+ {
195
+ "name": "stdout",
196
+ "output_type": "stream",
197
+ "text": [
198
+ "Closing server running on port: 7860\n"
199
+ ]
200
+ }
201
+ ],
202
+ "source": [
203
+ "ifc.close()"
204
+ ]
205
+ },
206
+ {
207
+ "cell_type": "code",
208
+ "execution_count": 75,
209
+ "metadata": {},
210
+ "outputs": [],
211
+ "source": [
212
+ "# from doctest import debug\n",
213
+ "from doctest import debug\n",
214
+ "from os import name\n",
215
+ "import nbdev\n",
216
+ "# ?nbdev.export.nb_export\n",
217
+ "!rm app.py\n",
218
+ "val = nbdev.export.nb_export('./app.ipynb', lib_path='.')\n"
219
+ ]
220
+ }
221
+ ],
222
+ "metadata": {
223
+ "kernelspec": {
224
+ "display_name": "Python 3 (ipykernel)",
225
+ "language": "python",
226
+ "name": "python3"
227
+ },
228
+ "language_info": {
229
+ "codemirror_mode": {
230
+ "name": "ipython",
231
+ "version": 3
232
+ },
233
+ "file_extension": ".py",
234
+ "mimetype": "text/x-python",
235
+ "name": "python",
236
+ "nbconvert_exporter": "python",
237
+ "pygments_lexer": "ipython3",
238
+ "version": "3.10.13"
239
+ }
240
+ },
241
+ "nbformat": 4,
242
+ "nbformat_minor": 2
243
+ }
app.py CHANGED
@@ -1,22 +1,39 @@
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
2
 
3
  # %% auto 0
4
- __all__ = ['iface', 'greet']
5
 
6
  # %% app.ipynb 1
7
- import gradio as gr
8
- # from fastai.vision.all import vision_learner
9
 
10
- def greet(val1, val2):
11
- return int(val1) + int(val2)
12
 
13
- iface = gr.Interface(fn=greet, inputs=["text", "text"], outputs="text")
14
- iface.launch(share=False)
15
 
16
  # %% app.ipynb 3
17
- # from fastai.vision.all import *
 
 
 
 
 
18
 
19
- # def label_func(fname):
20
- # return fname.name.split('_')[1].split('.')[0].strip()
 
 
 
 
 
 
 
 
21
 
22
- # learner = load_learner('/mnt/p/lab/ai_env/course22/fruits.pkl')
 
 
 
 
 
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
2
 
3
  # %% auto 0
4
+ __all__ = ['learner', 'categories', 'ifc', 'label_func', 'classify_image']
5
 
6
  # %% app.ipynb 1
7
+ # import gradio as gr
8
+ # # from fastai.vision.all import vision_learner
9
 
10
+ # def greet(val1, val2):
11
+ # return int(val1) + int(val2)
12
 
13
+ # iface = gr.Interface(fn=greet, inputs=["text", "text"], outputs="text")
14
+ # iface.launch(share=False)
15
 
16
  # %% app.ipynb 3
17
+ from fastai.vision.all import *
18
+
19
+ def label_func(fname):
20
+ return fname.name.split('_')[1].split('.')[0].strip()
21
+
22
+ learner = load_learner('fruits.pkl')
23
 
24
+ # %% app.ipynb 4
25
+ categories = learner.dls.vocab
26
+
27
+ def classify_image(img):
28
+ pred, pred_idx, probs = learner.predict(img)
29
+ return dict(zip(categories, map(float, probs)))
30
+
31
+ # %% app.ipynb 5
32
+ from re import I
33
+ import gradio as gr
34
 
35
+ ifc = gr.Interface(fn=classify_image,
36
+ inputs=gr.Image(type='pil', label='Input Image'),
37
+ outputs=gr.Label(num_top_classes=6)
38
+ )
39
+ ifc.launch()
fruits.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4c81151c9fdd91bcfd3b0202335f0aab57da72cab31db15a9d5bd261272a344f
3
+ size 87485933
requirements.txt CHANGED
@@ -0,0 +1 @@
 
 
1
+ fastai