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

Adds additional_inputs to gr.ChatInterface #4985

Merged
merged 18 commits into from
Jul 24, 2023
Merged

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Jul 20, 2023

Adds additional_inputs to gr.ChatInterface.

Basic usage example:

import gradio as gr
import time

def echo(message, history, system_prompt, tokens):
    response = f"System prompt: {system_prompt}\n Message: {message}."
    for i in range(min(len(response), int(tokens))):
        time.sleep(0.05)
        yield response[: i+1]

demo = gr.ChatInterface(echo, 
                        additional_inputs=[
                            gr.Textbox("You are helpful AI.", label="System Prompt"), 
                            gr.Slider(10, 100)
                        ]
                       )

if __name__ == "__main__":
    demo.queue().launch()

which produces this:

image

The nice thing is that you don't have to render the additional_inputs in this way. By putting the gr.ChatInterface in a gr.Blocks, you can have the additional_inputs show up wherever you want. For example, to have the system prompt show up on the top:

import gradio as gr
import time

def echo(message, history, system_prompt, tokens):
    response = f"System prompt: {system_prompt}\n Message: {message}."
    for i in range(min(len(response), int(tokens))):
        time.sleep(0.05)
        yield response[: i+1]

with gr.Blocks() as demo:
    system_prompt = gr.Textbox("You are helpful AI.", label="System Prompt")
    slider = gr.Slider(10, 100, render=False)

    gr.ChatInterface(
        echo, additional_inputs=[system_prompt, slider]
    )

demo.queue().launch()

I've added tests and updated the documentation. I'll be OOO so feel free to push directly to this branch if needed or merge it in.

@vercel
Copy link

vercel bot commented Jul 20, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
gradio ✅ Ready (Inspect) Visit Preview Jul 24, 2023 3:49pm

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jul 20, 2023

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-4985-all-demos


You can install the changes in this PR by running:

pip install https://gradio-builds.s3.amazonaws.com/4b0e98e40ac94b8c43ba2158d02ec0ce27366881/gradio-3.38.0-py3-none-any.whl

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jul 21, 2023

🎉 Chromatic build completed!

There are 0 visual changes to review.
There are 0 failed tests to fix.

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jul 21, 2023

🦄 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.

Adds additional_inputs to gr.ChatInterface

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.

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jul 21, 2023

🎉 The demo notebooks match the run.py files! 🎉

@abidlabs abidlabs marked this pull request as ready for review July 22, 2023 00:20
@abidlabs abidlabs changed the title WIP: adding additional_inputs to gr.ChatInterface Adds additional_inputs to gr.ChatInterface Jul 22, 2023
outputs=self.chatbot,
fn=examples_fn,
cache_examples=self.cache_examples,
# cache_examples=cache_examples
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm gonna delete this

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

@@ -130,9 +142,10 @@ def __init__(
self.textbox = Textbox(
container=False,
show_label=False,
label="Message",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we adding a label here if show_label is false?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the label is used as the header of the table when examples are provided with additional inputs

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.

Looks good to me @abidlabs ! Thanks for the tests and feature. Will merge by the end of the weekend unless there are other comments.

@abidlabs
Copy link
Member Author

Appreciate the review @freddyaboulton! I’d keep the label but remove the comment, thanks

This reverts commit 1004285.
@freddyaboulton freddyaboulton merged commit b74f845 into main Jul 24, 2023
12 checks passed
@freddyaboulton freddyaboulton deleted the additionalinputs branch July 24, 2023 15:55
@pngwn pngwn mentioned this pull request Jul 24, 2023
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.

3 participants