Skip to content

Commit

Permalink
chatbot conversation nodes can contain a copy button (#5125)
Browse files Browse the repository at this point in the history
* chatbot conversation nodes can contain a copy button

* add changeset

* the newly added chatbot copy message button is now called show_copy_button

* chatbot's Copy component styling improved

* chatbot's Copy component - typing fix

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 10, 2023
1 parent abec340 commit 80be7a1
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/famous-actors-clap.md
@@ -0,0 +1,6 @@
---
"@gradio/chatbot": minor
"gradio": minor
---

feat:chatbot conversation nodes can contain a copy button
6 changes: 6 additions & 0 deletions gradio/components/chatbot.py
Expand Up @@ -53,6 +53,7 @@ def __init__(
latex_delimiters: list[dict[str, str | bool]] | None = None,
rtl: bool = False,
show_share_button: bool | None = None,
show_copy_button: bool = False,
**kwargs,
):
"""
Expand All @@ -72,6 +73,7 @@ def __init__(
latex_delimiters: A list of dicts of the form {"left": open delimiter (str), "right": close delimiter (str), "display": whether to display in newline (bool)} that will be used to render LaTeX expressions. If not provided, `latex_delimiters` is set to `[{ "left": "$$", "right": "$$", "display": True }]`, so only expressions enclosed in $$ delimiters will be rendered as LaTeX, and in a new line. Pass in an empty list to disable LaTeX rendering. For more information, see the [KaTeX documentation](https://katex.org/docs/autorender.html).
rtl: If True, sets the direction of the rendered text to right-to-left. Default is False, which renders text left-to-right.
show_share_button: If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.
show_copy_button: If True, will show a copy button for each chatbot message.
"""
if color_map is not None:
warn_deprecation("The 'color_map' parameter has been deprecated.")
Expand All @@ -91,6 +93,7 @@ def __init__(
if show_share_button is None
else show_share_button
)
self.show_copy_button = show_copy_button

IOComponent.__init__(
self,
Expand All @@ -115,6 +118,7 @@ def get_config(self):
"height": self.height,
"show_share_button": self.show_share_button,
"rtl": self.rtl,
"show_copy_button": self.show_copy_button,
**IOComponent.get_config(self),
}

Expand All @@ -132,6 +136,7 @@ def update(
height: int | None = None,
rtl: bool | None = None,
show_share_button: bool | None = None,
show_copy_button: bool | None = None,
):
updated_config = {
"label": label,
Expand All @@ -144,6 +149,7 @@ def update(
"height": height,
"show_share_button": show_share_button,
"rtl": rtl,
"show_copy_button": show_copy_button,
"__type__": "update",
}
return updated_config
Expand Down
2 changes: 2 additions & 0 deletions js/chatbot/index.svelte
Expand Up @@ -23,6 +23,7 @@
export let theme_mode: ThemeMode;
export let show_share_button = false;
export let rtl = false;
export let show_copy_button = false;
export let loading_status: LoadingStatus | undefined = undefined;
export let height = 400;
Expand All @@ -43,6 +44,7 @@
{theme_mode}
{show_share_button}
{rtl}
{show_copy_button}
{latex_delimiters}
on:change
on:select
Expand Down
9 changes: 9 additions & 0 deletions js/chatbot/static/ChatBot.svelte
Expand Up @@ -7,6 +7,7 @@
import type { ThemeMode } from "js/app/src/components/types";
import type { FileData } from "@gradio/upload";
import Markdown from "./MarkdownCode.svelte";
import Copy from "./Copy.svelte";
const code_highlight_css = {
light: (): Promise<typeof import("prismjs/themes/prism.css")> =>
Expand All @@ -31,6 +32,7 @@
export let show_share_button = false;
export let theme_mode: ThemeMode;
export let rtl = false;
export let show_copy_button = false;
$: if (theme_mode == "dark") {
code_highlight_css.dark();
Expand Down Expand Up @@ -96,6 +98,7 @@
/>
</div>
{/if}

<div class="wrap" bind:this={div}>
<div class="message-wrap" use:copy>
{#if value !== null}
Expand All @@ -122,6 +125,12 @@
{/each}
</div>
{/if}

{#if show_copy_button && message}
<div class="icon-button">
<Copy value={message} />
</div>
{/if}
{:else if message !== null && message.mime_type?.includes("audio")}
<audio
data-testid="chatbot-audio"
Expand Down
26 changes: 10 additions & 16 deletions js/chatbot/static/Copy.svelte
@@ -1,6 +1,5 @@
<script lang="ts">
import { onDestroy } from "svelte";
import { fade } from "svelte/transition";
import { Copy, Check } from "@gradio/icons";
let copied = false;
Expand Down Expand Up @@ -46,30 +45,25 @@
</script>

<button on:click={handle_copy} title="copy">
<span class="copy-text" class:copied><Copy /> </span>
{#if !copied}
<span><Copy /> </span>
{/if}
{#if copied}
<span class="check" transition:fade><Check /></span>
<span><Check /></span>
{/if}
</button>

<style>
button {
position: relative;
cursor: pointer;
padding: 5px;
top: 0;
right: 0;
width: 22px;
height: 22px;
}
.check {
position: absolute;
top: 0;
right: 0;
z-index: var(--layer-top);
background: var(--background-fill-primary);
padding: var(--size-1);
width: 100%;
height: 100%;
color: var(--body-text-color);
padding: 5px;
cursor: pointer;
}
</style>
2 changes: 2 additions & 0 deletions js/chatbot/static/StaticChatbot.svelte
Expand Up @@ -22,6 +22,7 @@
export let theme_mode: ThemeMode;
export let show_share_button = false;
export let rtl = false;
export let show_copy_button = false;
export let latex_delimiters: {
left: string;
right: string;
Expand Down Expand Up @@ -82,6 +83,7 @@
{latex_delimiters}
pending_message={loading_status?.status === "pending"}
{rtl}
{show_copy_button}
on:change
on:select
on:share
Expand Down
1 change: 1 addition & 0 deletions test/test_components.py
Expand Up @@ -1997,6 +1997,7 @@ def test_component_functions(self):
"selectable": False,
"latex_delimiters": [{"display": True, "left": "$$", "right": "$$"}],
"rtl": False,
"show_copy_button": False,
}


Expand Down

1 comment on commit 80be7a1

@vercel
Copy link

@vercel vercel bot commented on 80be7a1 Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.