Delayed Streams Modeling (DSM)

repository·main·Indexed 25 days ago

https://github.com/kyutai-labs/delayed-streams-modeling

Implementations of Kyutai's Speech-to-Text (STT) and Text-to-Speech (TTS) models optimized for real-time streaming. Provides multiple implementation paths: PyTorch for research, Rust (moshi-server) for production websocket streaming, and MLX for on-device inference on Apple silicon. Includes support for models such as kyutai/stt-1b-en_fr and kyutai/stt-2.6b-en, featuring word-level timestamps and semantic Voice Activity Detection (VAD).

Tokens
3.5K
Snippets
13
Records
23
Agent score
84%

What's inside delayed-streams-modeling

  1. Overview of Kyutai Speech-To-Text (STT) Models

    main

    Kyutai STT models are optimized for real-time streaming inference, support batching for high efficiency, and provide word-level timestamps.

    Available models:

    • kyutai/stt-1b-en_fr: English and French, ~1B parameters, 0.5s delay, includes semantic Voice Activity Detection (VAD).
    • kyutai/stt-2.6b-en: English-only, ~2.6B parameters, 2.5s delay.
  2. Set up and run the Kyutai STT Rust Server

    main

    The Rust implementation is designed for production streaming via websockets.

    1. Install the server crate (requires CUDA feature for GPU support):
    cargo install --features cuda moshi-server
    1. Start the server using the appropriate config file:
    • For stt-1b-en_fr: configs/config-stt-en_fr.hf.toml
    • For stt-2.6b-en: configs/config-stt-en-hf.toml
    moshi-server worker --config configs/config-stt-en_fr-hf.toml
    1. Transcribe audio:
    • From microphone: uv run scripts/stt_from_mic_rust_server.py
    • From file: uv run scripts/stt_from_file_rust_server.py audio/bria.mp3 (use --rtf 1000 to process as fast as possible).
    cargo install --features cuda moshi-server
  3. Choose a Kyutai STT Implementation

    main

    Select an implementation based on your target environment:

    • PyTorch: Best for research, tinkering, and Python-based experimentation.
    • Rust: Best for production environments. Provides a robust server with streaming access over websockets.
    • MLX: Best for on-device inference on Apple silicon (Mac and iPhone).
  4. Run Kyutai STT using MLX (Apple Silicon)

    main

    For on-device inference on Mac or iPhone, use the moshi-mlx package (version 0.2.6 or later).

    Run inference on a file:

    python -m moshi_mlx.run_inference --hf-repo kyutai/stt-2.6b-en-mlx audio/bria.mp3 --temp 0

    Run inference from microphone:

    python scripts/stt_from_mic_mlx.py

    Note: MLX models can also be used in Swift via the moshi-swift codebase.

    python -m moshi_mlx.run_inference --hf-repo kyutai/stt-2.6b-en-mlx audio/bria.mp3 --temp 0
  5. Deploy the Rust server for production Kyutai TTS

    main

    The Rust implementation provides a robust server capable of processing multiple streaming queries in parallel via websockets. It is recommended for production environments.

    Installation: Installing the Rust server is complex because it relies on the Python implementation. It is recommended to use the start_tts.sh script from the unmute repository to ensure all Python dependencies are correctly installed. If you have a broken installation, run cargo uninstall moshi-server before reinstalling.

    Starting the Server: Run the server using the provided configuration file:

    moshi-server worker --config configs/config-tts.toml

    Connecting to the Server: Once the server is running, use the tts_rust_server.py script to interact with it:

    # From stdin, plays audio immediately
    echo "Hey, how are you?" | python scripts/tts_rust_server.py - -
    
    # From text file to audio file
    python scripts/tts_rust_server.py text_to_say.txt audio_output.wav

    Configuration: Modify configs/config-tts.toml to adjust server settings. Refer to the comments within the file for available options.

  6. Use the PyTorch implementation for Kyutai TTS

    main

    The PyTorch implementation is intended for research and experimentation. You can use the tts_pytorch.py script for standard generation (waits for all text before starting) or tts_pytorch_streaming.py for a fully streaming implementation.

    Requirements:

    • Requires the moshi package.
    • You can install it via pip install moshi or use uvx --with moshi to run without a manual install step.

    Usage Examples:

  7. Run Kyutai STT using PyTorch

    main

    The PyTorch implementation requires the moshi package (version 0.2.6 or later).

    To run inference on an audio file directly:

    python -m moshi.run_inference --hf-repo kyutai/stt-2.6b-en audio/bria.mp3

    If you use uv, you can run it without manual installation:

    uvx --with moshi python -m moshi.run_inference --hf-repo kyutai/stt-2.6b-en audio/bria.mp3
    python -m moshi.run_inference --hf-repo kyutai/stt-2.6b-en audio/bria.mp3
  8. Use the MLX implementation for Apple Silicon

    main

    The MLX implementation is optimized for on-device inference on iPhone and Mac using Apple's ML framework for hardware acceleration.

    Requirements:

    • Requires the moshi-mlx package.
    • Install via pip install moshi-mlx or use uvx --with moshi-mlx.

    Usage: Use the tts_mlx.py script. If the model cannot maintain real-time performance, use the --quantize flag with values 4 or 8 to speed up inference.

    Usage Examples:

  9. Install Moshi TTS

    main

    You can install the moshi package using two different methods depending on your requirements:

    Fast install (minimal dependencies): Use this if you want to avoid downloading large packages like torch and cuda immediately, but note it might break in the future.

    Slow install (future proof): Use this to ensure all necessary dependencies like torch and cuda are correctly installed.

    # Fast install
    !pip install 'safetensors<0.6'
    !pip install 'sphn<0.2'
    !pip install --no-deps "moshi==0.2.11"
    
    # Slow install (recommended for stability)
    !pip install "moshi==0.2.11"
    # Fast install
    !pip install 'safetensors<0.6'
    !pip install 'sphn<0.2'
    !pip install --no-deps "moshi==0.2.11"
    
    # Slow install
    # !pip install "moshi==0.2.11"
  10. Perform Speech-to-Text inference

    main

    To run STT inference, you must process audio in PCM format. It is recommended to pad the input audio based on the stt_config to account for silence prefixes and audio delays.

    1. Load audio using sphn.
    2. Retrieve stt_config from checkpoint_info.
    3. Calculate pad_left using audio_silence_prefix_seconds and pad_right using audio_delay_seconds + 1.0 (multiplied by the sample rate, typically 24000).
    4. Use an inference loop to encode audio chunks with mimi.encode() and step through the language model with lm_gen.step().
    # Example setup for inference
    stt_config = checkpoint_info.stt_config
    pad_left = int(stt_config.get("audio_silence_prefix_seconds", 0.0) * 24000)
    pad_right = int((stt_config.get("audio_delay_seconds", 0.0) + 1.0) * 24000)
    
    in_pcms = torch.nn.functional.pad(in_pcms, (pad_left, pad_right), mode="constant")
    in_pcms = in_pcms[None, 0:1].expand(1, -1, -1)
    
    # Assuming an InferenceState class is implemented as per the notebook
    state = InferenceState(mimi, text_tokenizer, lm, batch_size=1, device=device)
    text = state.run(in_pcms)
    print(text)