File size: 2,564 Bytes
8076f18
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: blocks_sidebar"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import random\n", "\n", "def generate_pet_name(animal_type, personality):\n", "    cute_prefixes = [\"Fluffy\", \"Ziggy\", \"Bubbles\", \"Pickle\", \"Waffle\", \"Mochi\", \"Cookie\", \"Pepper\"]\n", "    animal_suffixes = {\n", "        \"Cat\": [\"Whiskers\", \"Paws\", \"Mittens\", \"Purrington\"],\n", "        \"Dog\": [\"Woofles\", \"Barkington\", \"Waggins\", \"Pawsome\"],\n", "        \"Bird\": [\"Feathers\", \"Wings\", \"Chirpy\", \"Tweets\"],\n", "        \"Rabbit\": [\"Hops\", \"Cottontail\", \"Bouncy\", \"Fluff\"]\n", "    }\n", "\n", "    prefix = random.choice(cute_prefixes)\n", "    suffix = random.choice(animal_suffixes[animal_type])\n", "\n", "    if personality == \"Silly\":\n", "        prefix = random.choice([\"Sir\", \"Lady\", \"Captain\", \"Professor\"]) + \" \" + prefix\n", "    elif personality == \"Royal\":\n", "        suffix += \" the \" + random.choice([\"Great\", \"Magnificent\", \"Wise\", \"Brave\"])\n", "\n", "    return f\"{prefix} {suffix}\"\n", "\n", "with gr.Blocks(theme=gr.themes.Soft()) as demo:\n", "    with gr.Sidebar(position=\"left\"):\n", "        gr.Markdown(\"# \ud83d\udc3e Pet Name Generator\")\n", "        gr.Markdown(\"Use the options below to generate a unique pet name!\")\n", "\n", "        animal_type = gr.Dropdown(\n", "            choices=[\"Cat\", \"Dog\", \"Bird\", \"Rabbit\"],\n", "            label=\"Choose your pet type\",\n", "            value=\"Cat\"\n", "        )\n", "        personality = gr.Radio(\n", "            choices=[\"Normal\", \"Silly\", \"Royal\"],\n", "            label=\"Personality type\",\n", "            value=\"Normal\"\n", "        )\n", "\n", "    name_output = gr.Textbox(label=\"Your pet's fancy name:\", lines=2)\n", "    generate_btn = gr.Button(\"Generate Name! \ud83c\udfb2\", variant=\"primary\")\n", "    generate_btn.click(\n", "        fn=generate_pet_name,\n", "        inputs=[animal_type, personality],\n", "        outputs=name_output\n", "    )\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}