gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.89k stars 2.57k forks source link

Reload mode should preserve states #8855

Open irgolic opened 3 months ago

irgolic commented 3 months ago

Is your feature request related to a problem? Please describe.
I have some components where input variables are specified, they're generated by a gr.render decorator though, so the actual values are stored in a state. The state does not persist reloads, and I need to input the values every time it reloads.

The same does not hold for normal textboxes – those persist just fine.

Describe the solution you'd like
Upon hot reload, gr.State values should stay what they are. Perhaps a key argument should be added to gr.State to facilitate this.

abidlabs commented 3 months ago

Ah yeah I think adding key to gr.State like we have for the components makes sense -- cc @aliabid94

btw @irgolic can you share a small code snippet so that we can use to test?

irgolic commented 3 months ago

Sure, here's a quick demo:

import gradio as gr

with gr.Blocks() as demo:
    my_state = gr.State([])

    @gr.render(inputs=[my_state])
    def _(rows):
        for r in rows:
            gr.Textbox(r)

    button = gr.Button("+")
    button.click(lambda l: l + ["row"], my_state, my_state)

demo.launch()
irgolic commented 3 months ago

Any updates on this? Anything I can do to help expedite?

freddyaboulton commented 3 months ago

Hi @irgolic - we're busy preparing a 5.0 major release. Contributions on this are welcome - we'd be happy to review any PRs :) 🙏

irgolic commented 3 months ago

Hi @freddyaboulton, good luck with the release!

I'd love to contribute, could you review my other outstanding PR first?