-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add support for returning multiple messages from gr.ChatInterface
chat function
#10197
Conversation
🪼 branch checks and previews
Install Gradio from this PR pip install https://gradio-pypi-previews.s3.amazonaws.com/f8a50cffcf0d957c7347e1efee3644b95bddbe04/gradio-5.9.1-py3-none-any.whl Install Gradio Python Client from this PR pip install "gradio-client @ git+https://github.com/gradio-app/gradio@f8a50cffcf0d957c7347e1efee3644b95bddbe04#subdirectory=client/python" Install Gradio JS Client from this PR npm install https://gradio-npm-previews.s3.amazonaws.com/f8a50cffcf0d957c7347e1efee3644b95bddbe04/gradio-client-1.8.0.tgz Use Lite from this PR <script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/f8a50cffcf0d957c7347e1efee3644b95bddbe04/dist/lite.js""></script> |
🦄 change detectedThis Pull Request includes changes to the following packages.
With the following changelog entry.
Maintainers or the PR author can modify the PR title to modify this entry.
|
… multiple-messages
gr.ChatInterface
gr.ChatInterface
chat function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes lgtm @abidlabs. theres just two things:
- I think you need to fix the end of the docstring for the
fn
parameter to reflect the changes. - the $code_chatinterface_options breaks the page rendering because it contains
example_code = """
Here's the code I generated:
```python
def greet(x):
return f"Hello, {x}!"
\```
Is this correct?
"""
the ```python is breaking the page. I've seen this before and because i didn't want to change the demo, my suggestion is to instead copy/paste the code here and just remove the backticks/code syntax.
Thanks @aliabd, addressed both of the comments above |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice PR @abidlabs !
One comment is that instead of supporting tuples as files we should point developers towards using a gradio component like gr.File or gr.Image.
I tried converting the demo you wrote to the tuples case and it's resulting in an error because it's treating (None, "You wrote...")
as a file. I guess we could support returning a list of lists but that conflicts with the parameter name type="tuples"
.
import gradio as gr
def echo_multimodal(message, history):
response = []
response.append((None, "You wrote: '" + message["text"] + "' and uploaded:"))
for file in message.get("files", []):
response.append((None, gr.File(value=file)))
return response
demo = gr.ChatInterface(
echo_multimodal,
multimodal=True,
type="tuples",
textbox=gr.MultimodalTextbox(file_count="multiple"),
)
if __name__ == "__main__":
demo.launch()
So I think it would break the ambiguity to use a gradio component.
Ah I see okay yeah let's avoid that for now. I'll revert the docs changes around the file changes |
Should be ok now @freddyaboulton |
gradio/chat_interface.py
Outdated
response, *additional_outputs = response | ||
else: | ||
additional_outputs = None | ||
history = self._append_message_to_history(message, history, "user") | ||
response_ = self.response_as_dict(response) | ||
history = self._append_message_to_history(response_, history, "assistant") # type: ignore | ||
response_ = [response] if not isinstance(response, list) else response |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add this logic to _stream_fn
to support streaming. Otherwise LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some more refactoring to avoid duplication
And of course my refactor broke something |
Thanks for the feedback @freddyaboulton @aliabd! |
This PR firstly allows returning a
list
fromgr.ChatInterface
, which gets rendered as multiple assistant messages. Secondly, it allows files to be returned from a chat function just by returning atuple
, which is not only simpler but also more consistent with how files are actually stored in the history. Also improves test coverage for API use.See
demo/chatinterface_echo_multimodal/run.py
that demonstrates both of these changes.