|
7 | 7 | import builtins
|
8 | 8 | import copy
|
9 | 9 | import inspect
|
| 10 | +import os |
10 | 11 | import warnings
|
11 | 12 | from collections.abc import AsyncGenerator, Callable, Generator, Sequence
|
12 | 13 | from pathlib import Path
|
|
37 | 38 | from gradio.components.multimodal_textbox import MultimodalPostprocess, MultimodalValue
|
38 | 39 | from gradio.context import get_blocks_context
|
39 | 40 | from gradio.events import Dependency, EditData, SelectData
|
| 41 | +from gradio.flagging import ChatCSVLogger |
40 | 42 | from gradio.helpers import create_examples as Examples # noqa: N812
|
41 | 43 | from gradio.helpers import special_args, update
|
42 | 44 | from gradio.layouts import Accordion, Column, Group, Row
|
@@ -85,6 +87,9 @@ def __init__(
|
85 | 87 | title: str | None = None,
|
86 | 88 | description: str | None = None,
|
87 | 89 | theme: Theme | str | None = None,
|
| 90 | + flagging_mode: Literal["never", "manual"] | None = None, |
| 91 | + flagging_options: list[str] | tuple[str, ...] | None = ("Like", "Dislike"), |
| 92 | + flagging_dir: str = ".gradio/flagged", |
88 | 93 | css: str | None = None,
|
89 | 94 | css_paths: str | Path | Sequence[str | Path] | None = None,
|
90 | 95 | js: str | None = None,
|
@@ -122,6 +127,9 @@ def __init__(
|
122 | 127 | title: a title for the interface; if provided, appears above chatbot in large font. Also used as the tab title when opened in a browser window.
|
123 | 128 | description: a description for the interface; if provided, appears above the chatbot and beneath the title in regular font. Accepts Markdown and HTML content.
|
124 | 129 | theme: a Theme object or a string representing a theme. If a string, will look for a built-in theme with that name (e.g. "soft" or "default"), or will attempt to load a theme from the Hugging Face Hub (e.g. "gradio/monochrome"). If None, will use the Default theme.
|
| 130 | + flagging_mode: one of "never", "manual". If "never", users will not see a button to flag an input and output. If "manual", users will see a button to flag. |
| 131 | + flagging_options: a list of strings representing the options that users can choose from when flagging a message. Defaults to ["Like", "Dislike"]. These two case-sensitive strings will render as "thumbs up" and "thumbs down" icon respectively next to each bot message, but any other strings appear under a separate flag icon. |
| 132 | + flagging_dir: path to the the directory where flagged data is stored. If the directory does not exist, it will be created. |
125 | 133 | css: Custom css as a code string. This css will be included in the demo webpage.
|
126 | 134 | css_paths: Custom css as a pathlib.Path to a css file or a list of such paths. This css files will be read, concatenated, and included in the demo webpage. If the `css` parameter is also set, the css from `css` will be included first.
|
127 | 135 | js: Custom js as a code string. The custom js should be in the form of a single js function. This function will automatically be executed when the page loads. For more flexibility, use the head parameter to insert js inside <script> tags.
|
@@ -214,6 +222,18 @@ def __init__(
|
214 | 222 | if self._additional_inputs_in_examples:
|
215 | 223 | break
|
216 | 224 |
|
| 225 | + if flagging_mode is None: |
| 226 | + flagging_mode = os.getenv("GRADIO_CHAT_FLAGGING_MODE", "never") # type: ignore |
| 227 | + if flagging_mode in ["manual", "never"]: |
| 228 | + self.flagging_mode = flagging_mode |
| 229 | + else: |
| 230 | + raise ValueError( |
| 231 | + "Invalid value for `flagging_mode` parameter." |
| 232 | + "Must be: 'manual' or 'never'." |
| 233 | + ) |
| 234 | + self.flagging_options = flagging_options |
| 235 | + self.flagging_dir = flagging_dir |
| 236 | + |
217 | 237 | with self:
|
218 | 238 | with Column():
|
219 | 239 | if title:
|
@@ -501,6 +521,12 @@ def _setup_events(self) -> None:
|
501 | 521 | show_api=False,
|
502 | 522 | ).success(**submit_fn_kwargs).success(**synchronize_chat_state_kwargs)
|
503 | 523 |
|
| 524 | + if self.flagging_mode != "never": |
| 525 | + flagging_callback = ChatCSVLogger() |
| 526 | + flagging_callback.setup(self.flagging_dir) |
| 527 | + self.chatbot.feedback_options = self.flagging_options |
| 528 | + self.chatbot.like(flagging_callback.flag, self.chatbot) |
| 529 | + |
504 | 530 | def _setup_stop_events(
|
505 | 531 | self, event_triggers: list[Callable], events_to_cancel: list[Dependency]
|
506 | 532 | ) -> None:
|
|
0 commit comments