MichaelT8093 commited on
Commit
c73fb73
1 Parent(s): 3d6e090

gradio tutorial

Browse files
.ipynb_checkpoints/gradio_test-checkpoint.ipynb ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [],
3
+ "metadata": {},
4
+ "nbformat": 4,
5
+ "nbformat_minor": 5
6
+ }
demis.jpg ADDED
flagged/Upload hot dog candidate/tmp92qta5ft.jpg ADDED
flagged/log.csv ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Upload hot dog candidate,output,flag,username,timestamp
2
+ D:\Users\TFTang\python\git\HuggingFace\Playground\flagged\Upload hot dog candidate\tmp92qta5ft.jpg,,,,2023-03-20 22:38:33.481952
3
+ Michael,True,47,Good morning Michael. It is 47 degrees today,8.33,,,2023-03-20 22:41:48.177488
gradio_test.ipynb ADDED
@@ -0,0 +1,958 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "5f371cf3-7bbd-4481-860a-d724655b6a56",
7
+ "metadata": {
8
+ "tags": []
9
+ },
10
+ "outputs": [
11
+ {
12
+ "name": "stdout",
13
+ "output_type": "stream",
14
+ "text": [
15
+ "Running on local URL: http://127.0.0.1:7860\n",
16
+ "\n",
17
+ "To create a public link, set `share=True` in `launch()`.\n"
18
+ ]
19
+ },
20
+ {
21
+ "data": {
22
+ "text/html": [
23
+ "<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>"
24
+ ],
25
+ "text/plain": [
26
+ "<IPython.core.display.HTML object>"
27
+ ]
28
+ },
29
+ "metadata": {},
30
+ "output_type": "display_data"
31
+ },
32
+ {
33
+ "data": {
34
+ "text/plain": []
35
+ },
36
+ "execution_count": 1,
37
+ "metadata": {},
38
+ "output_type": "execute_result"
39
+ }
40
+ ],
41
+ "source": [
42
+ "import gradio as gr\n",
43
+ "\n",
44
+ "def greet(name):\n",
45
+ " return \"Hello \" + name + \"!\"\n",
46
+ "\n",
47
+ "demo = gr.Interface(fn=greet, inputs=\"text\", outputs=\"text\")\n",
48
+ "\n",
49
+ "demo.launch() "
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": 6,
55
+ "id": "84a74ed3-1ba3-4318-9ec2-8fbbae1f80d2",
56
+ "metadata": {
57
+ "tags": []
58
+ },
59
+ "outputs": [
60
+ {
61
+ "name": "stderr",
62
+ "output_type": "stream",
63
+ "text": [
64
+ "D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\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",
65
+ " warnings.warn(\n",
66
+ "D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
67
+ " warnings.warn(value)\n",
68
+ "D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\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",
69
+ " warnings.warn(\n",
70
+ "D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",
71
+ " warnings.warn(value)\n"
72
+ ]
73
+ },
74
+ {
75
+ "name": "stdout",
76
+ "output_type": "stream",
77
+ "text": [
78
+ "Running on local URL: http://127.0.0.1:7862\n",
79
+ "\n",
80
+ "To create a public link, set `share=True` in `launch()`.\n"
81
+ ]
82
+ },
83
+ {
84
+ "data": {
85
+ "text/html": [
86
+ "<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
87
+ ],
88
+ "text/plain": [
89
+ "<IPython.core.display.HTML object>"
90
+ ]
91
+ },
92
+ "metadata": {},
93
+ "output_type": "display_data"
94
+ },
95
+ {
96
+ "data": {
97
+ "text/plain": []
98
+ },
99
+ "execution_count": 6,
100
+ "metadata": {},
101
+ "output_type": "execute_result"
102
+ },
103
+ {
104
+ "name": "stderr",
105
+ "output_type": "stream",
106
+ "text": [
107
+ "Traceback (most recent call last):\n",
108
+ " File \"D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\routes.py\", line 384, in run_predict\n",
109
+ " output = await app.get_blocks().process_api(\n",
110
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
111
+ " File \"D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\blocks.py\", line 1032, in process_api\n",
112
+ " result = await self.call_function(\n",
113
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
114
+ " File \"D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\blocks.py\", line 844, in call_function\n",
115
+ " prediction = await anyio.to_thread.run_sync(\n",
116
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
117
+ " File \"D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\anyio\\to_thread.py\", line 31, in run_sync\n",
118
+ " return await get_asynclib().run_sync_in_worker_thread(\n",
119
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
120
+ " File \"D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 937, in run_sync_in_worker_thread\n",
121
+ " return await future\n",
122
+ " ^^^^^^^^^^^^\n",
123
+ " File \"D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 867, in run\n",
124
+ " result = context.run(func, *args)\n",
125
+ " ^^^^^^^^^^^^^^^^^^^^^^^^\n",
126
+ " File \"C:\\Users\\Celia\\AppData\\Local\\Temp\\ipykernel_11896\\3792782900.py\", line 10, in predict\n",
127
+ " predictions = pipeline(image)\n",
128
+ " ^^^^^^^^^^^^^^^\n",
129
+ " File \"D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\transformers\\pipelines\\__init__.py\", line 732, in pipeline\n",
130
+ " normalized_task, targeted_task, task_options = check_task(task)\n",
131
+ " ^^^^^^^^^^^^^^^^\n",
132
+ " File \"D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\transformers\\pipelines\\__init__.py\", line 475, in check_task\n",
133
+ " return PIPELINE_REGISTRY.check_task(task)\n",
134
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
135
+ " File \"D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\transformers\\pipelines\\base.py\", line 1184, in check_task\n",
136
+ " raise KeyError(\n",
137
+ "KeyError: \"Unknown task C:\\\\Users\\\\Celia\\\\AppData\\\\Local\\\\Temp\\\\tmp_g2css34.png, available tasks are ['audio-classification', 'automatic-speech-recognition', 'conversational', 'depth-estimation', 'document-question-answering', 'feature-extraction', 'fill-mask', 'image-classification', 'image-segmentation', 'image-to-text', 'ner', 'object-detection', 'question-answering', 'sentiment-analysis', 'summarization', 'table-question-answering', 'text-classification', 'text-generation', 'text2text-generation', 'token-classification', 'translation', 'video-classification', 'visual-question-answering', 'vqa', 'zero-shot-audio-classification', 'zero-shot-classification', 'zero-shot-image-classification', 'zero-shot-object-detection', 'translation_XX_to_YY']\"\n"
138
+ ]
139
+ }
140
+ ],
141
+ "source": [
142
+ "\"\"\"\n",
143
+ " This is the hotdog demo\n",
144
+ "\"\"\"\n",
145
+ "import gradio as gr\n",
146
+ "from transformers import pipeline\n",
147
+ "\n",
148
+ "# pipeline = pipeline(task=\"image-classification\", model=\"https://huggingface.co/julien-c/hotdog-not-hotdog\")\n",
149
+ "\n",
150
+ "def predict(image):\n",
151
+ " predictions = pipeline(image)\n",
152
+ " return {p[\"label\"]: p[\"score\"] for p in predictions}\n",
153
+ "\n",
154
+ "gr.Interface(\n",
155
+ " predict,\n",
156
+ " inputs=gr.inputs.Image(label=\"Upload hot dog candidate\", type=\"filepath\"),\n",
157
+ " outputs=gr.outputs.Label(num_top_classes=2),\n",
158
+ " title=\"Hot Dog? Or Not?\",\n",
159
+ ").launch()"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "execution_count": 7,
165
+ "id": "0e05cfae-7f84-432b-a2e4-40d1f4f1bac3",
166
+ "metadata": {
167
+ "tags": []
168
+ },
169
+ "outputs": [
170
+ {
171
+ "name": "stdout",
172
+ "output_type": "stream",
173
+ "text": [
174
+ "Running on local URL: http://127.0.0.1:7863\n",
175
+ "\n",
176
+ "To create a public link, set `share=True` in `launch()`.\n"
177
+ ]
178
+ },
179
+ {
180
+ "data": {
181
+ "text/html": [
182
+ "<div><iframe src=\"http://127.0.0.1:7863/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
183
+ ],
184
+ "text/plain": [
185
+ "<IPython.core.display.HTML object>"
186
+ ]
187
+ },
188
+ "metadata": {},
189
+ "output_type": "display_data"
190
+ },
191
+ {
192
+ "data": {
193
+ "text/plain": []
194
+ },
195
+ "execution_count": 7,
196
+ "metadata": {},
197
+ "output_type": "execute_result"
198
+ }
199
+ ],
200
+ "source": [
201
+ "import gradio as gr\n",
202
+ "\n",
203
+ "def greet(name, is_morning, temperature):\n",
204
+ " salutation = \"Good morning\" if is_morning else \"Good evening\"\n",
205
+ " greeting = f\"{salutation} {name}. It is {temperature} degrees today\"\n",
206
+ " celsius = (temperature - 32) * 5 / 9\n",
207
+ " return greeting, round(celsius, 2)\n",
208
+ "\n",
209
+ "demo = gr.Interface(\n",
210
+ " fn=greet,\n",
211
+ " inputs=[\"text\", \"checkbox\", gr.Slider(0, 100)],\n",
212
+ " outputs=[\"text\", \"number\"],\n",
213
+ ")\n",
214
+ "demo.launch()"
215
+ ]
216
+ },
217
+ {
218
+ "cell_type": "code",
219
+ "execution_count": 8,
220
+ "id": "aa1260e1-782b-49b7-b308-e88c95794e7b",
221
+ "metadata": {
222
+ "tags": []
223
+ },
224
+ "outputs": [
225
+ {
226
+ "name": "stdout",
227
+ "output_type": "stream",
228
+ "text": [
229
+ "Running on local URL: http://127.0.0.1:7864\n",
230
+ "\n",
231
+ "To create a public link, set `share=True` in `launch()`.\n"
232
+ ]
233
+ },
234
+ {
235
+ "data": {
236
+ "text/html": [
237
+ "<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
238
+ ],
239
+ "text/plain": [
240
+ "<IPython.core.display.HTML object>"
241
+ ]
242
+ },
243
+ "metadata": {},
244
+ "output_type": "display_data"
245
+ },
246
+ {
247
+ "data": {
248
+ "text/plain": []
249
+ },
250
+ "execution_count": 8,
251
+ "metadata": {},
252
+ "output_type": "execute_result"
253
+ }
254
+ ],
255
+ "source": [
256
+ "import numpy as np\n",
257
+ "import gradio as gr\n",
258
+ "\n",
259
+ "def sepia(input_img):\n",
260
+ " sepia_filter = np.array([\n",
261
+ " [0.393, 0.769, 0.189], \n",
262
+ " [0.349, 0.686, 0.168], \n",
263
+ " [0.272, 0.534, 0.131]\n",
264
+ " ])\n",
265
+ " sepia_img = input_img.dot(sepia_filter.T)\n",
266
+ " sepia_img /= sepia_img.max()\n",
267
+ " return sepia_img\n",
268
+ "\n",
269
+ "demo = gr.Interface(sepia, gr.Image(shape=(200, 200)), \"image\")\n",
270
+ "demo.launch()\n"
271
+ ]
272
+ },
273
+ {
274
+ "cell_type": "code",
275
+ "execution_count": 9,
276
+ "id": "8625b6fc-4cf4-46f2-b0c7-9024df60e03f",
277
+ "metadata": {
278
+ "tags": []
279
+ },
280
+ "outputs": [
281
+ {
282
+ "name": "stdout",
283
+ "output_type": "stream",
284
+ "text": [
285
+ "Running on local URL: http://127.0.0.1:7865\n",
286
+ "\n",
287
+ "To create a public link, set `share=True` in `launch()`.\n"
288
+ ]
289
+ },
290
+ {
291
+ "data": {
292
+ "text/html": [
293
+ "<div><iframe src=\"http://127.0.0.1:7865/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
294
+ ],
295
+ "text/plain": [
296
+ "<IPython.core.display.HTML object>"
297
+ ]
298
+ },
299
+ "metadata": {},
300
+ "output_type": "display_data"
301
+ },
302
+ {
303
+ "data": {
304
+ "text/plain": []
305
+ },
306
+ "execution_count": 9,
307
+ "metadata": {},
308
+ "output_type": "execute_result"
309
+ }
310
+ ],
311
+ "source": [
312
+ "import gradio as gr\n",
313
+ "\n",
314
+ "def greet(name):\n",
315
+ " return \"Hello \" + name + \"!\"\n",
316
+ "\n",
317
+ "with gr.Blocks() as demo:\n",
318
+ " name = gr.Textbox(label=\"Name\")\n",
319
+ " output = gr.Textbox(label=\"Output Box\")\n",
320
+ " greet_btn = gr.Button(\"Greet\")\n",
321
+ " greet_btn.click(fn=greet, inputs=name, outputs=output)\n",
322
+ "\n",
323
+ "demo.launch()"
324
+ ]
325
+ },
326
+ {
327
+ "cell_type": "code",
328
+ "execution_count": 10,
329
+ "id": "942fc790-2317-405f-919f-4d0ee0187d5f",
330
+ "metadata": {
331
+ "tags": []
332
+ },
333
+ "outputs": [
334
+ {
335
+ "name": "stdout",
336
+ "output_type": "stream",
337
+ "text": [
338
+ "Running on local URL: http://127.0.0.1:7866\n",
339
+ "\n",
340
+ "To create a public link, set `share=True` in `launch()`.\n"
341
+ ]
342
+ },
343
+ {
344
+ "data": {
345
+ "text/html": [
346
+ "<div><iframe src=\"http://127.0.0.1:7866/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
347
+ ],
348
+ "text/plain": [
349
+ "<IPython.core.display.HTML object>"
350
+ ]
351
+ },
352
+ "metadata": {},
353
+ "output_type": "display_data"
354
+ },
355
+ {
356
+ "data": {
357
+ "text/plain": []
358
+ },
359
+ "execution_count": 10,
360
+ "metadata": {},
361
+ "output_type": "execute_result"
362
+ }
363
+ ],
364
+ "source": [
365
+ "import numpy as np\n",
366
+ "import gradio as gr\n",
367
+ "\n",
368
+ "\n",
369
+ "def flip_text(x):\n",
370
+ " return x[::-1]\n",
371
+ "\n",
372
+ "\n",
373
+ "def flip_image(x):\n",
374
+ " return np.fliplr(x)\n",
375
+ "\n",
376
+ "\n",
377
+ "with gr.Blocks() as demo:\n",
378
+ " gr.Markdown(\"Flip text or image files using this demo.\")\n",
379
+ " with gr.Tab(\"Flip Text\"):\n",
380
+ " text_input = gr.Textbox()\n",
381
+ " text_output = gr.Textbox()\n",
382
+ " text_button = gr.Button(\"Flip\")\n",
383
+ " with gr.Tab(\"Flip Image\"):\n",
384
+ " with gr.Row():\n",
385
+ " image_input = gr.Image()\n",
386
+ " image_output = gr.Image()\n",
387
+ " image_button = gr.Button(\"Flip\")\n",
388
+ "\n",
389
+ " with gr.Accordion(\"Open for More!\"):\n",
390
+ " gr.Markdown(\"Look at me...\")\n",
391
+ "\n",
392
+ " text_button.click(flip_text, inputs=text_input, outputs=text_output)\n",
393
+ " image_button.click(flip_image, inputs=image_input, outputs=image_output)\n",
394
+ "\n",
395
+ "demo.launch()\n"
396
+ ]
397
+ },
398
+ {
399
+ "cell_type": "code",
400
+ "execution_count": 11,
401
+ "id": "64f4dcd9-7d89-475b-94c8-026d07118887",
402
+ "metadata": {
403
+ "tags": []
404
+ },
405
+ "outputs": [
406
+ {
407
+ "name": "stdout",
408
+ "output_type": "stream",
409
+ "text": [
410
+ "Running on local URL: http://127.0.0.1:7867\n",
411
+ "\n",
412
+ "To create a public link, set `share=True` in `launch()`.\n"
413
+ ]
414
+ },
415
+ {
416
+ "data": {
417
+ "text/html": [
418
+ "<div><iframe src=\"http://127.0.0.1:7867/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
419
+ ],
420
+ "text/plain": [
421
+ "<IPython.core.display.HTML object>"
422
+ ]
423
+ },
424
+ "metadata": {},
425
+ "output_type": "display_data"
426
+ },
427
+ {
428
+ "data": {
429
+ "text/plain": []
430
+ },
431
+ "execution_count": 11,
432
+ "metadata": {},
433
+ "output_type": "execute_result"
434
+ }
435
+ ],
436
+ "source": [
437
+ "import gradio as gr\n",
438
+ "\n",
439
+ "def calculator(num1, operation, num2):\n",
440
+ " if operation == \"add\":\n",
441
+ " return num1 + num2\n",
442
+ " elif operation == \"subtract\":\n",
443
+ " return num1 - num2\n",
444
+ " elif operation == \"multiply\":\n",
445
+ " return num1 * num2\n",
446
+ " elif operation == \"divide\":\n",
447
+ " if num2 == 0:\n",
448
+ " raise gr.Error(\"Cannot divide by zero!\")\n",
449
+ " return num1 / num2\n",
450
+ "\n",
451
+ "demo = gr.Interface(\n",
452
+ " calculator,\n",
453
+ " [\n",
454
+ " \"number\", \n",
455
+ " gr.Radio([\"add\", \"subtract\", \"multiply\", \"divide\"]),\n",
456
+ " \"number\"\n",
457
+ " ],\n",
458
+ " \"number\",\n",
459
+ " examples=[\n",
460
+ " [5, \"add\", 3],\n",
461
+ " [4, \"divide\", 2],\n",
462
+ " [-4, \"multiply\", 2.5],\n",
463
+ " [0, \"subtract\", 1.2],\n",
464
+ " ],\n",
465
+ " title=\"Toy Calculator\",\n",
466
+ " description=\"Here's a sample toy calculator. Allows you to calculate things like $2+2=4$\",\n",
467
+ ")\n",
468
+ "demo.launch()\n"
469
+ ]
470
+ },
471
+ {
472
+ "cell_type": "code",
473
+ "execution_count": 20,
474
+ "id": "f81f7d2d-8734-4ced-b90d-9bf20c820a26",
475
+ "metadata": {
476
+ "tags": []
477
+ },
478
+ "outputs": [
479
+ {
480
+ "name": "stdout",
481
+ "output_type": "stream",
482
+ "text": [
483
+ "Fetching Space from: https://huggingface.co/spaces/eugenesiow/remove-bg\n"
484
+ ]
485
+ },
486
+ {
487
+ "name": "stderr",
488
+ "output_type": "stream",
489
+ "text": [
490
+ "D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",
491
+ " warnings.warn(value)\n",
492
+ "D:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\interface.py:312: UserWarning: Currently, only the 'default' theme is supported.\n",
493
+ " warnings.warn(\"Currently, only the 'default' theme is supported.\")\n"
494
+ ]
495
+ },
496
+ {
497
+ "name": "stdout",
498
+ "output_type": "stream",
499
+ "text": [
500
+ "Running on local URL: http://127.0.0.1:7873\n",
501
+ "\n",
502
+ "To create a public link, set `share=True` in `launch()`.\n"
503
+ ]
504
+ },
505
+ {
506
+ "data": {
507
+ "text/html": [
508
+ "<div><iframe src=\"http://127.0.0.1:7873/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
509
+ ],
510
+ "text/plain": [
511
+ "<IPython.core.display.HTML object>"
512
+ ]
513
+ },
514
+ "metadata": {},
515
+ "output_type": "display_data"
516
+ },
517
+ {
518
+ "data": {
519
+ "text/plain": []
520
+ },
521
+ "execution_count": 20,
522
+ "metadata": {},
523
+ "output_type": "execute_result"
524
+ }
525
+ ],
526
+ "source": [
527
+ "import gradio as gr\n",
528
+ "\n",
529
+ "gr.Interface.load(\n",
530
+ " \"spaces/eugenesiow/remove-bg\", \n",
531
+ " inputs=[gr.Image(label=\"Input Image\", source=\"webcam\")]\n",
532
+ ").launch()\n"
533
+ ]
534
+ },
535
+ {
536
+ "cell_type": "code",
537
+ "execution_count": 15,
538
+ "id": "76cab1ff-0910-42b9-9f53-221559502e1d",
539
+ "metadata": {
540
+ "tags": []
541
+ },
542
+ "outputs": [
543
+ {
544
+ "name": "stdout",
545
+ "output_type": "stream",
546
+ "text": [
547
+ "Running on local URL: http://127.0.0.1:7870\n",
548
+ "\n",
549
+ "To create a public link, set `share=True` in `launch()`.\n"
550
+ ]
551
+ },
552
+ {
553
+ "data": {
554
+ "text/html": [
555
+ "<div><iframe src=\"http://127.0.0.1:7870/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
556
+ ],
557
+ "text/plain": [
558
+ "<IPython.core.display.HTML object>"
559
+ ]
560
+ },
561
+ "metadata": {},
562
+ "output_type": "display_data"
563
+ },
564
+ {
565
+ "data": {
566
+ "text/plain": []
567
+ },
568
+ "execution_count": 15,
569
+ "metadata": {},
570
+ "output_type": "execute_result"
571
+ }
572
+ ],
573
+ "source": [
574
+ "import gradio as gr\n",
575
+ "import numpy as np\n",
576
+ "import time\n",
577
+ "\n",
578
+ "# define core fn, which returns a generator {steps} times before returning the image\n",
579
+ "def fake_diffusion(steps):\n",
580
+ " for _ in range(steps):\n",
581
+ " time.sleep(0.1)\n",
582
+ " image = np.random.random((600, 600, 3))\n",
583
+ " yield image\n",
584
+ " image = \"https://gradio-builds.s3.amazonaws.com/diffusion_image/cute_dog.jpg\"\n",
585
+ " yield image\n",
586
+ "\n",
587
+ "\n",
588
+ "demo = gr.Interface(fake_diffusion, inputs=gr.Slider(1, 10, 3), outputs=\"image\")\n",
589
+ "\n",
590
+ "# define queue - required for generators\n",
591
+ "demo.queue()\n",
592
+ "\n",
593
+ "demo.launch()\n"
594
+ ]
595
+ },
596
+ {
597
+ "cell_type": "code",
598
+ "execution_count": 16,
599
+ "id": "6b27bd4a-cdbd-4804-b61d-8a84c0f24de2",
600
+ "metadata": {
601
+ "tags": []
602
+ },
603
+ "outputs": [
604
+ {
605
+ "name": "stdout",
606
+ "output_type": "stream",
607
+ "text": [
608
+ "Running on local URL: http://127.0.0.1:7871\n",
609
+ "\n",
610
+ "To create a public link, set `share=True` in `launch()`.\n"
611
+ ]
612
+ },
613
+ {
614
+ "data": {
615
+ "text/html": [
616
+ "<div><iframe src=\"http://127.0.0.1:7871/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
617
+ ],
618
+ "text/plain": [
619
+ "<IPython.core.display.HTML object>"
620
+ ]
621
+ },
622
+ "metadata": {},
623
+ "output_type": "display_data"
624
+ }
625
+ ],
626
+ "source": [
627
+ "import gradio as gr\n",
628
+ "import time\n",
629
+ "\n",
630
+ "def slowly_reverse(word, progress=gr.Progress()):\n",
631
+ " progress(0, desc=\"Starting\")\n",
632
+ " time.sleep(1)\n",
633
+ " progress(0.05)\n",
634
+ " new_string = \"\"\n",
635
+ " for letter in progress.tqdm(word, desc=\"Reversing\"):\n",
636
+ " time.sleep(0.25)\n",
637
+ " new_string = letter + new_string\n",
638
+ " return new_string\n",
639
+ "\n",
640
+ "demo = gr.Interface(slowly_reverse, gr.Text(), gr.Text())\n",
641
+ "\n",
642
+ "if __name__ == \"__main__\":\n",
643
+ " demo.queue(concurrency_count=10).launch()\n"
644
+ ]
645
+ },
646
+ {
647
+ "cell_type": "code",
648
+ "execution_count": 17,
649
+ "id": "211aaacb-eeb4-4cfe-9c9d-e9571ab799eb",
650
+ "metadata": {
651
+ "tags": []
652
+ },
653
+ "outputs": [
654
+ {
655
+ "ename": "ValueError",
656
+ "evalue": "No such component or layout: output",
657
+ "output_type": "error",
658
+ "traceback": [
659
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
660
+ "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
661
+ "Cell \u001b[1;32mIn[17], line 10\u001b[0m\n\u001b[0;32m 7\u001b[0m trimmed_words\u001b[38;5;241m.\u001b[39mappend(w[:\u001b[38;5;28mint\u001b[39m(l)]) \n\u001b[0;32m 8\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m [trimmed_words]\n\u001b[1;32m---> 10\u001b[0m demo \u001b[38;5;241m=\u001b[39m \u001b[43mgr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mInterface\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrim_words\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtextbox\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mnumber\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43moutput\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[0;32m 11\u001b[0m \u001b[43m \u001b[49m\u001b[43mbatch\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_batch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m16\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 12\u001b[0m demo\u001b[38;5;241m.\u001b[39mqueue()\n\u001b[0;32m 13\u001b[0m demo\u001b[38;5;241m.\u001b[39mlaunch()\n",
662
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\interface.py:246\u001b[0m, in \u001b[0;36mInterface.__init__\u001b[1;34m(self, fn, inputs, outputs, examples, cache_examples, examples_per_page, live, interpretation, num_shap, title, description, article, thumbnail, theme, css, allow_flagging, flagging_options, flagging_dir, flagging_callback, analytics_enabled, batch, max_batch_size, _api_mode, **kwargs)\u001b[0m\n\u001b[0;32m 241\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcache_examples \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m 243\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minput_components \u001b[38;5;241m=\u001b[39m [\n\u001b[0;32m 244\u001b[0m get_component_instance(i, render\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m) \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m inputs\n\u001b[0;32m 245\u001b[0m ]\n\u001b[1;32m--> 246\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_components \u001b[38;5;241m=\u001b[39m \u001b[43m[\u001b[49m\n\u001b[0;32m 247\u001b[0m \u001b[43m \u001b[49m\u001b[43mget_component_instance\u001b[49m\u001b[43m(\u001b[49m\u001b[43mo\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrender\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mo\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43moutputs\u001b[49m\n\u001b[0;32m 248\u001b[0m \u001b[43m\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m 250\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m component \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minput_components \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_components:\n\u001b[0;32m 251\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28misinstance\u001b[39m(component, IOComponent)):\n",
663
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\interface.py:247\u001b[0m, in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m 241\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcache_examples \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m 243\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minput_components \u001b[38;5;241m=\u001b[39m [\n\u001b[0;32m 244\u001b[0m get_component_instance(i, render\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m) \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m inputs\n\u001b[0;32m 245\u001b[0m ]\n\u001b[0;32m 246\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_components \u001b[38;5;241m=\u001b[39m [\n\u001b[1;32m--> 247\u001b[0m \u001b[43mget_component_instance\u001b[49m\u001b[43m(\u001b[49m\u001b[43mo\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrender\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m \u001b[38;5;28;01mfor\u001b[39;00m o \u001b[38;5;129;01min\u001b[39;00m outputs\n\u001b[0;32m 248\u001b[0m ]\n\u001b[0;32m 250\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m component \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minput_components \u001b[38;5;241m+\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutput_components:\n\u001b[0;32m 251\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28misinstance\u001b[39m(component, IOComponent)):\n",
664
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\components.py:5497\u001b[0m, in \u001b[0;36mget_component_instance\u001b[1;34m(comp, render)\u001b[0m\n\u001b[0;32m 5495\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_component_instance\u001b[39m(comp: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m|\u001b[39m \u001b[38;5;28mdict\u001b[39m \u001b[38;5;241m|\u001b[39m Component, render\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Component:\n\u001b[0;32m 5496\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(comp, \u001b[38;5;28mstr\u001b[39m):\n\u001b[1;32m-> 5497\u001b[0m component_obj \u001b[38;5;241m=\u001b[39m \u001b[43mcomponent\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcomp\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 5498\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (render):\n\u001b[0;32m 5499\u001b[0m component_obj\u001b[38;5;241m.\u001b[39munrender()\n",
665
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\components.py:5489\u001b[0m, in \u001b[0;36mcomponent\u001b[1;34m(cls_name)\u001b[0m\n\u001b[0;32m 5488\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcomponent\u001b[39m(cls_name: \u001b[38;5;28mstr\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Component:\n\u001b[1;32m-> 5489\u001b[0m obj \u001b[38;5;241m=\u001b[39m \u001b[43mutils\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcomponent_or_layout_class\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcls_name\u001b[49m\u001b[43m)\u001b[49m()\n\u001b[0;32m 5490\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(obj, BlockContext):\n\u001b[0;32m 5491\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid component: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mobj\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
666
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\gradio\\utils.py:397\u001b[0m, in \u001b[0;36mcomponent_or_layout_class\u001b[1;34m(cls_name)\u001b[0m\n\u001b[0;32m 392\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m name\u001b[38;5;241m.\u001b[39mlower() \u001b[38;5;241m==\u001b[39m cls_name\u001b[38;5;241m.\u001b[39mreplace(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m (\n\u001b[0;32m 393\u001b[0m \u001b[38;5;28missubclass\u001b[39m(\u001b[38;5;28mcls\u001b[39m, gradio\u001b[38;5;241m.\u001b[39mcomponents\u001b[38;5;241m.\u001b[39mComponent)\n\u001b[0;32m 394\u001b[0m \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28missubclass\u001b[39m(\u001b[38;5;28mcls\u001b[39m, gradio\u001b[38;5;241m.\u001b[39mblocks\u001b[38;5;241m.\u001b[39mBlockContext)\n\u001b[0;32m 395\u001b[0m ):\n\u001b[0;32m 396\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\n\u001b[1;32m--> 397\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo such component or layout: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mcls_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
667
+ "\u001b[1;31mValueError\u001b[0m: No such component or layout: output"
668
+ ]
669
+ }
670
+ ],
671
+ "source": [
672
+ "import time\n",
673
+ "\n",
674
+ "def trim_words(words, lens):\n",
675
+ " trimmed_words = []\n",
676
+ " time.sleep(5)\n",
677
+ " for w, l in zip(words, lens):\n",
678
+ " trimmed_words.append(w[:int(l)]) \n",
679
+ " return [trimmed_words]\n",
680
+ "\n",
681
+ "demo = gr.Interface(trim_words, [\"textbox\", \"number\"], [\"output\"], \n",
682
+ " batch=True, max_batch_size=16)\n",
683
+ "demo.queue()\n",
684
+ "demo.launch()\n"
685
+ ]
686
+ },
687
+ {
688
+ "cell_type": "code",
689
+ "execution_count": 18,
690
+ "id": "59789932-c78c-4c40-809f-3abbd0d2f239",
691
+ "metadata": {
692
+ "tags": []
693
+ },
694
+ "outputs": [
695
+ {
696
+ "name": "stdout",
697
+ "output_type": "stream",
698
+ "text": [
699
+ "Running on local URL: http://127.0.0.1:7872\n",
700
+ "\n",
701
+ "To create a public link, set `share=True` in `launch()`.\n"
702
+ ]
703
+ },
704
+ {
705
+ "data": {
706
+ "text/html": [
707
+ "<div><iframe src=\"http://127.0.0.1:7872/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
708
+ ],
709
+ "text/plain": [
710
+ "<IPython.core.display.HTML object>"
711
+ ]
712
+ },
713
+ "metadata": {},
714
+ "output_type": "display_data"
715
+ },
716
+ {
717
+ "data": {
718
+ "text/plain": []
719
+ },
720
+ "execution_count": 18,
721
+ "metadata": {},
722
+ "output_type": "execute_result"
723
+ }
724
+ ],
725
+ "source": [
726
+ "import gradio as gr\n",
727
+ "\n",
728
+ "with gr.Blocks() as demo:\n",
729
+ " with gr.Row():\n",
730
+ " word = gr.Textbox(label=\"word\")\n",
731
+ " leng = gr.Number(label=\"leng\")\n",
732
+ " output = gr.Textbox(label=\"Output\")\n",
733
+ " with gr.Row():\n",
734
+ " run = gr.Button()\n",
735
+ "\n",
736
+ " event = run.click(trim_words, [word, leng], output, batch=True, max_batch_size=16)\n",
737
+ "\n",
738
+ "demo.queue()\n",
739
+ "demo.launch()\n"
740
+ ]
741
+ },
742
+ {
743
+ "cell_type": "code",
744
+ "execution_count": 21,
745
+ "id": "4ec15938-e862-4d45-966b-90bc6172c129",
746
+ "metadata": {
747
+ "tags": []
748
+ },
749
+ "outputs": [
750
+ {
751
+ "name": "stdout",
752
+ "output_type": "stream",
753
+ "text": [
754
+ "Running on local URL: http://127.0.0.1:7874\n",
755
+ "\n",
756
+ "To create a public link, set `share=True` in `launch()`.\n"
757
+ ]
758
+ },
759
+ {
760
+ "data": {
761
+ "text/html": [
762
+ "<div><iframe src=\"http://127.0.0.1:7874/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
763
+ ],
764
+ "text/plain": [
765
+ "<IPython.core.display.HTML object>"
766
+ ]
767
+ },
768
+ "metadata": {},
769
+ "output_type": "display_data"
770
+ },
771
+ {
772
+ "name": "stdout",
773
+ "output_type": "stream",
774
+ "text": [
775
+ "Request headers dictionary: Headers({'host': '127.0.0.1:7874', 'connection': 'keep-alive', 'content-length': '59', 'sec-ch-ua': '\"Google Chrome\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"', 'sec-ch-ua-platform': '\"Windows\"', 'dnt': '1', 'sec-ch-ua-mobile': '?0', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'content-type': 'application/json', 'accept': '*/*', 'origin': 'http://127.0.0.1:7874', 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'empty', 'referer': 'http://127.0.0.1:7874/', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh-TW;q=0.7,zh;q=0.6,ms;q=0.5,id;q=0.4'})\n",
776
+ "IP address: 127.0.0.1\n"
777
+ ]
778
+ }
779
+ ],
780
+ "source": [
781
+ "import gradio as gr\n",
782
+ "\n",
783
+ "def echo(name, request: gr.Request):\n",
784
+ " if request:\n",
785
+ " print(\"Request headers dictionary:\", request.headers)\n",
786
+ " print(\"IP address:\", request.client.host)\n",
787
+ " return name\n",
788
+ "\n",
789
+ "io = gr.Interface(echo, \"textbox\", \"textbox\").launch()"
790
+ ]
791
+ },
792
+ {
793
+ "cell_type": "code",
794
+ "execution_count": 23,
795
+ "id": "d1846aa3-cce8-4edd-b06d-91deeaead793",
796
+ "metadata": {
797
+ "tags": []
798
+ },
799
+ "outputs": [
800
+ {
801
+ "name": "stderr",
802
+ "output_type": "stream",
803
+ "text": [
804
+ "100%|█████████████████████████████████████████████████████████████████��███████████| 29.0k/29.0k [00:00<00:00, 5.07MB/s]\n"
805
+ ]
806
+ },
807
+ {
808
+ "ename": "URLError",
809
+ "evalue": "<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)>",
810
+ "output_type": "error",
811
+ "traceback": [
812
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
813
+ "\u001b[1;31mSSLCertVerificationError\u001b[0m Traceback (most recent call last)",
814
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\urllib\\request.py:1348\u001b[0m, in \u001b[0;36mAbstractHTTPHandler.do_open\u001b[1;34m(self, http_class, req, **http_conn_args)\u001b[0m\n\u001b[0;32m 1347\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 1348\u001b[0m \u001b[43mh\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_method\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mreq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mselector\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mreq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 1349\u001b[0m \u001b[43m \u001b[49m\u001b[43mencode_chunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mreq\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhas_header\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mTransfer-encoding\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1350\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err: \u001b[38;5;66;03m# timeout error\u001b[39;00m\n",
815
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\http\\client.py:1282\u001b[0m, in \u001b[0;36mHTTPConnection.request\u001b[1;34m(self, method, url, body, headers, encode_chunked)\u001b[0m\n\u001b[0;32m 1281\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Send a complete request to the server.\"\"\"\u001b[39;00m\n\u001b[1;32m-> 1282\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmethod\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencode_chunked\u001b[49m\u001b[43m)\u001b[49m\n",
816
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\http\\client.py:1328\u001b[0m, in \u001b[0;36mHTTPConnection._send_request\u001b[1;34m(self, method, url, body, headers, encode_chunked)\u001b[0m\n\u001b[0;32m 1327\u001b[0m body \u001b[38;5;241m=\u001b[39m _encode(body, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mbody\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m-> 1328\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mendheaders\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbody\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencode_chunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mencode_chunked\u001b[49m\u001b[43m)\u001b[49m\n",
817
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\http\\client.py:1277\u001b[0m, in \u001b[0;36mHTTPConnection.endheaders\u001b[1;34m(self, message_body, encode_chunked)\u001b[0m\n\u001b[0;32m 1276\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CannotSendHeader()\n\u001b[1;32m-> 1277\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_output\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmessage_body\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencode_chunked\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mencode_chunked\u001b[49m\u001b[43m)\u001b[49m\n",
818
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\http\\client.py:1037\u001b[0m, in \u001b[0;36mHTTPConnection._send_output\u001b[1;34m(self, message_body, encode_chunked)\u001b[0m\n\u001b[0;32m 1036\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_buffer[:]\n\u001b[1;32m-> 1037\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmsg\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1039\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m message_body \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 1040\u001b[0m \n\u001b[0;32m 1041\u001b[0m \u001b[38;5;66;03m# create a consistent interface to message_body\u001b[39;00m\n",
819
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\http\\client.py:975\u001b[0m, in \u001b[0;36mHTTPConnection.send\u001b[1;34m(self, data)\u001b[0m\n\u001b[0;32m 974\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mauto_open:\n\u001b[1;32m--> 975\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnect\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 976\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
820
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\http\\client.py:1454\u001b[0m, in \u001b[0;36mHTTPSConnection.connect\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1452\u001b[0m server_hostname \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhost\n\u001b[1;32m-> 1454\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msock \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_context\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwrap_socket\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msock\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 1455\u001b[0m \u001b[43m \u001b[49m\u001b[43mserver_hostname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mserver_hostname\u001b[49m\u001b[43m)\u001b[49m\n",
821
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\ssl.py:517\u001b[0m, in \u001b[0;36mSSLContext.wrap_socket\u001b[1;34m(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session)\u001b[0m\n\u001b[0;32m 511\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mwrap_socket\u001b[39m(\u001b[38;5;28mself\u001b[39m, sock, server_side\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m 512\u001b[0m do_handshake_on_connect\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m 513\u001b[0m suppress_ragged_eofs\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m 514\u001b[0m server_hostname\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, session\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m 515\u001b[0m \u001b[38;5;66;03m# SSLSocket class handles server_hostname encoding before it calls\u001b[39;00m\n\u001b[0;32m 516\u001b[0m \u001b[38;5;66;03m# ctx._wrap_socket()\u001b[39;00m\n\u001b[1;32m--> 517\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msslsocket_class\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_create\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 518\u001b[0m \u001b[43m \u001b[49m\u001b[43msock\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msock\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 519\u001b[0m \u001b[43m \u001b[49m\u001b[43mserver_side\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mserver_side\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 520\u001b[0m \u001b[43m \u001b[49m\u001b[43mdo_handshake_on_connect\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdo_handshake_on_connect\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 521\u001b[0m \u001b[43m \u001b[49m\u001b[43msuppress_ragged_eofs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msuppress_ragged_eofs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 522\u001b[0m \u001b[43m \u001b[49m\u001b[43mserver_hostname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mserver_hostname\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 523\u001b[0m \u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 524\u001b[0m \u001b[43m \u001b[49m\u001b[43msession\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msession\u001b[49m\n\u001b[0;32m 525\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
822
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\ssl.py:1075\u001b[0m, in \u001b[0;36mSSLSocket._create\u001b[1;34m(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session)\u001b[0m\n\u001b[0;32m 1074\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdo_handshake_on_connect should not be specified for non-blocking sockets\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m-> 1075\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdo_handshake\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1076\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mOSError\u001b[39;00m, \u001b[38;5;167;01mValueError\u001b[39;00m):\n",
823
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\ssl.py:1346\u001b[0m, in \u001b[0;36mSSLSocket.do_handshake\u001b[1;34m(self, block)\u001b[0m\n\u001b[0;32m 1345\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msettimeout(\u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m-> 1346\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_sslobj\u001b[38;5;241m.\u001b[39mdo_handshake()\n\u001b[0;32m 1347\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n",
824
+ "\u001b[1;31mSSLCertVerificationError\u001b[0m: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)",
825
+ "\nDuring handling of the above exception, another exception occurred:\n",
826
+ "\u001b[1;31mURLError\u001b[0m Traceback (most recent call last)",
827
+ "Cell \u001b[1;32mIn[23], line 64\u001b[0m\n\u001b[0;32m 59\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m foreground\n\u001b[0;32m 62\u001b[0m torch\u001b[38;5;241m.\u001b[39mhub\u001b[38;5;241m.\u001b[39mdownload_url_to_file(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttps://pbs.twimg.com/profile_images/691700243809718272/z7XZUARB_400x400.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m 63\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdemis.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m---> 64\u001b[0m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhub\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdownload_url_to_file\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mhttps://hai.stanford.edu/sites/default/files/styles/person_medium/public/2020-03/hai_1512feifei.png?itok=INFuLABp\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m 65\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mlifeifei.png\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 66\u001b[0m model \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mhub\u001b[38;5;241m.\u001b[39mload(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpytorch/vision:v0.6.0\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdeeplabv3_resnet101\u001b[39m\u001b[38;5;124m'\u001b[39m, pretrained\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m 67\u001b[0m model\u001b[38;5;241m.\u001b[39meval()\n",
828
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\site-packages\\torch\\hub.py:611\u001b[0m, in \u001b[0;36mdownload_url_to_file\u001b[1;34m(url, dst, hash_prefix, progress)\u001b[0m\n\u001b[0;32m 609\u001b[0m file_size \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 610\u001b[0m req \u001b[38;5;241m=\u001b[39m Request(url, headers\u001b[38;5;241m=\u001b[39m{\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUser-Agent\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtorch.hub\u001b[39m\u001b[38;5;124m\"\u001b[39m})\n\u001b[1;32m--> 611\u001b[0m u \u001b[38;5;241m=\u001b[39m \u001b[43murlopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreq\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 612\u001b[0m meta \u001b[38;5;241m=\u001b[39m u\u001b[38;5;241m.\u001b[39minfo()\n\u001b[0;32m 613\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(meta, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgetheaders\u001b[39m\u001b[38;5;124m'\u001b[39m):\n",
829
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\urllib\\request.py:216\u001b[0m, in \u001b[0;36murlopen\u001b[1;34m(url, data, timeout, cafile, capath, cadefault, context)\u001b[0m\n\u001b[0;32m 214\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 215\u001b[0m opener \u001b[38;5;241m=\u001b[39m _opener\n\u001b[1;32m--> 216\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mopener\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n",
830
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\urllib\\request.py:519\u001b[0m, in \u001b[0;36mOpenerDirector.open\u001b[1;34m(self, fullurl, data, timeout)\u001b[0m\n\u001b[0;32m 516\u001b[0m req \u001b[38;5;241m=\u001b[39m meth(req)\n\u001b[0;32m 518\u001b[0m sys\u001b[38;5;241m.\u001b[39maudit(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124murllib.Request\u001b[39m\u001b[38;5;124m'\u001b[39m, req\u001b[38;5;241m.\u001b[39mfull_url, req\u001b[38;5;241m.\u001b[39mdata, req\u001b[38;5;241m.\u001b[39mheaders, req\u001b[38;5;241m.\u001b[39mget_method())\n\u001b[1;32m--> 519\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreq\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 521\u001b[0m \u001b[38;5;66;03m# post-process response\u001b[39;00m\n\u001b[0;32m 522\u001b[0m meth_name \u001b[38;5;241m=\u001b[39m protocol\u001b[38;5;241m+\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_response\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
831
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\urllib\\request.py:536\u001b[0m, in \u001b[0;36mOpenerDirector._open\u001b[1;34m(self, req, data)\u001b[0m\n\u001b[0;32m 533\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n\u001b[0;32m 535\u001b[0m protocol \u001b[38;5;241m=\u001b[39m req\u001b[38;5;241m.\u001b[39mtype\n\u001b[1;32m--> 536\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call_chain\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhandle_open\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprotocol\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprotocol\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\n\u001b[0;32m 537\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m_open\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mreq\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 538\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m result:\n\u001b[0;32m 539\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
832
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\urllib\\request.py:496\u001b[0m, in \u001b[0;36mOpenerDirector._call_chain\u001b[1;34m(self, chain, kind, meth_name, *args)\u001b[0m\n\u001b[0;32m 494\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m handler \u001b[38;5;129;01min\u001b[39;00m handlers:\n\u001b[0;32m 495\u001b[0m func \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(handler, meth_name)\n\u001b[1;32m--> 496\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 497\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m result \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 498\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
833
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\urllib\\request.py:1391\u001b[0m, in \u001b[0;36mHTTPSHandler.https_open\u001b[1;34m(self, req)\u001b[0m\n\u001b[0;32m 1390\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mhttps_open\u001b[39m(\u001b[38;5;28mself\u001b[39m, req):\n\u001b[1;32m-> 1391\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdo_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhttp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mclient\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mHTTPSConnection\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mreq\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 1392\u001b[0m \u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_context\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcheck_hostname\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_check_hostname\u001b[49m\u001b[43m)\u001b[49m\n",
834
+ "File \u001b[1;32mD:\\Users\\AppData\\Local\\Programs\\Python\\Python3.11.2\\Lib\\urllib\\request.py:1351\u001b[0m, in \u001b[0;36mAbstractHTTPHandler.do_open\u001b[1;34m(self, http_class, req, **http_conn_args)\u001b[0m\n\u001b[0;32m 1348\u001b[0m h\u001b[38;5;241m.\u001b[39mrequest(req\u001b[38;5;241m.\u001b[39mget_method(), req\u001b[38;5;241m.\u001b[39mselector, req\u001b[38;5;241m.\u001b[39mdata, headers,\n\u001b[0;32m 1349\u001b[0m encode_chunked\u001b[38;5;241m=\u001b[39mreq\u001b[38;5;241m.\u001b[39mhas_header(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTransfer-encoding\u001b[39m\u001b[38;5;124m'\u001b[39m))\n\u001b[0;32m 1350\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err: \u001b[38;5;66;03m# timeout error\u001b[39;00m\n\u001b[1;32m-> 1351\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m URLError(err)\n\u001b[0;32m 1352\u001b[0m r \u001b[38;5;241m=\u001b[39m h\u001b[38;5;241m.\u001b[39mgetresponse()\n\u001b[0;32m 1353\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m:\n",
835
+ "\u001b[1;31mURLError\u001b[0m: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)>"
836
+ ]
837
+ }
838
+ ],
839
+ "source": [
840
+ "import gradio as gr\n",
841
+ "import cv2\n",
842
+ "import torch\n",
843
+ "import numpy as np\n",
844
+ "from torchvision import transforms\n",
845
+ "\n",
846
+ "title = \"Remove Bg\"\n",
847
+ "description = \"Automatically remove the image background from a profile photo.\"\n",
848
+ "article = \"<p style='text-align: center'><a href='https://news.machinelearning.sg/posts/beautiful_profile_pics_remove_background_image_with_deeplabv3/'>Blog</a> | <a href='https://github.com/eugenesiow/practical-ml'>Github Repo</a></p>\"\n",
849
+ "\n",
850
+ "\n",
851
+ "def make_transparent_foreground(pic, mask):\n",
852
+ " # split the image into channels\n",
853
+ " b, g, r = cv2.split(np.array(pic).astype('uint8'))\n",
854
+ " # add an alpha channel with and fill all with transparent pixels (max 255)\n",
855
+ " a = np.ones(mask.shape, dtype='uint8') * 255\n",
856
+ " # merge the alpha channel back\n",
857
+ " alpha_im = cv2.merge([b, g, r, a], 4)\n",
858
+ " # create a transparent background\n",
859
+ " bg = np.zeros(alpha_im.shape)\n",
860
+ " # setup the new mask\n",
861
+ " new_mask = np.stack([mask, mask, mask, mask], axis=2)\n",
862
+ " # copy only the foreground color pixels from the original image where mask is set\n",
863
+ " foreground = np.where(new_mask, alpha_im, bg).astype(np.uint8)\n",
864
+ "\n",
865
+ " return foreground\n",
866
+ "\n",
867
+ "\n",
868
+ "def remove_background(input_image):\n",
869
+ " preprocess = transforms.Compose([\n",
870
+ " transforms.ToTensor(),\n",
871
+ " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n",
872
+ " ])\n",
873
+ "\n",
874
+ " input_tensor = preprocess(input_image)\n",
875
+ " input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model\n",
876
+ "\n",
877
+ " # move the input and model to GPU for speed if available\n",
878
+ " if torch.cuda.is_available():\n",
879
+ " input_batch = input_batch.to('cuda')\n",
880
+ " model.to('cuda')\n",
881
+ "\n",
882
+ " with torch.no_grad():\n",
883
+ " output = model(input_batch)['out'][0]\n",
884
+ " output_predictions = output.argmax(0)\n",
885
+ "\n",
886
+ " # create a binary (black and white) mask of the profile foreground\n",
887
+ " mask = output_predictions.byte().cpu().numpy()\n",
888
+ " background = np.zeros(mask.shape)\n",
889
+ " bin_mask = np.where(mask, 255, background).astype(np.uint8)\n",
890
+ "\n",
891
+ " foreground = make_transparent_foreground(input_image, bin_mask)\n",
892
+ "\n",
893
+ " return foreground, bin_mask\n",
894
+ "\n",
895
+ "\n",
896
+ "def inference(img):\n",
897
+ " foreground, _ = remove_background(img)\n",
898
+ " return foreground\n",
899
+ "\n",
900
+ "\n",
901
+ "torch.hub.download_url_to_file('https://pbs.twimg.com/profile_images/691700243809718272/z7XZUARB_400x400.jpg',\n",
902
+ " 'demis.jpg')\n",
903
+ "torch.hub.download_url_to_file('https://hai.stanford.edu/sites/default/files/styles/person_medium/public/2020-03/hai_1512feifei.png?itok=INFuLABp',\n",
904
+ " 'lifeifei.png')\n",
905
+ "model = torch.hub.load('pytorch/vision:v0.6.0', 'deeplabv3_resnet101', pretrained=True)\n",
906
+ "model.eval()\n",
907
+ "\n",
908
+ "gr.Interface(\n",
909
+ " inference,\n",
910
+ " gr.inputs.Image(type=\"pil\", label=\"Input\"),\n",
911
+ " gr.outputs.Image(type=\"pil\", label=\"Output\"),\n",
912
+ " title=title,\n",
913
+ " description=description,\n",
914
+ " article=article,\n",
915
+ " examples=[['demis.jpg'], ['lifeifei.png']],\n",
916
+ " enable_queue=True\n",
917
+ ").launch(debug=False)"
918
+ ]
919
+ },
920
+ {
921
+ "cell_type": "code",
922
+ "execution_count": null,
923
+ "id": "60d1be13-e522-42d4-b61e-ac1ac38ab58e",
924
+ "metadata": {},
925
+ "outputs": [],
926
+ "source": []
927
+ },
928
+ {
929
+ "cell_type": "code",
930
+ "execution_count": null,
931
+ "id": "9ee2074c-14f0-4bb8-9913-b37ae4af7e9c",
932
+ "metadata": {},
933
+ "outputs": [],
934
+ "source": []
935
+ }
936
+ ],
937
+ "metadata": {
938
+ "kernelspec": {
939
+ "display_name": "Python 3 (ipykernel)",
940
+ "language": "python",
941
+ "name": "python3"
942
+ },
943
+ "language_info": {
944
+ "codemirror_mode": {
945
+ "name": "ipython",
946
+ "version": 3
947
+ },
948
+ "file_extension": ".py",
949
+ "mimetype": "text/x-python",
950
+ "name": "python",
951
+ "nbconvert_exporter": "python",
952
+ "pygments_lexer": "ipython3",
953
+ "version": "3.11.2"
954
+ }
955
+ },
956
+ "nbformat": 4,
957
+ "nbformat_minor": 5
958
+ }