Fix component update bug (#6368)

* Add code

* Add comment

* add changeset

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Freddy Boulton 2023-11-10 15:26:07 -05:00 committed by GitHub
parent 4d3aad33a0
commit 8a3f45c261
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:Fix component update bug

View File

@ -132,7 +132,13 @@ def updateable(fn):
def wrapper(*args, **kwargs):
fn_args = inspect.getfullargspec(fn).args
self = args[0]
if not hasattr(self, "_constructor_args"):
# We need to ensure __init__ is always called at least once
# so that the component has all the variables in self defined
# test_blocks.py::test_async_iterator_update_with_new_component
# checks this
initialized_before = hasattr(self, "_constructor_args")
if not initialized_before:
self._constructor_args = []
for i, arg in enumerate(args):
if i == 0 or i >= len(fn_args): # skip self, *args
@ -140,7 +146,7 @@ def updateable(fn):
arg_name = fn_args[i]
kwargs[arg_name] = arg
self._constructor_args.append(kwargs)
if in_event_listener():
if in_event_listener() and initialized_before:
return None
else:
return fn(self, **kwargs)

View File

@ -1575,3 +1575,19 @@ def test_postprocess_update_dict():
("New Country B", "New Country B"),
],
}
def test_async_iterator_update_with_new_component(connect):
async def get_number_stream():
for i in range(10):
yield gr.Number(value=i, label="Number (updates every second)")
await asyncio.sleep(0.1)
demo = gr.Interface(fn=get_number_stream, inputs=None, outputs=["number"])
demo.queue()
with connect(demo) as client:
job = client.submit(api_name="/predict")
job.result()
assert [r["value"] for r in job.outputs()] == list(range(10))