diff --git a/.changeset/fluffy-cats-drop.md b/.changeset/fluffy-cats-drop.md new file mode 100644 index 0000000000000..e0a0154ce01f7 --- /dev/null +++ b/.changeset/fluffy-cats-drop.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:Fixes for ChatInterface Examples when additional inputs are provided diff --git a/gradio/chat_interface.py b/gradio/chat_interface.py index b2611ac1f34e6..7bdd427a6e6b2 100644 --- a/gradio/chat_interface.py +++ b/gradio/chat_interface.py @@ -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( @@ -225,7 +226,15 @@ 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", @@ -233,7 +242,9 @@ def __init__( 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(): @@ -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, @@ -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( @@ -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]: