File size: 4,942 Bytes
cbd2ac5
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: file_component_events"]}, {"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", "\n", "def delete_file(n: int, file: gr.DeletedFileData):\n", "    return [file.file.path, n + 1]\n", "\n", "with gr.Blocks() as demo:\n", "\n", "    with gr.Row():\n", "        with gr.Column():\n", "            file_component = gr.File(label=\"Upload Single File\", file_count=\"single\")\n", "        with gr.Column():\n", "            output_file_1 = gr.File(\n", "                label=\"Upload Single File Output\", file_count=\"single\"\n", "            )\n", "            num_load_btn_1 = gr.Number(label=\"# Load Upload Single File\", value=0)\n", "            file_component.upload(\n", "                lambda s, n: (s, n + 1),\n", "                [file_component, num_load_btn_1],\n", "                [output_file_1, num_load_btn_1],\n", "            )\n", "    with gr.Row():\n", "        with gr.Column():\n", "            file_component_multiple = gr.File(\n", "                label=\"Upload Multiple Files\", file_count=\"multiple\"\n", "            )\n", "        with gr.Column():\n", "            output_file_2 = gr.File(\n", "                label=\"Upload Multiple Files Output\", file_count=\"multiple\"\n", "            )\n", "            num_load_btn_2 = gr.Number(label=\"# Load Upload Multiple Files\", value=0)\n", "            file_component_multiple.upload(\n", "                lambda s, n: (s, n + 1),\n", "                [file_component_multiple, num_load_btn_2],\n", "                [output_file_2, num_load_btn_2],\n", "            )\n", "    with gr.Row():\n", "        with gr.Column():\n", "            file_component_specific = gr.File(\n", "                label=\"Upload Multiple Files Image/Video\",\n", "                file_count=\"multiple\",\n", "                file_types=[\"image\", \"video\"],\n", "            )\n", "        with gr.Column():\n", "            output_file_3 = gr.File(\n", "                label=\"Upload Multiple Files Output Image/Video\", file_count=\"multiple\"\n", "            )\n", "            num_load_btn_3 = gr.Number(\n", "                label=\"# Load Upload Multiple Files Image/Video\", value=0\n", "            )\n", "            file_component_specific.upload(\n", "                lambda s, n: (s, n + 1),\n", "                [file_component_specific, num_load_btn_3],\n", "                [output_file_3, num_load_btn_3],\n", "            )\n", "    with gr.Row():\n", "        with gr.Column():\n", "            file_component_pdf = gr.File(label=\"Upload PDF File\", file_types=[\".pdf\"])\n", "        with gr.Column():\n", "            output_file_4 = gr.File(label=\"Upload PDF File Output\")\n", "            num_load_btn_4 = gr.Number(label=\"# Load Upload PDF File\", value=0)\n", "            file_component_pdf.upload(\n", "                lambda s, n: (s, n + 1),\n", "                [file_component_pdf, num_load_btn_4],\n", "                [output_file_4, num_load_btn_4],\n", "            )\n", "    with gr.Row():\n", "        with gr.Column():\n", "            file_component_invalid = gr.File(\n", "                label=\"Upload File with Invalid file_types\",\n", "                file_types=[\"invalid file_type\"],\n", "            )\n", "        with gr.Column():\n", "            output_file_5 = gr.File(label=\"Upload File with Invalid file_types Output\")\n", "            num_load_btn_5 = gr.Number(\n", "                label=\"# Load Upload File with Invalid file_types\", value=0\n", "            )\n", "            file_component_invalid.upload(\n", "                lambda s, n: (s, n + 1),\n", "                [file_component_invalid, num_load_btn_5],\n", "                [output_file_5, num_load_btn_5],\n", "            )\n", "    with gr.Row():\n", "        with gr.Column():\n", "            del_file_input = gr.File(label=\"Delete File\", file_count=\"multiple\")\n", "        with gr.Column():\n", "            del_file_data = gr.Textbox(label=\"Delete file data\")\n", "            num_load_btn_6 = gr.Number(label=\"# Deleted File\", value=0)\n", "            del_file_input.delete(\n", "                delete_file,\n", "                [num_load_btn_6],\n", "                [del_file_data, num_load_btn_6],\n", "            )\n", "    # f = gr.File(label=\"Upload many File\", file_count=\"multiple\")\n", "    # # f.delete(delete_file)\n", "    # f.delete(delete_file, inputs=None, outputs=None)\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}