Chatterbox TTS Server

repository·main·Indexed 23 days ago

https://github.com/devnen/chatterbox-tts-server

A self-hosted text-to-speech solution providing an OpenAI-compatible API and Web UI for Resemble AI's Chatterbox models. It supports voice cloning, multilingual synthesis (23 languages), and expressive paralinguistic tags via the Chatterbox-Turbo engine. The server is optimized for NVIDIA (CUDA), AMD (ROCm), and Apple Silicon (MPS) GPUs, and includes features for audiobook generation through intelligent text chunking.

Tokens
26.6K
Snippets
58
Records
132
Agent score
76%

What's inside chatterbox-tts-server

  1. Overview of Chatterbox TTS Server

    main

    Chatterbox TTS Server is a self-hosted Text-to-Speech (TTS) solution that provides an OpenAI-compatible API and a modern Web UI. It is built around Resemble AI's Chatterbox open-source model family.

    Key capabilities include:

    • Model Support: Supports the original high-quality model, Multilingual support (23 languages), and Chatterbox-Turbo (a high-throughput 350M-parameter model).
    • Expressive Audio: Chatterbox-Turbo supports paralinguistic tags like [laugh], [cough], and [chuckle].
    • Advanced Features: Voice cloning, large text processing via intelligent chunking, audiobook generation, and reproducible voices using a generation seed.
    • Hardware Acceleration: Optimized for NVIDIA (CUDA), AMD (ROCm), and Apple Silicon (MPS) GPUs, with a CPU fallback.
    • Compatibility: Provides an OpenAI-compatible API, making it easy to swap into existing workflows designed for OpenAI's TTS.
  2. Select and hot-swap TTS models

    main

    You can select different models by setting the model.repo_id in config.yaml or by using the engine dropdown in the Web UI. All three models are hot-swappable from the Web UI without requiring a server restart.

    Available Models:

    • chatterbox (or original): Original 0.5B English model with emotion exaggeration.
    • chatterbox-turbo (or turbo): 350M Turbo model supporting paralinguistic tags like [laugh], [cough], and [chuckle].
    • chatterbox-multilingual (or multilingual): 0.5B multilingual model with 23-language support.
  3. Understand the Chatterbox TTS Server directory structure

    main

    The project is organized into several key functional areas:

    • Core Logic: server.py (FastAPI application), engine.py (model loading and generation), config.py (configuration management), and models.py (API validation).
    • Web UI: Located in the ui/ directory, containing index.html, script.js, and presets.yaml.
    • Data & Assets:
      • config.yaml: The primary configuration file.
      • outputs/: Default directory for generated audio files.
      • reference_audio/: Directory for user-uploaded audio used in voice cloning.
      • voices/: Directory for predefined voice audio files.
      • model_cache/: Local cache for models downloaded via download_model.py.
    Chatterbox-TTS-Server/
    │
    ├── config.py             # Manages config.yaml, default values, accessors
    ├── config.yaml           # PRIMARY configuration file (created/managed by server)
    ├── docker-compose.yml    # Docker Compose setup for containerized deployment
    ├── Dockerfile            # Docker image definition
    ├── documentation.md      # This comprehensive documentation file
    ├── download_model.py     # Utility to download specific model files to a local cache
    ├── engine.py             # Core model loading (from_pretrained) & generation logic
    ├── models.py             # Pydantic models for API request validation and structure
    ├── README.md             # Project summary and quick start guide
    ├── requirements.txt      # Python package dependencies
    ├── server.py             # Main FastAPI application, API endpoints, UI routes
    │
    ├── ui/                   # Contains all files for the Web User Interface
    │   ├── index.html        # Main HTML template for the UI
    │   ├── presets.yaml      # Predefined examples for TTS generation, loaded by the UI
    │   └── script.js         # Frontend JavaScript for UI interactivity and API communication
    │
    ├── model_cache/          # Default directory for download_model.py script
    ├── outputs/              # Default directory for audio files saved from UI or API
    ├── reference_audio/      # Default directory for user-uploaded reference audio files for voice cloning
    └── voices/               # Default directory for predefined voice audio files
  4. Initialize and use `config.yaml`

    main
    The server uses config.yaml for all settings (ports, paths, generation parameters). If the file does not exist in the project root, the server will automatically generate one with default values upon the first run. You can modify this file at any time to customize the server behavior.
  5. Security: Path Traversal Protection

    main

    The Chatterbox TTS Server implements path traversal protection for the predefined_voice_id, reference_audio_filename, and voice fields. All file access is sandboxed within the configured predefined_voices_path and reference_audio_path using utils.safe_resolve_within().

    Any attempt to use traversal sequences (e.g., ..), absolute paths, or symlinks pointing outside the sandbox will result in an HTTP 400 error.

  6. Available TTS Engines and their capabilities

    main

    The server supports three hot-swappable engines that can be switched via the Web UI without restarting the server:

    1. Original Chatterbox: High-quality English output with emotion exaggeration control (0.5B parameters).
    2. Chatterbox Multilingual: Supports 23 languages (including Arabic, Chinese, French, German, Japanese, and Spanish) with voice cloning and emotion control (0.5B parameters).
    3. Chatterbox-Turbo: Optimized for speed with a 350M-parameter architecture. It supports Paralinguistic Tags (e.g., [laugh], [cough], [chuckle]) for expressive narration.
  7. Understand the Chatterbox TTS Server component architecture

    main

    The server operates through the interaction of several components:

    1. User Interface/Client: Interacts with the FastAPI server via the Web UI or direct API calls.
    2. FastAPI Server (server.py): Serves the Web UI, handles API requests, and manages configuration via config.py.
    3. TTS Engine (engine.py): Receives parameters and text chunks from the server to perform actual generation using the ChatterboxTTS model (loaded from Hugging Face).
    4. Utilities: Handles chunking logic, audio processing (using soundfile and librosa), and file management.
    5. Backend Dependencies: Relies on PyTorch and CUDA for hardware acceleration.
    ┌───────────────────┐      ┌───────────────────┐      ┌───────────────────────────┐      ┌───────────────────┐
    │ User (Web UI /    │────→ │ FastAPI Server    │────→ │ TTS Engine (engine.py)    │────→ │ ChatterboxTTS     │
    │ API Client)       │      │ (server.py)       │      │ (Handles Chunks/Params)   │      │ (from HF Hub)     │
    └───────────────────┘      └─────────┬─────────┘      └──────┬─────────┬──────────┘      └─────────┬─────────┘
          ↑                            │                      │ Calls   │                            │ (Uses PyTorch)
          │ (Serves UI,               │ Uses                 │         │                            │
          │  API data)                ▼                      ▼         ▼                            │
          └───────────────── │ Configuration     │ ←─ │ config.yaml     │  │ Utilities         │      │
                             │ (config.py)       │  └───────────────────┘  │ (utils.py)        │      │
                             └───────────────────┘                         │ - Chunking Logic  │      │
                                       ▲                                   │ - Audio Proc.     │      │
                                       │ Uses                              │ - File Handling   │      │
                                       │                                   └──────┬────────────┘      │
    ┌───────────────────┐      ┌───────────────────┐      ┌───────────────────┐      │                   │
    │ Web UI Files      │ ←─── │ API Data / HTML   │      │ Audio Libraries   │←─────┘                   │
    │ (ui/*)            │      │ (via server.py)   │      │ (soundfile, librosa)│                       ▼
    └───────────────────┘      └───────────────────┘      └───────────────────┘                 ┌───────────────────┐
                                                                                            │ PyTorch / CUDA    │
                                                                                            └───────────────────┘
  8. Understand model downloading and caching

    main

    The server uses the Hugging Face Hub for model management.

    • Runtime Loading: When the server starts, engine.py calls ChatterboxTTS.from_pretrained(). If the model specified by model.repo_id in config.yaml is not found, it is automatically downloaded to the standard Hugging Face cache directory (e.g., ~/.cache/huggingface/hub on Linux/macOS or %USERPROFILE%\.cache\huggingface\hub on Windows). You can override this location using the HF_HOME environment variable.
    • Manual Download Utility: The download_model.py script is a separate utility that downloads model files to the directory specified by paths.model_cache in config.yaml (default: ./model_cache/). Note: The server's runtime engine does not load models from this custom directory; it only uses the global Hugging Face cache.
  9. Compare Chatterbox TTS model variants

    main

    The server supports three hot-swappable model variants via the Web UI engine dropdown or by setting model.repo_id in config.yaml.

    • Original Chatterbox (model.repo_id: chatterbox): Best for high-fidelity English-only speech. Supports exaggeration and cfg_weight for emotion control.
    • Chatterbox Turbo (model.repo_id: chatterbox-turbo): Optimized for low-latency/real-time workflows. Supports paralinguistic tags like [laugh], [cough], and [chuckle]. Note: It ignores exaggeration, cfg_weight, and min_p parameters.
    • Chatterbox Multilingual (model.repo_id: chatterbox-multilingual): Supports 23 languages (e.g., de, ja, fr). Provides zero-shot voice cloning across languages. Use the language parameter (ISO 639-1 code) on the /tts endpoint to select the target language.
  10. Optimize performance with BF16 and Caching

    main

    To increase throughput on supported hardware (RTX 30/40/50, A100, H100, Strix Halo), you can enable BF16 inference by setting the environment variable TTS_BF16=on (or TTS_BF16=auto). This can provide roughly 40% throughput gain.

    Other Performance Features:

    • Voice conditioning cache: Automatically caches re-encoding for repeated requests using the same reference voice. The cache is keyed by (path, mtime, exaggeration) and clears on reload_model() or /api/unload.
    • Chunk size: Adjust the chunk_size parameter on the /tts endpoint (default 120, range 50–500). Larger chunks reduce request frequency but increase VRAM usage.
    • Streaming: Use stream: true on the /tts endpoint for long-form content like audiobooks.
  11. Hot-swap between Chatterbox engines

    main

    The Web UI allows you to instantly switch between different models in the Chatterbox family without restarting the server or changing configuration. The backend automatically loads the selected engine.

    Available Engines:

    1. Original Chatterbox: High-quality English output with emotion exaggeration control (0.5B parameters).
    2. Chatterbox Multilingual: 23-language support with voice cloning and emotion control (0.5B parameters).
    3. Chatterbox-Turbo: Fastest inference, optimized for real-time/agent workflows using a 350M-parameter architecture and 1-step diffusion (supports paralinguistic tags).

    All UI and API requests are automatically routed through the currently active engine.