File size: 8,641 Bytes
b03ae25
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: kitchen_sink"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio numpy "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('files')\n", "!wget -q -O files/cantina.wav https://github.com/gradio-app/gradio/raw/main/demo/kitchen_sink/files/cantina.wav\n", "!wget -q -O files/cheetah1.jpg https://github.com/gradio-app/gradio/raw/main/demo/kitchen_sink/files/cheetah1.jpg\n", "!wget -q -O files/lion.jpg https://github.com/gradio-app/gradio/raw/main/demo/kitchen_sink/files/lion.jpg\n", "!wget -q -O files/logo.png https://github.com/gradio-app/gradio/raw/main/demo/kitchen_sink/files/logo.png\n", "!wget -q -O files/time.csv https://github.com/gradio-app/gradio/raw/main/demo/kitchen_sink/files/time.csv\n", "!wget -q -O files/titanic.csv https://github.com/gradio-app/gradio/raw/main/demo/kitchen_sink/files/titanic.csv\n", "!wget -q -O files/tower.jpg https://github.com/gradio-app/gradio/raw/main/demo/kitchen_sink/files/tower.jpg\n", "!wget -q -O files/world.mp4 https://github.com/gradio-app/gradio/raw/main/demo/kitchen_sink/files/world.mp4"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import os\n", "import json\n", "\n", "import numpy as np\n", "\n", "import gradio as gr\n", "\n", "CHOICES = [\"foo\", \"bar\", \"baz\"]\n", "JSONOBJ = \"\"\"{\"items\":{\"item\":[{\"id\": \"0001\",\"type\": null,\"is_good\": false,\"ppu\": 0.55,\"batters\":{\"batter\":[{ \"id\": \"1001\", \"type\": \"Regular\" },{ \"id\": \"1002\", \"type\": \"Chocolate\" },{ \"id\": \"1003\", \"type\": \"Blueberry\" },{ \"id\": \"1004\", \"type\": \"Devil's Food\" }]},\"topping\":[{ \"id\": \"5001\", \"type\": \"None\" },{ \"id\": \"5002\", \"type\": \"Glazed\" },{ \"id\": \"5005\", \"type\": \"Sugar\" },{ \"id\": \"5007\", \"type\": \"Powdered Sugar\" },{ \"id\": \"5006\", \"type\": \"Chocolate with Sprinkles\" },{ \"id\": \"5003\", \"type\": \"Chocolate\" },{ \"id\": \"5004\", \"type\": \"Maple\" }]}]}}\"\"\"\n", "\n", "def fn(\n", "    text1,\n", "    text2,\n", "    num,\n", "    slider1,\n", "    slider2,\n", "    single_checkbox,\n", "    checkboxes,\n", "    radio,\n", "    dropdown,\n", "    multi_dropdown,\n", "    im1,\n", "    # im2,\n", "    # im3,\n", "    im4,\n", "    video,\n", "    audio1,\n", "    audio2,\n", "    file,\n", "    df1,\n", "):\n", "    return (\n", "        (text1 if single_checkbox else text2)\n", "        + \", selected:\"\n", "        + \", \".join(checkboxes),  # Text\n", "        {\n", "            \"positive\": num / (num + slider1 + slider2),\n", "            \"negative\": slider1 / (num + slider1 + slider2),\n", "            \"neutral\": slider2 / (num + slider1 + slider2),\n", "        },  # Label\n", "        (audio1[0], np.flipud(audio1[1]))\n", "        if audio1 is not None\n", "        else os.path.join(os.path.abspath(''), \"files/cantina.wav\"),  # Audio\n", "        np.flipud(im1)\n", "        if im1 is not None\n", "        else os.path.join(os.path.abspath(''), \"files/cheetah1.jpg\"),  # Image\n", "        video\n", "        if video is not None\n", "        else os.path.join(os.path.abspath(''), \"files/world.mp4\"),  # Video\n", "        [\n", "            (\"The\", \"art\"),\n", "            (\"quick brown\", \"adj\"),\n", "            (\"fox\", \"nn\"),\n", "            (\"jumped\", \"vrb\"),\n", "            (\"testing testing testing\", None),\n", "            (\"over\", \"prp\"),\n", "            (\"the\", \"art\"),\n", "            (\"testing\", None),\n", "            (\"lazy\", \"adj\"),\n", "            (\"dogs\", \"nn\"),\n", "            (\".\", \"punc\"),\n", "        ]\n", "        + [(f\"test {x}\", f\"test {x}\") for x in range(10)],  # HighlightedText\n", "        # [(\"The testing testing testing\", None), (\"quick brown\", 0.2), (\"fox\", 1), (\"jumped\", -1), (\"testing testing testing\", 0), (\"over\", 0), (\"the\", 0), (\"testing\", 0), (\"lazy\", 1), (\"dogs\", 0), (\".\", 1)] + [(f\"test {x}\",  x/10) for x in range(-10, 10)],  # HighlightedText\n", "        [\n", "            (\"The testing testing testing\", None),\n", "            (\"over\", 0.6),\n", "            (\"the\", 0.2),\n", "            (\"testing\", None),\n", "            (\"lazy\", -0.1),\n", "            (\"dogs\", 0.4),\n", "            (\".\", 0),\n", "        ]\n", "        + [(\"test\", x / 10) for x in range(-10, 10)],  # HighlightedText\n", "        json.loads(JSONOBJ),  # JSON\n", "        \"<button style='background-color: red'>Click Me: \"\n", "        + radio\n", "        + \"</button>\",  # HTML\n", "        os.path.join(os.path.abspath(''), \"files/titanic.csv\"),\n", "        df1,  # Dataframe\n", "        np.random.randint(0, 10, (4, 4)),  # Dataframe\n", "    )\n", "\n", "demo = gr.Interface(\n", "    fn,\n", "    inputs=[\n", "        gr.Textbox(value=\"Lorem ipsum\", label=\"Textbox\"),\n", "        gr.Textbox(lines=3, placeholder=\"Type here..\", label=\"Textbox 2\"),\n", "        gr.Number(label=\"Number\", value=42),\n", "        gr.Slider(10, 20, value=15, label=\"Slider: 10 - 20\"),\n", "        gr.Slider(maximum=20, step=0.04, label=\"Slider: step @ 0.04\"),\n", "        gr.Checkbox(label=\"Checkbox\"),\n", "        gr.CheckboxGroup(label=\"CheckboxGroup\", choices=CHOICES, value=CHOICES[0:2]),\n", "        gr.Radio(label=\"Radio\", choices=CHOICES, value=CHOICES[2]),\n", "        gr.Dropdown(label=\"Dropdown\", choices=CHOICES),\n", "        gr.Dropdown(\n", "            label=\"Multiselect Dropdown (Max choice: 2)\",\n", "            choices=CHOICES,\n", "            multiselect=True,\n", "            max_choices=2,\n", "        ),\n", "        gr.Image(label=\"Image\"),\n", "        # gr.Image(label=\"Image w/ Cropper\", tool=\"select\"),\n", "        # gr.Image(label=\"Sketchpad\", source=\"canvas\"),\n", "        gr.Image(label=\"Webcam\", sources=[\"webcam\"]),\n", "        gr.Video(label=\"Video\"),\n", "        gr.Audio(label=\"Audio\"),\n", "        gr.Audio(label=\"Microphone\", sources=[\"microphone\"]),\n", "        gr.File(label=\"File\"),\n", "        gr.Dataframe(label=\"Dataframe\", headers=[\"Name\", \"Age\", \"Gender\"]),\n", "    ],\n", "    outputs=[\n", "        gr.Textbox(label=\"Textbox\"),\n", "        gr.Label(label=\"Label\"),\n", "        gr.Audio(label=\"Audio\"),\n", "        gr.Image(label=\"Image\", elem_id=\"output-img\"),\n", "        gr.Video(label=\"Video\"),\n", "        gr.HighlightedText(\n", "            label=\"HighlightedText\", color_map={\"punc\": \"pink\", \"test 0\": \"blue\"}\n", "        ),\n", "        gr.HighlightedText(label=\"HighlightedText\", show_legend=True),\n", "        gr.JSON(label=\"JSON\"),\n", "        gr.HTML(label=\"HTML\"),\n", "        gr.File(label=\"File\"),\n", "        gr.Dataframe(label=\"Dataframe\"),\n", "        gr.Dataframe(label=\"Numpy\"),\n", "    ],\n", "    examples=[\n", "        [\n", "            \"the quick brown fox\",\n", "            \"jumps over the lazy dog\",\n", "            10,\n", "            12,\n", "            4,\n", "            True,\n", "            [\"foo\", \"baz\"],\n", "            \"baz\",\n", "            \"bar\",\n", "            [\"foo\", \"bar\"],\n", "            os.path.join(os.path.abspath(''), \"files/cheetah1.jpg\"),\n", "            # os.path.join(os.path.abspath(''), \"files/cheetah1.jpg\"),\n", "            # os.path.join(os.path.abspath(''), \"files/cheetah1.jpg\"),\n", "            os.path.join(os.path.abspath(''), \"files/cheetah1.jpg\"),\n", "            os.path.join(os.path.abspath(''), \"files/world.mp4\"),\n", "            os.path.join(os.path.abspath(''), \"files/cantina.wav\"),\n", "            os.path.join(os.path.abspath(''), \"files/cantina.wav\"),\n", "            os.path.join(os.path.abspath(''), \"files/titanic.csv\"),\n", "            [[1, 2, 3, 4], [4, 5, 6, 7], [8, 9, 1, 2], [3, 4, 5, 6]],\n", "        ]\n", "    ]\n", "    * 3,\n", "    title=\"Kitchen Sink\",\n", "    description=\"Try out all the components!\",\n", "    article=\"Learn more about [Gradio](http://gradio.app)\",\n", "    cache_examples=True,\n", ")\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}