Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
da84c1c
added s3tokenizer model support
manmay-nakhashi Nov 20, 2025
5538f6d
add space
manmay-nakhashi Nov 20, 2025
81de3db
remove einops and fix tests
manmay-nakhashi Nov 20, 2025
40c587e
ruff formatting
manmay-nakhashi Nov 20, 2025
b20333e
added docs and fixed test
manmay-nakhashi Nov 20, 2025
dc8def9
fix tests
manmay-nakhashi Nov 20, 2025
cfc30cc
fix styles and unittests
manmay-nakhashi Nov 20, 2025
1ba22c0
added model docstring fixed init weights
manmay-nakhashi Nov 22, 2025
140a113
fix formatting
manmay-nakhashi Nov 24, 2025
8ed8746
fix tests
manmay-nakhashi Nov 24, 2025
469bd25
added to OBJECTS_TO_IGNORE
manmay-nakhashi Nov 24, 2025
a56e7d8
Merge branch 'main' into manmay-add-s3tokenizer
manmay-nakhashi Nov 24, 2025
bc5307b
Merge branch 'main' into manmay-add-s3tokenizer
manmay-nakhashi Nov 24, 2025
35323b9
update s3gen code
manmay-nakhashi Nov 24, 2025
8e53160
Merge branch 'add-s3gen-hifinet' of https://github.com/resemble-ai/tr…
manmay-nakhashi Nov 24, 2025
b6defe8
fix test imports
manmay-nakhashi Nov 24, 2025
578dd05
Merge branch 'add-s3gen-hifinet' of https://github.com/resemble-ai/tr…
manmay-nakhashi Nov 24, 2025
e9337b9
Merge branch 'main' into manmay-add-s3tokenizer
manmay-nakhashi Nov 24, 2025
194d53c
added working chatterbox support
manmay-nakhashi Nov 26, 2025
9f738c7
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Nov 26, 2025
2e99ea8
fix ruff formatting
manmay-nakhashi Nov 26, 2025
f26be53
Merge branch 'add-s3gen-hifinet' of https://github.com/resemble-ai/tr…
manmay-nakhashi Nov 26, 2025
b2a5b03
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Nov 26, 2025
0f367ec
fix style
manmay-nakhashi Nov 26, 2025
acc0dba
Merge branch 'add-s3gen-hifinet' of https://github.com/resemble-ai/tr…
manmay-nakhashi Nov 26, 2025
837b09c
remove T3huggigfaceBackend
manmay-nakhashi Nov 26, 2025
f9202f0
fix diffusers import and ruff fix
manmay-nakhashi Nov 26, 2025
5ec1a6c
ruff fix
manmay-nakhashi Nov 26, 2025
424b799
fix docstrings
manmay-nakhashi Nov 27, 2025
f6389a1
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Nov 27, 2025
f1ea87c
fix ruff format
manmay-nakhashi Nov 27, 2025
a16a7fa
Merge branch 'add-s3gen-hifinet' of https://github.com/resemble-ai/tr…
manmay-nakhashi Nov 27, 2025
9aa1d61
fix docstring
manmay-nakhashi Nov 27, 2025
d0059e1
ruff format
manmay-nakhashi Nov 27, 2025
c2248f0
merge models to reduce the independent submodels
manmay-nakhashi Nov 28, 2025
cb48a16
align it with diffuser class
manmay-nakhashi Nov 28, 2025
9ef0de4
fix models
manmay-nakhashi Dec 1, 2025
dfc7e75
fix docstring test
manmay-nakhashi Dec 1, 2025
8b76bb0
add internal model to private
manmay-nakhashi Dec 1, 2025
574b98d
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Dec 1, 2025
3d219f5
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Dec 1, 2025
f80e8be
fix config
manmay-nakhashi Dec 1, 2025
3ea4168
Merge branch 'add-s3gen-hifinet' of https://github.com/resemble-ai/tr…
manmay-nakhashi Dec 1, 2025
1c885dc
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Dec 1, 2025
b7a0ba0
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Dec 3, 2025
d74e6c0
resolve comments
manmay-nakhashi Dec 23, 2025
ad53461
fix test
manmay-nakhashi Dec 23, 2025
d0868a2
fix tests
manmay-nakhashi Dec 24, 2025
347a7ab
fix ruff added conversion script
manmay-nakhashi Dec 24, 2025
f520f02
fix unit tests
manmay-nakhashi Dec 24, 2025
0b511b2
fix ruff
manmay-nakhashi Dec 24, 2025
d8ccb98
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Dec 24, 2025
401105f
added kwargs
manmay-nakhashi Dec 24, 2025
ca9a968
add post init
manmay-nakhashi Jan 1, 2026
2dba921
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Jan 5, 2026
14cbca7
conditioning changes
manmay-nakhashi Jan 6, 2026
f4ac7c7
fix tests
manmay-nakhashi Jan 6, 2026
d416b19
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Jan 6, 2026
17d5e63
fix trch import check
manmay-nakhashi Jan 6, 2026
71f7f3c
fix torch import
manmay-nakhashi Jan 6, 2026
8226783
fixes
manmay-nakhashi Jan 6, 2026
2532b5a
fix
manmay-nakhashi Jan 6, 2026
ef14acd
fix import
manmay-nakhashi Jan 6, 2026
0904d33
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Jan 6, 2026
f75b192
fix unit test
manmay-nakhashi Jan 6, 2026
77cafeb
Merge branch 'main' into add-s3gen-hifinet
manmay-nakhashi Jan 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,10 @@
title: PE Audio
- local: model_doc/pop2piano
title: Pop2Piano
- local: model_doc/s3gen
title: S3Gen
- local: model_doc/s3tokenizer
title: S3Tokenizer
- local: model_doc/seamless_m4t
title: Seamless-M4T
- local: model_doc/seamless_m4t_v2
Expand Down
285 changes: 285 additions & 0 deletions docs/source/en/model_doc/chatterbox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
# Chatterbox

## Overview

Chatterbox is a complete text-to-speech (TTS) pipeline that combines three specialized models to convert text directly to natural-sounding speech. It was introduced in the [chatterbox repository](https://github.com/resemble-ai/chatterbox) and provides a unified interface for high-quality voice cloning and speech synthesis.

The pipeline consists of three main components:

1. **T3 Model**: Converts text tokens to speech tokens using a language model approach
2. **S3Gen Model**: Generates mel spectrograms from speech tokens using speaker-conditioned Conditional Flow Matching (CFM)
3. **HiFTNet Vocoder**: Converts mel spectrograms to high-fidelity waveforms

Chatterbox enables zero-shot voice cloning by conditioning the generation on a reference audio sample, allowing you to synthesize speech in any voice from just a few seconds of audio.

## Model Architecture

The Chatterbox model follows this end-to-end pipeline:

```
Text → Text Tokenizer → Text Tokens
Text Tokens + Style → T3 → Speech Tokens
Speech Tokens + Reference Audio → S3Gen → Mel Spectrograms
Mel Spectrograms → HiFTNet → Waveforms
```

### Key Features

- **End-to-end TTS**: Complete pipeline from text to waveform in a single model
- **Zero-shot voice cloning**: Clone any voice from a short reference audio sample
- **Multilingual support**: Supports both English-only and multilingual configurations
- **High-quality synthesis**: Uses state-of-the-art conditional flow matching and neural vocoding
- **Flexible configuration**: Each component can be configured independently
- **Style control**: Optional style conditioning for expressive speech synthesis

## Usage

### Basic Text-to-Speech

```python
from transformers import ChatterboxModel
import torch
import torchaudio

# Load model
model = ChatterboxModel.from_pretrained("ResembleAI/chatterbox-hf")
model = model.to("cuda") # or "cpu"
model.eval()

# Load text tokenizer
model.load_text_tokenizer("path/to/tokenizer.json")

# Load reference audio for voice cloning
ref_wav, ref_sr = torchaudio.load("reference.wav")

# Convert to mono if needed
if ref_wav.shape[0] > 1:
ref_wav = ref_wav.mean(dim=0, keepdim=True)

# Convert to numpy array
ref_audio = ref_wav.squeeze().numpy()

# Generate speech from text
text = "Hello, this is a text-to-speech demo using Chatterbox."
waveform = model.generate(
text=text,
reference_wav=ref_audio,
reference_sr=ref_sr,
exaggeration=0.5,
temperature=0.8,
top_p=0.95,
min_p=0.05,
repetition_penalty=1.2,
cfg_weight=0.5,
max_new_tokens=1000,
)

# Save output
torchaudio.save("output.wav", waveform.cpu().unsqueeze(0), 24000)
```

### Advanced: Two-Stage Generation

For more control, you can prepare the conditionals and run stages separately:

```python
import numpy as np

# Prepare conditionals (speaker embeddings and prompts)
conds = model.prepare_conditionals(
reference_wav=ref_audio, # numpy array
reference_sr=ref_sr,
exaggeration=0.5
)

# Stage 1: Prepare text tokens
text_tokens = model.prepare_text_tokens(text)

# Stage 2: Generate speech tokens using T3
with torch.no_grad():
speech_tokens = model.t3.inference(
t3_cond=conds.t3,
text_tokens=text_tokens[0],
max_new_tokens=1000,
temperature=0.8,
top_p=0.95,
)

# Stage 3: Generate waveform using S3Gen
with torch.no_grad():
waveform, _ = model.s3gen.inference(
speech_tokens=speech_tokens[0],
ref_dict=conds.gen,
finalize=True
)
```

### Pre-computed Conditionals for Batch Generation

For production use where you're generating multiple utterances with the same voice:

```python
import numpy as np

# Prepare conditionals once
conds = model.prepare_conditionals(
reference_wav=ref_audio,
reference_sr=ref_sr,
exaggeration=0.5
)

# Generate multiple outputs efficiently
texts = ["First sentence.", "Second sentence.", "Third sentence."]
waveforms = []

for text in texts:
# Prepare text tokens
text_tokens = model.prepare_text_tokens(text)

# Generate speech tokens
with torch.no_grad():
speech_tokens = model.t3.inference(
t3_cond=conds.t3,
text_tokens=text_tokens[0],
max_new_tokens=1000,
temperature=0.8,
)

# Generate waveform with cached embeddings
waveform, _ = model.s3gen.inference(
speech_tokens=speech_tokens[0],
ref_dict=conds.gen,
finalize=True
)
waveforms.append(waveform.squeeze(0))
```

### Generation with Return Intermediates

You can retrieve intermediate outputs (text tokens, speech tokens) for debugging:

```python
waveform, intermediates = model.generate(
text=text,
reference_wav=ref_audio,
reference_sr=ref_sr,
exaggeration=0.5,
temperature=0.8,
return_intermediates=True,
)

print(f"Text tokens: {intermediates['text_tokens'].shape}")
print(f"Speech tokens: {intermediates['speech_tokens'].shape}")
print(f"Waveform: {waveform.shape}")
```

## Model Details

### Configuration

The model can be configured via [`ChatterboxConfig`]:

```python
from transformers import ChatterboxConfig

# English-only configuration
config = ChatterboxConfig.english_only()

# Multilingual configuration
config = ChatterboxConfig.multilingual()

# Custom configuration
config = ChatterboxConfig(
t3_config={"num_layers": 12, "num_heads": 16},
s3gen_config={"encoder_num_blocks": 6},
hiftnet_config={"upsample_rates": [5, 5, 4, 2]},
is_multilingual=False,
)
```

### Input Requirements

For the `generate()` method:
- **Text**: String input (automatically normalized for punctuation via `punc_norm()`)
- **Reference Audio**: NumPy array of shape `(audio_length,)` - mono audio
- **Reference Sample Rate**: Integer (will be resampled internally to 16kHz for speaker encoder and 24kHz for mel extraction)
- **Tokenizer**: Optional - if not provided via parameter, must be loaded via `load_text_tokenizer()`

Generation parameters:
- **exaggeration** (float, default 0.5): Emotion/expressiveness level (0.0 to 1.0)
- **temperature** (float, default 0.8): Sampling temperature for token generation
- **top_p** (float, default 0.95): Top-p (nucleus) sampling threshold
- **min_p** (float, default 0.05): Minimum probability threshold
- **repetition_penalty** (float, default 1.2): Penalty for repeating tokens
- **cfg_weight** (float, default 0.5): Classifier-free guidance weight
- **max_new_tokens** (int, default 1000): Maximum speech tokens to generate

### Output

- **Waveforms**: Float tensor of shape `(batch_size, audio_samples)` at 24kHz sample rate

### Text Normalization

The model automatically applies text normalization via the `punc_norm()` function, which:
- Capitalizes the first letter
- Normalizes punctuation (replaces uncommon characters like "…" with ", ")
- Converts colons and semicolons to commas
- Replaces em-dashes and en-dashes with hyphens
- Ensures proper sentence ending (adds period if missing)
- Removes multiple spaces

Example:
```python
from transformers.models.chatterbox.modeling_chatterbox import punc_norm

text = "hello world... this is a test: with semicolons; and dashes—like this"
normalized = punc_norm(text)
# Output: "Hello world, this is a test, with semicolons, and dashes-like this."
```

## Limitations

- Requires GPU for real-time performance
- Reference audio quality directly affects output quality
- Reference audio should be 6-10 seconds for best results
- English-only model works best for English text; use multilingual model for other languages
- Text tokenizer must be loaded separately via `load_text_tokenizer()` if not passed as parameter
- Generated speech quality depends on T3 sampling parameters (temperature, top_p, etc.)

## Citation

If you use Chatterbox in your research, please cite:

```bibtex
@misc{chatterbox2025,
title={Chatterbox: High-Quality Text-to-Speech Synthesis},
author={Resemble AI},
year={2025},
publisher={GitHub},
url={https://github.com/resemble-ai/chatterbox}
}
```

## ChatterboxConfig

[[autodoc]] ChatterboxConfig
- english_only
- multilingual

## T3Config

[[autodoc]] T3Config

## ChatterboxFeatureExtractor

[[autodoc]] ChatterboxFeatureExtractor

## ChatterboxModel

[[autodoc]] ChatterboxModel
- forward
- generate
- prepare_text_tokens
- prepare_conditionals
- load_text_tokenizer

Loading