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

Add support for returning multiple messages from gr.ChatInterface chat function #10197

Merged
merged 34 commits into from
Dec 17, 2024

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Dec 13, 2024

This PR firstly allows returning a list from gr.ChatInterface, which gets rendered as multiple assistant messages. Secondly, it allows files to be returned from a chat function just by returning a tuple, 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.

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Dec 13, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
🦄 Changes detected! Details

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>

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Dec 13, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Add support for returning multiple messages from gr.ChatInterface chat function

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@abidlabs abidlabs self-assigned this Dec 13, 2024
@abidlabs abidlabs changed the title Improve returning messages from gr.ChatInterface Add support for returning multiple messages from gr.ChatInterface chat function Dec 13, 2024
@abidlabs abidlabs marked this pull request as ready for review December 13, 2024 21:12
Copy link
Collaborator

@aliabd aliabd left a 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.

@abidlabs
Copy link
Member Author

Thanks @aliabd, addressed both of the comments above

Copy link
Collaborator

@freddyaboulton freddyaboulton left a 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.

@abidlabs
Copy link
Member Author

Ah I see okay yeah let's avoid that for now. I'll revert the docs changes around the file changes

@abidlabs
Copy link
Member Author

Should be ok now @freddyaboulton

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
Copy link
Collaborator

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!

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch thank you!

Copy link
Member Author

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

@abidlabs
Copy link
Member Author

And of course my refactor broke something

@abidlabs
Copy link
Member Author

Thanks for the feedback @freddyaboulton @aliabd!

@abidlabs abidlabs merged commit a95f8ef into main Dec 17, 2024
22 checks passed
@abidlabs abidlabs deleted the multiple-messages branch December 17, 2024 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants