Spaces:
Running on Zero
fix: dimension sliders never auto-fill on first upload (use user-interaction flags)
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 programmaticgr.update()โ confirmed via Gradio's docstrings and source) that flips its flag toTrue. update_dimensions_on_upload(images, height_set, width_set)now treatsnot height_set/not width_setas "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.uploadinputs and thegr.oninference-trigger inputs list (positional order matches the updatedinfer()signature).
Verification
python3 -m py_compilepasses.- Handler logic unit-tested with real
gradio6.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 โ
- untouched (2048ร2048) โ auto-fills
- Diff: +22/-13 across 6 hunks, all in the dimension-handling regions (
update_dimensions_on_upload,infer()signature/fallback,input_images.upload,gr.oninputs). 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.