mdkhalid commited on
Commit
8d3d9b4
·
1 Parent(s): 85c70ad

gradio examples

Browse files
Files changed (1) hide show
  1. app.ipynb +420 -0
app.ipynb ADDED
@@ -0,0 +1,420 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stderr",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "/home/mks/.local/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
13
+ " from .autonotebook import tqdm as notebook_tqdm\n"
14
+ ]
15
+ }
16
+ ],
17
+ "source": [
18
+ "%load_ext gradio"
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": 4,
24
+ "metadata": {},
25
+ "outputs": [
26
+ {
27
+ "name": "stdout",
28
+ "output_type": "stream",
29
+ "text": [
30
+ "Running on local URL: http://127.0.0.1:7860\n"
31
+ ]
32
+ },
33
+ {
34
+ "data": {
35
+ "text/html": [
36
+ "<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>"
37
+ ],
38
+ "text/plain": [
39
+ "<IPython.core.display.HTML object>"
40
+ ]
41
+ },
42
+ "metadata": {},
43
+ "output_type": "display_data"
44
+ }
45
+ ],
46
+ "source": [
47
+ "%%blocks \n",
48
+ "\n",
49
+ "import gradio as gr\n",
50
+ "\n",
51
+ "gr.Markdown(\"# Greetings from Gradio!\")\n",
52
+ "inp = gr.Textbox(placeholder=\"What is your name?\")\n",
53
+ "out = gr.Textbox()\n",
54
+ "\n",
55
+ "inp.change(fn=lambda x: f\"Welcome, {x}!\", \n",
56
+ " inputs=inp, \n",
57
+ " outputs=out)"
58
+ ]
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "execution_count": 5,
63
+ "metadata": {},
64
+ "outputs": [
65
+ {
66
+ "name": "stderr",
67
+ "output_type": "stream",
68
+ "text": [
69
+ "/home/mks/.local/lib/python3.10/site-packages/gradio/ipython_ext.py:18: UserWarning: Usage of gradio.Interface with %%blocks may result in errors.\n",
70
+ " warnings.warn(\n"
71
+ ]
72
+ },
73
+ {
74
+ "name": "stdout",
75
+ "output_type": "stream",
76
+ "text": [
77
+ "Running on local URL: http://127.0.0.1:7861\n",
78
+ "\n",
79
+ "To create a public link, set `share=True` in `launch()`.\n"
80
+ ]
81
+ },
82
+ {
83
+ "data": {
84
+ "text/html": [
85
+ "<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
86
+ ],
87
+ "text/plain": [
88
+ "<IPython.core.display.HTML object>"
89
+ ]
90
+ },
91
+ "metadata": {},
92
+ "output_type": "display_data"
93
+ },
94
+ {
95
+ "name": "stdout",
96
+ "output_type": "stream",
97
+ "text": [
98
+ "Running on local URL: http://127.0.0.1:7860\n"
99
+ ]
100
+ },
101
+ {
102
+ "data": {
103
+ "text/html": [
104
+ "<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>"
105
+ ],
106
+ "text/plain": [
107
+ "<IPython.core.display.HTML object>"
108
+ ]
109
+ },
110
+ "metadata": {},
111
+ "output_type": "display_data"
112
+ }
113
+ ],
114
+ "source": [
115
+ "%%blocks \n",
116
+ "def greet(name):\n",
117
+ " return \"Hello \" + name + \"!\"\n",
118
+ "\n",
119
+ "demo = gr.Interface(fn=greet, inputs=\"text\", outputs=\"text\")\n",
120
+ " \n",
121
+ "demo.launch() "
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "code",
126
+ "execution_count": 7,
127
+ "metadata": {},
128
+ "outputs": [
129
+ {
130
+ "name": "stdout",
131
+ "output_type": "stream",
132
+ "text": [
133
+ "Running on local URL: http://127.0.0.1:7863\n",
134
+ "\n",
135
+ "To create a public link, set `share=True` in `launch()`.\n"
136
+ ]
137
+ },
138
+ {
139
+ "data": {
140
+ "text/html": [
141
+ "<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>"
142
+ ],
143
+ "text/plain": [
144
+ "<IPython.core.display.HTML object>"
145
+ ]
146
+ },
147
+ "metadata": {},
148
+ "output_type": "display_data"
149
+ },
150
+ {
151
+ "data": {
152
+ "text/plain": []
153
+ },
154
+ "execution_count": 7,
155
+ "metadata": {},
156
+ "output_type": "execute_result"
157
+ }
158
+ ],
159
+ "source": [
160
+ "import gradio as gr\n",
161
+ "\n",
162
+ "def greet(name):\n",
163
+ " return \"Hello \" + name + \"!\"\n",
164
+ "\n",
165
+ "demo = gr.Interface(\n",
166
+ " fn=greet,\n",
167
+ " inputs=gr.Textbox(lines=4, placeholder=\"Name Here...\"),\n",
168
+ " outputs=\"text\",\n",
169
+ ")\n",
170
+ "demo.launch()"
171
+ ]
172
+ },
173
+ {
174
+ "cell_type": "code",
175
+ "execution_count": 15,
176
+ "metadata": {},
177
+ "outputs": [
178
+ {
179
+ "name": "stdout",
180
+ "output_type": "stream",
181
+ "text": [
182
+ "Running on local URL: http://127.0.0.1:7869\n",
183
+ "\n",
184
+ "To create a public link, set `share=True` in `launch()`.\n"
185
+ ]
186
+ },
187
+ {
188
+ "data": {
189
+ "text/html": [
190
+ "<div><iframe src=\"http://127.0.0.1:7869/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
191
+ ],
192
+ "text/plain": [
193
+ "<IPython.core.display.HTML object>"
194
+ ]
195
+ },
196
+ "metadata": {},
197
+ "output_type": "display_data"
198
+ },
199
+ {
200
+ "data": {
201
+ "text/plain": []
202
+ },
203
+ "execution_count": 15,
204
+ "metadata": {},
205
+ "output_type": "execute_result"
206
+ }
207
+ ],
208
+ "source": [
209
+ "import gradio as gr\n",
210
+ "\n",
211
+ "def greet(yourname, is_morning, temperature):\n",
212
+ " salutation = \"Good morning\" if is_morning else \"Good evening\"\n",
213
+ " greeting = f\"{salutation} {name}. It is {temperature} degrees Fahrenheit today.\"\n",
214
+ " celsius = (temperature - 32) * 5 / 9\n",
215
+ " return greeting, round(celsius, 2)\n",
216
+ "\n",
217
+ "demo = gr.Interface(\n",
218
+ " fn=greet,\n",
219
+ " inputs=[\"text\", \"checkbox\", gr.Slider(-50, 100)],\n",
220
+ " outputs=[\"text\", \"number\"],\n",
221
+ ")\n",
222
+ "demo.launch()"
223
+ ]
224
+ },
225
+ {
226
+ "cell_type": "code",
227
+ "execution_count": 16,
228
+ "metadata": {},
229
+ "outputs": [
230
+ {
231
+ "name": "stdout",
232
+ "output_type": "stream",
233
+ "text": [
234
+ "Running on local URL: http://127.0.0.1:7870\n",
235
+ "\n",
236
+ "To create a public link, set `share=True` in `launch()`.\n"
237
+ ]
238
+ },
239
+ {
240
+ "data": {
241
+ "text/html": [
242
+ "<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>"
243
+ ],
244
+ "text/plain": [
245
+ "<IPython.core.display.HTML object>"
246
+ ]
247
+ },
248
+ "metadata": {},
249
+ "output_type": "display_data"
250
+ },
251
+ {
252
+ "data": {
253
+ "text/plain": []
254
+ },
255
+ "execution_count": 16,
256
+ "metadata": {},
257
+ "output_type": "execute_result"
258
+ }
259
+ ],
260
+ "source": [
261
+ "import numpy as np\n",
262
+ "import gradio as gr\n",
263
+ "\n",
264
+ "def sepia(input_img):\n",
265
+ " sepia_filter = np.array([\n",
266
+ " [0.393, 0.769, 0.189], \n",
267
+ " [0.349, 0.686, 0.168], \n",
268
+ " [0.272, 0.534, 0.131]\n",
269
+ " ])\n",
270
+ " sepia_img = input_img.dot(sepia_filter.T)\n",
271
+ " sepia_img /= sepia_img.max()\n",
272
+ " return sepia_img\n",
273
+ "\n",
274
+ "demo = gr.Interface(sepia, gr.Image(shape=(200, 200)), \"image\")\n",
275
+ "demo.launch()"
276
+ ]
277
+ },
278
+ {
279
+ "cell_type": "code",
280
+ "execution_count": 18,
281
+ "metadata": {},
282
+ "outputs": [
283
+ {
284
+ "name": "stdout",
285
+ "output_type": "stream",
286
+ "text": [
287
+ "Running on local URL: http://127.0.0.1:7871\n",
288
+ "\n",
289
+ "To create a public link, set `share=True` in `launch()`.\n"
290
+ ]
291
+ },
292
+ {
293
+ "data": {
294
+ "text/html": [
295
+ "<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>"
296
+ ],
297
+ "text/plain": [
298
+ "<IPython.core.display.HTML object>"
299
+ ]
300
+ },
301
+ "metadata": {},
302
+ "output_type": "display_data"
303
+ },
304
+ {
305
+ "data": {
306
+ "text/plain": []
307
+ },
308
+ "execution_count": 18,
309
+ "metadata": {},
310
+ "output_type": "execute_result"
311
+ }
312
+ ],
313
+ "source": [
314
+ "import gradio as gr\n",
315
+ "\n",
316
+ "def greet(name):\n",
317
+ " return \"Hello \" + name + \"!\"\n",
318
+ "\n",
319
+ "with gr.Blocks() as demo:\n",
320
+ " name = gr.Textbox(label=\"Name\")\n",
321
+ " output = gr.Textbox(label=\"Output Box\")\n",
322
+ " greet_btn = gr.Button(\"Greet\")\n",
323
+ " greet_btn.click(fn=greet, inputs=name, outputs=output, api_name=\"greet\")\n",
324
+ "\n",
325
+ "demo.launch()"
326
+ ]
327
+ },
328
+ {
329
+ "cell_type": "code",
330
+ "execution_count": 19,
331
+ "metadata": {},
332
+ "outputs": [
333
+ {
334
+ "name": "stdout",
335
+ "output_type": "stream",
336
+ "text": [
337
+ "Running on local URL: http://127.0.0.1:7872\n",
338
+ "\n",
339
+ "To create a public link, set `share=True` in `launch()`.\n"
340
+ ]
341
+ },
342
+ {
343
+ "data": {
344
+ "text/html": [
345
+ "<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>"
346
+ ],
347
+ "text/plain": [
348
+ "<IPython.core.display.HTML object>"
349
+ ]
350
+ },
351
+ "metadata": {},
352
+ "output_type": "display_data"
353
+ },
354
+ {
355
+ "data": {
356
+ "text/plain": []
357
+ },
358
+ "execution_count": 19,
359
+ "metadata": {},
360
+ "output_type": "execute_result"
361
+ }
362
+ ],
363
+ "source": [
364
+ "import numpy as np\n",
365
+ "import gradio as gr\n",
366
+ "\n",
367
+ "\n",
368
+ "def flip_text(x):\n",
369
+ " return x[::-1]\n",
370
+ "\n",
371
+ "\n",
372
+ "def flip_image(x):\n",
373
+ " return np.fliplr(x)\n",
374
+ "\n",
375
+ "\n",
376
+ "with gr.Blocks() as demo:\n",
377
+ " gr.Markdown(\"Flip text or image files using this demo.\")\n",
378
+ " with gr.Tab(\"Flip Text\"):\n",
379
+ " text_input = gr.Textbox()\n",
380
+ " text_output = gr.Textbox()\n",
381
+ " text_button = gr.Button(\"Flip\")\n",
382
+ " with gr.Tab(\"Flip Image\"):\n",
383
+ " with gr.Row():\n",
384
+ " image_input = gr.Image()\n",
385
+ " image_output = gr.Image()\n",
386
+ " image_button = gr.Button(\"Flip\")\n",
387
+ "\n",
388
+ " with gr.Accordion(\"Open for More!\"):\n",
389
+ " gr.Markdown(\"Look at me...\")\n",
390
+ "\n",
391
+ " text_button.click(flip_text, inputs=text_input, outputs=text_output)\n",
392
+ " image_button.click(flip_image, inputs=image_input, outputs=image_output)\n",
393
+ "\n",
394
+ "demo.launch()"
395
+ ]
396
+ }
397
+ ],
398
+ "metadata": {
399
+ "kernelspec": {
400
+ "display_name": "Python 3",
401
+ "language": "python",
402
+ "name": "python3"
403
+ },
404
+ "language_info": {
405
+ "codemirror_mode": {
406
+ "name": "ipython",
407
+ "version": 3
408
+ },
409
+ "file_extension": ".py",
410
+ "mimetype": "text/x-python",
411
+ "name": "python",
412
+ "nbconvert_exporter": "python",
413
+ "pygments_lexer": "ipython3",
414
+ "version": "3.10.6"
415
+ },
416
+ "orig_nbformat": 4
417
+ },
418
+ "nbformat": 4,
419
+ "nbformat_minor": 2
420
+ }