hypaai commited on
Commit
61985dd
·
verified ·
1 Parent(s): 5c3ab93

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +634 -10
README.md CHANGED
@@ -1,22 +1,646 @@
1
  ---
2
- base_model: unsloth/orpheus-3b-0.1-ft-unsloth-bnb-4bit
3
  tags:
4
- - text-generation-inference
5
- - transformers
6
  - unsloth
7
- - llama
8
- - trl
9
  license: apache-2.0
 
 
 
 
 
10
  language:
 
 
 
11
  - en
 
 
 
12
  ---
13
 
14
- # Uploaded model
15
 
16
- - **Developed by:** hypaai
17
- - **License:** apache-2.0
18
- - **Finetuned from model :** unsloth/orpheus-3b-0.1-ft-unsloth-bnb-4bit
19
 
20
- This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
 
1
  ---
2
+ library_name: transformers
3
  tags:
 
 
4
  - unsloth
 
 
5
  license: apache-2.0
6
+ datasets:
7
+ - hypaai/Hypa_Fleurs
8
+ - MrDragonFox/Elise
9
+ - canopylabs/zac-sample-dataset
10
+ - google/fleurs
11
  language:
12
+ - ig
13
+ - yo
14
+ - ha
15
  - en
16
+ base_model:
17
+ - canopylabs/orpheus-3b-0.1-ft
18
+ pipeline_tag: text-to-speech
19
  ---
20
 
21
+ # Hypa_Orpheus-3b-0.1-ft (4-bit LoRA Fine-tuned)
22
 
23
+ A 16-bit quantized and merged, memory-efficient fine-tuned version of [`canopylabs/orpheus-3b-0.1-ft`](https://huggingface.co/canopylabs/orpheus-3b-0.1-ft), optimized with Unsloth and LoRA, for expressive multilingual TTS, especially in low-resource African languages. This model provides the following capabilities:
 
 
24
 
25
+ * Text-to-Speech generation
26
+ * Speech synthesis for under-represented accents
27
+ * Voice cloning & emotion synthesis
28
+ * Research on multilingual low-resource voice AI
29
+
30
+
31
+ ---
32
+
33
+ ## Model Details
34
+
35
+ ### Model Summary
36
+
37
+ This model was trained on a parallel text-speech dataset totaling over 300 hours (75k samples) of Nigerian-accented and low-resource language audio (Igbo, Yoruba, Hausa). A key part of the dataset comes from AfroVoices' transcription of real-world YouTube data (denoted as Random speaker, ~100+ hrs).
38
+ To preserve and enhance multilingual capabilities while avoiding catastrophic forgetting, we included synthetic speech-text data sampled from the original 8 Orpheus voices using the default emotional prompts.
39
+ The final training set also included new speakers like:
40
+ * Eniola (40 hrs) – Female, bold, clear
41
+ * Moyo (40 hrs) – Female, professional, articulate
42
+ * Lovelyn (35 hrs) – Female, warm, shy
43
+ * Precious (30 hrs) – Female, friendly, gentle
44
+
45
+ This model sets state-of-the-art performance on low-resource Multilingual TTS tasks across African languages (see training stats below)
46
+
47
+ ### Base Model Details
48
+
49
+ The default Orpheus-TTS model released by [`Canopy Labs`](https://canopylabs.ai/releases/orpheus_can_speak_any_language) supports the below voices and emotions:
50
+
51
+ Voices: `tara`, `leah`, `jess`, `leo`, `dan`, `mia`, `zac`, and `zoe`.
52
+
53
+ Emotions: `<laugh>`, `<chuckle>`, `<sigh>`, `<cough>`, `<sniffle>`, `<groan>`, `<yawn>`, and `<gasp>`.
54
+
55
+ Through synthetic data generation and addition, our finetuned model also maintains these voices and emotions. Please visit the default model's card for mor info on voices and emotions.
56
+
57
+ ### Our Model Sample Generations
58
+
59
+ 🎧 Listen to samples generated by Hypa Orpheus-TTS
60
+
61
+ <div style="margin-top: 20px;">
62
+ <table style="width: 100%; border-collapse: collapse;">
63
+ <thead>
64
+ <tr>
65
+ <th style="border: 1px solid #ddd; padding: 8px; width: 30%;">Text Input</th>
66
+ <th style="border: 1px solid #ddd; padding: 8px; width: 40%;">Audio Output</th>
67
+ <th style="border: 1px solid #ddd; padding: 8px; width: 10%;">Language</th>
68
+ <th style="border: 1px solid #ddd; padding: 8px; width: 10%;">Voice</th>
69
+ </tr>
70
+ </thead>
71
+ <tbody>
72
+ <tr>
73
+ <td style="border: 1px solid #ddd; padding: 8px;">I am cooking for guests tomorrow and need to know how to make aioli. Can you give me a step-by-step recipe.</td>
74
+ <td style="border: 1px solid #ddd; padding: 8px;">
75
+ <audio controls style="width: 100%;">
76
+ <source src="https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit/resolve/main/assets/Emmanuel_English.wav" type="audio/wav">
77
+ </audio>
78
+ </td>
79
+ <td style="border: 1px solid #ddd; padding: 8px;">English</td>
80
+ <td style="border: 1px solid #ddd; padding: 8px;">Emmanuel</td>
81
+ </tr>
82
+ <tr>
83
+ <td style="border: 1px solid #ddd; padding: 8px;">Ina dafa abinci don bakin gobe kuma ina bukatar sanin yadda ake yin ailoli. Za ka iya ba ni girke-gireken matakan daya bayan daya?</td>
84
+ <td style="border: 1px solid #ddd; padding: 8px;">
85
+ <audio controls style="width: 100%;">
86
+ <source src="https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit/resolve/main/assets/Emmanuel_Hausa.wav" type="audio/wav">
87
+ </audio>
88
+ </td>
89
+ <td style="border: 1px solid #ddd; padding: 8px;">Hausa</td>
90
+ <td style="border: 1px solid #ddd; padding: 8px;">Emmanuel</td>
91
+ </tr>
92
+ <tr>
93
+ <td style="border: 1px solid #ddd; padding: 8px;">Ina dafa abinci don bakin gobe kuma ina bukatar sanin yadda ake yin ailoli. Za ka iya ba ni girke-gireken matakan daya bayan daya?</td>
94
+ <td style="border: 1px solid #ddd; padding: 8px;">
95
+ <audio controls style="width: 100%;">
96
+ <source src="https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit/resolve/main/assets/Eniola_Hausa.wav" type="audio/wav">
97
+ </audio>
98
+ </td>
99
+ <td style="border: 1px solid #ddd; padding: 8px;">Hausa</td>
100
+ <td style="border: 1px solid #ddd; padding: 8px;">Eniola</td>
101
+ </tr>
102
+ <tr>
103
+ <td style="border: 1px solid #ddd; padding: 8px;">Èmi máa se oúnjẹ fún àwọn àlejò l'ọ́la mo sì nílò láti mọ bí wọn ti ńṣe aioli. Ṣe o lè fún mi ni àwọn ìlànà ìdáná ẹlẹ́sẹẹsẹ?</td>
104
+ <td style="border: 1px solid #ddd; padding: 8px;">
105
+ <audio controls style="width: 100%;">
106
+ <source src="https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit/resolve/main/assets/Eniola_Yoruba.wav" type="audio/wav">
107
+ </audio>
108
+ </td>
109
+ <td style="border: 1px solid #ddd; padding: 8px;">Yoruba</td>
110
+ <td style="border: 1px solid #ddd; padding: 8px;">Eniola</td>
111
+ </tr>
112
+ <tr>
113
+ <td style="border: 1px solid #ddd; padding: 8px;">I am cooking for guests tomorrow and need to know how to make aioli. Can you give me a step-by-step recipe.</td>
114
+ <td style="border: 1px solid #ddd; padding: 8px;">
115
+ <audio controls style="width: 100%;">
116
+ <source src="https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit/resolve/main/assets/Eniola_English.wav" type="audio/wav">
117
+ </audio>
118
+ </td>
119
+ <td style="border: 1px solid #ddd; padding: 8px;">English</td>
120
+ <td style="border: 1px solid #ddd; padding: 8px;">Eniola</td>
121
+ </tr>
122
+ <tr>
123
+ <td style="border: 1px solid #ddd; padding: 8px;">M na-esi nri maka ndị ọbịa echi ma achọ ịmata otú esi esi aioli. Ị nwere ike inye m usoro ntụziaka?</td>
124
+ <td style="border: 1px solid #ddd; padding: 8px;">
125
+ <audio controls style="width: 100%;">
126
+ <source src="https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit/resolve/main/assets/Eniola_Igbo.wav" type="audio/wav">
127
+ </audio>
128
+ </td>
129
+ <td style="border: 1px solid #ddd; padding: 8px;">Igbo</td>
130
+ <td style="border: 1px solid #ddd; padding: 8px;">Eniola</td>
131
+ </tr>
132
+ <tr>
133
+ <td style="border: 1px solid #ddd; padding: 8px;">M na-esi nri maka ndị ọbịa echi ma achọ ịmata otú esi esi aioli. Ị nwere ike inye m usoro ntụziaka?</td>
134
+ <td style="border: 1px solid #ddd; padding: 8px;">
135
+ <audio controls style="width: 100%;">
136
+ <source src="https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit/resolve/main/assets/Lovelyn_Igbo.wav" type="audio/wav">
137
+ </audio>
138
+ </td>
139
+ <td style="border: 1px solid #ddd; padding: 8px;">Igbo</td>
140
+ <td style="border: 1px solid #ddd; padding: 8px;">Lovelyn</td>
141
+ </tr>
142
+ <tr>
143
+ <td style="border: 1px solid #ddd; padding: 8px;">I am cooking for guests tomorrow and need to know how to make aioli. Can you give me a step-by-step recipe.</td>
144
+ <td style="border: 1px solid #ddd; padding: 8px;">
145
+ <audio controls style="width: 100%;">
146
+ <source src="https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit/resolve/main/assets/Lovelyn_English.wav" type="audio/wav">
147
+ </audio>
148
+ </td>
149
+ <td style="border: 1px solid #ddd; padding: 8px;">English</td>
150
+ <td style="border: 1px solid #ddd; padding: 8px;">Lovelyn</td>
151
+ </tr>
152
+ </tbody>
153
+ </table>
154
+ </div>
155
+
156
+ ---
157
+
158
+ ## Training Details
159
+
160
+ ### Training Summary
161
+
162
+ * Base model: canopylabs/orpheus-3b-0.1-ft
163
+ * Training engine: Unsloth + LoRA
164
+ * LoRA config: r=1024, alpha=1024, dropout=0.0, full attention + FFN adaptation
165
+ * Quantization: 4-bit (bnb) for training; final model is highly memory-efficient
166
+ * Total steps: 18,014 (1 epoch)
167
+ * Batch size: 1 × 4 (grad accum)
168
+ * GPU: A100 40GB (max 55% VRAM used)
169
+
170
+
171
+ <div style="margin-top: 20px;">
172
+ <table style="border-collapse: collapse;">
173
+ <thead>
174
+ <tr>
175
+ <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">Step</th>
176
+ <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">Training Loss</th>
177
+ <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">Validation Loss</th>
178
+ </tr>
179
+ </thead>
180
+ <tbody>
181
+ <tr>
182
+ <td style="border: 1px solid #ddd; padding: 8px;">5,000</td>
183
+ <td style="border: 1px solid #ddd; padding: 8px;">3.9496</td>
184
+ <td style="border: 1px solid #ddd; padding: 8px;">3.8790</td>
185
+ </tr>
186
+ <tr>
187
+ <td style="border: 1px solid #ddd; padding: 8px;">10,000</td>
188
+ <td style="border: 1px solid #ddd; padding: 8px;">3.8863</td>
189
+ <td style="border: 1px solid #ddd; padding: 8px;">3.79497</td>
190
+ </tr>
191
+ <tr>
192
+ <td style="border: 1px solid #ddd; padding: 8px;">15,000</td>
193
+ <td style="border: 1px solid #ddd; padding: 8px;">3.8544</td>
194
+ <td style="border: 1px solid #ddd; padding: 8px;">3.75323</td>
195
+ </tr>
196
+ </tbody>
197
+ </table>
198
+ </div>
199
+
200
+ ### Dataset Summary
201
+
202
+ * Sources:
203
+ * ✅ Manually aligned YouTube transcriptions (aka Random)
204
+ * ✅ Synthetic voice generation from Orpheus TTS
205
+ * ✅ Parallel text-audio pairs for African-English, Igbo, Yoruba, Hausa
206
+ * Total Hours: 300+ (multi-accent)
207
+ * Key Speakers: 45+ unique voices (see speaker distribution chart below)
208
+
209
+ ![image/png](https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit/resolve/main/assets/stats.png)
210
+
211
+ We plan to open-source the full dataset shortly similar to the [Hypa_Fleurs](https://huggingface.co/datasets/hypaai/Hypa_Fleurs) initiative.
212
+
213
+ ---
214
+
215
+ ## Licensing and Citation
216
+
217
+ This model is released under an [Open Source License](./LICENSE) (apache-2.0). Please refer to the LICENSE file for full details.
218
+
219
+ When using this model in your work, please cite both this model as well as the base [`canopylabs/orpheus-3b-0.1-ft`](https://huggingface.co/canopylabs/orpheus-3b-0.1-ft) model as follows:
220
+
221
+ ```bibtex
222
+ @misc{canopylabsorpheus,
223
+ title={Orpheus-3b-0.1-ft: A Multilingual Text-to-Speech Model},
224
+ author={Canopy Labs},
225
+ year={2025},
226
+ publisher={Hugging Face},
227
+ howpublished={\url{https://huggingface.co/canopylabs/orpheus-3b-0.1-ft}},
228
+ note={Fine-tuned version of Orpheus for expressive TTS}
229
+ }
230
+
231
+ @misc{hypaorpheus4bit,
232
+ title={Hypa_Orpheus-3b-0.1-ft (LoRA-4bit)},
233
+ author={Hypa AI},
234
+ year={2025},
235
+ note={Fine-tuned Orpheus TTS on African languages},
236
+ url={https://huggingface.co/hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-bnb-4bit}
237
+ }
238
+ ```
239
+
240
+ ---
241
+
242
+ ## Acknowledgements
243
+
244
+ - **Canopy Labs Team:** For creating the foundational model and opensourcing it.
245
+ - **AfroVoices Experts:** For their translation expertise and high-quality datasets.
246
+ - **Community Support:** We thank all supporters, contributors, and users.
247
+
248
+ ---
249
+
250
+ ## Contact and Contributions
251
+
252
+ For any questions, issues, or contributions, please open an issue in this repository or contact [hypa.ai.ng@gmail.com](mailto:hypa.ai.ng@gmail.com). Contributions are welcome!
253
+
254
+ ---
255
+
256
+ ## Closing Remarks
257
+
258
+ By making Hypa_Orpheus available, we hope to empower research and development in multilingual speech technologies for African languages.
259
+
260
+ Hypa AI remains steadfast in its mission to pioneer intelligent solutions that are not just technologically advanced but are also culturally aware, ensuring that the future of AI is as diverse and inclusive as the world it serves.
261
+
262
+ AfroVoices, a subsidiary of Hypa AI, is dedicated to amplifying African voices, languages, and cultures in the intelligence age. Focused on bridging the digital representation gap, AfroVoices curates datasets and resources for African languages, promoting inclusivity and cultural appreciation in AI technologies. Their mission goes beyond technological innovation, aiming to celebrate the richness of African linguistic diversity on a global stage.
263
+
264
+ ---
265
+
266
+ ## Usage
267
+
268
+ ### Unsloth Inference
269
+
270
+ Download the nedded packages.
271
+
272
+ ```python
273
+ %%capture
274
+ import os
275
+ if "COLAB_" not in "".join(os.environ.keys()):
276
+ !pip install unsloth
277
+ else:
278
+ # Do this only in Colab notebooks! Otherwise use pip install unsloth
279
+ !pip install --no-deps bitsandbytes accelerate xformers==0.0.29.post3 peft trl==0.15.2 triton cut_cross_entropy unsloth_zoo
280
+ !pip install sentencepiece protobuf datasets huggingface_hub hf_transfer
281
+ !pip install --no-deps unsloth
282
+ !pip install snac
283
+ ```
284
+
285
+ Download the models (both the SNAC encoder/decoder as well as our finetuned Hypa_Orpheus).
286
+
287
+ ```python
288
+ import torch
289
+ from snac import SNAC
290
+ from unsloth import FastLanguageModel
291
+
292
+ dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
293
+ load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
294
+
295
+ model, tokenizer = FastLanguageModel.from_pretrained(
296
+ model_name = "hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-merged_16bit",
297
+ max_seq_length= 2048, # Choose any for long context!
298
+ dtype = dtype,
299
+ load_in_4bit = load_in_4bit,
300
+ #token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
301
+ )
302
+
303
+ snac_model = SNAC.from_pretrained("hubertsiuzdak/snac_24khz")
304
+ snac_model = snac_model.to("cuda")
305
+ ```
306
+
307
+ Create your text prompt, select the voice, and pass through the Model.
308
+
309
+ ```python
310
+ prompts = [
311
+ """Mo nífẹ̀ẹ́sí láti ṣe Ph.D sùgbọ́n mi ò ì tíì pinnu ẹ̀ka tí màá ṣe. Àwọn anfaani tí óń dé oríṣiríṣi àwọn olùgbọ́ káàkiri àgbáyé wo ni mo ní""",
312
+ ]
313
+ chosen_voice = "Eniola" # None for single-speaker
314
+
315
+
316
+ FastLanguageModel.for_inference(model) # Enable native 2x faster inference
317
+ snac_model.to("cpu")# Moving snac_model cuda to cpu
318
+
319
+ prompts_ = [(f"{chosen_voice}: " + p) if chosen_voice else p for p in prompts]
320
+
321
+ all_input_ids = []
322
+
323
+ for prompt in prompts_:
324
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids
325
+ all_input_ids.append(input_ids)
326
+
327
+ start_token = torch.tensor([[ 128259]], dtype=torch.int64) # Start of human
328
+ end_tokens = torch.tensor([[128009, 128260]], dtype=torch.int64) # End of text, End of human
329
+
330
+ all_modified_input_ids = []
331
+ for input_ids in all_input_ids:
332
+ modified_input_ids = torch.cat([start_token, input_ids, end_tokens], dim=1) # SOH SOT Text EOT EOH
333
+ all_modified_input_ids.append(modified_input_ids)
334
+
335
+ all_padded_tensors = []
336
+ all_attention_masks = []
337
+ max_length = max([modified_input_ids.shape[1] for modified_input_ids in all_modified_input_ids])
338
+ for modified_input_ids in all_modified_input_ids:
339
+ padding = max_length - modified_input_ids.shape[1]
340
+ padded_tensor = torch.cat([torch.full((1, padding), 128263, dtype=torch.int64), modified_input_ids], dim=1)
341
+ attention_mask = torch.cat([torch.zeros((1, padding), dtype=torch.int64), torch.ones((1, modified_input_ids.shape[1]), dtype=torch.int64)], dim=1)
342
+ all_padded_tensors.append(padded_tensor)
343
+ all_attention_masks.append(attention_mask)
344
+
345
+ all_padded_tensors = torch.cat(all_padded_tensors, dim=0)
346
+ all_attention_masks = torch.cat(all_attention_masks, dim=0)
347
+
348
+ input_ids = all_padded_tensors.to("cuda")
349
+ attention_mask = all_attention_masks.to("cuda")
350
+ generated_ids = model.generate(
351
+ input_ids=input_ids,
352
+ attention_mask=attention_mask,
353
+ max_new_tokens=1200,
354
+ do_sample=True,
355
+ temperature=0.6,
356
+ top_p=0.95,
357
+ repetition_penalty=1.1,
358
+ num_return_sequences=1,
359
+ eos_token_id=128258,
360
+ use_cache = True
361
+ )
362
+ token_to_find = 128257
363
+ token_to_remove = 128258
364
+
365
+ token_indices = (generated_ids == token_to_find).nonzero(as_tuple=True)
366
+
367
+ if len(token_indices[1]) > 0:
368
+ last_occurrence_idx = token_indices[1][-1].item()
369
+ cropped_tensor = generated_ids[:, last_occurrence_idx+1:]
370
+ else:
371
+ cropped_tensor = generated_ids
372
+
373
+ mask = cropped_tensor != token_to_remove
374
+
375
+ processed_rows = []
376
+
377
+ for row in cropped_tensor:
378
+ masked_row = row[row != token_to_remove]
379
+ processed_rows.append(masked_row)
380
+
381
+ code_lists = []
382
+
383
+ for row in processed_rows:
384
+ row_length = row.size(0)
385
+ new_length = (row_length // 7) * 7
386
+ trimmed_row = row[:new_length]
387
+ trimmed_row = [t - 128266 for t in trimmed_row]
388
+ code_lists.append(trimmed_row)
389
+
390
+
391
+ def redistribute_codes(code_list):
392
+ layer_1 = []
393
+ layer_2 = []
394
+ layer_3 = []
395
+ for i in range((len(code_list)+1)//7):
396
+ layer_1.append(code_list[7*i])
397
+ layer_2.append(code_list[7*i+1]-4096)
398
+ layer_3.append(code_list[7*i+2]-(2*4096))
399
+ layer_3.append(code_list[7*i+3]-(3*4096))
400
+ layer_2.append(code_list[7*i+4]-(4*4096))
401
+ layer_3.append(code_list[7*i+5]-(5*4096))
402
+ layer_3.append(code_list[7*i+6]-(6*4096))
403
+ codes = [torch.tensor(layer_1).unsqueeze(0),
404
+ torch.tensor(layer_2).unsqueeze(0),
405
+ torch.tensor(layer_3).unsqueeze(0)]
406
+
407
+ # codes = [c.to("cuda") for c in codes]
408
+ audio_hat = snac_model.decode(codes)
409
+ return audio_hat
410
+
411
+ my_samples = []
412
+ for code_list in code_lists:
413
+ samples = redistribute_codes(code_list)
414
+ my_samples.append(samples)
415
+ from IPython.display import display, Audio
416
+ if len(prompts) != len(my_samples):
417
+ raise Exception("Number of prompts and samples do not match")
418
+ else:
419
+ for i in range(len(my_samples)):
420
+ print(prompts[i])
421
+ samples = my_samples[i]
422
+ display(Audio(samples.detach().squeeze().to("cpu").numpy(), rate=24000))
423
+ # Clean up to save RAM
424
+ del my_samples,samples
425
+ ```
426
+
427
+ ### Standard Inference
428
+
429
+ Download the nedded packages.
430
+
431
+ ```python
432
+ %%capture
433
+ !pip install snac ipywebrtc
434
+ ```
435
+
436
+ Download the Models (SNAC & Hypa_Orpheus)
437
+
438
+ ```python
439
+ import torch
440
+ from transformers import AutoModelForCausalLM, Trainer, TrainingArguments, AutoTokenizer
441
+ from snac import SNAC
442
+
443
+ # Loads the pre-trained SNAC model and moves it to the CPU.
444
+ snac_model = SNAC.from_pretrained("hubertsiuzdak/snac_24khz")
445
+ snac_model = snac_model #.to("cpu")
446
+
447
+ print("We have loaded the Encoder/Decoder model to the cpu, to use vram - use the gpu for faster inference")
448
+
449
+ # Loading the Orpheus Model and Tokenizer, moving the model to the GPU for faster inference
450
+ model_name = "hypaai/Hypa_Orpheus-3b-0.1-ft-unsloth-merged_16bit"
451
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
452
+ model.cuda()
453
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
454
+ ```
455
+
456
+ Create Prompt(s) and Select Voice & Emotions as needed.
457
+
458
+ ```python
459
+ # List of supported voices in Orpheus-TTS
460
+ voices = [
461
+ "Eniola", "tara", # Female, conversational, clear
462
+ "Moyo", "leah", # Female, warm, gentle
463
+ "Gift", "jess", # Female, energetic, youthful
464
+ "Prince", "leo", # Male, authoritative, deep
465
+ "Emmanuel", "dan", # Male, friendly, casual
466
+ "Cynthia", "mia", # Female, professional, articulate
467
+ "Kolade", "zac", # Male, enthusiastic, dynamic
468
+ "Lovelyn", "zoe" # Female, calm, soothing
469
+ ]
470
+
471
+ # List of supported emotion tags in Orpheus-TTS
472
+ emotions = [
473
+ "<laugh>", # Laughter
474
+ "<chuckle>", # Soft chuckle
475
+ "<sigh>", # Sighing
476
+ "<cough>", # Coughing
477
+ "<sniffle>", # Sniffling
478
+ "<groan>", # Groaning
479
+ "<yawn>", # Yawning
480
+ "<gasp>" # Gasping
481
+ ]
482
+
483
+ # Creating Prompts
484
+ prompts = [
485
+ "Hey there my name is Eniola 9000, and I'm a speech generation model that can sound like a person.",
486
+ # "I've also been taught to understand and produce paralinguistic things like sighing, or chuckling, or yawning!",
487
+ # "I live in San Francisco, and have, uhm let's see, 3 billion 7 hundred ... well, lets just say a lot of parameters.",
488
+ ]
489
+
490
+ chosen_voice = "Eniola" # "tara" # see github for other voices
491
+ prompts = [f"{chosen_voice}: " + p for p in prompts] # Creating the prompts (as a batch)
492
+ print(prompts)
493
+ ```
494
+
495
+ Tokenize prompt(s) into inputIDs, pad, and create attention masks.
496
+
497
+ ```python
498
+ # Tokenizing each prompt into input IDs.
499
+ all_input_ids = []
500
+ for prompt in prompts:
501
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids
502
+ all_input_ids.append(input_ids)
503
+
504
+ # Adds special tokens to mark the beginning and end of each prompt
505
+ start_token = torch.tensor([[128259]], dtype=torch.int64) # Start of human
506
+ end_tokens = torch.tensor([[128009, 128260]], dtype=torch.int64) # End of text, End of human
507
+
508
+ all_modified_input_ids = []
509
+ for input_ids in all_input_ids:
510
+ modified_input_ids = torch.cat([start_token, input_ids, end_tokens], dim=1) # SOH SOT Text EOT EOH
511
+ all_modified_input_ids.append(modified_input_ids)
512
+
513
+ # Padding All sequences to same length and creating corresponding attention masks
514
+ all_padded_tensors = []
515
+ all_attention_masks = []
516
+ max_length = max([modified_input_ids.shape[1] for modified_input_ids in all_modified_input_ids])
517
+ for modified_input_ids in all_modified_input_ids:
518
+ padding = max_length - modified_input_ids.shape[1]
519
+ # Left Padding
520
+ padded_tensor = torch.cat([torch.full((1, padding), 128263, dtype=torch.int64), modified_input_ids], dim=1)
521
+ attention_mask = torch.cat([torch.zeros((1, padding), dtype=torch.int64), torch.ones((1, modified_input_ids.shape[1]), dtype=torch.int64)], dim=1)
522
+ all_padded_tensors.append(padded_tensor)
523
+ all_attention_masks.append(attention_mask)
524
+
525
+ all_padded_tensors = torch.cat(all_padded_tensors, dim=0)
526
+ all_attention_masks = torch.cat(all_attention_masks, dim=0)
527
+
528
+ # Moving all padded sequences to GPU for Faster computation
529
+ input_ids = all_padded_tensors.to("cuda")
530
+ attention_mask = all_attention_masks.to("cuda")
531
+ ```
532
+
533
+ Generate Output Tokens from the and Parse output tokens as speech
534
+
535
+ ```python
536
+ print("*** Model.generate is slow - see vllm implementation on github for realtime streaming and inference")
537
+ print("*** Increase/decrease inference params for more expressive less stable generations")
538
+
539
+ # Generating Output Tokens
540
+ with torch.no_grad():
541
+ generated_ids = model.generate(
542
+ input_ids=input_ids,
543
+ attention_mask=attention_mask,
544
+ max_new_tokens=1200,
545
+ do_sample=True,
546
+ temperature=0.6,
547
+ top_p=0.95,
548
+ repetition_penalty=1.1,
549
+ num_return_sequences=1,
550
+ eos_token_id=128258,
551
+ )
552
+
553
+ # Processing Generated Tokens (Parse Output as speech)
554
+ token_to_find = 128257 # Start of Audio token (relevant output)
555
+ token_to_remove = 128258 # End/ Terminal Token (End of Audio/ relevant output)
556
+
557
+ token_indices = (generated_ids == token_to_find).nonzero(as_tuple=True)
558
+ print(token_indices)
559
+
560
+ # Slices the tensor to exclude unwanted tokens.
561
+ if len(token_indices[1]) > 0:
562
+ last_occurrence_idx = token_indices[1][-1].item()
563
+ cropped_tensor = generated_ids[:, last_occurrence_idx+1:]
564
+ else:
565
+ cropped_tensor = generated_ids
566
+
567
+ # mask = cropped_tensor != token_to_remove
568
+
569
+ # Storing the cleaned-up token sequences#
570
+ processed_rows = []
571
+ for row in cropped_tensor:
572
+ masked_row = row[row != token_to_remove]
573
+ processed_rows.append(masked_row)
574
+
575
+ # Preparing (Audio Codes) the token sequences for audio decoding by trimming and adjusting token values.
576
+ code_lists = []
577
+ for row in processed_rows:
578
+ row_length = row.size(0) # Determines the length of the token sequence.
579
+ new_length = (row_length // 7) * 7 # Ensures the sequence length is a multiple of 7, as required by the decoder.
580
+ trimmed_row = row[:new_length]
581
+ trimmed_row = [t - 128266 for t in trimmed_row] # Adjusts token values to match the expected input range for the decoder.
582
+ code_lists.append(trimmed_row)
583
+ ```
584
+
585
+ Decode Outputs with SNAC Decoder
586
+
587
+ ```python
588
+ # Processes the token sequences into the format expected by the SNAC decoder:
589
+ def redistribute_codes(code_list):
590
+ """ Reorganizes the flattened token list into three separate layers, adjusting each token's value to align with the decoder's expectations"""
591
+ layer_1 = [] # Coarsest layer
592
+ layer_2 = [] # Intermediate layer
593
+ layer_3 = [] # Finest layer
594
+
595
+ num_groups = (len(code_list) + 1) // 7 #Calculate the number of complete 7-token groups in the code_list
596
+ for i in range(num_groups):
597
+ idx = 7 * i # starting index for the current group
598
+ # Layer 1 receives the first token of the group
599
+ layer_1.append(code_list[idx])
600
+
601
+ # Layer 2 receives the second token, adjusted by subtracting 4096
602
+ layer_2.append(code_list[idx + 1] - 4096)
603
+
604
+ # Layer 3 receives the third and fourth tokens, adjusted by subtracting 8192 and 12288 respectively
605
+ layer_3.append(code_list[idx+2]-(2*4096))
606
+ layer_3.append(code_list[idx+3]-(3*4096))
607
+
608
+ # Layer 2 receives the fifth token, adjusted by subtracting 16384
609
+ layer_2.append(code_list[idx+4]-(4*4096))
610
+
611
+ # Layer 3 receives the sixth and seventh tokens, adjusted by subtracting 20480 and 24576 respectively
612
+ layer_3.append(code_list[idx+5]-(5*4096))
613
+ layer_3.append(code_list[idx+6]-(6*4096))
614
+
615
+ codes = [
616
+ torch.tensor(layer_1).unsqueeze(0), # Shape: (1, len(layer_1))
617
+ torch.tensor(layer_2).unsqueeze(0), # Shape: (1, len(layer_2))
618
+ torch.tensor(layer_3).unsqueeze(0) # Shape: (1, len(layer_3))
619
+ ] # Convert the lists to PyTorch tensors and add a batch dimension
620
+ audio_hat = snac_model.decode(codes) # Decode the structured codes into an audio waveform using the SNAC model
621
+ return audio_hat
622
+
623
+ my_samples = []
624
+ for code_list in code_lists:
625
+ samples = redistribute_codes(code_list) # Generates audio samples from the processed token sequences
626
+ my_samples.append(samples)
627
+
628
+ # Display Audio
629
+ from IPython.display import display, Audio
630
+
631
+ if len(prompts) != len(my_samples):
632
+ raise Exception("Number of prompts and samples do not match")
633
+ else:
634
+ for i in range(len(my_samples)):
635
+ print(prompts[i])
636
+ samples = my_samples[i]
637
+ display(Audio(samples.detach().squeeze().to("cpu").numpy(), rate=24000))
638
+ ```
639
+
640
+ - **Repository:** [N/A]
641
+ - **Paper:** [N/A]
642
+ - **Demo:** [N/A]
643
+
644
+ This llama based model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
645
 
646
  [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)