diff options
| author | historia <historiavg@proton.me> | 2026-08-26 01:43:41 -0400 |
|---|---|---|
| committer | historia <historiavg@proton.me> | 2026-08-26 01:43:41 -0400 |
| commit | acbd9ff2c91182d96c57ffb57bee6e9b3fcbcbd4 (patch) | |
| tree | e336c11f2a57cff5566e249aa5d5477a3dc63c55 /app/converter/clients/transcribe.py | |
| parent | 104a0d65c1ba37847c15b64212b7fec8ba371ccb (diff) | |
| download | tts-audiobook-generator-acbd9ff2c91182d96c57ffb57bee6e9b3fcbcbd4.tar.gz | |
refactor: split tts.py into per-backend packages
Diffstat (limited to 'app/converter/clients/transcribe.py')
| -rw-r--r-- | app/converter/clients/transcribe.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/app/converter/clients/transcribe.py b/app/converter/clients/transcribe.py new file mode 100644 index 0000000..d2db9f1 --- /dev/null +++ b/app/converter/clients/transcribe.py @@ -0,0 +1,54 @@ +"""Optional local Whisper transcription of reference audio.""" + +import logging +from typing import Optional + +logger = logging.getLogger(__name__) + + +def transcribe_reference_audio(audio_path: str, model_name: str = "base") -> Optional[str]: + """Transcribe reference audio locally using an optional Whisper backend. + + The current qwen-tts demo does not expose a transcription endpoint, so + transcription is done client-side when a Whisper package is available. + Returns None if no backend is installed. + """ + for backend in ("faster_whisper", "whisper"): + try: + if backend == "faster_whisper": + from faster_whisper import WhisperModel + model = WhisperModel(model_name, device="cpu", compute_type="int8") + segments, _ = model.transcribe(audio_path) + text = " ".join(seg.text.strip() for seg in segments).strip() + else: + import whisper + model = whisper.load_model(model_name) + result = model.transcribe(audio_path) + text = (result.get("text") or "").strip() + if text: + logger.info("Transcription complete via %s: %s", backend, text) + return text + except ImportError: + continue + except Exception as exc: + logger.warning("%s transcription failed: %s", backend, exc) + logger.warning("No Whisper backend available; transcription skipped.") + return None + + +def whisper_backend_available() -> Optional[str]: + """Return the name of an importable Whisper backend, or None. + + Checks faster_whisper first (preferred), then the openai-whisper + package, without importing the heavy model code: a bare import probe + is enough to tell whether the package is installed in the current + environment. Used by the make_audiocpp_server_json tool to warn when + neither is present (e.g. the wrong conda environment is active). + """ + for backend in ("faster_whisper", "whisper"): + try: + __import__(backend) + except ImportError: + continue + return backend + return None |
