Aniketh/Week_1_Lab
0
1{2 "nbformat": 4,3 "nbformat_minor": 0,4 "metadata": {5 "colab": {6 "name": "Week 1 Lab -Part III.ipynb",7 "provenance": [],8 "collapsed_sections": []9 },10 "kernelspec": {11 "name": "python3",12 "display_name": "Python 3"13 },14 "language_info": {15 "name": "python"16 }17 },18 "cells": [19 {20 "cell_type": "markdown",21 "source": [22 "Question 1"23 ],24 "metadata": {25 "id": "EJbgjST3i0p1"26 }27 },28 {29 "cell_type": "code",30 "source": [31 "!pip install --quiet gradio"32 ],33 "metadata": {34 "colab": {35 "base_uri": "https://localhost:8080/"36 },37 "id": "gmfQUGC2iZO2",38 "outputId": "0a4d801d-6e66-4115-f43d-b6404d145867"39 },40 "execution_count": 2,41 "outputs": [42 {43 "output_type": "stream",44 "name": "stdout",45 "text": [46 "\u001b[K |████████████████████████████████| 5.1 MB 5.3 MB/s \n",47 "\u001b[K |████████████████████████████████| 54 kB 2.8 MB/s \n",48 "\u001b[K |████████████████████████████████| 84 kB 3.2 MB/s \n",49 "\u001b[K |████████████████████████████████| 53 kB 1.9 MB/s \n",50 "\u001b[K |████████████████████████████████| 1.1 MB 34.2 MB/s \n",51 "\u001b[K |████████████████████████████████| 2.0 MB 40.7 MB/s \n",52 "\u001b[K |████████████████████████████████| 256 kB 54.7 MB/s \n",53 "\u001b[K |████████████████████████████████| 212 kB 67.8 MB/s \n",54 "\u001b[K |████████████████████████████████| 94 kB 3.1 MB/s \n",55 "\u001b[K |████████████████████████████████| 144 kB 77.9 MB/s \n",56 "\u001b[K |████████████████████████████████| 271 kB 58.0 MB/s \n",57 "\u001b[K |████████████████████████████████| 11.1 MB 56.3 MB/s \n",58 "\u001b[K |████████████████████████████████| 63 kB 1.7 MB/s \n",59 "\u001b[K |████████████████████████████████| 80 kB 8.5 MB/s \n",60 "\u001b[K |████████████████████████████████| 43 kB 1.8 MB/s \n",61 "\u001b[K |████████████████████████████████| 856 kB 47.0 MB/s \n",62 "\u001b[K |████████████████████████████████| 4.0 MB 43.0 MB/s \n",63 "\u001b[K |████████████████████████████████| 62 kB 583 kB/s \n",64 "\u001b[K |████████████████████████████████| 58 kB 4.6 MB/s \n",65 "\u001b[?25h Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n",66 " Building wheel for python-multipart (setup.py) ... \u001b[?25l\u001b[?25hdone\n"67 ]68 }69 ]70 },71 {72 "cell_type": "markdown",73 "source": [74 "Question 2"75 ],76 "metadata": {77 "id": "jeFEpq_5i3w6"78 }79 },80 {81 "cell_type": "code",82 "source": [83 "import gradio as gr"84 ],85 "metadata": {86 "id": "iOvSnPquiqsM"87 },88 "execution_count": 3,89 "outputs": []90 },91 {92 "cell_type": "code",93 "source": [94 "print(gr.__version__)"95 ],96 "metadata": {97 "colab": {98 "base_uri": "https://localhost:8080/"99 },100 "id": "zOyWylTiituM",101 "outputId": "dcd55761-5692-4217-fbc2-fd73c9f89f0b"102 },103 "execution_count": 4,104 "outputs": [105 {106 "output_type": "stream",107 "name": "stdout",108 "text": [109 "3.0.10\n"110 ]111 }112 ]113 },114 {115 "cell_type": "markdown",116 "source": [117 "Question 3"118 ],119 "metadata": {120 "id": "facoR56ujJsl"121 }122 },123 {124 "cell_type": "code",125 "source": [126 "def greet(name):\n",127 " return \"Hello \" + name + \"!!\"\n"128 ],129 "metadata": {130 "id": "NVJWGqEnjCAI"131 },132 "execution_count": 5,133 "outputs": []134 },135 {136 "cell_type": "code",137 "source": [138 "import gradio as gr\n",139 "input_module = gr.inputs.Textbox(label = \"Input Text\")\n",140 "output_module = gr.outputs.Textbox(label = \"Output Text\")"141 ],142 "metadata": {143 "colab": {144 "base_uri": "https://localhost:8080/"145 },146 "id": "ZY9Bu1nDjHgx",147 "outputId": "f36a74df-3232-499b-e768-813424622719"148 },149 "execution_count": 6,150 "outputs": [151 {152 "output_type": "stream",153 "name": "stderr",154 "text": [155 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",156 " warnings.warn(value)\n",157 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `numeric` parameter is deprecated, and it has no effect\n",158 " warnings.warn(value)\n",159 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",160 " warnings.warn(value)\n"161 ]162 }163 ]164 },165 {166 "cell_type": "code",167 "source": [168 "gr.Interface(fn=greet, inputs=input_module, outputs=output_module).launch()"169 ],170 "metadata": {171 "colab": {172 "base_uri": "https://localhost:8080/",173 "height": 646174 },175 "id": "IccvcT9PjPjC",176 "outputId": "0879fdab-3e0b-4100-e193-d5031bbeeb71"177 },178 "execution_count": 7,179 "outputs": [180 {181 "output_type": "stream",182 "name": "stdout",183 "text": [184 "Colab notebook detected. To show errors in colab notebook, set `debug=True` in `launch()`\n",185 "Running on public URL: https://46715.gradio.app\n",186 "\n",187 "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"188 ]189 },190 {191 "output_type": "display_data",192 "data": {193 "text/plain": [194 "<IPython.core.display.HTML object>"195 ],196 "text/html": [197 "<div><iframe src=\"https://46715.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"198 ]199 },200 "metadata": {}201 },202 {203 "output_type": "execute_result",204 "data": {205 "text/plain": [206 "(<gradio.routes.App at 0x7f9ccce058d0>,\n",207 " 'http://127.0.0.1:7860/',\n",208 " 'https://46715.gradio.app')"209 ]210 },211 "metadata": {},212 "execution_count": 7213 }214 ]215 },216 {217 "cell_type": "markdown",218 "source": [219 "Question 4"220 ],221 "metadata": {222 "id": "UJ30Bl8Cjwyw"223 }224 },225 {226 "cell_type": "code",227 "source": [228 "examples = [[\"June 1st\"], [\"Week 2\"]]"229 ],230 "metadata": {231 "id": "uliYcYJJjs8A"232 },233 "execution_count": 8,234 "outputs": []235 },236 {237 "cell_type": "code",238 "source": [239 "gr.Interface(fn=greet, inputs=input_module, outputs=output_module, examples = examples).launch()"240 ],241 "metadata": {242 "colab": {243 "base_uri": "https://localhost:8080/",244 "height": 646245 },246 "id": "HxYx8WwXjybe",247 "outputId": "2586bb15-4f56-4769-95f1-90ddf0e01fb6"248 },249 "execution_count": 9,250 "outputs": [251 {252 "output_type": "stream",253 "name": "stdout",254 "text": [255 "Colab notebook detected. To show errors in colab notebook, set `debug=True` in `launch()`\n",256 "Running on public URL: https://20785.gradio.app\n",257 "\n",258 "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"259 ]260 },261 {262 "output_type": "display_data",263 "data": {264 "text/plain": [265 "<IPython.core.display.HTML object>"266 ],267 "text/html": [268 "<div><iframe src=\"https://20785.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"269 ]270 },271 "metadata": {}272 },273 {274 "output_type": "execute_result",275 "data": {276 "text/plain": [277 "(<gradio.routes.App at 0x7f9cca496bd0>,\n",278 " 'http://127.0.0.1:7861/',\n",279 " 'https://20785.gradio.app')"280 ]281 },282 "metadata": {},283 "execution_count": 9284 }285 ]286 },287 {288 "cell_type": "markdown",289 "source": [290 "Question 5"291 ],292 "metadata": {293 "id": "a6bGAhDrj5WO"294 }295 },296 {297 "cell_type": "code",298 "source": [299 "def caption(image):\n",300 " return \"Image is processed!!\""301 ],302 "metadata": {303 "id": "X2fwOsXzj6v5"304 },305 "execution_count": 10,306 "outputs": []307 },308 {309 "cell_type": "code",310 "source": [311 "import gradio as gr\n",312 "input_module = gr.inputs.Image(label = \"Input Image\")\n"313 ],314 "metadata": {315 "colab": {316 "base_uri": "https://localhost:8080/"317 },318 "id": "s6Kr1lqjj_T9",319 "outputId": "4bf21c66-5794-4f76-b4c4-9698dedb8f84"320 },321 "execution_count": 11,322 "outputs": [323 {324 "output_type": "stream",325 "name": "stderr",326 "text": [327 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",328 " warnings.warn(value)\n"329 ]330 }331 ]332 },333 {334 "cell_type": "code",335 "source": [336 "output_module = gr.outputs.Textbox(label = \"Output Text\")"337 ],338 "metadata": {339 "colab": {340 "base_uri": "https://localhost:8080/"341 },342 "id": "Z7AANhskkB1K",343 "outputId": "b1fc656e-8321-4fc5-c9dd-1cb4b7340115"344 },345 "execution_count": 12,346 "outputs": [347 {348 "output_type": "stream",349 "name": "stderr",350 "text": [351 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",352 " warnings.warn(value)\n"353 ]354 }355 ]356 },357 {358 "cell_type": "code",359 "source": [360 "gr.Interface(fn=caption, inputs=input_module, outputs=output_module).launch()"361 ],362 "metadata": {363 "colab": {364 "base_uri": "https://localhost:8080/",365 "height": 646366 },367 "id": "ff-_iIetkF05",368 "outputId": "149f5383-ec31-4f95-db02-7caa6b9da2e9"369 },370 "execution_count": 13,371 "outputs": [372 {373 "output_type": "stream",374 "name": "stdout",375 "text": [376 "Colab notebook detected. To show errors in colab notebook, set `debug=True` in `launch()`\n",377 "Running on public URL: https://55801.gradio.app\n",378 "\n",379 "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"380 ]381 },382 {383 "output_type": "display_data",384 "data": {385 "text/plain": [386 "<IPython.core.display.HTML object>"387 ],388 "text/html": [389 "<div><iframe src=\"https://55801.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"390 ]391 },392 "metadata": {}393 },394 {395 "output_type": "execute_result",396 "data": {397 "text/plain": [398 "(<gradio.routes.App at 0x7f9ccb489090>,\n",399 " 'http://127.0.0.1:7862/',\n",400 " 'https://55801.gradio.app')"401 ]402 },403 "metadata": {},404 "execution_count": 13405 }406 ]407 },408 {409 "cell_type": "markdown",410 "source": [411 "Question 6"412 ],413 "metadata": {414 "id": "sZu8m8WNkOx_"415 }416 },417 {418 "cell_type": "code",419 "source": [420 "import gradio as gr\n",421 "\n",422 "# a. define text data type\n",423 "input_module1 = gr.inputs.Textbox(label = \"Input Text\")\n",424 "\n",425 "# b. define image data type\n",426 "input_module2 = gr.inputs.Image(label = \"Input Image\")\n",427 "\n",428 "# c. define Number data type\n",429 "input_module3 = gr.inputs.Number(label = \"Input Number\")\n",430 "\n",431 "# d. define Slider data type\n",432 "input_module4 = gr.inputs.Slider(1, 100, step=5, label = \"Input Slider\")\n",433 "\n",434 "# e. define Checkbox data type\n",435 "input_module5 = gr.inputs.Checkbox(label = \"Does it work?\")\n",436 "\n",437 "# f. define Radio data type\n",438 "input_module6 = gr.inputs.Radio(choices=[\"park\", \"zoo\", \"road\"], label = \"Input Radio\")\n",439 "\n",440 "# g. define Dropdown data type\n",441 "input_module7 = gr.inputs.Dropdown(choices=[\"park\", \"zoo\", \"road\"], label = \"Input Dropdown\")"442 ],443 "metadata": {444 "colab": {445 "base_uri": "https://localhost:8080/"446 },447 "id": "7EaQOf7ikQcd",448 "outputId": "a74907f0-bace-45c0-ff0e-11018b5cab9d"449 },450 "execution_count": 14,451 "outputs": [452 {453 "output_type": "stream",454 "name": "stderr",455 "text": [456 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",457 " warnings.warn(value)\n",458 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `numeric` parameter is deprecated, and it has no effect\n",459 " warnings.warn(value)\n",460 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",461 " warnings.warn(value)\n"462 ]463 }464 ]465 },466 {467 "cell_type": "code",468 "source": [469 "# a. define text data type\n",470 "output_module1 = gr.outputs.Textbox(label = \"Output Text\")\n",471 "\n",472 "# b. define image data type\n",473 "output_module2 = gr.outputs.Image(label = \"Output Image\")\n",474 "\n",475 "# you can define more output components"476 ],477 "metadata": {478 "colab": {479 "base_uri": "https://localhost:8080/"480 },481 "id": "mo86TIe-kXXD",482 "outputId": "6cc219ae-1073-41ad-d587-5fc267d37e99"483 },484 "execution_count": 15,485 "outputs": [486 {487 "output_type": "stream",488 "name": "stderr",489 "text": [490 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",491 " warnings.warn(value)\n"492 ]493 }494 ]495 },496 {497 "cell_type": "code",498 "source": [499 "def multi_inputs(input1, input2, input3, input4, input5, input6, input7 ):\n",500 " import numpy as np\n",501 " ## processing inputs\n",502 "\n",503 " ## return outputs\n",504 " output1 = \"Processing inputs and return outputs\" # text output example\n",505 " output2 = np.random.rand(6,6) # image-like array output example\n",506 " return output1,output2"507 ],508 "metadata": {509 "id": "JbRLwKHskaHN"510 },511 "execution_count": 16,512 "outputs": []513 },514 {515 "cell_type": "code",516 "source": [517 "gr.Interface(fn=multi_inputs, \n",518 " inputs=[input_module1, input_module2, input_module3,\n",519 " input_module4, input_module5, input_module6,\n",520 " input_module7], \n",521 " outputs=[output_module1, output_module2]\n",522 " ).launch()"523 ],524 "metadata": {525 "colab": {526 "base_uri": "https://localhost:8080/",527 "height": 646528 },529 "id": "cmnEH3UYkc7Q",530 "outputId": "08290c25-02cf-4804-973a-6eb8c22d5d8c"531 },532 "execution_count": 17,533 "outputs": [534 {535 "output_type": "stream",536 "name": "stdout",537 "text": [538 "Colab notebook detected. To show errors in colab notebook, set `debug=True` in `launch()`\n",539 "Running on public URL: https://42506.gradio.app\n",540 "\n",541 "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"542 ]543 },544 {545 "output_type": "display_data",546 "data": {547 "text/plain": [548 "<IPython.core.display.HTML object>"549 ],550 "text/html": [551 "<div><iframe src=\"https://42506.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"552 ]553 },554 "metadata": {}555 },556 {557 "output_type": "execute_result",558 "data": {559 "text/plain": [560 "(<gradio.routes.App at 0x7f9ccb346510>,\n",561 " 'http://127.0.0.1:7863/',\n",562 " 'https://42506.gradio.app')"563 ]564 },565 "metadata": {},566 "execution_count": 17567 }568 ]569 },570 {571 "cell_type": "markdown",572 "source": [573 "Question 7"574 ],575 "metadata": {576 "id": "MAOhd5Vak2i6"577 }578 },579 {580 "cell_type": "code",581 "source": [582 "import gradio as gr\n",583 "input_module1 = gr.inputs.Slider(-124.35, -114.35, step=0.5, label = \"LONG\")\n",584 "input_module2 = gr.inputs.Slider(32, 41, step=0.5, label = \"LAT\")\n",585 "input_module3 = gr.inputs.Slider(1, 52, step=1, label = \"AGE(YEAR)\")\n",586 "input_module4 = gr.inputs.Slider(1, 39996, step=5, label = \"TOTAL_ROOMS\")\n",587 "input_module5 = gr.inputs.Slider(1, 6441, step=5, label = \"TOTAL_BEDROOMS\")\n",588 "input_module6 = gr.inputs.Slider(3, 35678, step=5, label = \"POPULATION\")\n",589 "input_module7 = gr.inputs.Slider(1, 6081, step=5, label = \"HOUSEHOLDS\")\n",590 "input_module8 = gr.inputs.Slider(0, 15, step=0.5, label = \"INCOME\")"591 ],592 "metadata": {593 "colab": {594 "base_uri": "https://localhost:8080/"595 },596 "id": "XpevRhFRk37Z",597 "outputId": "431ff865-ff0b-4d84-f98e-214cc2c9eb32"598 },599 "execution_count": 18,600 "outputs": [601 {602 "output_type": "stream",603 "name": "stderr",604 "text": [605 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n",606 " warnings.warn(value)\n"607 ]608 }609 ]610 },611 {612 "cell_type": "code",613 "source": [614 "output_module1 = gr.outputs.Textbox(label = \"Predicted Housing Prices \")\n",615 "\n",616 "output_module2 = gr.outputs.Image(label = \"VISUALIZE YOUR LOCATION\")"617 ],618 "metadata": {619 "colab": {620 "base_uri": "https://localhost:8080/"621 },622 "id": "sAzujfTYlEHZ",623 "outputId": "02da4289-e699-457e-e43e-98b73292a2a5"624 },625 "execution_count": 19,626 "outputs": [627 {628 "output_type": "stream",629 "name": "stderr",630 "text": [631 "/usr/local/lib/python3.7/dist-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n",632 " warnings.warn(value)\n"633 ]634 }635 ]636 },637 {638 "cell_type": "code",639 "source": [640 "def machine_learning_pipeline(input1, input2, input3, input4, input5, input6, input7, input8 ):\n",641 " import numpy as np\n",642 " output1 = \"Predicted Housing Prices \"\n",643 " output2 = np.random.rand(6,6)\n",644 " return output1,output2"645 ],646 "metadata": {647 "id": "_AQLiMzdlKN-"648 },649 "execution_count": 20,650 "outputs": []651 },652 {653 "cell_type": "code",654 "source": [655 "# Step 6.4: Put all three component together into the gradio's interface function \n",656 "gr.Interface(fn=machine_learning_pipeline, \n",657 " inputs=[input_module1, input_module2, input_module3,\n",658 " input_module4, input_module5, input_module6,\n",659 " input_module7, input_module8], \n",660 " outputs=[output_module1, output_module2]\n",661 " ).launch()"662 ],663 "metadata": {664 "colab": {665 "base_uri": "https://localhost:8080/",666 "height": 646667 },668 "id": "o7O1m5bGlNPw",669 "outputId": "add0860e-72c0-450d-9c0c-4bc78ff4a27f"670 },671 "execution_count": 21,672 "outputs": [673 {674 "output_type": "stream",675 "name": "stdout",676 "text": [677 "Colab notebook detected. To show errors in colab notebook, set `debug=True` in `launch()`\n",678 "Running on public URL: https://54200.gradio.app\n",679 "\n",680 "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"681 ]682 },683 {684 "output_type": "display_data",685 "data": {686 "text/plain": [687 "<IPython.core.display.HTML object>"688 ],689 "text/html": [690 "<div><iframe src=\"https://54200.gradio.app\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone;\" frameborder=\"0\" allowfullscreen></iframe></div>"691 ]692 },693 "metadata": {}694 },695 {696 "output_type": "execute_result",697 "data": {698 "text/plain": [699 "(<gradio.routes.App at 0x7f9ccacc32d0>,\n",700 " 'http://127.0.0.1:7864/',\n",701 " 'https://54200.gradio.app')"702 ]703 },704 "metadata": {},705 "execution_count": 21706 }707 ]708 }709 ]710}