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

Fixes for ChatInterface Examples when additional inputs are provided #9804

Merged
merged 8 commits into from
Oct 30, 2024
Merged
Changes from 1 commit
Commits
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
24 changes: 21 additions & 3 deletions gradio/chat_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def __init__(
examples_messages.append(example_message)

self.provided_chatbot = chatbot is not None

if chatbot:
if self.type != chatbot.type:
warnings.warn(
Expand All @@ -225,15 +226,25 @@ def __init__(
self.chatbot = cast(
Chatbot, get_component_instance(chatbot, render=True)
)
self.chatbot.examples = examples_messages
if self.chatbot.examples and examples_messages:
warnings.warn(
"The ChatInterface already has examples set. The examples provided in the chatbot will be ignored."
)
self.chatbot.examples = (
examples_messages
if not self._additional_inputs_in_examples()
else None
)
else:
self.chatbot = Chatbot(
label="Chatbot",
scale=1,
height=200 if fill_height else None,
type=self.type,
autoscroll=autoscroll,
examples=examples_messages if not self.additional_inputs else None,
examples=examples_messages
if not self._additional_inputs_in_examples()
else None,
)

with Group():
Expand Down Expand Up @@ -272,7 +283,7 @@ def __init__(
examples_fn = self._examples_stream_fn
else:
examples_fn = self._examples_fn
if self.examples and self.additional_inputs:
if self.examples and self._additional_inputs_in_examples():
self.examples_handler = Examples(
examples=examples,
inputs=[self.textbox] + self.additional_inputs,
Expand Down Expand Up @@ -717,6 +728,13 @@ def _process_example(
]
return result

def _additional_inputs_in_examples(self):
dawoodkhan82 marked this conversation as resolved.
Show resolved Hide resolved
if self.examples is not None:
for example in self.examples:
if isinstance(example, list) and len(example) > 1:
return True
return False

async def _examples_fn(
self, message: ExampleMessage | str, *args
) -> TupleFormat | list[MessageDict]:
Expand Down
Loading