Skip to content

Commit

Permalink
Fixes for ChatInterface Examples when additional inputs are provided (#…
Browse files Browse the repository at this point in the history
…9804)

* fix

* add changeset

* fix

* remove prints

* fix

* fix

---------

Co-authored-by: gradio-pr-bot <[email protected]>
  • Loading branch information
dawoodkhan82 and gradio-pr-bot authored Oct 30, 2024
1 parent 85731e7 commit 458a38c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-cats-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Fixes for ChatInterface Examples when additional inputs are provided
30 changes: 26 additions & 4 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 @@ -354,7 +365,7 @@ def _setup_events(self) -> None:
if (
isinstance(self.chatbot, Chatbot)
and self.examples
and not self.additional_inputs
and not self._additional_inputs_in_examples()
):
if self.cache_examples:
self.chatbot.example_select(
Expand Down Expand Up @@ -717,6 +728,17 @@ def _process_example(
]
return result

def _additional_inputs_in_examples(self):
if self.examples is not None:
for example in self.examples:
for idx, example_for_input in enumerate(example):
if example_for_input is not None:
if idx > 0:
return True
else:
continue
return False

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

0 comments on commit 458a38c

Please sign in to comment.