dial: coerce knob string value so turning the dial never crashes

#1
Files changed (1) hide show
  1. app.py +14 -0
app.py CHANGED
@@ -1578,6 +1578,20 @@ with gr.Blocks(title="Glossolalia Dial") as demo:
1578
  )
1579
  level = gr.Slider(0, 4, value=0, step=1, label="", elem_id="dial-slider",
1580
  show_label=False, visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1581
 
1582
  # Live preview of what the audio will say, updates as the dial / mode / lyric change.
1583
  ghost_lyric = gr.Textbox(label="what the voice will say at this dial position",
 
1578
  )
1579
  level = gr.Slider(0, 4, value=0, step=1, label="", elem_id="dial-slider",
1580
  show_label=False, visible=True)
1581
+ # The custom knob writes the dial value into this slider's <input type="range"> via JS,
1582
+ # where a range input's value is always a STRING. Gradio's slider bounds-check does
1583
+ # `value < minimum`, which raises "TypeError: '<' not supported between str and int" on a
1584
+ # string and kills the request (it surfaced as the dial erroring when turned). Coerce to a
1585
+ # number before the original preprocess so turning the knob can never crash. Instance-level
1586
+ # override (not a subclass) so the component stays a plain Slider and the knob wiring holds.
1587
+ _level_orig_preprocess = level.preprocess
1588
+ def _level_safe_preprocess(payload, *args, **kwargs):
1589
+ try:
1590
+ payload = float(payload)
1591
+ except (TypeError, ValueError):
1592
+ payload = 0.0
1593
+ return _level_orig_preprocess(payload, *args, **kwargs)
1594
+ level.preprocess = _level_safe_preprocess
1595
 
1596
  # Live preview of what the audio will say, updates as the dial / mode / lyric change.
1597
  ghost_lyric = gr.Textbox(label="what the voice will say at this dial position",