From c00da89c3ec6aa9ce43b25f12fde575d681d6870 Mon Sep 17 00:00:00 2001 From: Abubakar Abid Date: Wed, 3 Jan 2024 11:26:58 -0800 Subject: [PATCH] Fix returning copies of a component instance from a prediction function (#6940) * add test * add changeset --------- Co-authored-by: gradio-pr-bot --- .changeset/tangy-spoons-hug.md | 5 +++++ gradio/blocks.py | 2 +- test/test_blocks.py | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .changeset/tangy-spoons-hug.md diff --git a/.changeset/tangy-spoons-hug.md b/.changeset/tangy-spoons-hug.md new file mode 100644 index 0000000000..00cb5b5c3e --- /dev/null +++ b/.changeset/tangy-spoons-hug.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:Fix returning copies of a component instance from a prediction function diff --git a/gradio/blocks.py b/gradio/blocks.py index c7077bfff0..57cbafe1e2 100644 --- a/gradio/blocks.py +++ b/gradio/blocks.py @@ -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: diff --git a/test/test_blocks.py b/test/test_blocks.py index 05758d8016..fbb860bb42 100644 --- a/test/test_blocks.py +++ b/test/test_blocks.py @@ -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 + )