Skip to content

Commit

Permalink
Use updated component in postprocess() (#7402)
Browse files Browse the repository at this point in the history
* component update

* add changeset

* add test

* lint

* refactor

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
abidlabs and gradio-pr-bot authored Feb 12, 2024
1 parent 5b1ab37 commit fa8225d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/blue-walls-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Use updated component in `postprocess()`
3 changes: 3 additions & 0 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,10 @@ def postprocess_data(
raise InvalidComponentError(
f"{block.__class__} Component with id {output_id} not a valid output component."
)
if output_id in state:
block = state[output_id]
prediction_value = block.postprocess(prediction_value)

outputs_cached = processing_utils.move_files_to_cache(
prediction_value,
block, # type: ignore
Expand Down
22 changes: 21 additions & 1 deletion test/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import pytest
import uvicorn
from fastapi.testclient import TestClient
from gradio_client import media_data
from gradio_client import Client, media_data
from PIL import Image

import gradio as gr
Expand Down Expand Up @@ -823,6 +823,26 @@ def run(min, num):
"error" not in session_1.json()
) # no error because sesssion 1 block config was lost when session 3 was added

def test_state_holder_is_used_in_postprocess(self, connect):
with gr.Blocks() as demo:
dropdown = gr.Dropdown(label="list", choices=["Choice 1"], interactive=True)
button = gr.Button("Get dropdown value")
button2 = gr.Button("Convert dropdown to multiselect")
button.click(
lambda x: x, inputs=dropdown, outputs=dropdown, api_name="predict"
)
button2.click(
lambda: gr.Dropdown(multiselect=True),
outputs=dropdown,
api_name="set_multiselect",
)

client: Client
with connect(demo) as client:
assert client.predict("Choice 1", api_name="/predict") == "Choice 1"
client.predict(api_name="/set_multiselect")
assert client.predict("Choice 1", api_name="/predict") == ["Choice 1"]


class TestCallFunction:
@pytest.mark.asyncio
Expand Down

0 comments on commit fa8225d

Please sign in to comment.