Skip to content
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

Improve like/dislike functionality #6572

Merged
merged 9 commits into from Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/long-candles-burn.md
@@ -0,0 +1,7 @@
---
"@gradio/chatbot": patch
"@gradio/icons": patch
"gradio": patch
---

fix:Improve like/dislike functionality
2 changes: 1 addition & 1 deletion demo/chatbot_component/run.ipynb
@@ -1 +1 @@
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_component"]}, {"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", " gr.Chatbot(value=[[\"Hello World\",\"Hey Gradio!\"],[\"\u2764\ufe0f\",\"\ud83d\ude0d\"],[\"\ud83d\udd25\",\"\ud83e\udd17\"]])\n", "\n", "demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_component"]}, {"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", "\n", "def test(x: gr.LikeData):\n", " print(x.index, x.value, x.liked)\n", "\n", "\n", "with gr.Blocks() as demo:\n", " chatbot = gr.Chatbot(\n", " value=[[\"Hello World\", \"Hey Gradio!\"], [\"\u2764\ufe0f\", \"\ud83d\ude0d\"], [\"\ud83d\udd25\", \"\ud83e\udd17\"]]\n", " )\n", "\n", " chatbot.like(test, None, None)\n", "\n", "demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
13 changes: 11 additions & 2 deletions demo/chatbot_component/run.py
@@ -1,6 +1,15 @@
import gradio as gr


def test(x: gr.LikeData):
print(x.index, x.value, x.liked)


with gr.Blocks() as demo:
gr.Chatbot(value=[["Hello World","Hey Gradio!"],["❤️","😍"],["🔥","🤗"]])
chatbot = gr.Chatbot(
value=[["Hello World", "Hey Gradio!"], ["❤️", "😍"], ["🔥", "🤗"]]
)

chatbot.like(test, None, None)

demo.launch()
demo.launch()
18 changes: 18 additions & 0 deletions js/app/test/chatbot_component.spec.ts
@@ -0,0 +1,18 @@
import { test, expect } from "@gradio/tootils";

test("chatbot like and dislike functionality", async ({ page }) => {
const textbox = await page.getByTestId("textbox");
hannahblair marked this conversation as resolved.
Show resolved Hide resolved

await textbox.click();
await page.getByTestId("textbox").fill("hello");
await page.getByLabel("like", { exact: true }).click();
expect(await page.getByLabel("clicked like").count()).toEqual(1);

await page.getByTestId("textbox").click();
await page.getByTestId("textbox").fill("how are you?");

await page.getByLabel("dislike").nth(1).click();
await page.getByLabel("like", { exact: true }).click();

expect(await page.getByLabel("clicked dislike").count()).toEqual(0);
});
12 changes: 4 additions & 8 deletions js/chatbot/shared/ChatBot.svelte
Expand Up @@ -98,12 +98,12 @@
i: number,
j: number,
message: string | { file: FileData; alt_text: string | null } | null,
liked: boolean
selected: string | null
): void {
dispatch("like", {
index: [i, j],
value: message,
liked: liked
liked: selected === "like"
});
}
</script>
Expand Down Expand Up @@ -237,12 +237,8 @@
>
{#if likeable && j == 1}
<LikeDislike
action="like"
handle_action={() => handle_like(i, j, message, true)}
/>
<LikeDislike
action="dislike"
handle_action={() => handle_like(i, j, message, false)}
handle_action={(selected) =>
handle_like(i, j, message, selected)}
/>
{/if}
{#if show_copy_button && message && typeof message === "string"}
Expand Down
33 changes: 15 additions & 18 deletions js/chatbot/shared/LikeDislike.svelte
Expand Up @@ -2,32 +2,29 @@
import { Like } from "@gradio/icons";
import { Dislike } from "@gradio/icons";

export let action: "like" | "dislike";
export let handle_action: () => void;
export let handle_action: (selected: string | null) => void;

let actioned = false;
let Icon = action === "like" ? Like : Dislike;

function action_feedback(): void {
actioned = true;
}
let selected: "like" | "dislike" | null = null;
</script>

<button
on:click={() => {
action_feedback();
handle_action();
selected = "like";
handle_action(selected);
}}
on:keydown={(e) => {
if (e.key === "Enter") {
action_feedback();
handle_action();
}
aria-label={selected === "like" ? "clicked like" : "like"}
>
<Like selected={selected === "like"} />
</button>

<button
on:click={() => {
selected = "dislike";
handle_action(selected);
}}
title={action + " message"}
aria-label={actioned ? `clicked ${action}` : action}
aria-label={selected === "dislike" ? "clicked dislike" : "dislike"}
>
<Icon {actioned} />
<Dislike selected={selected === "dislike"} />
</button>

<style>
Expand Down
4 changes: 2 additions & 2 deletions js/icons/src/Dislike.svelte
@@ -1,13 +1,13 @@
<script lang="ts">
export let actioned: boolean;
export let selected: boolean;
</script>

<svg
xmlns="http://www.w3.org/2000/svg"
width="15px"
height="15px"
viewBox="0 0 24 24"
fill={actioned ? "currentColor" : "none"}
fill={selected ? "currentColor" : "none"}
stroke-width="1.5"
color="currentColor"
><path
Expand Down
4 changes: 2 additions & 2 deletions js/icons/src/Like.svelte
@@ -1,13 +1,13 @@
<script lang="ts">
export let actioned: boolean;
export let selected: boolean;
</script>

<svg
xmlns="http://www.w3.org/2000/svg"
width="15px"
height="15px"
viewBox="0 0 24 24"
fill={actioned ? "currentColor" : "none"}
fill={selected ? "currentColor" : "none"}
stroke-width="1.5"
color="currentColor"
><path
Expand Down