Vadim Kulagin commited on
Commit
a3b65c1
·
1 Parent(s): d6e0101

attempt 4

Browse files
Files changed (4) hide show
  1. app.ipynb +195 -0
  2. app.py +13 -4
  3. cucumber2.jpg +0 -0
  4. zucchini2.jpg +0 -0
app.ipynb ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 5,
6
+ "id": "08fe0aa2-57ce-4c75-92d4-ed5a490bfe19",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [],
11
+ "source": [
12
+ "from fastai.vision.all import *\n",
13
+ "import gradio as gr"
14
+ ]
15
+ },
16
+ {
17
+ "cell_type": "code",
18
+ "execution_count": 6,
19
+ "id": "8b1e28b4-67de-454d-91a6-b716ab2edced",
20
+ "metadata": {
21
+ "tags": []
22
+ },
23
+ "outputs": [],
24
+ "source": [
25
+ "learn = load_learner('model.pkl')"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "execution_count": 9,
31
+ "id": "5e0ab34a-736e-41ef-b858-d193146cdb0f",
32
+ "metadata": {
33
+ "tags": []
34
+ },
35
+ "outputs": [],
36
+ "source": [
37
+ "categories = ('Cucumber', 'Zucchini')\n",
38
+ "\n",
39
+ "def classify_images(img):\n",
40
+ " class_name, _, probs = learn.predict(img)\n",
41
+ " return dict(zip(categories, map(float, probs)))"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": 10,
47
+ "id": "b0a93fe2-755a-4807-9afb-0f9dd5a2dedd",
48
+ "metadata": {
49
+ "tags": []
50
+ },
51
+ "outputs": [],
52
+ "source": [
53
+ "im = PILImage.create('cucumber2.jpg')"
54
+ ]
55
+ },
56
+ {
57
+ "cell_type": "code",
58
+ "execution_count": 11,
59
+ "id": "78eacbd9-3aa8-4d49-b032-d213541cf19e",
60
+ "metadata": {
61
+ "tags": []
62
+ },
63
+ "outputs": [
64
+ {
65
+ "data": {
66
+ "text/html": [
67
+ "\n",
68
+ "<style>\n",
69
+ " /* Turns off some styling */\n",
70
+ " progress {\n",
71
+ " /* gets rid of default border in Firefox and Opera. */\n",
72
+ " border: none;\n",
73
+ " /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
74
+ " background-size: auto;\n",
75
+ " }\n",
76
+ " progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
77
+ " background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
78
+ " }\n",
79
+ " .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
80
+ " background: #F44336;\n",
81
+ " }\n",
82
+ "</style>\n"
83
+ ],
84
+ "text/plain": [
85
+ "<IPython.core.display.HTML object>"
86
+ ]
87
+ },
88
+ "metadata": {},
89
+ "output_type": "display_data"
90
+ },
91
+ {
92
+ "data": {
93
+ "text/html": [],
94
+ "text/plain": [
95
+ "<IPython.core.display.HTML object>"
96
+ ]
97
+ },
98
+ "metadata": {},
99
+ "output_type": "display_data"
100
+ },
101
+ {
102
+ "data": {
103
+ "text/plain": [
104
+ "{'Cucumber': 0.9630787968635559, 'Zucchini': 0.03692116588354111}"
105
+ ]
106
+ },
107
+ "execution_count": 11,
108
+ "metadata": {},
109
+ "output_type": "execute_result"
110
+ }
111
+ ],
112
+ "source": [
113
+ "classify_images(im)"
114
+ ]
115
+ },
116
+ {
117
+ "cell_type": "code",
118
+ "execution_count": 13,
119
+ "id": "9475a921-2a55-49ac-bd6d-472dee0e6377",
120
+ "metadata": {
121
+ "tags": []
122
+ },
123
+ "outputs": [
124
+ {
125
+ "name": "stderr",
126
+ "output_type": "stream",
127
+ "text": [
128
+ "/home/vadim/mambaforge/envs/fastai_env/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",
129
+ " warnings.warn(\n",
130
+ "/home/vadim/mambaforge/envs/fastai_env/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
131
+ " warnings.warn(value)\n",
132
+ "/home/vadim/mambaforge/envs/fastai_env/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",
133
+ " warnings.warn(\n",
134
+ "/home/vadim/mambaforge/envs/fastai_env/lib/python3.10/site-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",
135
+ " warnings.warn(value)\n"
136
+ ]
137
+ },
138
+ {
139
+ "name": "stdout",
140
+ "output_type": "stream",
141
+ "text": [
142
+ "Running on local URL: http://127.0.0.1:7860\n",
143
+ "\n",
144
+ "To create a public link, set `share=True` in `launch()`.\n"
145
+ ]
146
+ },
147
+ {
148
+ "data": {
149
+ "text/plain": []
150
+ },
151
+ "execution_count": 13,
152
+ "metadata": {},
153
+ "output_type": "execute_result"
154
+ }
155
+ ],
156
+ "source": [
157
+ "image = gr.inputs.Image(shape=(192, 192))\n",
158
+ "label = gr.outputs.Label()\n",
159
+ "examples = ['cucumber2.jpg', 'zucchini2.jpg']\n",
160
+ "\n",
161
+ "intf = gr.Interface(fn=classify_images, inputs=image, outputs=label, examples=examples)\n",
162
+ "intf.launch(inline=False)"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "code",
167
+ "execution_count": null,
168
+ "id": "84125255-99df-4cac-8b47-00cdf6f205a8",
169
+ "metadata": {},
170
+ "outputs": [],
171
+ "source": []
172
+ }
173
+ ],
174
+ "metadata": {
175
+ "kernelspec": {
176
+ "display_name": "Python 3 (ipykernel)",
177
+ "language": "python",
178
+ "name": "python3"
179
+ },
180
+ "language_info": {
181
+ "codemirror_mode": {
182
+ "name": "ipython",
183
+ "version": 3
184
+ },
185
+ "file_extension": ".py",
186
+ "mimetype": "text/x-python",
187
+ "name": "python",
188
+ "nbconvert_exporter": "python",
189
+ "pygments_lexer": "ipython3",
190
+ "version": "3.10.10"
191
+ }
192
+ },
193
+ "nbformat": 4,
194
+ "nbformat_minor": 5
195
+ }
app.py CHANGED
@@ -1,7 +1,16 @@
 
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
+ from fastai.vision.all import *
2
  import gradio as gr
3
 
4
+ learn = load_learner('model.pkl')
5
+ categories = ('Cucumber', 'Zucchini')
6
 
7
+ def classify_images(img):
8
+ class_name, _, probs = learn.predict(img)
9
+ return dict(zip(categories, map(float, probs)))
10
+
11
+ image = gr.inputs.Image(shape=(192, 192))
12
+ label = gr.outputs.Label()
13
+ examples = ['cucumber2.jpg', 'zucchini2.jpg']
14
+
15
+ intf = gr.Interface(fn=classify_images, inputs=image, outputs=label, examples=examples)
16
+ intf.launch(inline=False)
cucumber2.jpg ADDED
zucchini2.jpg ADDED