Having a Gradio button call a function which returns large elements


Sorry, I am a newbie here, but let me start by telling you that what you do rocks! This is awesome what we can do, with just few lines of code with Gradio.

My goal is:

  • create a button B1 which calls f1, which updates some fields gr.Textbox or gr.Number for example, but also f1 prepares a large item (imagine a numpy.array of 10 MB eg), which I could print (truncated) or not
  • then, have a second button B2 which would call another function f2, which would use this large item.

I have tried to do my B1 and B2, but I can’t have the large item. I have the impression that Gradio freezes when the large item is too large (eg, ETA clocks stop).

If it is not clear enough, I’ll prepare a small repro code. I have something, but much too large now, and that I can’t share for now. Thanks a lot

Sorry, by trying to have a small repro, I realize that it may be completely my fault, it looks to work with eg np.ones(shape=(1000, 1000, 1000)), my bad!

1 Like

Sounds good @binoua ! Let us know if you have other questions.

1 Like

Finally, I may have something.

import gradio as gr

def func(how_many):
    x = (
        "We believe people shouldn't care about privacy. Not because it doesn't matter, but because it shouldn't be an issue! From data breaches and surveillance of cloud applications, to blockchain smart contracts where everything is public, doing things privately online is becoming increasingly complicated. The only way to solve this is to make everything encrypted end-to-end, regardless of where the application is running. And the only way this can happen is if homomorphic encryption becomes mainstream."
        * int(how_many)
    return 42, x

demo = gr.Blocks()

with demo:
    n = gr.Number(label="Answer to the universal question:", value=0, interactive=False)
    x = gr.Textbox(label="Random text", max_lines=2, interactive=False)
    how_many = gr.Number(label="Small: no bug, Large (eg, 20000): well", value=2)
    btn = gr.Button(value="Please press")
    btn.click(func, inputs=[how_many], outputs=[n, x])

if __name__ == "__main__":

and then python3 app.py and then: press the button with a small value (eg, the default 2), it works; press it with something large like 20000, it looks to block and I even see a 0.1/0.0

Thanks @freddyaboulton

I finally made an issue for it, Issue with a Gradio button if the returned parameter from the function is too large · Issue #1877 · gradio-app/gradio · GitHub