aboutsummaryrefslogtreecommitdiff
path: root/app/ui/hub.py
diff options
context:
space:
mode:
authorhistoria <historiavg@proton.me>2026-08-28 02:27:10 -0400
committerhistoria <historiavg@proton.me>2026-08-28 02:27:10 -0400
commita7c653313d2bb1e185cfbc3f0f52c2fe33218600 (patch)
treef28f57fa5509ddb54176116cc4cc1198d034e9ff /app/ui/hub.py
parent975053f1789771ba5cb9dbe50ba7fe0aa396f0ab (diff)
downloadtts-audiobook-generator-a7c653313d2bb1e185cfbc3f0f52c2fe33218600.tar.gz
feat: qwen-tts backend widgets updated to same style as audio.cpp
Diffstat (limited to 'app/ui/hub.py')
-rw-r--r--app/ui/hub.py97
1 files changed, 73 insertions, 24 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)