fix: eliminate scene canvas latency in remote Gradio event chain#3
Closed
lonrencn wants to merge 1 commit into
Closed
fix: eliminate scene canvas latency in remote Gradio event chain#3lonrencn wants to merge 1 commit into
lonrencn wants to merge 1 commit into
Conversation
scene_canvas_image is a gr.Textbox storing multi-MB base64 PNG data. Gradio's blob dedup (walk_and_store_blobs) returns [] for string inputs, so every event-chain step re-uploads the full base64 payload. For remote deployments (browser -> reverse proxy -> server), this adds ~15-20s of dead time between clicking Generate and actual generation. Solution: cache the canvas server-side during cache_input_image_func (step 2), then pass gr.State(None) in later chain steps (4 and 7). The Python handlers recover the cached value via user_did key. Measured: ~27s click-to-generate -> ~10s on remote deployments. Local deployments are unaffected (sub-ms transfers).
Contributor
Author
|
Tested on a remote deployment (browser in China → reverse proxy → server). After applying this fix, the click-to-generate delay dropped from ~27s to ~10s. The improvement is very noticeable in actual use. |
Owner
|
Manually merged and update.Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
scene_canvas_imageis agr.Textboxthat stores a full base64 PNG JSON string (multiple MB). Gradio'swalk_and_store_blobs()returns[]for string inputs, so every event-chain step re-uploads the entire base64 payload in the POST body.For remote deployments (browser → reverse proxy → server), this adds ~15-20s of dead time between clicking Generate and actual generation start.
Root Cause
The Generate button triggers a 13-step Gradio event chain. Steps 1-12 run with
queue=False, each as a separate HTTP POST. Three of these steps receivescene_canvas_imageas an input:cache_input_image_funcprocess_before_generationavoid_empty_prompt_for_sceneEach step sends the multi-MB base64 string over the wire.
Solution
Server-side cache bypass: Store the canvas data once during step 2 (
cache_input_image_func), then pass a lightweightgr.State(None)in steps 4 and 7. The Python handlers transparently recover the cached value viauser_didas the cache key.Changes (3 files, +47/-2)
modules/util.py: Thread-safe in-memory cache with 300s TTL auto-expirywebui.py:_scene_canvas_pass = gr.State(None)dummy component; cache store in step 2; steps 4 & 7 inputs changedenhanced/topbar.py: Cache fallback inprocess_before_generation()andavoid_empty_prompt_for_scene()Measured Results (remote deployment, China → server)
null)Local deployments are unaffected (sub-ms transfers). The cache auto-expires after 300s and uses
user_didas the key, so concurrent users are isolated.