Last active
February 22, 2022 19:16
-
-
Save nateraw/147d1bd360263022ac3639ddd961fec0 to your computer and use it in GitHub Desktop.
gradio-blocks-demo.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "gradio-blocks-demo.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyPRUprPOfdoYeyHX8K2djOq", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/nateraw/147d1bd360263022ac3639ddd961fec0/gradio-blocks-demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"! pip install gradio==2.8.0a100" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Z4-LfOTZuMUI", | |
"outputId": "7e37a7a5-bffb-4134-b887-53717e6be8af" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Collecting gradio==2.8.0a100\n", | |
" Downloading gradio-2.8.0a100-py3-none-any.whl (1.0 MB)\n", | |
"\u001b[K |████████████████████████████████| 1.0 MB 13.0 MB/s \n", | |
"\u001b[?25hCollecting markdown-it-py[linkify,plugins]\n", | |
" Downloading markdown_it_py-2.0.1-py3-none-any.whl (84 kB)\n", | |
"\u001b[K |████████████████████████████████| 84 kB 2.4 MB/s \n", | |
"\u001b[?25hCollecting aiohttp\n", | |
" Downloading aiohttp-3.8.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.1 MB)\n", | |
"\u001b[K |████████████████████████████████| 1.1 MB 34.8 MB/s \n", | |
"\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from gradio==2.8.0a100) (1.21.5)\n", | |
"Collecting paramiko\n", | |
" Downloading paramiko-2.9.2-py2.py3-none-any.whl (210 kB)\n", | |
"\u001b[K |████████████████████████████████| 210 kB 60.9 MB/s \n", | |
"\u001b[?25hRequirement already satisfied: pillow in /usr/local/lib/python3.7/dist-packages (from gradio==2.8.0a100) (7.1.2)\n", | |
"Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from gradio==2.8.0a100) (3.2.2)\n", | |
"Collecting python-multipart\n", | |
" Downloading python-multipart-0.0.5.tar.gz (32 kB)\n", | |
"Collecting uvicorn\n", | |
" Downloading uvicorn-0.17.5-py3-none-any.whl (53 kB)\n", | |
"\u001b[K |████████████████████████████████| 53 kB 1.9 MB/s \n", | |
"\u001b[?25hCollecting ffmpy\n", | |
" Downloading ffmpy-0.3.0.tar.gz (4.8 kB)\n", | |
"Collecting pydub\n", | |
" Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n", | |
"Collecting pycryptodome\n", | |
" Downloading pycryptodome-3.14.1-cp35-abi3-manylinux2010_x86_64.whl (2.0 MB)\n", | |
"\u001b[K |████████████████████████████████| 2.0 MB 69.3 MB/s \n", | |
"\u001b[?25hCollecting analytics-python\n", | |
" Downloading analytics_python-1.4.0-py2.py3-none-any.whl (15 kB)\n", | |
"Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from gradio==2.8.0a100) (1.3.5)\n", | |
"Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from gradio==2.8.0a100) (2.23.0)\n", | |
"Collecting fastapi\n", | |
" Downloading fastapi-0.74.1-py3-none-any.whl (53 kB)\n", | |
"\u001b[K |████████████████████████████████| 53 kB 1.9 MB/s \n", | |
"\u001b[?25hCollecting aiosignal>=1.1.2\n", | |
" Downloading aiosignal-1.2.0-py3-none-any.whl (8.2 kB)\n", | |
"Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /usr/local/lib/python3.7/dist-packages (from aiohttp->gradio==2.8.0a100) (2.0.12)\n", | |
"Collecting yarl<2.0,>=1.0\n", | |
" Downloading yarl-1.7.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (271 kB)\n", | |
"\u001b[K |████████████████████████████████| 271 kB 77.6 MB/s \n", | |
"\u001b[?25hCollecting multidict<7.0,>=4.5\n", | |
" Downloading multidict-6.0.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (94 kB)\n", | |
"\u001b[K |████████████████████████████████| 94 kB 2.9 MB/s \n", | |
"\u001b[?25hRequirement already satisfied: typing-extensions>=3.7.4 in /usr/local/lib/python3.7/dist-packages (from aiohttp->gradio==2.8.0a100) (3.10.0.2)\n", | |
"Collecting async-timeout<5.0,>=4.0.0a3\n", | |
" Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)\n", | |
"Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.7/dist-packages (from aiohttp->gradio==2.8.0a100) (21.4.0)\n", | |
"Collecting asynctest==0.13.0\n", | |
" Downloading asynctest-0.13.0-py3-none-any.whl (26 kB)\n", | |
"Collecting frozenlist>=1.1.1\n", | |
" Downloading frozenlist-1.3.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (144 kB)\n", | |
"\u001b[K |████████████████████████████████| 144 kB 71.5 MB/s \n", | |
"\u001b[?25hRequirement already satisfied: idna>=2.0 in /usr/local/lib/python3.7/dist-packages (from yarl<2.0,>=1.0->aiohttp->gradio==2.8.0a100) (2.10)\n", | |
"Requirement already satisfied: python-dateutil>2.1 in /usr/local/lib/python3.7/dist-packages (from analytics-python->gradio==2.8.0a100) (2.8.2)\n", | |
"Collecting monotonic>=1.5\n", | |
" Downloading monotonic-1.6-py2.py3-none-any.whl (8.2 kB)\n", | |
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from analytics-python->gradio==2.8.0a100) (1.15.0)\n", | |
"Collecting backoff==1.10.0\n", | |
" Downloading backoff-1.10.0-py2.py3-none-any.whl (31 kB)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->gradio==2.8.0a100) (2021.10.8)\n", | |
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->gradio==2.8.0a100) (3.0.4)\n", | |
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->gradio==2.8.0a100) (1.24.3)\n", | |
"Collecting starlette==0.17.1\n", | |
" Downloading starlette-0.17.1-py3-none-any.whl (58 kB)\n", | |
"\u001b[K |████████████████████████████████| 58 kB 4.4 MB/s \n", | |
"\u001b[?25hCollecting pydantic!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0,>=1.6.2\n", | |
" Downloading pydantic-1.9.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.9 MB)\n", | |
"\u001b[K |████████████████████████████████| 10.9 MB 54.1 MB/s \n", | |
"\u001b[?25hCollecting anyio<4,>=3.0.0\n", | |
" Downloading anyio-3.5.0-py3-none-any.whl (79 kB)\n", | |
"\u001b[K |████████████████████████████████| 79 kB 7.4 MB/s \n", | |
"\u001b[?25hCollecting sniffio>=1.1\n", | |
" Downloading sniffio-1.2.0-py3-none-any.whl (10 kB)\n", | |
"Collecting mdurl~=0.1\n", | |
" Downloading mdurl-0.1.0-py3-none-any.whl (11 kB)\n", | |
"Collecting linkify-it-py~=1.0\n", | |
" Downloading linkify_it_py-1.0.3-py3-none-any.whl (19 kB)\n", | |
"Collecting mdit-py-plugins\n", | |
" Downloading mdit_py_plugins-0.3.0-py3-none-any.whl (43 kB)\n", | |
"\u001b[K |████████████████████████████████| 43 kB 2.0 MB/s \n", | |
"\u001b[?25hCollecting uc-micro-py\n", | |
" Downloading uc_micro_py-1.0.1-py3-none-any.whl (6.2 kB)\n", | |
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->gradio==2.8.0a100) (0.11.0)\n", | |
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->gradio==2.8.0a100) (3.0.7)\n", | |
"Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->gradio==2.8.0a100) (1.3.2)\n", | |
"Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas->gradio==2.8.0a100) (2018.9)\n", | |
"Collecting bcrypt>=3.1.3\n", | |
" Downloading bcrypt-3.2.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (61 kB)\n", | |
"\u001b[K |████████████████████████████████| 61 kB 409 kB/s \n", | |
"\u001b[?25hCollecting pynacl>=1.0.1\n", | |
" Downloading PyNaCl-1.5.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (856 kB)\n", | |
"\u001b[K |████████████████████████████████| 856 kB 57.5 MB/s \n", | |
"\u001b[?25hCollecting cryptography>=2.5\n", | |
" Downloading cryptography-36.0.1-cp36-abi3-manylinux_2_24_x86_64.whl (3.6 MB)\n", | |
"\u001b[K |████████████████████████████████| 3.6 MB 47.9 MB/s \n", | |
"\u001b[?25hRequirement already satisfied: cffi>=1.1 in /usr/local/lib/python3.7/dist-packages (from bcrypt>=3.1.3->paramiko->gradio==2.8.0a100) (1.15.0)\n", | |
"Requirement already satisfied: pycparser in /usr/local/lib/python3.7/dist-packages (from cffi>=1.1->bcrypt>=3.1.3->paramiko->gradio==2.8.0a100) (2.21)\n", | |
"Collecting h11>=0.8\n", | |
" Downloading h11-0.13.0-py3-none-any.whl (58 kB)\n", | |
"\u001b[K |████████████████████████████████| 58 kB 4.5 MB/s \n", | |
"\u001b[?25hCollecting asgiref>=3.4.0\n", | |
" Downloading asgiref-3.5.0-py3-none-any.whl (22 kB)\n", | |
"Requirement already satisfied: click>=7.0 in /usr/local/lib/python3.7/dist-packages (from uvicorn->gradio==2.8.0a100) (7.1.2)\n", | |
"Building wheels for collected packages: ffmpy, python-multipart\n", | |
" Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Created wheel for ffmpy: filename=ffmpy-0.3.0-py3-none-any.whl size=4712 sha256=fbecaf5e17233322984026ba0bcbbb4f95b02ac8410d2fab4affcb12006c0b31\n", | |
" Stored in directory: /root/.cache/pip/wheels/13/e4/6c/e8059816e86796a597c6e6b0d4c880630f51a1fcfa0befd5e6\n", | |
" Building wheel for python-multipart (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Created wheel for python-multipart: filename=python_multipart-0.0.5-py3-none-any.whl size=31678 sha256=3701c08b4cc56ce656d6cb24ed2b0aae4a9f28450b1f8c1189d7369550a1672c\n", | |
" Stored in directory: /root/.cache/pip/wheels/2c/41/7c/bfd1c180534ffdcc0972f78c5758f89881602175d48a8bcd2c\n", | |
"Successfully built ffmpy python-multipart\n", | |
"Installing collected packages: sniffio, mdurl, uc-micro-py, multidict, markdown-it-py, frozenlist, anyio, yarl, starlette, pynacl, pydantic, monotonic, mdit-py-plugins, linkify-it-py, h11, cryptography, bcrypt, backoff, asynctest, async-timeout, asgiref, aiosignal, uvicorn, python-multipart, pydub, pycryptodome, paramiko, ffmpy, fastapi, analytics-python, aiohttp, gradio\n", | |
"Successfully installed aiohttp-3.8.1 aiosignal-1.2.0 analytics-python-1.4.0 anyio-3.5.0 asgiref-3.5.0 async-timeout-4.0.2 asynctest-0.13.0 backoff-1.10.0 bcrypt-3.2.0 cryptography-36.0.1 fastapi-0.74.1 ffmpy-0.3.0 frozenlist-1.3.0 gradio-2.8.0a100 h11-0.13.0 linkify-it-py-1.0.3 markdown-it-py-2.0.1 mdit-py-plugins-0.3.0 mdurl-0.1.0 monotonic-1.6 multidict-6.0.2 paramiko-2.9.2 pycryptodome-3.14.1 pydantic-1.9.0 pydub-0.25.1 pynacl-1.5.0 python-multipart-0.0.5 sniffio-1.2.0 starlette-0.17.1 uc-micro-py-1.0.1 uvicorn-0.17.5 yarl-1.7.2\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 375 | |
}, | |
"id": "CDM3M5equLpC", | |
"outputId": "879d558b-1070-4f8b-c073-283ba75deca1" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Colab notebook detected. To show errors in colab notebook, set `debug=True` in `launch()`\n" | |
] | |
}, | |
{ | |
"output_type": "error", | |
"ename": "AttributeError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-2-ca5cdb9cb25a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[0mct_run\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclick\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mct_model\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mdisease\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mct_scan\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mct_results\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m \u001b[0mblock\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlaunch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m/usr/local/lib/python3.7/dist-packages/gradio/launchable.py\u001b[0m in \u001b[0;36mlaunch\u001b[0;34m(self, inline, inbrowser, share, debug, auth, auth_message, private_endpoint, prevent_thread_lock, show_error, server_name, server_port, show_tips, enable_queue, height, width, encrypt, cache_examples, favicon_path, ssl_keyfile, ssl_certfile)\u001b[0m\n\u001b[1;32m 126\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 127\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstrings\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0men\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"RUNNING_LOCALLY\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpath_to_local_server\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 128\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mis_colab\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrequires_permissions\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 129\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstrings\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0men\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"MEDIA_PERMISSIONS_IN_COLAB\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 130\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mAttributeError\u001b[0m: 'Blocks' object has no attribute 'requires_permissions'" | |
] | |
} | |
], | |
"source": [ | |
"import gradio as gr\n", | |
"import random\n", | |
"\n", | |
"def xray_model(diseases, img): \n", | |
" return {disease: random.random() for disease in diseases}\n", | |
"\n", | |
"def ct_model(diseases, desc):\n", | |
" return {disease: 0.5 for disease in diseases}\n", | |
"\n", | |
"block = gr.Blocks()\n", | |
"\n", | |
"with block:\n", | |
" gr.Markdown(\"Choose the disease(s) to predict:\")\n", | |
" disease = gr.inputs.CheckboxGroup([\"Covid\", \"Malaria\", \"Lung Cancer\"],\n", | |
" label=\"Disease to Scan For\")\n", | |
"\n", | |
" with gr.Tab(\"X-ray Image\"):\n", | |
" with gr.Row():\n", | |
" xray_scan = gr.inputs.Image()\n", | |
" xray_results = gr.outputs.JSON()\n", | |
" xray_run = gr.Button(\"Run\")\n", | |
" xray_run.click(xray_model, inputs=[disease, xray_scan], outputs=xray_results)\n", | |
"\n", | |
" with gr.Tab(\"Clinical Notes\"):\n", | |
" with gr.Row():\n", | |
" ct_scan = gr.inputs.Textbox(placeholder=\"The patient has reported findings\", lines=7)\n", | |
" ct_results = gr.outputs.JSON()\n", | |
" ct_run = gr.Button(\"Run\")\n", | |
" ct_run.click(ct_model, inputs=[disease, ct_scan], outputs=ct_results)\n", | |
"\n", | |
"block.launch()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "MB8erB2huVyi" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment