Thorsten-Voice commited on
Commit
0119dc0
·
verified ·
1 Parent(s): 4737328

Added jupyter notebook of OrpheusTTS used to tokenize Thorsten-Voice dataset.

Browse files
OrpheusTTS_Tokenize_NB_used_by_Thorsten_Voice.ipynb ADDED
@@ -0,0 +1,309 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "machine_shape": "hm",
8
+ "gpuType": "T4"
9
+ },
10
+ "kernelspec": {
11
+ "name": "python3",
12
+ "display_name": "Python 3"
13
+ },
14
+ "language_info": {
15
+ "name": "python"
16
+ },
17
+ "accelerator": "GPU"
18
+ },
19
+ "cells": [
20
+ {
21
+ "cell_type": "code",
22
+ "source": [
23
+ "my_original_dataset_name = \"Thorsten-Voice/TV-24kHz-2025.12-Neutral-FT-Mini\"\n",
24
+ "name_to_push_dataset_to = \"Thorsten-Voice/TV-24kHz-2025.12-Neutral-FT-Mini-tokenised\"\n",
25
+ "!huggingface-cli login --token=SECRET"
26
+ ],
27
+ "metadata": {
28
+ "id": "5uX_IoEpnnL2"
29
+ },
30
+ "execution_count": null,
31
+ "outputs": []
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "source": [
36
+ "!pip install torchcodec\n",
37
+ "!pip install datasets==3.5.1 # Using datasets >= 4.0 had some issues with this codebase"
38
+ ],
39
+ "metadata": {
40
+ "id": "4Y6bf-Kgxpz2"
41
+ },
42
+ "execution_count": null,
43
+ "outputs": []
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": null,
48
+ "metadata": {
49
+ "id": "Y1BlCraIs9bh"
50
+ },
51
+ "outputs": [],
52
+ "source": [
53
+ "#@title Installation & Setup\n",
54
+ "#%%capture\n",
55
+ "import locale\n",
56
+ "locale.getpreferredencoding = lambda: \"UTF-8\"\n",
57
+ "!pip install datasets==3.5.1\n",
58
+ "!pip install snac\n",
59
+ "import torch\n",
60
+ "import torchcodec\n",
61
+ "from snac import SNAC\n",
62
+ "from datasets import load_dataset\n",
63
+ "from huggingface_hub import snapshot_download\n",
64
+ "from datasets import load_dataset\n",
65
+ "\n",
66
+ "dsn = my_original_dataset_name\n",
67
+ "\n",
68
+ "snapshot_download(\n",
69
+ " repo_id=dsn,\n",
70
+ " repo_type=\"dataset\",\n",
71
+ " revision=\"main\",\n",
72
+ " max_workers=64,\n",
73
+ ")\n",
74
+ "\n",
75
+ "\n",
76
+ "ds = load_dataset(dsn, split=\"train\")\n",
77
+ "ds_sample_rate = ds[0][\"audio\"][\"sampling_rate\"]\n",
78
+ "\n",
79
+ "print(ds_sample_rate)\n",
80
+ "\n",
81
+ "model = SNAC.from_pretrained(\"hubertsiuzdak/snac_24khz\")\n",
82
+ "model = model.to(\"cuda\")"
83
+ ]
84
+ },
85
+ {
86
+ "cell_type": "code",
87
+ "source": [
88
+ "# Just for testing purpose, to check if sample rate is correct on Thorsten-Voice dataset\n",
89
+ "ds[0][\"audio\"][\"sampling_rate\"]"
90
+ ],
91
+ "metadata": {
92
+ "colab": {
93
+ "base_uri": "https://localhost:8080/"
94
+ },
95
+ "id": "l_ycyGL_7X70",
96
+ "outputId": "1002b1a6-be2e-42d2-cde9-19eac01c0beb"
97
+ },
98
+ "execution_count": null,
99
+ "outputs": [
100
+ {
101
+ "output_type": "execute_result",
102
+ "data": {
103
+ "text/plain": [
104
+ "24000"
105
+ ]
106
+ },
107
+ "metadata": {},
108
+ "execution_count": 5
109
+ }
110
+ ]
111
+ },
112
+ {
113
+ "cell_type": "code",
114
+ "source": [
115
+ "#@title Tokenisation Function\n",
116
+ "import torchaudio.transforms as T\n",
117
+ "def tokenise_audio(waveform):\n",
118
+ " waveform = torch.from_numpy(waveform).unsqueeze(0)\n",
119
+ " waveform = waveform.to(dtype=torch.float32)\n",
120
+ " resample_transform = T.Resample(orig_freq=ds_sample_rate, new_freq=24000)\n",
121
+ " waveform = resample_transform(waveform)\n",
122
+ "\n",
123
+ " waveform = waveform.unsqueeze(0).to(\"cuda\")\n",
124
+ "\n",
125
+ " #generate the codes from snac\n",
126
+ " with torch.inference_mode():\n",
127
+ " codes = model.encode(waveform)\n",
128
+ "\n",
129
+ " all_codes = []\n",
130
+ " for i in range(codes[0].shape[1]):\n",
131
+ " all_codes.append(codes[0][0][i].item()+128266)\n",
132
+ " all_codes.append(codes[1][0][2*i].item()+128266+4096)\n",
133
+ " all_codes.append(codes[2][0][4*i].item()+128266+(2*4096))\n",
134
+ " all_codes.append(codes[2][0][(4*i)+1].item()+128266+(3*4096))\n",
135
+ " all_codes.append(codes[1][0][(2*i)+1].item()+128266+(4*4096))\n",
136
+ " all_codes.append(codes[2][0][(4*i)+2].item()+128266+(5*4096))\n",
137
+ " all_codes.append(codes[2][0][(4*i)+3].item()+128266+(6*4096))\n",
138
+ "\n",
139
+ "\n",
140
+ " return all_codes\n",
141
+ "\n",
142
+ "\n"
143
+ ],
144
+ "metadata": {
145
+ "id": "kbZENwXltYSC"
146
+ },
147
+ "execution_count": null,
148
+ "outputs": []
149
+ },
150
+ {
151
+ "cell_type": "code",
152
+ "source": [
153
+ "#@title Map Tokenize\n",
154
+ "import random\n",
155
+ "def add_codes(example):\n",
156
+ " # Always initialize codes_list to None\n",
157
+ " codes_list = None\n",
158
+ "\n",
159
+ " print(example.get(\"audio\"))\n",
160
+ "\n",
161
+ "\n",
162
+ " try:\n",
163
+ " answer_audio = example.get(\"audio\")\n",
164
+ " # If there's a valid audio array, tokenise it\n",
165
+ " if answer_audio and \"array\" in answer_audio:\n",
166
+ " audio_array = answer_audio[\"array\"]\n",
167
+ " codes_list = tokenise_audio(audio_array)\n",
168
+ " except Exception as e:\n",
169
+ " print(f\"Skipping row due to error: {e}\")\n",
170
+ " # Keep codes_list as None if we fail\n",
171
+ " example[\"codes_list\"] = codes_list\n",
172
+ "\n",
173
+ " return example\n",
174
+ "\n",
175
+ "ds = ds.map(add_codes, remove_columns=[\"audio\"])\n"
176
+ ],
177
+ "metadata": {
178
+ "id": "Yv9OPDpRwWOy"
179
+ },
180
+ "execution_count": null,
181
+ "outputs": []
182
+ },
183
+ {
184
+ "cell_type": "code",
185
+ "source": [
186
+ "#@title Load Tokenizer\n",
187
+ "tokeniser_length = 128256\n",
188
+ "start_of_text = 128000\n",
189
+ "end_of_text = 128009\n",
190
+ "\n",
191
+ "start_of_speech = tokeniser_length + 1\n",
192
+ "end_of_speech = tokeniser_length + 2\n",
193
+ "\n",
194
+ "start_of_human = tokeniser_length + 3\n",
195
+ "end_of_human = tokeniser_length + 4\n",
196
+ "\n",
197
+ "start_of_ai = tokeniser_length + 5\n",
198
+ "end_of_ai = tokeniser_length + 6\n",
199
+ "pad_token = tokeniser_length + 7\n",
200
+ "\n",
201
+ "audio_tokens_start = tokeniser_length + 10\n",
202
+ "\n",
203
+ "tokenizer_name = \"canopylabs/orpheus-3b-0.1-pretrained\"\n",
204
+ "\n",
205
+ "from transformers import AutoTokenizer\n",
206
+ "import os\n",
207
+ "tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)\n",
208
+ "num_proc = os.cpu_count()\n",
209
+ "\n",
210
+ "ds = ds.filter(lambda x: x[\"codes_list\"] is not None)\n",
211
+ "ds = ds.filter(lambda x: len(x[\"codes_list\"]) > 0)"
212
+ ],
213
+ "metadata": {
214
+ "id": "2G9uppg0H3-X"
215
+ },
216
+ "execution_count": null,
217
+ "outputs": []
218
+ },
219
+ {
220
+ "cell_type": "code",
221
+ "source": [
222
+ "#@title Create Input Ids\n",
223
+ "def remove_duplicate_frames(example):\n",
224
+ " vals = example[\"codes_list\"]\n",
225
+ " if len(vals) % 7 != 0:\n",
226
+ " raise ValueError(\"Input list length must be divisible by 7\")\n",
227
+ "\n",
228
+ " result = vals[:7]\n",
229
+ "\n",
230
+ " removed_frames = 0\n",
231
+ "\n",
232
+ " for i in range(7, len(vals), 7):\n",
233
+ " current_first = vals[i]\n",
234
+ " previous_first = result[-7]\n",
235
+ "\n",
236
+ " if current_first != previous_first:\n",
237
+ " result.extend(vals[i:i+7])\n",
238
+ " else:\n",
239
+ " removed_frames += 1\n",
240
+ "\n",
241
+ " example[\"codes_list\"] = result\n",
242
+ "\n",
243
+ " return example\n",
244
+ "\n",
245
+ "ds = ds.map(remove_duplicate_frames, num_proc=num_proc)\n",
246
+ "\n",
247
+ "tok_info = '''*** HERE you can modify the text prompt\n",
248
+ "i.e. if you wanted a multispeaker model like canopylabs/orpheus-3b-0.1-ft, you can pass:\n",
249
+ "f\"{example[\"source\"]}: {example[\"text\"]}\", as is passed.\n",
250
+ "'''\n",
251
+ "print(tok_info)\n",
252
+ "\n",
253
+ "def create_input_ids(example):\n",
254
+ " text_ids = tokenizer.encode(example[\"text\"], add_special_tokens=True)\n",
255
+ " text_ids.append(end_of_text)\n",
256
+ " example[\"text_tokens\"] = text_ids\n",
257
+ " input_ids = (\n",
258
+ " [start_of_human]\n",
259
+ " + example[\"text_tokens\"]\n",
260
+ " + [end_of_human]\n",
261
+ " + [start_of_ai]\n",
262
+ " + [start_of_speech]\n",
263
+ " + example[\"codes_list\"]\n",
264
+ " + [end_of_speech]\n",
265
+ " + [end_of_ai]\n",
266
+ " )\n",
267
+ " example[\"input_ids\"] = input_ids\n",
268
+ " example[\"labels\"] = input_ids\n",
269
+ " example[\"attention_mask\"] = [1] * len(input_ids)\n",
270
+ "\n",
271
+ " return example\n",
272
+ "\n",
273
+ "ds = ds.map(create_input_ids, num_proc=num_proc, remove_columns=[\"text\", \"codes_list\"])\n"
274
+ ],
275
+ "metadata": {
276
+ "id": "hWGtOc5QIPcn"
277
+ },
278
+ "execution_count": null,
279
+ "outputs": []
280
+ },
281
+ {
282
+ "cell_type": "code",
283
+ "source": [
284
+ "#@title Remove unnecessary columns\n",
285
+ "columns_to_keep = [\"input_ids\", \"labels\", \"attention_mask\"]\n",
286
+ "columns_to_remove = [col for col in ds.column_names if col not in columns_to_keep]\n",
287
+ "\n",
288
+ "ds = ds.remove_columns(columns_to_remove)"
289
+ ],
290
+ "metadata": {
291
+ "id": "ee3zbdCUIWV6"
292
+ },
293
+ "execution_count": null,
294
+ "outputs": []
295
+ },
296
+ {
297
+ "cell_type": "code",
298
+ "source": [
299
+ "ame_to_push_dataset_to = \"Thorsten-Voice/TV-24kHz-2025.12-Neutral-FT-Mini-tokenised\"\n",
300
+ "ds.push_to_hub(name_to_push_dataset_to)"
301
+ ],
302
+ "metadata": {
303
+ "id": "Ov_2ItW6nldr"
304
+ },
305
+ "execution_count": null,
306
+ "outputs": []
307
+ }
308
+ ]
309
+ }