Skip to content

Commit

Permalink
Pending events behavior (#5826)
Browse files Browse the repository at this point in the history
* enter changesets pre-release mode

* release first version (#5500)

* release packages

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* fix changeset (#5502)

* add changeset

* release first version

* Custom components (#5507)

* Add flag

* Copy front-end

* Add backend code

* Remove breakpoint

* Fix init

* Add code

* File reorg

* Fix types

* Upload files

* Fix code

* Custom components

* Add code

* Client changes

* More changes

* Add code

* Add code

* Fix reversion

* build hook integration

* Add code

* rm file

* Add code

* rename cli

* Add code

* Nicer cli

* Add display class

* Add backend devmide

* Add code?

* Add code

* Add paths and fix reload mdoe

* Fix .then

* changes

* fix

* fix

* changes

* changes

* changes

* Fix versions

* Fix wheel build from source

* Add code

* Get no template case working

* Cc fe (#5509)

* tweaks

* fix 18n

* fix 18n

* tweaks

* Change name

---------

Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>

* Do not use live display for dev mode

* Clean up front-end server print statements a little bit

* Fix success event

* Cc fe 2 (#5526)

* tweaks

* fix examples

* fix backend port

* fix things

* fix

* Fix json component flagging

* fix

* fuix

---------

Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>

* Fix template (#5533)

* Fix type hint (#5536)

* fix custom components when installed from wheel (#5552)

* Fix all demos custom components (#5555)

* Fix all_demos

* Fix issues

* trigger ci

* add changeset

* Fix merge

* lockfile

* Add code

* add code

* Fix command

* Add code

* Fix highlighted text

* fix interactive highlighted text

* tweak ci

* Disable example caching for load right now

---------

Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* image fixes (#5589)

* Add flag

* Copy front-end

* Add backend code

* Remove breakpoint

* Fix init

* Add code

* File reorg

* Fix types

* Upload files

* Fix code

* Custom components

* Add code

* Client changes

* More changes

* Add code

* Add code

* Fix reversion

* build hook integration

* Add code

* rm file

* Add code

* rename cli

* Add code

* Nicer cli

* Add display class

* Add backend devmide

* Add code?

* Add code

* Add paths and fix reload mdoe

* Fix .then

* changes

* fix

* fix

* changes

* changes

* changes

* Fix versions

* Fix wheel build from source

* Add code

* Get no template case working

* Cc fe (#5509)

* tweaks

* fix 18n

* fix 18n

* tweaks

* Change name

---------

Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>

* Do not use live display for dev mode

* Clean up front-end server print statements a little bit

* Fix success event

* Cc fe 2 (#5526)

* tweaks

* fix examples

* fix backend port

* fix things

* fix

* Fix json component flagging

* fix

* fuix

---------

Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>

* Fix template (#5533)

* Fix type hint (#5536)

* fix custom components when installed from wheel (#5552)

* Fix all demos custom components (#5555)

* Fix all_demos

* Fix issues

* trigger ci

* add changeset

* Fix merge

* lockfile

* Add code

* add code

* Fix command

* Add code

* Fix highlighted text

* fix interactive highlighted text

* tweak ci

* fix image uploads

* add changeset

* remove changeset

* add changeset

---------

Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Support Literal Typehints (#5591)

* Fix updateable

* Deploy for failing actions

* fix enum

* Add code

* remove gradio lite from publish ci

* fix

* fix

* chore: update versions (beta) (#5501)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Fix js deps in cli and add gradio-preview artifacts to build (#5610)

* Fix js dependencies in cli

* Add preview to build command

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (beta) (#5612)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* backend linting (#5613)

* backend linting

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Publish js theme (#5615)

* Publish js theme

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (beta) (#5614)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Add code (#5617)

* Add docstring to trigger release (#5618)

* Add docstring

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (beta) (#5619)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* fix build and broken imports (#5620)

* fix broken import

* fix build scripts

* add changeset

* Update js/statustracker/static/index.svelte

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (beta) (#5621)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Fix esbuild (#5624)

* fix build

* fix build

* add changeset

* fix build

* fix

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (beta) (#5626)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Fix esbuild (#5629)

* fix build

* fix build

* add changeset

* fix build

* fix

* asd

* more fix

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* asd (#5630)

* chore: update versions (beta) (#5631)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Some minor v4 fixes (#5637)

* Add code

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Publish all components to npm (#5648)

* Publish image component

* publish components

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (beta) (#5638)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Fix front-end imports + other misc fixes (#5649)

* Fix console error

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Add overwrite flag to create command (#5651)

* Add overwrite flag to create command

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (beta) (#5650)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Make layout components templateable (#5665)

* Split out layouts

* Refactor cli

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Patch fixes (#5678)

* Handle lowercase template name frontend

* Lint

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Fix build and file route (#5680)

* Add code

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (beta) (#5662)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* V4 fix typing (#5686)

* Add examples for series and parallel

* v4 fix typing

* add changeset

* Fix

* Fix

* Fix 3.8

* Fix typing 3.8

* Lint

* Add code

* Add key

* Fix typing

* Add code

* Fix deps

* Fix fastapi

* Fix version
'
:

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Use overrides (#5695)

* Use overrides

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* ensure client is copied along with components (#5709)

* ensure client is copied along with components

* add changeset

* add changeset

* chore: update versions (beta) (#5688)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* fix build config (#5710)

* Fix python unit tests for v4 (#5715)

* Add code

* Fix tests

* Add ci

* Add code

* fix test_blocks

* More fixes

* Last changes

* add changeset

* Add code

* Continue for the sake of some green ci

* Proper api info

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Better test dir check (#5719)

* FIx test-dir check

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Use path to npm executable in subprocess (#5736)

* Use path to npm in subprocess

* Fix dev mode too

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Add Error + test (#5738)

* Add Error + test

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Fix component regex (#5739)

* Add code

* Fix component regex

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Fix layout templates (#5740)

* Fix templates

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Fix template remaining components (#5743)

* add code

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* --overwrite deletes previous content (#5744)

* Add code

* Add code

* add changeset

* Add test

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Support call method (#5751)

* Support call method

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Fix windows ci build (#5752)

* Try bash

* Use cross-env

* Trigger CI

* Fix comma

* add changeset

* Modify lock file

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Fix windows paths (#5745)

* Use posix path

* add changeset

* add changeset

* Try normalize

* to_posix v2

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (beta) (#5717)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Simplify how files are handled in components in 4.0 (#5768)

* Client and upload route hash files

* Refactor logic

* add changeset

* Add code

* View API fix

* Fix client tests

* Dumb windows fix

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Fix deployed demos on v4 branch (#5770)

* Template fixes

* add changeset

* add changeset

* Fix streaming audio

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Name Endpoints if api_name is None (#5782)

* Implementation and test

* add changeset

* fix lint

* Fix nits

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* pending events

* add changeset

* Set api=False for cancel events (#5851)

* Set api=False for cancel events

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* V4: Set cache dir for some component tests (#5852)

* Add code

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* V4: Use async version of shutil in upload route (#5853)

* Use async shutil copy

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* trigger_mode

* changes

* undo

* undo

* add changeset

* undo

* add changeset

* image undo

* add changeset

* fix merge errors

* remove prints

* change

* fix

* fix error

* change

* quick fix

* trigger_mode param

* type

* value error

---------

Co-authored-by: pngwn <hello@pngwn.io>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
  • Loading branch information
6 people committed Oct 25, 2023
1 parent 037e5af commit ce036c5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/floppy-glasses-wish.md
@@ -0,0 +1,6 @@
---
"@gradio/app": patch
"gradio": patch
---

fix:Pending events behavior
14 changes: 13 additions & 1 deletion gradio/blocks.py
Expand Up @@ -753,11 +753,12 @@ def set_event_trigger(
collects_event_data: bool | None = None,
trigger_after: int | None = None,
trigger_only_on_success: bool = False,
trigger_mode: Literal["once", "multiple", "always_last"] | None = "once",
) -> tuple[dict[str, Any], int]:
"""
Adds an event to the component's dependencies.
Parameters:
event_name: event name
targets: a list of EventListenerMethod objects that define the event trigger
fn: Callable function
inputs: input list
outputs: output list
Expand All @@ -776,6 +777,7 @@ def set_event_trigger(
collects_event_data: whether to collect event data for this event
trigger_after: if set, this event will be triggered after 'trigger_after' function index
trigger_only_on_success: if True, this event will only be triggered if the previous event was successful (only applies if `trigger_after` is set)
trigger_mode: If "once" (default for all events except `.change()`) would not allow any submissions while an event is pending. If set to "multiple", unlimited submissions are allowed while pending, and "always_last" (default for `.change()` event) would allow a second submission after the pending event is complete.
Returns: dependency information, dependency index
"""
# Support for singular parameter
Expand Down Expand Up @@ -819,6 +821,15 @@ def set_event_trigger(
elif every:
raise ValueError("Cannot set a value for `every` without a `fn`.")

if _targets[0][1] == "change" and trigger_mode == None:
trigger_mode = "always_last"
elif trigger_mode == None:
trigger_mode = "once"
elif trigger_mode not in ["once", "multiple", "always_last"]:
raise ValueError(
f"Invalid value for parameter `trigger_mode`: {trigger_mode}. Please choose from: {['once', 'multiple', 'always_last']}"
)

_, progress_index, event_data_index = (
special_args(fn) if fn else (None, None, None)
)
Expand Down Expand Up @@ -865,6 +876,7 @@ def set_event_trigger(
"collects_event_data": collects_event_data,
"trigger_after": trigger_after,
"trigger_only_on_success": trigger_only_on_success,
"trigger_mode": trigger_mode,
}
self.dependencies.append(dependency)
return dependency, len(self.dependencies) - 1
Expand Down
2 changes: 2 additions & 0 deletions gradio/component_meta.py
Expand Up @@ -31,6 +31,7 @@ def {{ event }}(self,
postprocess: bool = True,
cancels: dict[str, Any] | list[dict[str, Any]] | None = None,
every: float | None = None,
trigger_mode: Literal["once", "multiple", "always_last"] | None = None,
_js: str | None = None,) -> Dependency:
"""
Parameters:
Expand All @@ -47,6 +48,7 @@ def {{ event }}(self,
postprocess: If False, will not run postprocessing of component data before returning 'fn' output to the browser.
cancels: A list of other events to cancel when this listener is triggered. For example, setting cancels=[click_event] will cancel the click_event, where click_event is the return value of another components .click method. Functions that have not yet run (or generators that are iterating) will be cancelled, but functions that are currently running will be allowed to finish.
every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled.
trigger_mode: If "once" (default for all events except `.change()`) would not allow any submissions while an event is pending. If set to "multiple", unlimited submissions are allowed while pending, and "always_last" (default for `.change()` event) would allow a second submission after the pending event is complete.
"""
...
{% endfor %}
Expand Down
4 changes: 4 additions & 0 deletions gradio/events.py
Expand Up @@ -207,6 +207,7 @@ def event_trigger(
postprocess: bool = True,
cancels: dict[str, Any] | list[dict[str, Any]] | None = None,
every: float | None = None,
trigger_mode: Literal["once", "multiple", "always_last"] | None = None,
_js: str | None = None,
) -> Dependency:
"""
Expand All @@ -224,6 +225,7 @@ def event_trigger(
postprocess: If False, will not run postprocessing of component data before returning 'fn' output to the browser.
cancels: A list of other events to cancel when this listener is triggered. For example, setting cancels=[click_event] will cancel the click_event, where click_event is the return value of another components .click method. Functions that have not yet run (or generators that are iterating) will be cancelled, but functions that are currently running will be allowed to finish.
every: Run this event 'every' number of seconds while the client connection is open. Interpreted in seconds. Queue must be enabled.
trigger_mode: If "once" (default for all events except `.change()`) would not allow any submissions while an event is pending. If set to "multiple", unlimited submissions are allowed while pending, and "always_last" (default for `.change()` event) would allow a second submission after the pending event is complete.
"""

if fn == "decorator":
Expand All @@ -244,6 +246,7 @@ def wrapper(func):
postprocess,
cancels,
every,
trigger_mode,
_js,
)

Expand Down Expand Up @@ -306,6 +309,7 @@ def inner(*args, **kwargs):
every=every,
trigger_after=_trigger_after,
trigger_only_on_success=_trigger_only_on_success,
trigger_mode=trigger_mode,
)
set_cancel_events(
[EventListenerMethod(block if _has_trigger else None, _event_name)],
Expand Down
28 changes: 21 additions & 7 deletions js/app/src/Blocks.svelte
Expand Up @@ -11,7 +11,7 @@
import type { ComponentMeta, Dependency, LayoutNode } from "./types";
import { setupi18n } from "./i18n";
import { ApiDocs } from "./api_docs/";
import type { ThemeMode } from "./types";
import type { ThemeMode, Payload } from "./types";
import { Toast } from "@gradio/statustracker";
import type { ToastMessage } from "@gradio/statustracker";
import type { ShareData } from "@gradio/utils";
Expand Down Expand Up @@ -372,12 +372,11 @@
})
);
}
if (current_status === "pending" || current_status === "generating") {
return;
dep.pending_request = true;
}
let payload = {
let payload: Payload = {
fn_index: dep_index,
data: dep.inputs.map((id) => instance_map[id].props.value),
event_data: dep.collects_event_data ? event_data : null
Expand All @@ -393,21 +392,36 @@
.then((v: unknown[]) => {
if (dep.backend_fn) {
payload.data = v;
make_prediction();
make_prediction(payload);
} else {
handle_update(v, dep_index);
}
});
} else {
if (dep.backend_fn) {
make_prediction();
if (dep.trigger_mode === "once") {
if (!dep.pending_request) make_prediction(payload);
} else if (dep.trigger_mode === "multiple") {
make_prediction(payload);
} else if (dep.trigger_mode === "always_last") {
if (!dep.pending_request) {
make_prediction(payload);
} else {
dep.final_event = payload;
}
}
}
}
function make_prediction(): void {
function make_prediction(payload: Payload): void {
const submission = app
.submit(payload.fn_index, payload.data as unknown[], payload.event_data)
.on("data", ({ data, fn_index }) => {
if (dep.pending_request && dep.final_event) {
dep.pending_request = false;
make_prediction(dep.final_event);
}
dep.pending_request = false;
handle_update(data, fn_index);
})
.on("status", ({ fn_index, ...status }) => {
Expand Down
9 changes: 9 additions & 0 deletions js/app/src/types.ts
Expand Up @@ -25,6 +25,12 @@ export interface DependencyTypes {
generator: boolean;
}

export interface Payload {
fn_index: number,
data: unknown[],
event_data: unknown | null,
}

export interface Dependency {
targets: [number, string][];
inputs: number[];
Expand All @@ -40,8 +46,11 @@ export interface Dependency {
cancels: number[];
types: DependencyTypes;
collects_event_data: boolean;
pending_request?: boolean;
trigger_after?: number;
trigger_only_on_success?: boolean;
trigger_mode: "once" | "multiple" | "always_last";
final_event: Payload | null;
}

interface TypeDescription {
Expand Down

0 comments on commit ce036c5

Please sign in to comment.