diff options
| author | historia <historiavg@proton.me> | 2026-08-26 18:39:33 -0400 |
|---|---|---|
| committer | historia <historiavg@proton.me> | 2026-08-26 18:39:33 -0400 |
| commit | df95e7034df683c38fde67890430ab4c2abfa4ba (patch) | |
| tree | f46d3eb393e605e82a4d058a2290061b77403649 /app/backends | |
| parent | 544486a374cd5cae7acce1302648d8dad079db48 (diff) | |
| download | tts-audiobook-generator-df95e7034df683c38fde67890430ab4c2abfa4ba.tar.gz | |
feat: in-place toggle field, updated transcription tui to use new field
Diffstat (limited to 'app/backends')
| -rw-r--r-- | app/backends/audiocpp/wizard.py | 99 | ||||
| -rwxr-xr-x | app/backends/faster.py | 82 |
2 files changed, 38 insertions, 143 deletions
diff --git a/app/backends/audiocpp/wizard.py b/app/backends/audiocpp/wizard.py index 831ae3d..bfc0eb7 100644 --- a/app/backends/audiocpp/wizard.py +++ b/app/backends/audiocpp/wizard.py @@ -143,33 +143,14 @@ def _write_and_advise(audiocpp_dir: Path, wav_dir: Optional[Path], f"{'entry' if count == 1 else 'entries'}.") -def _transcription_choices(wav_files: list, existing: Dict[str, str], - prompt_exists: bool) -> Tuple[list, str]: - """Shape the transcription question for the setup form. - - Returns ``(choices, default_mode)`` where MODE is ``"all"`` - (re-transcribe everything), ``"missing"`` (only .wavs without an - existing transcript) or ``"keep"`` (reuse prompt_text untouched). - Plain choice pairs the combined config form can show on one row. - """ - if not prompt_exists: - return [("Re-transcribe all", "all")], "all" - missing = [wav for wav in wav_files - if not existing.get(wav.stem, "").strip()] - if not missing: - return ([("Keep the existing transcripts", "keep"), - ("Re-transcribe all", "all")], "keep") - return ([("Only transcribe new voices", "missing"), - ("Re-transcribe all", "all")], "missing") - - def _plan_from_mode(mode: str, wav_files: list, existing: Dict[str, str]) -> dict: """Build the transcription PLAN for the chosen form MODE. The plan dict is what ``voices._transcribe`` consumes: "missing" - carries the .wavs lacking a transcript plus the existing mapping; - "all"/"keep" name the mode and reuse the mapping read while asking. + carries the .wavs lacking a transcript plus the existing mapping, + "all" re-transcribes everything; both reuse the mapping read while + applying the form. """ if mode == "missing": missing = [wav for wav in wav_files @@ -365,18 +346,6 @@ def _wizard(stdscr, args: argparse.Namespace, parser: argparse.ArgumentParser return next((f["value"] for f in fields_list if f.get("key") == key), default) - def _transcription_state(wav_dir): - """(wav_files, existing transcripts, prompt_text exists) or None.""" - if wav_dir is None: - return None - wav_files = find_wav_files(Path(wav_dir)) - if not wav_files: - return None - prompt_path = Path(wav_dir) / PROMPT_TEXT_FILENAME - prompt_exists = bool(prompt_path.exists()) and not args.force - existing = read_prompt_text(prompt_path) if prompt_exists else {} - return wav_files, existing, prompt_exists - def _apply_form(result: dict) -> dict: """Fold the form's answers into the settings and finalize.""" # Backend/build: the interactive combination. A backend whose @@ -400,15 +369,18 @@ def _wizard(stdscr, args: argparse.Namespace, parser: argparse.ArgumentParser # Transcription plan (transcription itself runs in the tail). s["plan"] = None if s["include_clone"]: - state = _transcription_state(s["wav_dir"]) - if state is not None: - wav_files, existing, prompt_exists = state - choices, default_mode = _transcription_choices( - wav_files, existing, prompt_exists) - mode = result.get("transcription") - if mode not in [candidate for _label, candidate in choices]: - mode = default_mode - s["plan"] = _plan_from_mode(mode, wav_files, existing) + wav_files = find_wav_files(Path(s["wav_dir"])) \ + if s["wav_dir"] is not None else [] + prompt_path = Path(s["wav_dir"]) / PROMPT_TEXT_FILENAME \ + if s["wav_dir"] is not None else None + existing = {} + if prompt_path is not None and prompt_path.exists() \ + and not args.force: + existing = read_prompt_text(prompt_path) + mode = result.get("transcription") + if mode not in ("missing", "all"): + mode = "missing" + s["plan"] = _plan_from_mode(mode, wav_files, existing) s["download"] = bool(result.get("download")) and ( _models.download_applicable(s["audiocpp_dir"], @@ -499,41 +471,16 @@ def _wizard(stdscr, args: argparse.Namespace, parser: argparse.ArgumentParser } fields.append(wav_field) - def state_of(fs): - return _transcription_state(_field_val(fs, "wav_dir")) - - initial_state = state_of([wav_field]) - initial_default = _transcription_choices(*initial_state)[1] \ - if initial_state is not None else "missing" - - def transcription_choices(fs): - state = state_of(fs) - if state is None: - return [("Re-transcribe all", "all")] - return _transcription_choices(*state)[0] - - def transcription_visible(fs) -> bool: - return state_of(fs) is not None - - def reset_transcription(fs_list) -> None: - # The directory changed: snap the stale choice to a valid one. - field = next((f for f in fs_list - if f.get("key") == "transcription"), None) - if field is not None: - modes = [mode for _label, mode in transcription_choices( - fs_list)] - if field["value"] not in modes: - state = state_of(fs_list) - field["value"] = _transcription_choices(*state)[1] \ - if state is not None else "missing" - + # Voice transcript handling: a plain in-place toggle, always + # offered whenever any clone-capable model is hosted (no + # dependency on what the picked directory currently holds). fields.append({ "key": "transcription", "label": "Voice transcripts", - "kind": "choice", "value": initial_default, - "choices": transcription_choices, - "visible": transcription_visible, + "kind": "toggle", "value": "missing", + "choices": [("Transcribe new voices", "missing"), + ("Re-transcribe all voices", "all")], + "visible": lambda fs: bool(s["include_clone"]), }) - wav_field["on_change"] = reset_transcription if _models.download_applicable(s["audiocpp_dir"], s["model_entries"]): fields.append({ @@ -575,7 +522,7 @@ def _wizard(stdscr, args: argparse.Namespace, parser: argparse.ArgumentParser result = tui.form( stdscr, "Configure audio.cpp", fields, buttons=("Continue", "Cancel"), - start_on_buttons=False, back_value=tui.Wizard.BACK) + start_on_buttons=True, back_value=tui.Wizard.BACK) if result is tui.Wizard.BACK: return tui.Wizard.BACK return _apply_form(result) diff --git a/app/backends/faster.py b/app/backends/faster.py index 60ddddf..6cfa53e 100755 --- a/app/backends/faster.py +++ b/app/backends/faster.py @@ -117,36 +117,16 @@ def load_voices(path: Path) -> dict: return data -def _decide_faster_transcription(wav_files: list, existing_voices: dict - ) -> tuple: - """Shape the transcription question for the setup form. - - Returns ``(choices, default_mode)``: CHOICES is a list of - ``(label, mode)`` pairs where MODE is ``"missing"`` (only the new - voices), ``"all"`` (re-transcribe everything) or ``"keep"`` - (reuse voices.json untouched). With new .wavs present transcribing - only those is offered first (and is the default); otherwise — and - always, per the modify design — re-transcribing everything stays - available, but keeping the existing file is the default. - """ - existing = dict(existing_voices) - new_wavs = [wav for wav in wav_files if wav.stem not in existing] - if new_wavs: - return ([("Only transcribe new voices", "missing"), - ("Re-transcribe all", "all")], "missing") - return ([("Keep the existing voices.json", "keep"), - ("Re-transcribe all", "all")], "keep") - - def _write_voices_json(output_path: Path, wav_dir: Path, language: str, - whisper_model: str, plan: Optional[dict]) -> Optional[dict]: + whisper_model: str, + plan: Optional[dict]) -> Optional[dict]: """Transcribe the wav dir and write voices.json; return the voices dict. - PLAN (built by ``_decide_faster_transcription`` in the wizard, or an - "all" plan for a fresh/flag run) decides whether every voice is - re-transcribed ("all"), only the new ones ("missing" — merged into the - existing entries), or nothing changes ("keep" — the existing file is - left untouched and returned as-is). None (cancelled) writes nothing. + PLAN (from the wizard's transcription toggle, or an "all" plan for a + fresh/flag run) decides whether every voice is re-transcribed + ("all"), only the new ones ("missing" — merged into the existing + entries), or nothing changes ("keep" — the existing file is left + untouched and returned as-is). None (cancelled) writes nothing. """ if plan is None: return None @@ -242,49 +222,14 @@ def _wizard(stdscr, args: argparse.Namespace) -> Optional[dict]: ] modifying = bool(existing_voices) and not args.force if modifying: - # Modify flow: offer keep/new-only/all when the picked directory - # holds .wavs. Recomputed live so switching directories updates it. - - def current_dir(fields_list): - value = next(f["value"] for f in fields_list - if f.get("key") == "wav_dir") - return Path(value) if value else wav_start - - choices_cache: dict = {} - - def transcription_field() -> dict: - wav_files = find_wav_files(current_dir(fields)) - if choices_cache.get("dir") != wav_files: - choices, default = _decide_faster_transcription( - wav_files, existing_voices) - choices_cache.clear() - choices_cache.update({"dir": wav_files, - "choices": choices, - "default": default}) - return choices_cache - - def transcription_choices(_fields_list): - return list(transcription_field()["choices"]) - - def reset_transcription(fields_list) -> None: - field = next(f for f in fields_list - if f.get("key") == "transcription") - modes = [mode for _label, mode - in transcription_field()["choices"]] - if field["value"] not in modes: - field["value"] = transcription_field()["default"] - + # Modify flow: a plain in-place toggle, always offered. fields.append({ "key": "transcription", "label": "Transcription", - "kind": "choice", - "value": transcription_field()["default"], - "choices": transcription_choices, - "visible": lambda fs: bool(find_wav_files(current_dir(fs))), + "kind": "toggle", "value": "missing", + "choices": [("Transcribe new voices", "missing"), + ("Re-transcribe all voices", "all")], "note": "An existing voices.json was found.", }) - # Changing the directory refreshes the transcription offer; - # tui.form calls the field's `on_change` with the field list. - fields[0]["on_change"] = reset_transcription result = tui.form( stdscr, "Set up faster-qwen3-tts", fields, @@ -299,7 +244,10 @@ def _wizard(stdscr, args: argparse.Namespace) -> Optional[dict]: if not modifying: plan = {"mode": "all", "missing": [], "existing": {}} elif find_wav_files(wav_dir): - plan = _plan_for(result["transcription"], wav_dir, existing_voices) + mode = result.get("transcription") + if mode not in ("missing", "all"): + mode = "missing" + plan = _plan_for(mode, wav_dir, existing_voices) else: # Directory without .wavs on a modify run: keep the existing file. plan = {"mode": "keep", "missing": [], |
