gradio/test/components/test_chatbot.py
Freddy Boulton 4221290d84
Support Message API for chatbot and chatinterface (#8422)
* first commit

* Add code

* Tests + code

* lint

* Add code

* notebook

* add changeset

* type

* Add client test

* type

* Add code

* Chatbot type

* Add code

* test chatbot

* fix e2e test

* js tests

* Consolidate Error and Tool message. Allow Messages in postprocess

* Rename to messages

* fix tests

* notebook clean

* More tests and messages

* add changeset

* notebook

* client test

* Fix issues

* Chatbot docs

* add changeset

* Add image

* Add img tag

* Address comments

* Add code

* Revert chatinterface streaming change. Use title in metadata. Address pngwn comments

* Add code

* changelog highlight

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
2024-07-10 11:08:06 +00:00

70 lines
2.5 KiB
Python

from pathlib import Path
import gradio as gr
from gradio import utils
class TestChatbot:
def test_component_functions(self):
"""
Postprocess, get_config
"""
chatbot = gr.Chatbot()
assert chatbot.postprocess(
[["You are **cool**\nand fun", "so are *you*"]]
).model_dump() == [("You are **cool**\nand fun", "so are *you*")]
multimodal_msg = [
[("test/test_files/video_sample.mp4",), "cool video"],
[("test/test_files/audio_sample.wav",), "cool audio"],
[("test/test_files/bus.png", "A bus"), "cool pic"],
[(Path("test/test_files/video_sample.mp4"),), "cool video"],
[(Path("test/test_files/audio_sample.wav"),), "cool audio"],
[(Path("test/test_files/bus.png"), "A bus"), "cool pic"],
]
postprocessed_multimodal_msg = chatbot.postprocess(multimodal_msg).model_dump()
for msg in postprocessed_multimodal_msg:
assert "file" in msg[0]
assert msg[1] in {"cool video", "cool audio", "cool pic"}
assert msg[0]["file"]["path"].split(".")[-1] in {"mp4", "wav", "png"}
if msg[0]["alt_text"]:
assert msg[0]["alt_text"] == "A bus"
assert chatbot.get_config() == {
"value": [],
"label": None,
"show_label": True,
"name": "chatbot",
"show_share_button": False,
"visible": True,
"elem_id": None,
"elem_classes": [],
"container": True,
"min_width": 160,
"scale": None,
"placeholder": None,
"height": None,
"proxy_url": None,
"_selectable": False,
"key": None,
"msg_format": "tuples",
"latex_delimiters": [{"display": True, "left": "$$", "right": "$$"}],
"likeable": False,
"rtl": False,
"show_copy_button": False,
"avatar_images": [None, None],
"sanitize_html": True,
"render_markdown": True,
"bubble_full_width": True,
"line_breaks": True,
"layout": None,
}
def test_avatar_images_are_moved_to_cache(self):
chatbot = gr.Chatbot(avatar_images=("test/test_files/bus.png", None))
assert chatbot.avatar_images[0]
assert utils.is_in_or_equal(
chatbot.avatar_images[0]["path"], chatbot.GRADIO_CACHE
)
assert chatbot.avatar_images[1] is None