Fix returning copies of a component instance from a prediction function (#6940)

* add test

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Abubakar Abid 2024-01-03 11:26:58 -08:00 committed by GitHub
parent de603edfe2
commit c00da89c3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:Fix returning copies of a component instance from a prediction function

View File

@ -1429,7 +1429,7 @@ Received outputs:
)
if isinstance(prediction_value, Block):
prediction_value = prediction_value.constructor_args
prediction_value = prediction_value.constructor_args.copy()
prediction_value["__type__"] = "update"
if utils.is_update(prediction_value):
if output_id in state:

View File

@ -1603,3 +1603,24 @@ def test_emptry_string_api_name_gets_set_as_fn_name():
t1.change(test_fn, t1, t2, api_name="")
assert demo.dependencies[0]["api_name"] == "test_fn"
def test_blocks_postprocessing_with_copies_of_component_instance():
# Test for: https://github.com/gradio-app/gradio/issues/6608
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
chatbot2 = gr.Chatbot()
chatbot3 = gr.Chatbot()
clear = gr.Button("Clear")
def clear_func():
return tuple([gr.Chatbot(value=[])] * 3)
clear.click(
fn=clear_func, outputs=[chatbot, chatbot2, chatbot3], api_name="clear"
)
assert (
demo.postprocess_data(0, [gr.Chatbot(value=[])] * 3, None)
== [{"value": [], "__type__": "update"}] * 3
)