diff options
| author | historia <historiavg@proton.me> | 2026-08-26 19:12:11 -0400 |
|---|---|---|
| committer | historia <historiavg@proton.me> | 2026-08-26 19:12:11 -0400 |
| commit | 5af37bbd575eec89c6ac2fecf2d8c2eda4c1728d (patch) | |
| tree | 4f8fe9b79d339695ca83968e03301b853eb2e2ef /app/ui | |
| parent | df95e7034df683c38fde67890430ab4c2abfa4ba (diff) | |
| download | tts-audiobook-generator-5af37bbd575eec89c6ac2fecf2d8c2eda4c1728d.tar.gz | |
fix: crash when menu of clone voices is empty
Diffstat (limited to 'app/ui')
| -rw-r--r-- | app/ui/hub.py | 31 | ||||
| -rw-r--r-- | app/ui/tui.py | 43 |
2 files changed, 58 insertions, 16 deletions
diff --git a/app/ui/hub.py b/app/ui/hub.py index 6a82c83..80fe843 100644 --- a/app/ui/hub.py +++ b/app/ui/hub.py @@ -927,6 +927,13 @@ def _audiocpp_fields(stdscr, api_url: Optional[str] = None, the field keys ("" for the managed entry) so two entries of this backend can share one form without overwriting each other. + The Voice field tracks the selected entry's capability — built-in + speakers on CustomVoice, the server's clone voices on every other + entry. A clone-capable entry whose server lists no voices cannot be + picked from (empty menu) and refuses Generate! with a hint pointing + at the voice-clone .wav directory instead of crashing or producing a + run that fails at model-load time. + With API_URL None (the managed entry) the model list is fed from the local checkout's server.json — the config of the server this tool manages. With API_URL set (the "[remote]" entry) the models and voices @@ -1045,6 +1052,25 @@ def _audiocpp_fields(stdscr, api_url: Optional[str] = None, fields, prefix + "model_id"))] return [] # design: the field is hidden + def no_voices_hint() -> str: + """Why a clone-capable entry has no selectable voices.""" + if local: + return ("No .wav files available to clone — run Configure " + "backends → audio.cpp and add voices to its " + "voice-clone .wav directory.") + return ("No .wav files available to clone — the audio.cpp server " + f"at {url} hosts none. Configure its voice-clone .wav " + "directory on that machine.") + + def voice_validate(value): + """Refuse Generate! when this entry's clone voice is unavailable.""" + if model_capability(fields) != AUDIOCPP_VOICE_CLONE: + return None + if not voices_for(_field_value(fields, prefix + "model_id")): + return no_voices_hint() + return None if value \ + else "This model needs a voice — pick one or switch models" + model_ids = [m.get("id") for m in models] default_model = config.AUDIOCPP_MODEL_ID \ if config.AUDIOCPP_MODEL_ID in model_ids else model_ids[0] @@ -1080,9 +1106,8 @@ def _audiocpp_fields(stdscr, api_url: Optional[str] = None, "value": initial_voice, "choices": lambda fs: voice_choices(fs), "visible": lambda fs: model_capability(fs) != AUDIOCPP_VOICE_DESIGN, - "validate": lambda value: None - if (model_capability(fields) != AUDIOCPP_VOICE_CLONE or value) - else "This model needs a voice — pick one or switch models"}, + "on_empty_choices": no_voices_hint, + "validate": voice_validate}, {"key": prefix + "instructions", "label": "Instructions", "kind": "text", "value": config.AUDIOCPP_INSTRUCTIONS, "visible": lambda fs: model_capability(fs) in (AUDIOCPP_VOICE_DESIGN, diff --git a/app/ui/tui.py b/app/ui/tui.py index e2118eb..56aa79e 100644 --- a/app/ui/tui.py +++ b/app/ui/tui.py @@ -909,6 +909,10 @@ def form(scr, title: str, fields: Sequence[dict], keep their value across hide/show. A field may set ``on_change`` to a callable of the field list, invoked whenever its value changes so dependent fields (choices, visibility, defaults) can be recomputed. + A choice field whose resolved list is empty cannot be opened: Enter + is a no-op, or flashes the field's optional ``on_empty_choices`` + message (string or callable of the field list) — an explanation the + submit-time ``validate`` can echo when an empty pick must be refused. An optional ``note`` string on a field renders as a dim, non-selectable line in a blank-line frame above that field's row — a @@ -1064,20 +1068,33 @@ def form(scr, title: str, fields: Sequence[dict], if callable(choices): choices = choices(fields) choices = list(choices) - if choices and isinstance(choices[0], (tuple, list)) \ - and len(choices[0]) == 2: - pairs = [(label, value) for label, value in choices] + if not choices: + # A dynamic choice list can legitimately come + # back empty (e.g. an audio.cpp model whose + # server hosts no clone voices). menu() would + # raise; explain instead when the field says + # how to fill the list. + message = field.get("on_empty_choices") + if callable(message): + message = message(fields) + if message: + frame.flash(str(message), "err") else: - pairs = [(c, c) for c in choices] - values = [value for _, value in pairs] - default = values.index(field["value"]) \ - if field["value"] in values else 0 - chosen = menu(scr, field["label"], pairs, - default_index=default, - back_value=edit_cancel) - if chosen is not edit_cancel: - field["value"] = chosen - run_on_change(field) + if isinstance(choices[0], (tuple, list)) \ + and len(choices[0]) == 2: + pairs = [(label, value) + for label, value in choices] + else: + pairs = [(c, c) for c in choices] + values = [value for _, value in pairs] + default = values.index(field["value"]) \ + if field["value"] in values else 0 + chosen = menu(scr, field["label"], pairs, + default_index=default, + back_value=edit_cancel) + if chosen is not edit_cancel: + field["value"] = chosen + run_on_change(field) elif field.get("kind") == "dir": start = field["value"] start = Path(start) if start else Path.cwd() |
