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.98k stars 2.58k forks source link

Async state changes do not trigger events #9172

Open irgolic opened 2 months ago

irgolic commented 2 months ago

Describe the bug

Changes to a gr.State do not trigger a render decorator to re-render. This does not happen with other components (I've tested Textbox).

This only happens when updating the value with an async function that yields updates.

Have you searched existing issues? 🔎

Reproduction

Try swapping out text_count, or the increment func

import gradio as gr

async def increment(count):
    yield str(int(count) + 1)

# async def increment(count):
#     return str(int(count) + 1)

with gr.Blocks() as demo:
    text_count = gr.State("0")
    # text_count = gr.Textbox("0")

    add_btn = gr.Button("Add Box")
    add_btn.click(increment, text_count, text_count)

    @gr.render(inputs=text_count)
    def render_count(count):
        boxes = []
        for i in range(int(count)):
            box = gr.Textbox(key=i, label=f"Box {i}")
            boxes.append(box)

        def merge(*args):
            return " ".join(args)

        merge_btn.click(merge, boxes, output)

    merge_btn = gr.Button("Merge")
    output = gr.Textbox(label="Merged Output")

demo.launch()

Screenshot

No response

Logs

No response

System Info

Gradio Environment Information:
------------------------------
Operating System: Darwin
gradio version: 4.42.0
gradio_client version: 1.3.0

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
anyio: 3.7.1
fastapi: 0.112.1
ffmpy: 0.4.0
gradio-client==1.3.0 is not installed.
httpx: 0.27.0
huggingface-hub: 0.24.6
importlib-resources: 6.4.4
jinja2: 3.1.4
markupsafe: 2.1.5
matplotlib: 3.9.2
numpy: 1.26.4
orjson: 3.10.7
packaging: 24.1
pandas: 2.2.2
pillow: 10.4.0
pydantic: 2.8.2
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.2
ruff: 0.6.1
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.4
typing-extensions: 4.12.2
urllib3: 2.2.2
uvicorn: 0.30.6
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.

gradio_client dependencies in your environment:

fsspec: 2024.6.1
httpx: 0.27.0
huggingface-hub: 0.24.6
packaging: 24.1
typing-extensions: 4.12.2
websockets: 12.0

Severity

Blocking usage of gradio

irgolic commented 2 months ago

Update: I've noticed this does not only apply to the render decorator, but to the gr.State.change event too

freddyaboulton commented 2 months ago

Taking a look

irgolic commented 2 months ago

Did you manage to replicate it?

freddyaboulton commented 2 months ago

Yes I think I know what the problem is