Add types field to dependency config (#3315)

* Add to config

* CHANGELOG

* Safe pop

* Fix test
This commit is contained in:
Freddy Boulton 2023-02-24 20:07:31 -05:00 committed by GitHub
parent e54042b43b
commit 53e4733f2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 1 deletions

View File

@ -42,7 +42,7 @@ By [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3297](https://git
- Fix change event listed twice in image docs by [@aliabd](https://github.com/aliabd) in [PR 3318](https://github.com/gradio-app/gradio/pull/3318)
## Documentation Changes:
No changes to highlight.
- Added the `types` field to the dependency field in the config by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3315](https://github.com/gradio-app/gradio/pull/3315)
## Testing and Infrastructure Changes:
* Adds a script to benchmark the performance of the queue and adds some instructions on how to use it. By [@freddyaboulton](https://github.com/freddyaboulton) and [@abidlabs](https://github.com/abidlabs) in [PR 3272](https://github.com/gradio-app/gradio/pull/3272)

View File

@ -236,6 +236,10 @@ class Block:
"batch": batch,
"max_batch_size": max_batch_size,
"cancels": cancels or [],
"types": {
"continuous": bool(every),
"generator": inspect.isgeneratorfunction(fn) or bool(every),
},
}
Context.root_block.dependencies.append(dependency)
return dependency
@ -579,6 +583,8 @@ class Blocks(BlockContext):
with block:
iterate_over_children(children)
derived_fields = ["types"]
with Blocks(theme=config["theme"], css=config["theme"]) as blocks:
# ID 0 should be the root Blocks component
original_mapping[0] = Context.root_block or blocks
@ -596,6 +602,8 @@ class Blocks(BlockContext):
# older demos
if dependency["trigger"] == "fake_event":
continue
for field in derived_fields:
dependency.pop(field, None)
targets = dependency.pop("targets")
trigger = dependency.pop("trigger")
dependency.pop("backend_fn")

View File

@ -196,6 +196,7 @@ XRAY_CONFIG = {
"max_batch_size": 4,
"cancels": [],
"every": None,
"types": {"continuous": False, "generator": False},
},
{
"targets": [39],
@ -212,6 +213,7 @@ XRAY_CONFIG = {
"max_batch_size": 4,
"cancels": [],
"every": None,
"types": {"continuous": False, "generator": False},
},
{
"targets": [],
@ -228,6 +230,7 @@ XRAY_CONFIG = {
"max_batch_size": 4,
"cancels": [],
"every": None,
"types": {"continuous": False, "generator": False},
},
],
}
@ -431,6 +434,7 @@ XRAY_CONFIG_DIFF_IDS = {
"max_batch_size": 4,
"cancels": [],
"every": None,
"types": {"continuous": False, "generator": False},
},
{
"targets": [933],
@ -447,6 +451,7 @@ XRAY_CONFIG_DIFF_IDS = {
"max_batch_size": 4,
"cancels": [],
"every": None,
"types": {"continuous": False, "generator": False},
},
{
"targets": [],
@ -463,6 +468,7 @@ XRAY_CONFIG_DIFF_IDS = {
"max_batch_size": 4,
"cancels": [],
"every": None,
"types": {"continuous": False, "generator": False},
},
],
}

View File

@ -325,6 +325,37 @@ class TestBlocksMethods:
completed = True
assert msg["output"]["data"][0] == "Victor"
def test_function_types_documented_in_config(self):
def continuous_fn():
return 42
def generator_function():
for index in range(10):
yield index
with gr.Blocks() as demo:
gr.Number(value=lambda: 2, every=2)
meaning_of_life = gr.Number()
counter = gr.Number()
generator_btn = gr.Button(value="Generate")
greeting = gr.Textbox()
greet_btn = gr.Button(value="Greet")
greet_btn.click(lambda: "Hello!", inputs=None, outputs=[greeting])
generator_btn.click(generator_function, inputs=None, outputs=[counter])
demo.load(continuous_fn, inputs=None, outputs=[meaning_of_life], every=1)
for i, dependency in enumerate(demo.config["dependencies"]):
if i == 0:
assert dependency["types"] == {"continuous": True, "generator": True}
if i == 1:
assert dependency["types"] == {"continuous": False, "generator": False}
if i == 2:
assert dependency["types"] == {"continuous": False, "generator": True}
if i == 3:
assert dependency["types"] == {"continuous": True, "generator": True}
@pytest.mark.asyncio
async def test_run_without_launching(self):
"""Test that we can start the app and use queue without calling .launch().

View File

@ -17,6 +17,11 @@ export interface ComponentMeta {
value?: any;
}
export interface DependencyTypes {
continuous: boolean;
generator: boolean;
}
export interface Dependency {
trigger: string;
targets: Array<number>;
@ -31,6 +36,7 @@ export interface Dependency {
queue: boolean | null;
api_name: string | null;
cancels: Array<number>;
types: DependencyTypes;
}
interface TypeDescription {