Skip to content

Commit f2bd72f

Browse files
abidlabsgradio-pr-botAli Abid
authored
Reset flagged values when switching conversations in chat history (#10292)
* chat history * add changeset * changes * add changeset * changes * changes * more changes * changes * changes * format * notebook * fix test * changes * add changeset * changes * changes * changes * add changeset --------- Co-authored-by: gradio-pr-bot <[email protected]> Co-authored-by: Ali Abid <[email protected]>
1 parent 890eaa3 commit f2bd72f

File tree

14 files changed

+95
-45
lines changed

14 files changed

+95
-45
lines changed

.changeset/short-rice-clean.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@gradio/atoms": minor
3+
"@gradio/chatbot": minor
4+
"gradio": minor
5+
---
6+
7+
feat:Reset flagged values when switching conversations in chat history
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_streaming_echo"]}, {"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 time\n", "import gradio as gr\n", "\n", "def slow_echo(message, history):\n", " for i in range(len(message)):\n", " time.sleep(0.05)\n", " yield \"You typed: \" + message[: i + 1]\n", "\n", "demo = gr.ChatInterface(\n", " slow_echo,\n", " type=\"messages\",\n", " flagging_mode=\"manual\",\n", " flagging_options=[\"Like\", \"Spam\", \"Inappropriate\", \"Other\"], \n", " save_history=True,\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
1+
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_streaming_echo"]}, {"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 time\n", "import gradio as gr\n", "\n", "def slow_echo(message, history):\n", " for i in range(len(message)):\n", " time.sleep(0.05)\n", " yield \"You typed: \" + message[: i + 1]\n", "\n", "demo = gr.ChatInterface(\n", " slow_echo,\n", " type=\"messages\",\n", " flagging_mode=\"manual\",\n", " flagging_options=[\"Like\", \"Spam\", \"Inappropriate\", \"Other\"],\n", " save_history=True,\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}

demo/chatinterface_streaming_echo/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def slow_echo(message, history):
1010
slow_echo,
1111
type="messages",
1212
flagging_mode="manual",
13-
flagging_options=["Like", "Spam", "Inappropriate", "Other"],
13+
flagging_options=["Like", "Spam", "Inappropriate", "Other"],
1414
save_history=True,
1515
)
1616

gradio/chat_interface.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def __init__(
249249

250250
with self:
251251
self.saved_conversations = BrowserState(
252-
[], storage_key="_saved_conversations"
252+
[], storage_key=f"_saved_conversations_{self._id}"
253253
)
254254
self.conversation_id = State(None)
255255
self.saved_input = State() # Stores the most recent user message
@@ -279,7 +279,7 @@ def _render_history_area(self):
279279
with Column(scale=1, min_width=100):
280280
self.new_chat_button = Button(
281281
"New chat",
282-
variant="secondary",
282+
variant="primary",
283283
size="md",
284284
icon=utils.get_icon_path("plus.svg"),
285285
)
@@ -467,6 +467,28 @@ def _delete_conversation(
467467
saved_conversations.pop(index)
468468
return None, saved_conversations
469469

470+
def _load_chat_history(self, conversations):
471+
return Dataset(
472+
samples=[
473+
[self._generate_chat_title(conv)]
474+
for conv in conversations or []
475+
if conv
476+
]
477+
)
478+
479+
def _load_conversation(
480+
self,
481+
index: int,
482+
conversations: list[list[MessageDict]],
483+
):
484+
return (
485+
index,
486+
Chatbot(
487+
value=conversations[index], # type: ignore
488+
feedback_value=[],
489+
),
490+
)
491+
470492
def _setup_events(self) -> None:
471493
from gradio import on
472494

@@ -645,28 +667,29 @@ def _setup_events(self) -> None:
645667
queue=False,
646668
)
647669

648-
@on(
649-
[self.load, self.saved_conversations.change],
670+
on(
671+
triggers=[self.load, self.saved_conversations.change],
672+
fn=self._load_chat_history,
650673
inputs=[self.saved_conversations],
651674
outputs=[self.chat_history_dataset],
652675
show_api=False,
653676
queue=False,
654677
)
655-
def load_chat_history(conversations):
656-
return Dataset(
657-
samples=[
658-
[self._generate_chat_title(conv)]
659-
for conv in conversations or []
660-
if conv
661-
]
662-
)
663678

664679
self.chat_history_dataset.click(
665-
lambda index, conversations: (index, conversations[index]),
680+
lambda: [],
681+
None,
682+
[self.chatbot],
683+
show_api=False,
684+
queue=False,
685+
show_progress="hidden",
686+
).then(
687+
self._load_conversation,
666688
[self.chat_history_dataset, self.saved_conversations],
667689
[self.conversation_id, self.chatbot],
668690
show_api=False,
669691
queue=False,
692+
show_progress="hidden",
670693
).then(**synchronize_chat_state_kwargs)
671694

672695
if self.flagging_mode != "never":

gradio/components/chatbot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ def __init__(
195195
sanitize_html: bool = True,
196196
render_markdown: bool = True,
197197
feedback_options: list[str] | tuple[str, ...] | None = ("Like", "Dislike"),
198+
feedback_value: Sequence[str | None] | None = None,
198199
bubble_full_width=None,
199200
line_breaks: bool = True,
200201
layout: Literal["panel", "bubble"] | None = None,
@@ -234,6 +235,7 @@ def __init__(
234235
sanitize_html: If False, will disable HTML sanitization for chatbot messages. This is not recommended, as it can lead to security vulnerabilities.
235236
render_markdown: If False, will disable Markdown rendering for chatbot messages.
236237
feedback_options: A list of strings representing the feedback options that will be displayed to the user. The exact case-sensitive strings "Like" and "Dislike" will render as thumb icons, but any other choices will appear under a separate flag icon.
238+
feedback_value: A list of strings representing the feedback state for entire chat. Only works when type="messages". Each entry in the list corresponds to that assistant message, in order, and the value is the feedback given (e.g. "Like", "Dislike", or any custom feedback option) or None if no feedback was given for that message.
237239
bubble_full_width: Deprecated.
238240
line_breaks: If True (default), will enable Github-flavored Markdown line breaks in chatbot messages. If False, single new lines will be ignored. Only applies if `render_markdown` is True.
239241
layout: If "panel", will display the chatbot in a llm style layout. If "bubble", will display the chatbot with message bubbles, with the user and bot messages on alterating sides. Will default to "bubble".
@@ -290,6 +292,7 @@ def __init__(
290292
self.show_copy_all_button = show_copy_all_button
291293
self.allow_file_downloads = allow_file_downloads
292294
self.feedback_options = feedback_options
295+
self.feedback_value = feedback_value
293296
super().__init__(
294297
label=label,
295298
every=every,

gradio/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def test(value, like_data: gr.LikeData):
280280
"chatbot_value": value,
281281
"liked_message": like_data.value,
282282
"liked_index": like_data.index,
283-
"liked_or_disliked_as_bool": like_data.liked
283+
"liked_or_disliked": like_data.liked
284284
}
285285
with gr.Blocks() as demo:
286286
c = gr.Chatbot([("abc", "def")])

gradio/icons/plus.svg

Lines changed: 2 additions & 2 deletions
Loading

js/atoms/src/IconButtonWrapper.svelte

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,9 @@
5959
}
6060
6161
.icon-button-wrapper
62-
:global(
63-
a.download-link:not(:last-child):not(.extra-feedback-option)::after
64-
),
62+
:global(a.download-link:not(:last-child):not(.no-border *)::after),
6563
.icon-button-wrapper
66-
:global(button:not(:last-child):not(.extra-feedback-option)::after) {
64+
:global(button:not(:last-child):not(.no-border *)::after) {
6765
content: "";
6866
position: absolute;
6967
right: -4.5px;

js/chatbot/Index.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
export let _selectable = false;
3434
export let likeable = false;
3535
export let feedback_options: string[] = ["Like", "Dislike"];
36+
export let feedback_value: (string | null)[] | null = null;
3637
export let show_share_button = false;
3738
export let rtl = false;
3839
export let show_copy_button = true;
@@ -128,6 +129,7 @@
128129
selectable={_selectable}
129130
{likeable}
130131
{feedback_options}
132+
{feedback_value}
131133
{show_share_button}
132134
{show_copy_all_button}
133135
value={_value}

js/chatbot/shared/ButtonPanel.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
export let position: "right" | "left";
1717
export let avatar: FileData | null;
1818
export let generating: boolean;
19+
export let current_feedback: string | null;
1920
2021
export let handle_action: (selected: string | null) => void;
2122
export let layout: "bubble" | "panel";
@@ -94,7 +95,11 @@
9495
/>
9596
{/if}
9697
{#if likeable}
97-
<LikeDislike {handle_action} {feedback_options} />
98+
<LikeDislike
99+
{handle_action}
100+
{feedback_options}
101+
selected={current_feedback}
102+
/>
98103
{/if}
99104
{/if}
100105
</IconButtonWrapper>

0 commit comments

Comments
 (0)