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

Fixes .change() in Video, Audio, Image #4793

Merged
merged 33 commits into from Jul 11, 2023
Merged

Fixes .change() in Video, Audio, Image #4793

merged 33 commits into from Jul 11, 2023

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Jul 5, 2023

We had multiple separate issues:

  • The change event would get triggered twice when a file would be uploaded to Video or Image

Test with:

import gradio as gr

def test(x):
    print("change called")
    return x

with gr.Blocks() as demo:
    a = gr.Video()
    b = gr.Video()
    
    a.change(test, a, b)
    
demo.launch()

And:

import gradio as gr

def test(x):
    print("change called")
    return x

with gr.Blocks() as demo:
    a = gr.Image()
    b = gr.Image()
    
    a.change(test, a, b)
    
demo.launch()

or

import gradio as gr

def testa():
    print("a change called")
    return 

def testb():
    print("b change called")
    return 


with gr.Blocks() as demo:
    a = gr.Image(source="canvas", tool="color-sketch")  # try different tools here
    b = gr.Image()
    c = gr.Image()
    
    a.change(lambda x:x, a, b)
    a.change(testa, None, None)
    b.change(lambda x:x, b, c)
    b.change(testb, None, None)
    
demo.launch()
  • The change event would not get triggered for Audio if its value was changed programmatically

Try:

import gradio as gr

with gr.Blocks() as demo:
    a = gr.Audio()
    b = gr.Audio()
    c = gr.Audio()
    
    a.change(lambda x:x, a, b)
    a.change(lambda x:print("a change_called"), a, None)
    b.change(lambda x:x, b, c)
    b.change(lambda x:print("b change_called"), b, None)
    
demo.launch()

This PR fixes these issues above

Closes: #4378, Closes: #4589

Adds unit tests and updates a demo (change_vs_input) that can be used to test the changes as well

@vercel
Copy link

vercel bot commented Jul 5, 2023

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

Name Status Preview Comments Updated (UTC)
gradio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 10, 2023 8:47pm

@gradio-pr-bot
Copy link
Contributor

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

@@ -57,8 +57,6 @@
} else {
value = null;
}

dispatch("change");
Copy link
Member Author

Choose a reason for hiding this comment

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

Not needed because we already dispatch change if the value changes in the subsequent lines

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Jul 5, 2023

Chromatic build successful 🎉

@abidlabs abidlabs changed the title Fixes .change() in Video and Audio Fixes .change() in Video and Audio and Image Jul 5, 2023
@abidlabs abidlabs changed the title Fixes .change() in Video and Audio and Image Fixes .change() in Video, Audio, Image, File Jul 5, 2023
@abidlabs abidlabs changed the title Fixes .change() in Video, Audio, Image, File Fixes .change() in Video, Audio, Image Jul 5, 2023
@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Jul 5, 2023

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

@abidlabs abidlabs marked this pull request as ready for review July 7, 2023 20:57
CHANGELOG.md Outdated
Comment on lines 9 to 10
* The `.change()` event is fixed in `Video`, `Image`, `Audio` by [@abidlabs](https://github.com/abidlabs) in [PR 4793](https://github.com/gradio-app/gradio/pull/4793)

Copy link
Member

Choose a reason for hiding this comment

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

Can we add a more descriptive changelog entry here explaining exactly what was fixed?

Copy link
Member

Choose a reason for hiding this comment

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

I think these should be unit tests as events can easily be tested without spinning up a browser.

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, I'll look into it!

@abidlabs
Copy link
Member Author

Thanks @pngwn for the suggestion. I've replaced the e2e test with unit tests for each of the affected components, following the approach in #4811.

Ready for a re-review!

@abidlabs
Copy link
Member Author

abidlabs commented Jul 10, 2023

Hmmm CI is complaining about this error:

⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯
ReferenceError: CustomEvent is not defined
 ❯ Module.custom_event node_modules/.pnpm/svelte@4.0.0/node_modules/svelte/src/runtime/internal/dom.js:1072:2
    1070|   * @returns {void}
    1071|   */
    1072|  c(html) {
       |  ^
    1073|   this.h(html);
    1074|  }
 ❯ node_modules/.pnpm/svelte@4.0.0/node_modules/svelte/src/runtime/internal/lifecycle.js:111:40
 ❯ dispatch_blob js/audio/src/Audio.svelte:919:3
 ❯ js/audio/src/Audio.svelte:972:5
 ❯ Object.<anonymous> js/app/src/components/Audio/Audio.test.ts:135:11

This error originated in "js/app/src/components/Audio/Audio.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
This error was caught after test environment was torn down. Make sure to cancel any running tasks before test finishes:

But I don't get this error when running locally.

Edit: its no longer complaining about it. Not sure what could be responsible for the flake.

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.

This looks good to me @abidlabs ! Gonna let @hannahblair @pngwn @dawoodkhan82 give the final approval.

@@ -41,6 +44,13 @@
let _value: null | FileData;
$: _value = normalise_file(value, root, root_url);

$: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we're trying to move away from JSON.stringify. I think the Label component has the "preferred" way but gonna defer to @pngwn

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah good to know! Will take a look later

@@ -125,7 +125,7 @@ describe("Video", () => {
root_url: null,
streaming: false,
pending: false,
source: "microphone",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to change the source?

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 Video doesn't have a "microphone" source :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

lol

Copy link
Member

@pngwn pngwn 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! Thanks for fixing this @abidlabs!

@pngwn pngwn merged commit abd0ced into main Jul 11, 2023
11 checks passed
@pngwn pngwn deleted the audio-change branch July 11, 2023 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants