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

Support saving chat history in gr.ChatInterface #10191

Merged
merged 85 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 78 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
b6bca34
save history prototype
abidlabs Dec 13, 2024
a846b38
add changeset
gradio-pr-bot Dec 13, 2024
d8d64bc
merge
dawoodkhan82 Dec 23, 2024
dfd3ad5
Merge branch 'main' into chat-history
abidlabs Dec 23, 2024
4572a74
Declare exports in __all__ for type checking (#10238)
dustalov Dec 23, 2024
bcd383e
Add `gr.BrowserState` change event (#10245)
abidlabs Dec 23, 2024
a1e0bd1
history
dawoodkhan82 Dec 27, 2024
02caf94
Merge branch 'main' into chat-history
dawoodkhan82 Dec 27, 2024
8ace831
Merge branch 'main' into chat-history
abidlabs Dec 30, 2024
be531b6
Merge branch 'chat-history' of github.com:gradio-app/gradio into chat…
abidlabs Dec 30, 2024
caccb0b
changes
abidlabs Dec 30, 2024
9251dea
changes
abidlabs Dec 30, 2024
08e6615
changes
abidlabs Dec 30, 2024
3cdb67b
history
abidlabs Dec 30, 2024
c20ceda
changes
abidlabs Dec 30, 2024
8438764
changes
abidlabs Dec 30, 2024
269ff00
changes
abidlabs Dec 30, 2024
5477552
format
abidlabs Dec 30, 2024
a51cbef
add changeset
gradio-pr-bot Dec 30, 2024
d8ca538
Merge branch 'main' into chat-history
abidlabs Dec 30, 2024
59baf96
changes
abidlabs Dec 30, 2024
25558a0
changes
abidlabs Dec 30, 2024
ea42b9e
more changes
abidlabs Dec 30, 2024
95b49fb
changes
abidlabs Dec 30, 2024
99bb7df
dataset changes
abidlabs Dec 31, 2024
7a6a8b7
changes
abidlabs Dec 31, 2024
2580513
add changeset
gradio-pr-bot Dec 31, 2024
9ce023f
add md variant for button
abidlabs Dec 31, 2024
dac913b
Merge branch 'chat-history' of github.com:gradio-app/gradio into chat…
abidlabs Dec 31, 2024
4e3a537
add changeset
gradio-pr-bot Dec 31, 2024
464db2b
changes
abidlabs Dec 31, 2024
690912e
changes
abidlabs Dec 31, 2024
303be42
format
abidlabs Dec 31, 2024
8b7cc0e
format
abidlabs Dec 31, 2024
11c34c3
add changeset
gradio-pr-bot Dec 31, 2024
21aa096
changes
abidlabs Dec 31, 2024
9d76b7d
Merge branch 'chat-history' of github.com:gradio-app/gradio into chat…
abidlabs Dec 31, 2024
a2b7542
changes
abidlabs Dec 31, 2024
b7357c1
more changes
abidlabs Dec 31, 2024
7987774
changes
abidlabs Dec 31, 2024
89e5c71
changes
abidlabs Dec 31, 2024
51e1daa
add changeset
gradio-pr-bot Dec 31, 2024
d95022b
changes
abidlabs Dec 31, 2024
5ec7613
Merge branch 'chat-history' of github.com:gradio-app/gradio into chat…
abidlabs Dec 31, 2024
86f4432
docs
abidlabs Dec 31, 2024
d18879b
changes
abidlabs Dec 31, 2024
7c4a8e3
changes
abidlabs Dec 31, 2024
af6523e
changes
abidlabs Dec 31, 2024
037b1e9
changes
abidlabs Dec 31, 2024
ded0b3d
fix
abidlabs Dec 31, 2024
7cbc66f
fix tests
abidlabs Dec 31, 2024
37f4a84
change
abidlabs Dec 31, 2024
6e90ca3
add changeset
gradio-pr-bot Dec 31, 2024
bff664e
fix logo issue
abidlabs Dec 31, 2024
0ec7f25
Merge branch 'chat-history' of github.com:gradio-app/gradio into chat…
abidlabs Dec 31, 2024
6842fa5
changes
abidlabs Dec 31, 2024
5052e5d
version
abidlabs Dec 31, 2024
791deca
add changeset
gradio-pr-bot Dec 31, 2024
59bcf92
fix typecheck
abidlabs Dec 31, 2024
67770ec
Merge branch 'chat-history' of github.com:gradio-app/gradio into chat…
abidlabs Dec 31, 2024
ddbe6a8
remove redundant
abidlabs Dec 31, 2024
92e905e
pkg version
abidlabs Dec 31, 2024
9075df8
add changeset
gradio-pr-bot Dec 31, 2024
13bfe8c
changes
abidlabs Dec 31, 2024
42a4333
Merge branch 'chat-history' of github.com:gradio-app/gradio into chat…
abidlabs Dec 31, 2024
32bed0d
Revert "changes"
abidlabs Dec 31, 2024
48093c1
reorganize code
abidlabs Dec 31, 2024
edeb035
format
abidlabs Dec 31, 2024
abb8a8e
changes
abidlabs Dec 31, 2024
df7c388
add to deployed demos
abidlabs Jan 2, 2025
9e79e86
fix icons
abidlabs Jan 2, 2025
4fe0eaf
fix icon
abidlabs Jan 2, 2025
993b84e
Merge branch 'main' into chat-history
abidlabs Jan 2, 2025
b63a9bb
lint
abidlabs Jan 2, 2025
49be8b0
changes
abidlabs Jan 2, 2025
bcb7bc2
Merge branch 'chat-history' of github.com:gradio-app/gradio into chat…
abidlabs Jan 2, 2025
eaeb078
Merge branch 'main' into chat-history
abidlabs Jan 3, 2025
fc00a7d
example
abidlabs Jan 3, 2025
14f4899
changes
abidlabs Jan 3, 2025
25b590c
fix buttons
abidlabs Jan 3, 2025
01363e2
add changeset
gradio-pr-bot Jan 3, 2025
1d9fb13
format
abidlabs Jan 4, 2025
ba3d100
add changeset
gradio-pr-bot Jan 4, 2025
45deb34
update icon
abidlabs Jan 4, 2025
f136fec
Merge branch 'chat-history' of github.com:gradio-app/gradio into chat…
abidlabs Jan 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/tiny-areas-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@gradio/button": minor
"@gradio/dataset": minor
"@gradio/textbox": minor
"gradio": minor
---

feat:Support saving chat history in `gr.ChatInterface`
1 change: 1 addition & 0 deletions demo/chatinterface_save_history/run.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatinterface_save_history"]}, {"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", "def echo_multimodal(message, history):\n", " response = \"You wrote: '\" + message[\"text\"] + \"' and uploaded: \" + str(len(message[\"files\"])) + \" files\"\n", " return response\n", "\n", "demo = gr.ChatInterface(\n", " echo_multimodal,\n", " type=\"messages\",\n", " multimodal=True,\n", " textbox=gr.MultimodalTextbox(file_count=\"multiple\"),\n", " save_history=True,\n", ")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
16 changes: 16 additions & 0 deletions demo/chatinterface_save_history/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import gradio as gr

def echo_multimodal(message, history):
response = "You wrote: '" + message["text"] + "' and uploaded: " + str(len(message["files"])) + " files"
return response

demo = gr.ChatInterface(
echo_multimodal,
type="messages",
multimodal=True,
textbox=gr.MultimodalTextbox(file_count="multiple"),
save_history=True,
)

if __name__ == "__main__":
demo.launch()
2 changes: 1 addition & 1 deletion demo/chatinterface_streaming_echo/run.ipynb
Original file line number Diff line number Diff line change
@@ -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(slow_echo, type=\"messages\", flagging_mode=\"manual\", flagging_options=[\"Like\", \"Spam\", \"Inappropriate\", \"Other\"])\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
{"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(slow_echo, type=\"messages\", save_history=True)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
8 changes: 7 additions & 1 deletion demo/chatinterface_streaming_echo/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ def slow_echo(message, history):
time.sleep(0.05)
yield "You typed: " + message[: i + 1]

demo = gr.ChatInterface(slow_echo, type="messages", flagging_mode="manual", flagging_options=["Like", "Spam", "Inappropriate", "Other"])
demo = gr.ChatInterface(
slow_echo,
type="messages",
flagging_mode="manual",
flagging_options=["Like", "Spam", "Inappropriate", "Other"],
save_history=True,
)

if __name__ == "__main__":
demo.launch()
2 changes: 1 addition & 1 deletion gradio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@
"ImageEditor",
"ImageMask",
"Info",
"Success",
"Interface",
"JSON",
"Json",
Expand Down Expand Up @@ -204,6 +203,7 @@
"Sketchpad",
"Slider",
"State",
"Success",
"Tab",
"TabItem",
"TabbedInterface",
Expand Down
364 changes: 256 additions & 108 deletions gradio/chat_interface.py

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions gradio/components/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

from collections.abc import Callable, Sequence
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal

from gradio_client.documentation import document
Expand All @@ -29,8 +30,8 @@ def __init__(
every: Timer | float | None = None,
inputs: Component | Sequence[Component] | set[Component] | None = None,
variant: Literal["primary", "secondary", "stop", "huggingface"] = "secondary",
size: Literal["sm", "lg"] | None = None,
icon: str | None = None,
size: Literal["sm", "md", "lg"] | None = None,
Copy link
Member Author

Choose a reason for hiding this comment

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

I added a "md"-sized button as that was ideal for this UI and I think it could be more broadly useful.

Copy link
Member Author

Choose a reason for hiding this comment

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

Made the same change to all of the subclasses of Button below

icon: str | Path | None = None,
link: str | None = None,
visible: bool = True,
interactive: bool = True,
Expand Down
5 changes: 3 additions & 2 deletions gradio/components/clear_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import copy
import json
from collections.abc import Sequence
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal

from gradio_client.documentation import document
Expand Down Expand Up @@ -36,8 +37,8 @@ def __init__(
every: Timer | float | None = None,
inputs: Component | Sequence[Component] | set[Component] | None = None,
variant: Literal["primary", "secondary", "stop"] = "secondary",
size: Literal["sm", "lg"] | None = None,
icon: str | None = None,
size: Literal["sm", "md", "lg"] | None = None,
icon: str | Path | None = None,
link: str | None = None,
visible: bool = True,
interactive: bool = True,
Expand Down
6 changes: 6 additions & 0 deletions gradio/components/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ def __init__(
self,
*,
label: str | None = None,
show_label: bool = True,
components: Sequence[Component] | list[str] | None = None,
component_props: list[dict[str, Any]] | None = None,
samples: list[list[Any]] | None = None,
headers: list[str] | None = None,
type: Literal["values", "index", "tuple"] = "values",
layout: Literal["gallery", "table"] | None = None,
samples_per_page: int = 10,
visible: bool = True,
elem_id: str | None = None,
Expand All @@ -49,10 +51,12 @@ def __init__(
"""
Parameters:
label: the label for this component, appears above the component.
show_label: If True, the label will be shown above the component.
components: Which component types to show in this dataset widget, can be passed in as a list of string names or Components instances. The following components are supported in a Dataset: Audio, Checkbox, CheckboxGroup, ColorPicker, Dataframe, Dropdown, File, HTML, Image, Markdown, Model3D, Number, Radio, Slider, Textbox, TimeSeries, Video
samples: a nested list of samples. Each sublist within the outer list represents a data sample, and each element within the sublist represents an value for each component
headers: Column headers in the Dataset widget, should be the same len as components. If not provided, inferred from component labels
type: "values" if clicking on a sample should pass the value of the sample, "index" if it should pass the index of the sample, or "tuple" if it should pass both the index and the value of the sample.
layout: "gallery" if the dataset should be displayed as a gallery with each sample in a clickable card, or "table" if it should be displayed as a table with each sample in a row. By default, "gallery" is used if there is a single component, and "table" is used if there are more than one component. If there are more than one component, the layout can only be "table".
Copy link
Member Author

Choose a reason for hiding this comment

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

I added some parameters to gr.Dataset that are broadly useful. For example, this one comes up a lot.

samples_per_page: how many examples to show per page.
visible: If False, component will be hidden.
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
Expand All @@ -75,6 +79,8 @@ def __init__(
self.container = container
self.scale = scale
self.min_width = min_width
self.layout = layout
self.show_label = show_label
self._components = [get_component_instance(c) for c in components or []]
if component_props is None:
self.component_props = [
Expand Down
2 changes: 1 addition & 1 deletion gradio/components/download_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(
inputs: Component | Sequence[Component] | set[Component] | None = None,
variant: Literal["primary", "secondary", "stop"] = "secondary",
visible: bool = True,
size: Literal["sm", "lg"] | None = None,
size: Literal["sm", "md", "lg"] | None = None,
abidlabs marked this conversation as resolved.
Show resolved Hide resolved
icon: str | None = None,
scale: int | None = None,
min_width: int | None = None,
Expand Down
5 changes: 3 additions & 2 deletions gradio/components/duplicate_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

from collections.abc import Sequence
from pathlib import Path
from typing import TYPE_CHECKING, Literal

from gradio_client.documentation import document
Expand Down Expand Up @@ -30,8 +31,8 @@ def __init__(
every: Timer | float | None = None,
inputs: Component | Sequence[Component] | set[Component] | None = None,
variant: Literal["primary", "secondary", "stop", "huggingface"] = "huggingface",
size: Literal["sm", "lg"] | None = "sm",
icon: str | None = None,
size: Literal["sm", "md", "lg"] | None = "sm",
abidlabs marked this conversation as resolved.
Show resolved Hide resolved
icon: str | Path | None = None,
link: str | None = None,
visible: bool = True,
interactive: bool = True,
Expand Down
7 changes: 4 additions & 3 deletions gradio/components/login_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import time
import warnings
from collections.abc import Sequence
from pathlib import Path
from typing import TYPE_CHECKING, Literal

from gradio_client.documentation import document

from gradio import utils
from gradio.components import Button, Component
from gradio.context import get_blocks_context
from gradio.routes import Request
Expand All @@ -34,9 +36,8 @@ def __init__(
every: Timer | float | None = None,
inputs: Component | Sequence[Component] | set[Component] | None = None,
variant: Literal["primary", "secondary", "stop", "huggingface"] = "huggingface",
size: Literal["sm", "lg"] | None = None,
icon: str
| None = "https://huggingface.co/front/assets/huggingface_logo-noborder.svg",
size: Literal["sm", "md", "lg"] | None = None,
icon: str | Path | None = utils.get_icon_path("huggingface-logo.svg"),
link: str | None = None,
visible: bool = True,
interactive: bool = True,
Expand Down
2 changes: 2 additions & 0 deletions gradio/icons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The icons in this directory are loaded via `gradio.utils.get_icon_path` and
can be used directly in backend code (e.g. to populate icons in components).
37 changes: 37 additions & 0 deletions gradio/icons/huggingface-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions gradio/icons/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions gradio/themes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,10 @@ def set(
button_small_radius=None,
button_small_text_size=None,
button_small_text_weight=None,
button_medium_padding=None,
button_medium_radius=None,
button_medium_text_size=None,
button_medium_text_weight=None,
button_primary_background_fill=None,
button_primary_background_fill_dark=None,
button_primary_background_fill_hover=None,
Expand Down Expand Up @@ -1006,6 +1010,10 @@ def set(
button_small_radius: The corner radius of a button set to "small" size.
button_small_text_size: The text size of a button set to "small" size.
button_small_text_weight: The text weight of a button set to "small" size.
button_medium_padding: The padding of a button set to "medium" size.
button_medium_radius: The corner radius of a button set to "medium" size.
button_medium_text_size: The text size of a button set to "medium" size.
button_medium_text_weight: The text weight of a button set to "medium" size.
button_transition: The transition animation duration of a button between regular, hover, and focused states.
button_transform_hover: The transform animation of a button on hover.
button_transform_active: The transform animation of a button when pressed.
Expand Down Expand Up @@ -1956,5 +1964,17 @@ def set(
self.button_small_text_weight = button_small_text_weight or getattr(
self, "button_small_text_weight", "400"
)
self.button_medium_padding = button_medium_padding or getattr(
self, "button_medium_padding", "*spacing_md calc(2 * *spacing_md)"
)
self.button_medium_radius = button_medium_radius or getattr(
self, "button_medium_radius", "*radius_md"
)
self.button_medium_text_size = button_medium_text_size or getattr(
self, "button_medium_text_size", "*text_md"
)
self.button_medium_text_weight = button_medium_text_weight or getattr(
self, "button_medium_text_weight", "600"
)

return self
Loading
Loading