Skip to content

fix: eliminate scene canvas latency in remote Gradio event chain#3

Closed
lonrencn wants to merge 1 commit into
Windecay:mainfrom
lonrencn:fix/scene-canvas-event-chain-latency
Closed

fix: eliminate scene canvas latency in remote Gradio event chain#3
lonrencn wants to merge 1 commit into
Windecay:mainfrom
lonrencn:fix/scene-canvas-event-chain-latency

Conversation

@lonrencn

Copy link
Copy Markdown
Contributor

Problem

scene_canvas_image is a gr.Textbox that stores a full base64 PNG JSON string (multiple MB). Gradio's walk_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 receive scene_canvas_image as an input:

Step Handler Purpose
2 cache_input_image_func Caches input image
4 process_before_generation Pre-generation setup
7 avoid_empty_prompt_for_scene Prompt fallback

Each 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 lightweight gr.State(None) in steps 4 and 7. The Python handlers transparently recover the cached value via user_did as the cache key.

Changes (3 files, +47/-2)

  • modules/util.py: Thread-safe in-memory cache with 300s TTL auto-expiry
  • webui.py: _scene_canvas_pass = gr.State(None) dummy component; cache store in step 2; steps 4 & 7 inputs changed
  • enhanced/topbar.py: Cache fallback in process_before_generation() and avoid_empty_prompt_for_scene()

Measured Results (remote deployment, China → server)

Metric Before After
Click → Generation start ~27s ~10s
Payload per chain step multi-MB base64 ~4 bytes (null)

Local deployments are unaffected (sub-ms transfers). The cache auto-expires after 300s and uses user_did as the key, so concurrent users are isolated.

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).
@lonrencn

Copy link
Copy Markdown
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.

@Windecay

Copy link
Copy Markdown
Owner

Manually merged and update.Thanks!

@Windecay Windecay closed this Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants