Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to provide a gr.update() dictionary even if postprocess=False #2385

Merged
merged 8 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
docs
  • Loading branch information
abidlabs committed Oct 3, 2022
commit 13c8357a847f609697c58174351a02f22b278cb2
24 changes: 20 additions & 4 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,33 @@ def skip() -> dict:
return update()


def postprocess_update_dict(block: Block, update_dict: Dict):
def postprocess_update_dict(block: Block, update_dict: Dict, postprocess: bool = True):
"""
Converts a dictionary of updates into a format that can be sent to the frontend.
E.g. {"__type__": "generic_update", "value": "2", "interactive": False}
Into -> {"__type__": "update", "value": 2.0, "mode": "static"}

Parameters:
block: The Block that is being updated with this update dictionary.
update_dict: The original update dictionary
postprocess: Whether to postprocess the "value" key of the update dictionary.
"""
prediction_value = block.get_specific_update(update_dict)
if prediction_value.get("value") is components._Keywords.NO_VALUE:
prediction_value.pop("value")
prediction_value = delete_none(prediction_value, skip_value=True)
if "value" in prediction_value:
if "value" in prediction_value and postprocess:
prediction_value["value"] = block.postprocess(prediction_value["value"])
return prediction_value


def convert_update_dict_to_list(outputs_ids: List[int], predictions: Dict) -> List:
def convert_component_dict_to_list(outputs_ids: List[int], predictions: Dict) -> List:
"""
Converts a dictionary of component updates into a list of updates in the order of
the outputs_ids.
E.g. {"textbox": "hello", "number": {"__type__": "generic_update", "value": "2"}}
Into -> ["hello", {"__type__": "generic_update"}, {"__type__": "generic_update", "value": "2"}]
"""
keys_are_blocks = [isinstance(key, Block) for key in predictions.keys()]
if all(keys_are_blocks):
reordered_predictions = [skip() for _ in outputs_ids]
Expand Down Expand Up @@ -705,7 +721,7 @@ def postprocess_data(self, fn_index, predictions, state):
dependency = self.dependencies[fn_index]

if type(predictions) is dict and len(predictions) > 0:
predictions = convert_update_dict_to_list(
predictions = convert_component_dict_to_list(
dependency["outputs"], predictions
)

Expand Down
4 changes: 2 additions & 2 deletions gradio/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import anyio

from gradio import utils
from gradio.blocks import convert_update_dict_to_list, postprocess_update_dict
from gradio.blocks import convert_component_dict_to_list, postprocess_update_dict
from gradio.components import Dataset
from gradio.context import Context
from gradio.documentation import document, set_documentation_group
Expand Down Expand Up @@ -285,7 +285,7 @@ async def predict_example(self, example_id: int) -> List[Any]:

output_ids = [output._id for output in self.outputs]
if type(predictions) is dict and len(predictions) > 0:
predictions = convert_update_dict_to_list(output_ids, predictions)
predictions = convert_component_dict_to_list(output_ids, predictions)
abidlabs marked this conversation as resolved.
Show resolved Hide resolved

if len(self.outputs) == 1:
predictions = [predictions]
Expand Down