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

Improvements to gr.Examples: adds events as attributes and documents, them, adds sample_labels, and visible properties #8733

Merged
merged 17 commits into from
Jul 10, 2024

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Jul 10, 2024

Makes several enhancements to gr.Examples:

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jul 10, 2024

🪼 branch checks and previews

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

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/f9d31cd15c783f747253934990591a1d9a04212a/gradio-4.37.2-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@f9d31cd15c783f747253934990591a1d9a04212a#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-builds.s3.amazonaws.com/f9d31cd15c783f747253934990591a1d9a04212a/gradio-client-1.2.1.tgz

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Jul 10, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/dataset minor
gradio minor
website minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Improvements to gr.Examples: adds events as attributes and documents, them, adds sample_labels, and visible properties

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 changed the title examples Improvements to gr.Examples: adds events as attributes and documents, them, adds sample_labels, and visible properties Jul 10, 2024
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.

I'm having issues using the example_labels prop. I get the ValueError even when my example_labels list is the same length as the examples.

ValueError: The length of `example_labels` should be the same as the number of examples

Tried with this demo:

import gradio as gr


def calculator(num1, operation, num2):
    if operation == "add":
        return num1 + num2
    elif operation == "subtract":
        return num1 - num2
    elif operation == "multiply":
        return num1 * num2
    elif operation == "divide":
        return num1 / num2
    
def calculator2(num1, operation, num2):
    if operation == "add":
        return num1 + num2
    elif operation == "subtract":
        return num1 - num2
    elif operation == "multiply":
        return num1 * num2 * num1
    elif operation == "divide":
        return num1 / num2


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            num_1 = gr.Number(value=4)
            operation = gr.Radio(["add", "subtract", "multiply", "divide"])
            num_2 = gr.Number(value=0)
            submit_btn = gr.Button(value="Calculate")
        with gr.Column():
            result = gr.Number()

    submit_btn.click(
        calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False
    )
    examples = gr.Examples(
        examples=[
            [5, "add", 3],
            [4, "divide", 2],
            [-4, "multiply", 2.5],
            [0, "subtract", 1.2],
        ],
        inputs=[num_1, operation, num_2],
        example_labels=["add", "divide", "multiply", "subtract"],
    )

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

Am I missing something?

gradio/components/dataset.py Outdated Show resolved Hide resolved
gradio/helpers.py Outdated Show resolved Hide resolved
abidlabs and others added 2 commits July 10, 2024 09:24
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
@abidlabs
Copy link
Member Author

Thanks @aliabd, let me take a look

@abidlabs
Copy link
Member Author

Should be fixed now if you can take another look @aliabd!

test/test_helpers.py Outdated Show resolved Hide resolved
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
@aliabd
Copy link
Collaborator

aliabd commented Jul 10, 2024

I'm not sure what I'm looking at here, aren't the labels supposed to show up in the UI?

Screen.Recording.2024-07-10.at.10.18.29.AM.mov

@abidlabs
Copy link
Member Author

abidlabs commented Jul 10, 2024

what?? coulda sworn this was working. you don't need to advertise my bugs in HD video 😆

@abidlabs
Copy link
Member Author

anyways I can repro, will fix in a bit

@abidlabs
Copy link
Member Author

Thanks @aliabd. Turns out that my original approach was broken and I had gotten lucky with my test case. Should be fixed now. Here are a couple of test cases:

(your original example)

import gradio as gr


def calculator(num1, operation, num2):
    if operation == "add":
        return num1 + num2
    elif operation == "subtract":
        return num1 - num2
    elif operation == "multiply":
        return num1 * num2
    elif operation == "divide":
        return num1 / num2
    
def calculator2(num1, operation, num2):
    if operation == "add":
        return num1 + num2
    elif operation == "subtract":
        return num1 - num2
    elif operation == "multiply":
        return num1 * num2 * num1
    elif operation == "divide":
        return num1 / num2


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            num_1 = gr.Number(value=4)
            operation = gr.Radio(["add", "subtract", "multiply", "divide"])
            num_2 = gr.Number(value=0)
            submit_btn = gr.Button(value="Calculate")
        with gr.Column():
            result = gr.Number()

    submit_btn.click(
        calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False
    )
    examples = gr.Examples(
        examples=[
            [5, "add", 3],
            [4, "divide", 2],
            [-4, "multiply", 2.5],
            [0, "subtract", 1.2],
        ],
        inputs=[num_1, operation, num_2],
        example_labels=["add", "divide", "multiply", "subtract"],
    )

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

and

import gradio as gr

with gr.Blocks() as demo:
    t=gr.Textbox()
    i=gr.Image()
    gr.Examples(examples=[["cheetah", "https://github.com/raw/gradio-app/gradio/main/test/test_files/bus.png"], ["cat", "https://github.com/raw/gradio-app/gradio/main/test/test_files/bus.png"]], inputs=[t, i],
               example_labels=["big cat", "small cat"])
    
demo.launch()

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.

Everything works great! 🎉 Thanks @abidlabs!

@abidlabs
Copy link
Member Author

Thanks for testing @aliabd!

@abidlabs abidlabs merged commit fb0daf3 into main Jul 10, 2024
8 checks passed
@abidlabs abidlabs deleted the examples-enhanced branch July 10, 2024 23:35
@pngwn pngwn mentioned this pull request Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants