Dynamical/Flexible output

Hey :wave:
I wonder if there any way to make dynamical output.
For example if my model found 3 audio then show 3 gr.outputs.Audio()

Hi @Herde, yes this is possible using the new Blocks API:

Basically the idea would be that you can have multiple gr.Audio() outputs, and for each output, you can either use it to play audio or update its visible property to False if you would like to hide it.

Let me know if you have follow up questions!

Hi @abidlabs, I have the same question, but with the documentation I am unable to implement it. Let’s say my model returns a variable number of images (as a list or a tuple of np.arrays) and I want the app to dynamically set the number of output blocks based on the model output’s length. I found on the website an example with a Slider:

but I am stuck if I want to implement something similar with a button, because I have no access to the length of my output within the interface.

Here is my (dummy) code, based on that example:

max_outputs = 11

def variable_outputs(k):
    k = int(k)
    return[gr.Image(visible=True)]*k + [gr.Image(visible=False)] * (max_outputs - k)

def predict(img):
    return [img, ~img, img]

with gr.Blocks() as demo:
    with gr.Row():
        img_input = gr.Image()
        out_boxes = []
        for i in range(max_outputs):
            out_box = gr.Image()
            out_boxes.append(out_box)
        
    gr.Button("Predict").click(predict, inputs=img_input, outputs=variable_outputs(len(out_func)))

Not surprisingly, this code raises the error

TypeError: object of type 'function' has no len()

How can I have access to the output’s length during or after the click on the button?