fix: dimension sliders never auto-fill on first upload (use user-interaction flags)

#6
by LPX55 - opened
Owner

What this fixes

After the dimensions UI shipped in PR #4 (auto-fill from the first uploaded image, per-dimension fallback in infer()), the sliders never auto-fill on the first upload โ€” and infer() always falls back to 1024. The root cause is in Gradio's Slider implementation:

self.minimum if value is None else value   # Slider line 147

A gr.Slider(value=None, minimum=256) reports 256 (its minimum), not None, to every event handler. PR #4's "unset" check (current_height is None / height is None) therefore never fires โ€” the slider looks already-set to 256, and infer() always bails to the 1024 fallback.

Fix

Replaces value-based unset detection with user-interaction flags:

  • Two per-session gr.State(False) components: height_user_set, width_user_set.
  • Each slider gets a .input() listener (fires only on user interaction, never on programmatic gr.update() โ€” confirmed via Gradio's docstrings and source) that flips its flag to True.
  • update_dimensions_on_upload(images, height_set, width_set) now treats not height_set/not width_set as "unset" and auto-fills those dimensions from the uploaded image (preserving dimensions the user already set).
  • infer(..., height_user_set=False, width_user_set=False, ...) mirrors this in the runtime fallback โ€” only dimensions the user hasn't touched get derived from the first input image.
  • These flags are appended to the input_images.upload inputs and the gr.on inference-trigger inputs list (positional order matches the updated infer() signature).

Verification

  • python3 -m py_compile passes.
  • Handler logic unit-tested with real gradio 6.17.3 + PIL:
    • untouched (2048ร—2048) โ†’ auto-fills 1024ร—1024 โœ“
    • height user-set, width unset โ†’ height preserved, width auto-filled โœ“
    • both user-set โ†’ both preserved (no-op gr.update()) โœ“
    • empty gallery โ†’ both untouched โœ“
  • Diff: +22/-13 across 6 hunks, all in the dimension-handling regions (update_dimensions_on_upload, infer() signature/fallback, input_images.upload, gr.on inputs). No overlap with PR #5's LoRA regions โ€” applied cleanly onto the post-#5 main.

Why not value-based

gr.Slider eagerly coerces None โ†’ minimum. Tracking via .input() is the documented, stable way to distinguish user-set values from untouched defaults โ€” and it survives Gradio's coercion without fighting it.

LPX55 changed pull request status to merged

Sign up or log in to comment