How do we provide AUDIO Examples when using Blocks?

Update: Editing question now that I seem to be on the correct track, but having trouble with Audio specifically. If you could scroll down my 3rd message in this thread, I’d appreciate it. Thanks.

When we’re not using gr.Interface(...), but rather using gr.Blocks(), how/where do we… specify examples?

I’m not finding any information in the Key Features Guide or More on Examples for the case of Blocks, or the docs page for Blocks.

I’m also not seeing examples= on the list of supported kwargs to gr.Blocks(). And there doesn’t seem to be any gr.Example() object; the docs go from “Dropdown” to “File”, skipping the letter E.

Wow, ok, just found the actual gr.Examples object! lol. Didn’t see it further down the screen.

You know, it might be a good idea to mention its existence somewhere in the “More on Examples” page. :wink:

1 Like

UPDATE: Ok, I’m having a hard time specifying Audio examples. Maybe I just need to search around for examples. I’m only finding Images and text.

I load in a couple audio waveforms, convert them to channels-last PCM int 16 values, and supply as in the examples string, a tuple of ( sample_rate, waveform_numpy_int16 ).

But I’m getting this error:

    gr.Examples(
  File "/fsx/shawley/envs_sm/aa/lib/python3.10/site-packages/gradio/helpers.py", line 55, in create_examples
    examples_obj = Examples(
  File "/fsx/shawley/envs_sm/aa/lib/python3.10/site-packages/gradio/helpers.py", line 228, in __init__
    self.dataset = components.Dataset(
  File "/fsx/shawley/envs_sm/aa/lib/python3.10/site-packages/gradio/components.py", line 6136, in __init__
    example[i] = component.as_example(ex)
  File "/fsx/shawley/envs_sm/aa/lib/python3.10/site-packages/gradio/components.py", line 2563, in as_example
    return Path(input_data).name if input_data else ""
  File "/home/ec2-user/anaconda3/lib/python3.10/pathlib.py", line 960, in __new__
    self = cls._from_parts(args)
  File "/home/ec2-user/anaconda3/lib/python3.10/pathlib.py", line 594, in _from_parts
    drv, root, parts = self._parse_args(args)
  File "/home/ec2-user/anaconda3/lib/python3.10/pathlib.py", line 578, in _parse_args
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not tuple

But Audio objects pass tuples to their routines right? I tried just passing in the waveform, but my processing routine had expected tuples so it didn’t work.

If I try using filename strings as examples, then when they get to my processing function the variable there is None instead of the string I provided. :person_shrugging:

Ok, one workaround is to turn off caching, outputs, fn, and execute on click. Then the inputs get populated and users can just go press Submit. Works for now.

Hi @drscotthawley !

You should be able to pass in a filepath to a .wav or .mp3 file. Here is an example:

@freddyaboulton thanks; i’ll have to try converting your example from Interface to Blocks,…because despite my trying to pass in strings, they arrive at the function being called as None instead of the string.
This only happens when cache_examples or run_on_click are enabled. When those are disabled, then the string arrives correctly and the user can manually press the Submit button.

Or perhaps the fact that I’m using the queue leads to unexpected behavior?

@freddyaboulton I get similar behavior when I convert your example to Blocks:

with gr.Blocks() as demo: 
 
    inputs = [
        gr.Audio(source="microphone", type="filepath", optional=True, label="Speaker #1"),
        gr.Audio(source="microphone", type="filepath", optional=True, label="Speaker #2"),
    ]
    output = gr.HTML(label="")

    examples = [
        #["samples/cate_blanch.mp3", "samples/cate_blanch_2.mp3"],  # I didn't have these files but you get the idea
        ["/content/metal_trap.wav","/content/metal_trap.wav",]
    ]
    gr.Examples( fn=similarity_fn, examples=examples, inputs=inputs,  outputs=output,
            run_on_click=True,   # <-----****RIGHT HERE. Enabling this causes None to arrive at the inputs when an example is clicked.
            )
    submit_btn = gr.Button("Submit")
    submit_btn.click(fn=similarity_fn, inputs=inputs, outputs=output)

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

I notice this GitHub Issue shows another user reporting a similar problem for Blocks.

Ok we’ll look into it! Thank you @drscotthawley

1 Like

@freddyaboulton Any movement on this?