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

Handle the case where examples is null for all components #7192

Merged
merged 14 commits into from Jan 30, 2024
Merged

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Jan 27, 2024

Example values can be null but this case wasn't really handled for any of the components. This PR handles the case for all components as part of fixing #5067. Also fixes the image distortion issue for examples mentioned here: #7180

To test, at least for gr.Textbox and gr.Image, use @radames's test code from #7180:

import gradio as gr
from PIL import Image
import requests

img_s_example = ["https://source.unsplash.com/random/512x512",
                 "https://source.unsplash.com/random/512x1000",
                 "https://source.unsplash.com/random/1000x512"]

pil_imgs = [Image.open(requests.get(url, stream=True).raw) for url in img_s_example]
names = [img.save(f'example_{i}.jpg') or f'example_{i}.jpg' for i, img in enumerate(pil_imgs)]
def predict(text, image):
    return image.rotate(45)

with gr.Blocks() as demo:
    with gr.Row():
      with gr.Column():
        image = gr.Image(label="in")
        text = gr.Text()
      with gr.Column(): 
        image_out = gr.Image(label="ou")
        greet_btn = gr.Button("Rotate")
    greet_btn.click(fn=predict,
                    inputs=[text, image],
                    outputs=[image_out])
    gr.Examples(
        examples=[
            ["hello", "./example_0.jpg"],
            [None, None],
            [None, "./example_1.jpg"],
            [None, "./example_2.jpg"],
            ["dsdsd", None],
        ],
        fn=predict,
        inputs=[text, image],
        outputs=[image_out]
    )

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

Closes: #7180
Closes: #5067

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Jan 27, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
Visual tests all good! Build review
🦄 Changes detected! Details

Install Gradio from this PR

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

Install Gradio Python Client from this PR

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

@abidlabs abidlabs changed the title Handle the case where examples is null Handle the case where examples is null for all components Jan 27, 2024
@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Jan 27, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/audio patch
@gradio/checkbox patch
@gradio/code patch
@gradio/colorpicker patch
@gradio/dataset patch
@gradio/dropdown patch
@gradio/file patch
@gradio/fileexplorer patch
@gradio/image patch
@gradio/markdown patch
@gradio/model3d patch
@gradio/number patch
@gradio/radio patch
@gradio/simpledropdown patch
@gradio/simpleimage patch
@gradio/simpletextbox patch
@gradio/textbox patch
@gradio/video patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Handle the case where examples is null for all components

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 marked this pull request as ready for review January 27, 2024 17:39
@abidlabs
Copy link
Member Author

The static tests are failing even though those files are untouched by this PR. @whitphx do you have any idea of what could be going on?

@abidlabs abidlabs self-assigned this Jan 29, 2024
@whitphx
Copy link
Member

whitphx commented Jan 29, 2024

Hmm I can't find what's the problem

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 but noticed an issue with the empty video. Is it possible to test this via story book? Or is a unit test better here? CC @hannahblair

pnpm-lock.yaml Outdated Show resolved Hide resolved
js/image/Example.svelte Show resolved Hide resolved
@@ -38,7 +38,7 @@
/>
</div>
{:else}
<div>{value}</div>
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case we see a black box - I think it would be better to omit it entirely to be consistent with image and the other components

image

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense

@abidlabs
Copy link
Member Author

Hmm I can't find what's the problem

Thanks @whitphx. Should be fixed now, it was this issue (internal link)

@abidlabs
Copy link
Member Author

Thanks @freddyaboulton for the review, I'll fix those issues and add visual tests to storybook.

@abidlabs
Copy link
Member Author

abidlabs commented Jan 29, 2024

Ok I've added stories for 4 components -- figured that this should be good to catch any regressions without bloating the number of visual tests.

Should be good for another look @freddyaboulton @hannahblair

@@ -9,7 +9,7 @@
class:gallery={type === "gallery"}
class:selected
>
{value.toLocaleString()}
{value ? value.toLocaleString() : ""}
Copy link
Collaborator

Choose a reason for hiding this comment

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

non-blocking + tiny nit: these expressions could be written as value && value.toLocaleString()

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 for pointing this out @hannahblair. As I was testing this, I realized that my original approach won't work since value for Checkbox can be a boolean and if its False, that gets rendered as an empty string. Will revise

@hannahblair
Copy link
Collaborator

Nice! Stories look good to me - agreed that they'll be a good way of catching regressions 👌

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.

Very nice @abidlabs !

@abidlabs
Copy link
Member Author

Thanks @freddyaboulton and @hannahblair for reviewing!

@abidlabs abidlabs merged commit 8dd6f4b into main Jan 30, 2024
18 checks passed
@abidlabs abidlabs deleted the examples-null branch January 30, 2024 00:51
@pngwn pngwn mentioned this pull request Jan 30, 2024
@TonyAssi
Copy link

TonyAssi commented Feb 5, 2024

I'm still having trouble displaying example images on mobile (iOS Chrome),
https://huggingface.co/spaces/tonyassi/image-editor-example

4.16.0 didn't work

Also the solution here doesn't work. In requirements.txt I'm using:
https://gradio-builds.s3.amazonaws.com/f3e3c5c02f1069c1180004a46909309544f42523/gradio-4.16.0-py3-none-any.whl

@abidlabs
Copy link
Member Author

abidlabs commented Feb 5, 2024

Hi @TonyAssi what issue are you running into? This PR didn't handle displaying images on mobile, it was related to setting examples to None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants