File size: 1,806 Bytes
0ad74ed
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: blocks_flag"]}, {"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": ["import numpy as np\n", "import gradio as gr\n", "\n", "def sepia(input_img, strength):\n", "    sepia_filter = strength * np.array(\n", "        [[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]\n", "    ) + (1-strength) * np.identity(3)\n", "    sepia_img = input_img.dot(sepia_filter.T)\n", "    sepia_img /= sepia_img.max()\n", "    return sepia_img\n", "\n", "callback = gr.CSVLogger()\n", "\n", "with gr.Blocks() as demo:\n", "    with gr.Row():\n", "        with gr.Column():\n", "            img_input = gr.Image()\n", "            strength = gr.Slider(0, 1, 0.5)\n", "        img_output = gr.Image()\n", "    with gr.Row():\n", "        btn = gr.Button(\"Flag\")\n", "\n", "    # This needs to be called at some point prior to the first call to callback.flag()\n", "    callback.setup([img_input, strength, img_output], \"flagged_data_points\")\n", "\n", "    img_input.change(sepia, [img_input, strength], img_output)\n", "    strength.change(sepia, [img_input, strength], img_output)\n", "\n", "    # We can choose which components to flag -- in this case, we'll flag all of them\n", "    btn.click(lambda *args: callback.flag(list(args)), [img_input, strength, img_output], None, preprocess=False)\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}