From 391a4d09a90406b768eee30e1c12a0bc84fc27a2 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Thu, 9 Jan 2025 13:39:03 -0800 Subject: [PATCH] Add `.previous_value` to `gr.EditData` (#10323) * changes * add changeset * changes --------- Co-authored-by: gradio-pr-bot --- .changeset/funny-pets-win.md | 6 ++++++ demo/chatbot_editable/run.ipynb | 2 +- demo/chatbot_editable/run.py | 2 +- gradio/events.py | 6 +++++- js/chatbot/shared/ChatBot.svelte | 3 ++- js/chatbot/shared/utils.ts | 1 + js/spa/test/chatbot_editable.spec.ts | 6 ++++-- 7 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 .changeset/funny-pets-win.md diff --git a/.changeset/funny-pets-win.md b/.changeset/funny-pets-win.md new file mode 100644 index 0000000000000..796e7ac39a41c --- /dev/null +++ b/.changeset/funny-pets-win.md @@ -0,0 +1,6 @@ +--- +"@gradio/chatbot": minor +"gradio": minor +--- + +feat:Add `.previous_value` to `gr.EditData` diff --git a/demo/chatbot_editable/run.ipynb b/demo/chatbot_editable/run.ipynb index 8d69157334946..52e7b86c3fdfa 100644 --- a/demo/chatbot_editable/run.ipynb +++ b/demo/chatbot_editable/run.ipynb @@ -1 +1 @@ -{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_editable"]}, {"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", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " chatbot = gr.Chatbot(value=[], type=\"messages\", editable=\"user\")\n", " chatbot2 = gr.Chatbot(value=[], type=\"tuples\", editable=\"user\")\n", " add_message_btn = gr.Button(\"Add Message\")\n", "\n", " with gr.Row():\n", " concatenated_text1 = gr.Textbox(label=\"Concatenated Chat 1\")\n", " concatenated_text2 = gr.Textbox(label=\"Concatenated Chat 2\")\n", " edited_messages = gr.Textbox(label=\"Edited Message\")\n", "\n", " def add_message(history: list, history2: list[list[str]]):\n", " usr_msg = \"I'm a user\"\n", " bot_msg = \"I'm a bot\"\n", " history.append({\"role\": \"user\", \"content\": usr_msg})\n", " history.append({\"role\": \"assistant\", \"content\": bot_msg})\n", " history2.append([usr_msg, bot_msg])\n", " return history, history2\n", " \n", " add_message_btn.click(add_message, [chatbot, chatbot2], [chatbot, chatbot2])\n", " chatbot.change(lambda m: \"|\".join(m[\"content\"] for m in m), chatbot, concatenated_text1)\n", " chatbot2.change(lambda m: \"|\".join(\"|\".join(m) for m in m), chatbot2, concatenated_text2)\n", "\n", " def edit_message(edited_message: gr.EditData):\n", " return f\"{edited_message.value} at {edited_message.index}\"\n", " \n", " chatbot.edit(edit_message, None, edited_messages)\n", " chatbot2.edit(edit_message, None, edited_messages)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file +{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_editable"]}, {"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", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " chatbot = gr.Chatbot(value=[], type=\"messages\", editable=\"user\")\n", " chatbot2 = gr.Chatbot(value=[], type=\"tuples\", editable=\"user\")\n", " add_message_btn = gr.Button(\"Add Message\")\n", "\n", " with gr.Row():\n", " concatenated_text1 = gr.Textbox(label=\"Concatenated Chat 1\")\n", " concatenated_text2 = gr.Textbox(label=\"Concatenated Chat 2\")\n", " edited_messages = gr.Textbox(label=\"Edited Message\")\n", "\n", " def add_message(history: list, history2: list[list[str]]):\n", " usr_msg = \"I'm a user\"\n", " bot_msg = \"I'm a bot\"\n", " history.append({\"role\": \"user\", \"content\": usr_msg})\n", " history.append({\"role\": \"assistant\", \"content\": bot_msg})\n", " history2.append([usr_msg, bot_msg])\n", " return history, history2\n", " \n", " add_message_btn.click(add_message, [chatbot, chatbot2], [chatbot, chatbot2])\n", " chatbot.change(lambda m: \"|\".join(m[\"content\"] for m in m), chatbot, concatenated_text1)\n", " chatbot2.change(lambda m: \"|\".join(\"|\".join(m) for m in m), chatbot2, concatenated_text2)\n", "\n", " def edit_message(edited_message: gr.EditData):\n", " return f\"from {edited_message.previous_value} to {edited_message.value} at {edited_message.index}\"\n", " \n", " chatbot.edit(edit_message, None, edited_messages)\n", " chatbot2.edit(edit_message, None, edited_messages)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file diff --git a/demo/chatbot_editable/run.py b/demo/chatbot_editable/run.py index 69e46e5efb5f1..c20640828531a 100644 --- a/demo/chatbot_editable/run.py +++ b/demo/chatbot_editable/run.py @@ -24,7 +24,7 @@ def add_message(history: list, history2: list[list[str]]): chatbot2.change(lambda m: "|".join("|".join(m) for m in m), chatbot2, concatenated_text2) def edit_message(edited_message: gr.EditData): - return f"{edited_message.value} at {edited_message.index}" + return f"from {edited_message.previous_value} to {edited_message.value} at {edited_message.index}" chatbot.edit(edit_message, None, edited_messages) chatbot2.edit(edit_message, None, edited_messages) diff --git a/gradio/events.py b/gradio/events.py index b3ee2ec73cd6b..fa41a26c18a24 100644 --- a/gradio/events.py +++ b/gradio/events.py @@ -397,9 +397,13 @@ def __init__(self, target: Block | None, data: Any): """ The index of the message that was edited. """ + self.previous_value: Any = data["previous_value"] + """ + The previous content of the message that was edited. + """ self.value: Any = data["value"] """ - The content of the message that was edited. + The new content of the message that was edited. """ diff --git a/js/chatbot/shared/ChatBot.svelte b/js/chatbot/shared/ChatBot.svelte index 4017a376a9458..4a5a73b8766f3 100644 --- a/js/chatbot/shared/ChatBot.svelte +++ b/js/chatbot/shared/ChatBot.svelte @@ -201,7 +201,8 @@ edit_index = null; dispatch("edit", { index: message.index, - value: edit_message + value: edit_message, + previous_value: message.content as string }); } else { let feedback = diff --git a/js/chatbot/shared/utils.ts b/js/chatbot/shared/utils.ts index d0ca141f2b563..2634c656a960b 100644 --- a/js/chatbot/shared/utils.ts +++ b/js/chatbot/shared/utils.ts @@ -70,6 +70,7 @@ export interface UndoRetryData { export interface EditData { index: number | [number, number]; value: string; + previous_value: string; } const redirect_src_url = (src: string, root: string): string => diff --git a/js/spa/test/chatbot_editable.spec.ts b/js/spa/test/chatbot_editable.spec.ts index 3f333ff4e2087..19e3583831683 100644 --- a/js/spa/test/chatbot_editable.spec.ts +++ b/js/spa/test/chatbot_editable.spec.ts @@ -12,7 +12,9 @@ test("test editing chatbot messages", async ({ page }) => { await page.getByLabel("Edit").nth(0).click(); await page.locator("textarea").first().fill("GRADIO"); await page.getByLabel("Submit").click(); - await expect(page.getByLabel("Edited Message")).toHaveValue("GRADIO at 0"); + await expect(page.getByLabel("Edited Message")).toHaveValue( + "from I'm a user to GRADIO at 0" + ); await page.getByLabel("Edit").nth(2).click(); await page.locator("textarea").first().fill("FAIL"); await page.getByLabel("Cancel").click(); @@ -20,7 +22,7 @@ test("test editing chatbot messages", async ({ page }) => { await page.locator("textarea").first().fill("SUCCESS"); await page.getByLabel("Submit").click(); await expect(page.getByLabel("Edited Message")).toHaveValue( - "SUCCESS at [1, 0]" + "from I'm a user to SUCCESS at [1, 0]" ); await expect(page.getByLabel("Concatenated Chat 1")).toHaveValue( "GRADIO|I'm a bot|I'm a user|I'm a bot"