diff options
Diffstat (limited to 'app/ui')
| -rw-r--r-- | app/ui/hub.py | 97 | ||||
| -rw-r--r-- | app/ui/tui.py | 21 |
2 files changed, 88 insertions, 30 deletions
diff --git a/app/ui/hub.py b/app/ui/hub.py index 4fae67b..a5c3dc4 100644 --- a/app/ui/hub.py +++ b/app/ui/hub.py @@ -1225,8 +1225,12 @@ 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.""" + def no_voices_hint(_fs=None) -> str: + """Why a clone-capable entry has no selectable voices. + + The form invokes ``on_empty_choices`` with the field list (see + tui.form); validate's echo calls it without one. + """ if local: return ("No .wav files available to clone — run Configure " "Backends → audio.cpp and add voices to its " @@ -1376,7 +1380,8 @@ def _qwen_fields(remote_modes: Optional[list] = None, Returns ``(fields, mapper)`` where FIELDS are the qwen options — which model the demo server hosts (Base (voice cloning) / CustomVoice (built-in voices) / VoiceDesign (design)), plus the per-model controls: Speaker on - CustomVoice, Clone .wav path on Base, Instructions on VoiceDesign — and + CustomVoice, a Clone .wav directory browser (default ./voices) + Voice- + to-clone .wav picker on Base, Instructions on VoiceDesign — and MAPPER turns a submitted form values dict into the qwen converter kwargs. qwen always has options to offer, so it never signals unavailability. PREFIX namespaces the field keys ("" for the managed @@ -1396,34 +1401,73 @@ def _qwen_fields(remote_modes: Optional[list] = None, urls = dict(urls or {}) mode_keys = (("custom", "CustomVoice"), ("clone", "Base"), ("design", "VoiceDesign")) - model_choices = [ - ("CustomVoice (built-in voices)", "custom"), - ("Base (voice cloning)", "clone"), - ("VoiceDesign (design)", "design"), - ] + purposes = {"custom": "built-in voices", "clone": "voice cloning", + "design": "design"} + # The Model picker reads as a two-column table (like the audio.cpp + # picker): pad every model name to the widest one so the (purpose) + # column starts on the same position. + name_width = max(len(model) for _mode, model in mode_keys) + model_choices = [(f"{model:<{name_width}} ({purposes[mode]})", mode) + for mode, model in mode_keys] if remote_modes: available = set(remote_modes) model_choices = [(label, value) for (label, value) in model_choices if dict(mode_keys)[value] in available] - by_value = dict(model_choices) - configured_mode = dict(mode_keys).get( + by_value = {value: label for label, value in model_choices} + configured_mode = {model: mode for mode, model in mode_keys}.get( qwen_backend.current_model(), model_choices[0][1]) default_mode = configured_mode if configured_mode in by_value \ else model_choices[0][1] speakers = list(qwen_backend.QWEN_SPEAKERS) default_speaker = config.SPEAKER if config.SPEAKER in speakers \ else speakers[0] + + # Voice cloning references: the directory the .wavs live in — browsed + # with the directory widget, defaulting to the project's ./voices (the + # folder the Help screen points at) — plus a picker of the .wav files + # found there (the same directory + voice picker the audio.cpp form + # uses; the demo uploads exactly one reference file). + def clone_wav_choices(fs) -> list: + """(file name, full path) pairs for the clone directory's .wavs.""" + return [(p.name, str(p)) for p in _list_wavs( + _field_value(fs, prefix + "clone_dir"))] + + def reset_clone_wav(fs) -> None: + """Re-point the .wav picker at the newly chosen directory.""" + wav_field = next(f for f in fields + if f.get("key") == prefix + "clone") + wav_field["value"] = next( + (path for _name, path in clone_wav_choices(fs)), "") + + def no_wavs_hint(_fs=None) -> str: + """Why the .wav picker is empty (validate echoes it on Generate!).""" + directory = next((f.get("value") for f in fields + if f.get("key") == prefix + "clone_dir"), None) + return (f"No .wav files in {directory} — put a reference .wav " + "there or pick another directory.") + + def clone_wav_validate(value) -> Optional[str]: + """Refuse Generate! when no reference .wav is available to clone.""" + if value: + return None + return no_wavs_hint() + + initial_wavs = _list_wavs(common.VOICES_DIR) + initial_clone = str(initial_wavs[0]) if initial_wavs else "" fields = [ {"key": prefix + "mode", "label": "Model", "kind": "choice", "value": default_mode, "choices": model_choices}, {"key": prefix + "speaker", "label": "Speaker", "kind": "choice", "value": default_speaker, "choices": speakers, "visible": lambda fs: _field_value(fs, prefix + "mode") == "custom"}, - {"key": prefix + "clone", "label": "Clone .wav path", "kind": "text", - "value": "", - "validate": lambda s: None if (s and Path(s).is_file() - and s.lower().endswith(".wav")) - else "Enter the path to an existing .wav file", + {"key": prefix + "clone_dir", "label": "Clone .wav directory", + "kind": "dir", "value": common.VOICES_DIR, + "on_change": reset_clone_wav, + "visible": lambda fs: _field_value(fs, prefix + "mode") == "clone"}, + {"key": prefix + "clone", "label": "Voice to clone", + "kind": "choice", "value": initial_clone, + "choices": clone_wav_choices, "on_empty_choices": no_wavs_hint, + "validate": clone_wav_validate, "visible": lambda fs: _field_value(fs, prefix + "mode") == "clone"}, {"key": prefix + "qwen_instructions", "label": "Instructions", "kind": "text", "value": config.INSTRUCT, @@ -1436,7 +1480,9 @@ def _qwen_fields(remote_modes: Optional[list] = None, def mapper(result) -> Optional[tuple]: mode = result[prefix + "mode"] - clone = result[prefix + "clone"].strip() if mode == "clone" else None + clone = None + if mode == "clone": + clone = str(result[prefix + "clone"] or "").strip() or None kwargs = {"clone": clone, **_common_kwargs(result)} if mode == "design": kwargs["instructions"] = result[prefix + "qwen_instructions"] @@ -1950,21 +1996,24 @@ def _find_spec(name: str) -> Optional[ServerSpec]: return None -def _list_voices(voice_dir: str) -> list: - """Return sorted .wav stems in VOICE_DIR (best-effort).""" +def _list_wavs(directory) -> list: + """Return the .wav file Paths directly inside DIRECTORY (best-effort).""" try: - path = Path(voice_dir) + path = Path(directory) if not path.is_dir(): return [] - return sorted( - (p.stem for p in path.iterdir() - if p.is_file() and p.suffix.lower() == ".wav"), - key=str.lower, - ) + return sorted((p for p in path.iterdir() + if p.is_file() and p.suffix.lower() == ".wav"), + key=lambda p: p.name.lower()) except OSError: return [] +def _list_voices(voice_dir: str) -> list: + """Return sorted .wav stems in VOICE_DIR (best-effort).""" + return [p.stem for p in _list_wavs(voice_dir)] + + def _is_float(value: str) -> bool: try: float(value) diff --git a/app/ui/tui.py b/app/ui/tui.py index 7da1dd1..e2216a3 100644 --- a/app/ui/tui.py +++ b/app/ui/tui.py @@ -1063,16 +1063,25 @@ def form(scr, title: str, fields: Sequence[dict], field["value"] = values[(index + 1) % len(values)] run_on_change(field) - def display_value(field: dict) -> str: + def display_value(field: dict, fields: list) -> str: if field.get("kind") == "bool": return "Yes" if field["value"] else "No" if field.get("kind") == "dir": value = field["value"] return str(value) if value is not None else "" - if field.get("kind") == "toggle": - for label, value in field.get("choices") or []: - if value == field["value"]: - return label + if field.get("kind") in ("toggle", "choice"): + # Show the selected value's label, not the raw value: a + # (label, value) choice's label is what the pick menu shows, + # so the row reads the same before and after the pick (the + # Backend key "qwen" displays as its label "qwen-tts"). + choices = field.get("choices") or [] + if callable(choices): + choices = choices(fields) + if choices and isinstance(choices[0], (tuple, list)) \ + and len(choices[0]) == 2: + for label, value in choices: + if value == field["value"]: + return label return str(field["value"]) while True: @@ -1097,7 +1106,7 @@ def form(scr, title: str, fields: Sequence[dict], name = f"{field_label(field)}:".ljust(label_w + 1) frame.mark_segments( [(name, frame.theme["body"]), - (" " + display_value(field), frame.theme["input"])], + (" " + display_value(field, fields), frame.theme["input"])], selectable=True, align="left") frame.cursor = None if on_buttons else field_rows[cursor] frame.buttons = (list(buttons), btn_index if on_buttons else None) |
