Spaces:
Running
on
Zero
Running
on
Zero
Den Pavloff
commited on
Commit
·
8a1b058
1
Parent(s):
f97fc67
fix token conflict
Browse files
CLAUDE.md
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# CLAUDE.md
|
2 |
+
|
3 |
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
4 |
+
|
5 |
+
## Project Overview
|
6 |
+
|
7 |
+
KaniTTS is a Text-to-Speech system that uses causal language models to generate speech via NeMo audio codec tokens. The project is deployed as a HuggingFace Gradio Space.
|
8 |
+
|
9 |
+
## Running the Application
|
10 |
+
|
11 |
+
```bash
|
12 |
+
# Run the Gradio app (launches on http://0.0.0.0:7860)
|
13 |
+
python app.py
|
14 |
+
```
|
15 |
+
|
16 |
+
The app requires a HuggingFace token set as the `HF_TOKEN` environment variable to download models.
|
17 |
+
|
18 |
+
## Architecture
|
19 |
+
|
20 |
+
### Token Flow Pipeline
|
21 |
+
|
22 |
+
The system uses a custom token layout that interleaves text and audio in a single sequence:
|
23 |
+
|
24 |
+
1. **Input prompt construction** (`KaniModel.get_input_ids`):
|
25 |
+
- `START_OF_HUMAN` → text tokens → `END_OF_TEXT` → `END_OF_HUMAN`
|
26 |
+
- Optionally prefixed with speaker ID (e.g., "andrew: Hello world")
|
27 |
+
|
28 |
+
2. **LLM generation** (`KaniModel.model_request`):
|
29 |
+
- Model generates sequence containing: text section + `START_OF_SPEECH` + audio codec tokens + `END_OF_SPEECH`
|
30 |
+
|
31 |
+
3. **Audio decoding** (`NemoAudioPlayer.get_waveform`):
|
32 |
+
- Extracts audio tokens between `START_OF_SPEECH` and `END_OF_SPEECH`
|
33 |
+
- Audio tokens are arranged in 4 interleaved codebooks (q=4)
|
34 |
+
- Tokens are offset by `audio_tokens_start + (codebook_size * codebook_index)`
|
35 |
+
- NeMo codec reconstructs waveform from the 4 codebooks
|
36 |
+
|
37 |
+
### Key Classes
|
38 |
+
|
39 |
+
**`NemoAudioPlayer`** (util.py:27-170)
|
40 |
+
- Loads NeMo AudioCodecModel for waveform reconstruction
|
41 |
+
- Manages special token IDs (derived from `tokeniser_length` base)
|
42 |
+
- Validates output has required speech markers
|
43 |
+
- Extracts and decodes 4-codebook audio tokens from LLM output
|
44 |
+
- Returns 22050 Hz audio as NumPy array
|
45 |
+
|
46 |
+
**`KaniModel`** (util.py:172-303)
|
47 |
+
- Wraps HuggingFace causal LM (loaded with bfloat16, auto device mapping)
|
48 |
+
- Prepares prompts with conversation/modality control tokens
|
49 |
+
- Runs generation with sampling parameters (temp, top_p, repetition_penalty)
|
50 |
+
- Delegates audio reconstruction to `NemoAudioPlayer`
|
51 |
+
- Returns tuple: (audio_array, text, timing_report)
|
52 |
+
|
53 |
+
**`InitModels`** (util.py:305-343)
|
54 |
+
- Factory that loads all models from `model_config.yaml` at startup
|
55 |
+
- Returns dict mapping model names to `KaniModel` instances
|
56 |
+
- All models share the same `NemoAudioPlayer` instance
|
57 |
+
|
58 |
+
**`Examples`** (util.py:345-387)
|
59 |
+
- Converts `examples.yaml` structure into Gradio Examples format
|
60 |
+
- Output order: `[text, model, speaker_id, temperature, top_p, repetition_penalty, max_len]`
|
61 |
+
|
62 |
+
### Configuration Files
|
63 |
+
|
64 |
+
**`model_config.yaml`**
|
65 |
+
- `nemo_player`: NeMo codec config (model name, token layout constants)
|
66 |
+
- `models`: Dict of available TTS models with device_map and optional speaker_id mappings
|
67 |
+
|
68 |
+
**`examples.yaml`**
|
69 |
+
- List of example prompts with associated parameters for Gradio UI
|
70 |
+
|
71 |
+
### Dependency Setup
|
72 |
+
|
73 |
+
`create_env.py` runs before imports in `app.py` to:
|
74 |
+
- Install transformers from git main branch (required for compatibility)
|
75 |
+
- Set `OMP_NUM_THREADS=4`
|
76 |
+
- Uses `/tmp/deps_installed` marker to avoid reinstalling on every run
|
77 |
+
|
78 |
+
## Important Token Constants
|
79 |
+
|
80 |
+
All special tokens are defined relative to `tokeniser_length` (64400):
|
81 |
+
- `start_of_speech = tokeniser_length + 1`
|
82 |
+
- `end_of_speech = tokeniser_length + 2`
|
83 |
+
- `start_of_human = tokeniser_length + 3`
|
84 |
+
- `end_of_human = tokeniser_length + 4`
|
85 |
+
- `start_of_ai = tokeniser_length + 5`
|
86 |
+
- `end_of_ai = tokeniser_length + 6`
|
87 |
+
- `pad_token = tokeniser_length + 7`
|
88 |
+
- `audio_tokens_start = tokeniser_length + 10`
|
89 |
+
- `codebook_size = 4032`
|
90 |
+
|
91 |
+
## Multi-Speaker Support
|
92 |
+
|
93 |
+
Models with `speaker_id` mappings in `model_config.yaml` support voice selection:
|
94 |
+
- Speaker IDs are prefixed to the text prompt (e.g., "andrew: Hello")
|
95 |
+
- The Gradio UI shows/hides speaker dropdown based on selected model
|
96 |
+
- Base models (v.0.1, v.0.2) generate random voices without speaker control
|
97 |
+
|
98 |
+
## HuggingFace Spaces Deployment
|
99 |
+
|
100 |
+
The README.md header contains HF Spaces metadata:
|
101 |
+
- `sdk: gradio` with version 5.46.0
|
102 |
+
- `app_file: app.py` as entrypoint
|
103 |
+
- References 3 model checkpoints and the NeMo codec
|
util.py
CHANGED
@@ -215,18 +215,26 @@ class KaniModel:
|
|
215 |
print(f"Target device: {self.device}")
|
216 |
|
217 |
# Load model with proper configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
self.model = AutoModelForCausalLM.from_pretrained(
|
219 |
self.conf.model_name,
|
220 |
-
|
221 |
-
device_map=self.conf.device_map,
|
222 |
-
token=self.hf_token,
|
223 |
-
trust_remote_code=True # May be needed for some models
|
224 |
)
|
225 |
|
|
|
|
|
|
|
|
|
226 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
227 |
-
self.conf.model_name,
|
228 |
-
|
229 |
-
trust_remote_code=True
|
230 |
)
|
231 |
|
232 |
print(f"Model loaded successfully on device: {next(self.model.parameters()).device}")
|
|
|
215 |
print(f"Target device: {self.device}")
|
216 |
|
217 |
# Load model with proper configuration
|
218 |
+
load_kwargs = {
|
219 |
+
"dtype": torch.bfloat16,
|
220 |
+
"device_map": self.conf.device_map,
|
221 |
+
"trust_remote_code": True
|
222 |
+
}
|
223 |
+
if self.hf_token:
|
224 |
+
load_kwargs["token"] = self.hf_token
|
225 |
+
|
226 |
self.model = AutoModelForCausalLM.from_pretrained(
|
227 |
self.conf.model_name,
|
228 |
+
**load_kwargs
|
|
|
|
|
|
|
229 |
)
|
230 |
|
231 |
+
tokenizer_kwargs = {"trust_remote_code": True}
|
232 |
+
if self.hf_token:
|
233 |
+
tokenizer_kwargs["token"] = self.hf_token
|
234 |
+
|
235 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
236 |
+
self.conf.model_name,
|
237 |
+
**tokenizer_kwargs
|
|
|
238 |
)
|
239 |
|
240 |
print(f"Model loaded successfully on device: {next(self.model.parameters()).device}")
|