File size: 7,238 Bytes
308155b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import os
import requests
import sys
import json
from tqdm import tqdm
from transformers import AutoTokenizer
from src.config import TrainConfig


DEST_DIR = "pretrained_models"
 
CHATTERBOX_TURBO_FILES = {
    "ve.safetensors": "https://huggingface.co/ResembleAI/chatterbox-turbo/resolve/main/ve.safetensors?download=true",
    "t3_turbo_v1.safetensors": "https://huggingface.co/ResembleAI/chatterbox-turbo/resolve/main/t3_turbo_v1.safetensors?download=true",
    "s3gen_meanflow.safetensors": "https://huggingface.co/ResembleAI/chatterbox-turbo/resolve/main/s3gen_meanflow.safetensors?download=true",
    "conds.pt": "https://huggingface.co/ResembleAI/chatterbox-turbo/resolve/main/conds.pt?download=true",
    "vocab.json": "https://huggingface.co/ResembleAI/chatterbox-turbo/resolve/main/vocab.json?download=true",
    "added_tokens.json": "https://huggingface.co/ResembleAI/chatterbox-turbo/resolve/main/added_tokens.json?download=true",
    "special_tokens_map.json": "https://huggingface.co/ResembleAI/chatterbox-turbo/resolve/main/special_tokens_map.json?download=true",
    "tokenizer_config.json": "https://huggingface.co/ResembleAI/chatterbox-turbo/resolve/main/tokenizer_config.json?download=true",
    "merges.txt": "https://huggingface.co/ResembleAI/chatterbox-turbo/resolve/main/merges.txt?download=true",
    "grapheme_mtl_merged_expanded_v1.json": "https://huggingface.co/ResembleAI/chatterbox/resolve/main/grapheme_mtl_merged_expanded_v1.json?download=true"
}


CHATTERBOX_FILES = {
    "ve.safetensors": "https://huggingface.co/ResembleAI/chatterbox/resolve/main/ve.safetensors?download=true",
    "t3_cfg.safetensors": "https://huggingface.co/ResembleAI/chatterbox/resolve/main/t3_mtl23ls_v2.safetensors?download=true",
    "s3gen.safetensors": "https://huggingface.co/ResembleAI/chatterbox/resolve/main/s3gen.safetensors?download=true",
    "conds.pt": "https://huggingface.co/ResembleAI/chatterbox/resolve/main/conds.pt?download=true",
    "tokenizer.json": "https://huggingface.co/ResembleAI/chatterbox/resolve/main/grapheme_mtl_merged_expanded_v1.json?download=true"
}

def download_file(url, dest_path):
    """Downloads a file from a URL to a specific destination with a progress bar."""
    
    if os.path.exists(dest_path):
        print(f"File already exists: {dest_path}")
        return

    print(f"Downloading: {os.path.basename(dest_path)}...")
    
    try:
        
        response = requests.get(url, stream=True)
        response.raise_for_status()
        
        total_size = int(response.headers.get('content-length', 0))
        block_size = 1024
        
        with open(dest_path, 'wb') as file, tqdm(
            desc=os.path.basename(dest_path),
            total=total_size,
            unit='iB',
            unit_scale=True,
            unit_divisor=1024,
        ) as bar:
            
            for data in response.iter_content(block_size):
                
                size = file.write(data)
                bar.update(size)
                
        print(f"Download complete: {dest_path}\n")
        
        
    except requests.exceptions.RequestException as e:
        print(f"Error downloading {url}: {e}")
        sys.exit(1)



def merge_and_save_turbo_tokenizer():
    """

    It combines the downloaded original GPT-2 tokenizer with our custom vocab

    and overwrites the original files.

    """
    print("\n--- Turbo Vocab Merging Begins ---")
    
    try:
        base_tokenizer = AutoTokenizer.from_pretrained("gpt2-medium")
    except Exception as e:
        print(f"ERROR: The original tokenizer could not be loaded. Did you download the files correctly? -> {e}")
        return 0
        
        
    initial_len = len(base_tokenizer)
    print(f"   Original Size: {initial_len}")


    custom_vocab_path = os.path.join(DEST_DIR, "grapheme_mtl_merged_expanded_v1.json")
    
    print(f"Loading: Custom Vocab ({custom_vocab_path})")
    
    with open(custom_vocab_path, 'r', encoding='utf-8') as f:
        custom_data = json.load(f)


    if "model" in custom_data and "vocab" in custom_data["model"]:
        vocab_dict = custom_data["model"]["vocab"]
        
    else:
        print("Warning: The custom VOCAB format may differ from what is expected.")
        return 0

    unique_tokens_to_add = list(vocab_dict.keys())
    added_count = base_tokenizer.add_tokens(unique_tokens_to_add)
    final_len = len(base_tokenizer)

    print(f"Merging: {added_count} new token added.")
    print(f"   New Dimension: {final_len}")


    print(f"Saving: Writing the combined tokenizer to the '{DEST_DIR}' folder...")
    base_tokenizer.save_pretrained(DEST_DIR)
    
    print("MERGER SUCCESSFUL!")
    
    return final_len



def test_merge_tokenizer_process(tokenizer_path):
    
    try:

        tok = AutoTokenizer.from_pretrained(tokenizer_path)
        
        print(f"--- RESULTS ---")
        print(f"Folder: {tokenizer_path}")
        print(f"Actual Vocab Size (len): {len(tok)}")

        test_token = "[ta]"
        test_id = tok.encode(test_token, add_special_tokens=False)
        
        print(f"Test Token '{test_token}' ID: {test_id}")
        
        if len(tok) > 50276:
            print("SUCCESS! New tokens have been added.")
            
        else:
            print("ERROR: The size still appears old.")


    except Exception as e:
        print(f"Error: {e}")




def main():
    
    print("--- Chatterbox Pretrained Model Setup ---\n")
    
    # 1. Create the directory if it doesn't exist
    if not os.path.exists(DEST_DIR):
        
        print(f"Creating directory: {DEST_DIR}")
        os.makedirs(DEST_DIR, exist_ok=True)
        
    else:
        print(f"Directory found: {DEST_DIR}")
        

    cfg = TrainConfig()

    if cfg.is_turbo:
        print(f"Mode: CHATTERBOX-TURBO (Checking {len(CHATTERBOX_TURBO_FILES)} files)")
        FILES_TO_DOWNLOAD = CHATTERBOX_TURBO_FILES
        
    else:
        print(f"Mode: CHATTERBOX-TTS (Checking {len(CHATTERBOX_FILES)} files)")
        FILES_TO_DOWNLOAD = CHATTERBOX_FILES

    # 2. Download files
    for filename, url in FILES_TO_DOWNLOAD.items():
        dest_path = os.path.join(DEST_DIR, filename)
        download_file(url, dest_path)

    if cfg.is_turbo:
        new_vocab_size = merge_and_save_turbo_tokenizer()
        if new_vocab_size > 0:
            
            #test_merge_tokenizer_process(DEST_DIR)

            print("\n" + "="*60)
            print("INSTALLATION COMPLETE (CHATTERBOX-TURBO MODE)")
            print("All models are set up in 'pretrained_models/' folder.")
            print(f"Please update the 'new_vocab_size' value in the 'src/config.py' file")
            print(f"to: {new_vocab_size}")
            print("="*60 + "\n")

    else:
        print("\nINSTALLATION COMPLETE (CHATTERBOX-TTS MOD)")
        print("All models are set up in 'pretrained_models/' folder.")
        print(f"Note: 'grapheme_mtl_merged_expanded_v1.json' was saved as 'tokenizer.json' for the new vocabulary.")



if __name__ == "__main__":
    main()