-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow editing chatbot messages #10203
Open
aliabid94
wants to merge
5
commits into
main
Choose a base branch
from
chatbot_editing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+318
−159
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@gradio/chatbot": minor | ||
"gradio": minor | ||
--- | ||
|
||
feat:Allow editing chatbot messages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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=True)\n", " chatbot2 = gr.Chatbot(value=[], type=\"tuples\", editable=True)\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.SelectData):\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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import gradio as gr | ||
|
||
with gr.Blocks() as demo: | ||
with gr.Row(): | ||
chatbot = gr.Chatbot(value=[], type="messages", editable=True) | ||
chatbot2 = gr.Chatbot(value=[], type="tuples", editable=True) | ||
add_message_btn = gr.Button("Add Message") | ||
|
||
with gr.Row(): | ||
concatenated_text1 = gr.Textbox(label="Concatenated Chat 1") | ||
concatenated_text2 = gr.Textbox(label="Concatenated Chat 2") | ||
edited_messages = gr.Textbox(label="Edited Message") | ||
|
||
def add_message(history: list, history2: list[list[str]]): | ||
usr_msg = "I'm a user" | ||
bot_msg = "I'm a bot" | ||
history.append({"role": "user", "content": usr_msg}) | ||
history.append({"role": "assistant", "content": bot_msg}) | ||
history2.append([usr_msg, bot_msg]) | ||
return history, history2 | ||
|
||
add_message_btn.click(add_message, [chatbot, chatbot2], [chatbot, chatbot2]) | ||
chatbot.change(lambda m: "|".join(m["content"] for m in m), chatbot, concatenated_text1) | ||
chatbot2.change(lambda m: "|".join("|".join(m) for m in m), chatbot2, concatenated_text2) | ||
|
||
def edit_message(edited_message: gr.SelectData): | ||
return f"{edited_message.value} at {edited_message.index}" | ||
|
||
chatbot.edit(edit_message, None, edited_messages) | ||
chatbot2.edit(edit_message, None, edited_messages) | ||
|
||
if __name__ == "__main__": | ||
demo.launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import { | ||
format_chat_for_sharing, | ||
type UndoRetryData, | ||
type EditData, | ||
is_last_bot_message, | ||
group_messages, | ||
load_components, | ||
|
@@ -11,7 +12,6 @@ | |
import { copy } from "@gradio/utils"; | ||
import type { CopyData } from "@gradio/utils"; | ||
import Message from "./Message.svelte"; | ||
import { DownloadLink } from "@gradio/wasm/svelte"; | ||
|
||
import { dequal } from "dequal/lite"; | ||
import { | ||
|
@@ -21,13 +21,11 @@ | |
tick, | ||
onMount | ||
} from "svelte"; | ||
import { Image } from "@gradio/image/shared"; | ||
|
||
import { Trash, Community, ScrollDownArrow } from "@gradio/icons"; | ||
import { IconButtonWrapper, IconButton } from "@gradio/atoms"; | ||
import type { SelectData, LikeData } from "@gradio/utils"; | ||
import type { ExampleMessage } from "../types"; | ||
import { MarkdownCode as Markdown } from "@gradio/markdown-code"; | ||
import type { FileData, Client } from "@gradio/client"; | ||
import type { I18nFormatter } from "js/core/src/gradio_helper"; | ||
import Pending from "./Pending.svelte"; | ||
|
@@ -69,6 +67,7 @@ | |
export let generating = false; | ||
export let selectable = false; | ||
export let likeable = false; | ||
export let editable: boolean | "all" = false; | ||
export let show_share_button = false; | ||
export let show_copy_all_button = false; | ||
export let rtl = false; | ||
|
@@ -91,6 +90,8 @@ | |
export let root: string; | ||
|
||
let target: HTMLElement | null = null; | ||
let edit_index: number | null = null; | ||
let edit_message: string = ""; | ||
|
||
onMount(() => { | ||
target = document.querySelector("div.gradio-container"); | ||
|
@@ -104,6 +105,7 @@ | |
change: undefined; | ||
select: SelectData; | ||
like: LikeData; | ||
edit: EditData; | ||
undo: UndoRetryData; | ||
retry: UndoRetryData; | ||
clear: undefined; | ||
|
@@ -125,12 +127,6 @@ | |
} | ||
|
||
let scroll_after_component_load = false; | ||
function on_child_component_load(): void { | ||
if (scroll_after_component_load) { | ||
scroll_to_bottom(); | ||
scroll_after_component_load = false; | ||
} | ||
} | ||
|
||
async function scroll_on_value_update(): Promise<void> { | ||
if (!autoscroll) return; | ||
|
@@ -170,21 +166,15 @@ | |
|
||
$: { | ||
if (!dequal(value, old_value)) { | ||
console.log("CHANGE!", JSON.stringify(value)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: remove |
||
old_value = value; | ||
dispatch("change"); | ||
} | ||
} | ||
$: groupedMessages = value && group_messages(value, msg_format); | ||
$: options = value && get_last_bot_options(); | ||
|
||
function handle_example_select(i: number, example: ExampleMessage): void { | ||
dispatch("example_select", { | ||
index: i, | ||
value: { text: example.text, files: example.files } | ||
}); | ||
} | ||
|
||
function handle_like( | ||
function handle_action( | ||
i: number, | ||
message: NormalisedMessage, | ||
selected: string | null | ||
|
@@ -201,29 +191,39 @@ | |
index: val_[last_index].index, | ||
value: val_[last_index].content | ||
}); | ||
return; | ||
} | ||
|
||
if (msg_format === "tuples") { | ||
dispatch("like", { | ||
} else if (selected == "edit") { | ||
edit_index = i; | ||
edit_message = message.content as string; | ||
} else if (selected == "edit_cancel") { | ||
edit_index = null; | ||
} else if (selected == "edit_submit") { | ||
edit_index = null; | ||
dispatch("edit", { | ||
index: message.index, | ||
value: message.content, | ||
liked: selected === "like" | ||
value: edit_message | ||
}); | ||
} else { | ||
if (!groupedMessages) return; | ||
|
||
const message_group = groupedMessages[i]; | ||
const [first, last] = [ | ||
message_group[0], | ||
message_group[message_group.length - 1] | ||
]; | ||
|
||
dispatch("like", { | ||
index: [first.index, last.index] as [number, number], | ||
value: message_group.map((m) => m.content), | ||
liked: selected === "like" | ||
}); | ||
if (msg_format === "tuples") { | ||
dispatch("like", { | ||
index: message.index, | ||
value: message.content, | ||
liked: selected === "like" | ||
}); | ||
} else { | ||
if (!groupedMessages) return; | ||
|
||
const message_group = groupedMessages[i]; | ||
const [first, last] = [ | ||
message_group[0], | ||
message_group[message_group.length - 1] | ||
]; | ||
|
||
dispatch("like", { | ||
index: [first.index, last.index] as [number, number], | ||
value: message_group.map((m) => m.content), | ||
liked: selected === "like" | ||
}); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -305,8 +305,15 @@ | |
show_like={role === "user" ? likeable && like_user_message : likeable} | ||
show_retry={_retryable && is_last_bot_message(messages, value)} | ||
show_undo={_undoable && is_last_bot_message(messages, value)} | ||
show_edit={editable === "all" || | ||
(editable && | ||
role === "user" && | ||
messages.length > 0 && | ||
messages[messages.length - 1].type == "text")} | ||
in_edit_mode={edit_index === i} | ||
bind:edit_message | ||
{show_copy_button} | ||
handle_action={(selected) => handle_like(i, messages[0], selected)} | ||
handle_action={(selected) => handle_action(i, messages[0], selected)} | ||
scroll={is_browser ? scroll : () => {}} | ||
{allow_file_downloads} | ||
on:copy={(e) => dispatch("copy", e.detail)} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since "all" is allowed, how about the type is
editable: Literal["user", "all"] | None = None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of creating a new
editable
attribute, why not useinteractive
, which is meant for exactly this use case (i.e. that a user can edit the component's value via the UI)?If the concern is that we want to restrict the scope of interactivity to either user messages or all messages, then I think its time we allow
interactive
to take non-boolean values as well, where it makes sense. For example, we've had requests to restrict the scope of interactivity in the DataFrame component, and this is analogous.In that case, we could use the
interactive
parameter like this:if
interactive
isTrue
, then all messages can be edited. Ifinteractive
is "user", then only user messages can be edited. We may choose to addLiteral["assistant"]
later on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't go this route because a lot of gr.Blocks() demos use chatbot as input to store history, and therefore by default, interactive would be true, so these chatbots would become editable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, ok that makes sense, I agree with @freddyaboulton's suggestion above 👍