How to Use Whisper as an STT Backend in the Speech-to-Speech Pipeline
To use Whisper as an STT backend, pass --stt whisper to the CLI and configure model parameters via --stt_model_name, --stt_device, and generation settings prefixed with --stt_gen_.
The huggingface/speech-to-speech repository implements a modular voice-agent pipeline (VAD → STT → LLM → TTS) where each stage is a pluggable backend. When you use Whisper as an STT backend, the system dynamically loads WhisperSTTHandler from src/speech_to_speech/STT/whisper_stt_handler.py to process audio chunks into text transcriptions.
Architecture Overview
The Speech-to-Speech pipeline uses a registry pattern to map backend names to handler implementations. When you configure Whisper as your STT provider, the system resolves the whisper key in the STT_BACKENDS registry defined in src/speech_to_speech/backend_registry.py (lines 97-108).
The data flow follows this sequence:
-
Argument Parsing – The
parse_argumentsfunction insrc/speech_to_speech/s2s_pipeline.pyreads the--sttflag and instantiatesWhisperSTTHandlerArgumentsfromsrc/speech_to_speech/arguments_classes/whisper_stt_arguments.py. -
Configuration Normalization – The
BackendSelection.normalizemethod converts the dataclass into a flat dictionary of kwargs, merging any generation parameters prefixed withgen_. -
Handler Instantiation – The
create_backend_handlerfactory dynamically importsWhisperSTTHandlerand passes the normalized configuration. -
Runtime Execution – The handler receives audio chunks from the VAD stage, preprocesses them using
AutoProcessor, runsmodel.generate, and pushes aTranscriptionobject downstream to the LLM.
The handler supports auto-detection of spoken languages when you specify --language auto, or you can constrain recognition to a specific language code (defined in the handler's SUPPORTED_LANGUAGES constant).
Configuration Options
Whisper-specific settings are exposed through CLI flags defined in WhisperSTTHandlerArguments. All generation parameters for the underlying transformers model use the --stt_gen_* prefix.
Key parameters:
--stt_model_name– Hugging Face Hub model ID (e.g.,openai/whisper-large-v3ordistil-whisper/distil-large-v3)--stt_device– Compute device (cuda,cpu, ormps)--stt_torch_dtype– Model precision (float16,bfloat16, orfloat32)--stt_compile_mode– Torch compile optimization mode--language– Language code (e.g.,en,fr) orautofor auto-detection--stt_gen_max_new_tokens– Maximum tokens per generation--stt_gen_num_beams– Beam search width--stt_gen_temperature– Sampling temperature
Running Whisper STT
Server Mode with Whisper
Deploy the Realtime server using Whisper for speech-to-text transcription:
# Install optional Whisper dependencies
pip install "speech-to-speech[whisper]"
# Start server with Whisper large-v3 on GPU
speech-to-speech serve \
--stt whisper \
--stt_model_name openai/whisper-large-v3 \
--stt_device cuda \
--stt_torch_dtype float16 \
--stt_gen_max_new_tokens 128 \
--language auto
This command initializes the WhisperSTTHandler with AutoModelForSpeechSeq2Seq.from_pretrained, executes a warmup forward pass to prime CUDA graphs, and listens for WebSocket connections.
Connect a client to stream microphone audio:
speech-to-speech talk \
--url ws://127.0.0.1:8765/v1/realtime \
--model local
Local Mode
Run both the server and client in a single process for local testing:
speech-to-speech local \
--stt whisper \
--stt_model_name distil-whisper/distil-large-v3 \
--stt_device cpu \
--language en
Local mode instantiates the handler in the main process, bypassing the network layer while maintaining the same STT_BACKENDS registry resolution logic found in src/speech_to_speech/s2s_pipeline.py (lines 70-77).
Custom Generation Settings
Tune the transcription behavior by passing generation kwargs directly to the underlying model:
speech-to-speech serve \
--stt whisper \
--stt_gen_max_new_tokens 256 \
--stt_gen_temperature 0.0 \
--stt_gen_num_beams 4
The prepare_model_inputs method in whisper_stt_handler.py builds the gen_kwargs dictionary from these flags and passes it to model.generate().
Implementation Details
The WhisperSTTHandler class in src/speech_to_speech/STT/whisper_stt_handler.py handles model lifecycle and inference:
- Model Loading – Uses
AutoProcessor.from_pretrainedandAutoModelForSpeechSeq2Seq.from_pretrainedwith device mapping and dtype conversion - Warmup – The
warmupmethod runs a dummy forward pass to initialize Torch-Compile caches or CUDA graphs before processing real-time audio - Processing – The
processmethod implements the handler interface, receiving audio chunks and returningTranscriptiondataclasses
The backend registration in src/speech_to_speech/backend_registry.py wires the whisper string key to this handler class, enabling runtime selection via the --stt CLI argument.
Summary
- Use
--stt whisperto select the Whisper backend when runningspeech-to-speech serveorspeech-to-speech local - Configure the model via
--stt_model_name,--stt_device, and--stt_torch_dtypeflags - Pass generation parameters using the
--stt_gen_*prefix to control beam search, temperature, and token limits - The handler auto-detects language with
--language autoor accepts specific language codes - Source implementations reside in
whisper_stt_handler.pyand register viabackend_registry.py
Frequently Asked Questions
What Whisper models are supported?
You can use any Whisper checkpoint available on the Hugging Face Hub, including openai/whisper-large-v3, openai/whisper-base, and distilled variants like distil-whisper/distil-large-v3. Pass the model ID to --stt_model_name.
How do I enable language auto-detection?
Add --language auto to your CLI command. The handler uses the model's built-in language classification capabilities to identify the spoken language from the audio content. Alternatively, specify a language code (e.g., --language en) to constrain recognition.
Can I use Torch compile with Whisper STT?
Yes. Pass --stt_compile_mode default or --stt_compile_mode max-autotune to enable Torch compilation. The warmup method in WhisperSTTHandler runs a dummy inference pass to compile the graph before processing real audio chunks, preventing compilation latency during live transcription.
Why is there a warmup phase when starting the server?
The warmup method executes a forward pass with dummy inputs to initialize CUDA memory pools and Torch-Compile caches. This prevents cold-start latency when the first real audio arrives from the VAD stage, ensuring consistent real-time performance for the voice pipeline.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →