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

Allow supplying custom gr.Chatbot with events to gr.ChatInterface #8677

Merged
merged 13 commits into from
Jul 2, 2024
5 changes: 5 additions & 0 deletions .changeset/soft-months-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Allow putting suppling custom `gr.Chatbot` with events to `gr.ChatInterface`
4 changes: 2 additions & 2 deletions gradio/chat_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def __init__(
Markdown(description)

if chatbot:
self.chatbot = chatbot.render()
self.chatbot = get_component_instance(chatbot, render=True)
else:
self.chatbot = Chatbot(
label="Chatbot", scale=1, height=200 if fill_height else None
Expand Down Expand Up @@ -211,7 +211,7 @@ def __init__(
else:
textbox.container = False
textbox.show_label = False
textbox_ = textbox.render()
textbox_ = get_component_instance(textbox, render=True)
if not isinstance(textbox_, (Textbox, MultimodalTextbox)):
raise TypeError(
f"Expected a gr.Textbox or gr.MultimodalTextbox component, but got {type(textbox_)}"
Expand Down
19 changes: 19 additions & 0 deletions guides/05_chatbots/01_creating-a-chatbot-fast.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ gr.ChatInterface(

The placeholder appears vertically and horizontally centered in the chatbot.

If you would like to attach event listeners to your custom chatbot, wrap the chatbot as well as the `gr.ChatInterface` inside of a `gr.Blocks` like this:
Copy link
Collaborator

Choose a reason for hiding this comment

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

feel like the "creating a chatbot fast" guide should be quick and sweet, this particular usecase doesn't seem common enough to be here.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's fair, this guide is getting quite bloated. It would be better to move this over to the docs page for gr.ChatInterface. Let me do that


```py
import gradio as gr

def vote(data: gr.LikeData):
if data.liked:
print("You upvoted this response: " + data.value["value"])
else:
print("You downvoted this response: " + data.value["value"])

with gr.Blocks() as demo:
chatbot = gr.Chatbot()
chatbot.like(vote, None, None)
gr.ChatInterface(fn=lambda x,y:x, chatbot=chatbot)

demo.launch()
```

## Add Multimodal Capability to your chatbot

You may want to add multimodal capability to your chatbot. For example, you may want users to be able to easily upload images or files to your chatbot and ask questions about it. You can make your chatbot "multimodal" by passing in a single parameter (`multimodal=True`) to the `gr.ChatInterface` class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def greet(history, input):

def vote(data: gr.LikeData):
if data.liked:
print("You upvoted this response: " + data.value)
print("You upvoted this response: " + data.value["value"])
else:
print("You downvoted this response: " + data.value)
print("You downvoted this response: " + data.value["value"])


with gr.Blocks() as demo:
Expand Down
11 changes: 11 additions & 0 deletions test/test_chat_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ def test_example_caching_with_additional_inputs_already_rendered(self, monkeypat
assert prediction_hello[0].root[0] == ("hello", "robot hello")
assert prediction_hi[0].root[0] == ("hi", "ro")

def test_custom_chatbot_with_events(self):
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
chatbot.like(lambda: None, None, None)
gr.ChatInterface(fn=lambda x, y: x, chatbot=chatbot)
dependencies = demo.fns.values()
assert next(
(d for d in dependencies if d.targets == [(chatbot._id, "like")]),
None,
)


class TestAPI:
def test_get_api_info(self):
Expand Down