源文件:chapter7/orpheus/README.md Orpheus TTS —— 基于 Unsloth 的文本转语音微调 本项目演示如何使用 Unsloth 高效地微调 Orpheus 3B 文本转语音模型,并完成推理。 概览 Orpheus 是一个文本转语音(TTS)模型,能把文本转换为自然听感的语音。本实现使用: Unsloth 做高效的 LoRA 微调(显存占用减少 30%,batch size 可放大 2 倍) SNAC(随机神经音频编解码器)做 24kHz 的音频分词 Hugging Face Transformers 做模型训练与推理 功能特性 ✅ 以极低的显存占用微调 Orpheus 3B 模型 ✅ 支持单说话人与多说话人 TTS ✅
源文件:chapter7/orpheus/README.md
本项目演示如何使用 Unsloth 高效地微调 Orpheus 3B 文本转语音模型,并完成推理。
Orpheus 是一个文本转语音(TTS)模型,能把文本转换为自然听感的语音。本实现使用:
重要:为保证兼容性,datasets 包的版本须在 3.4.1 到 4.0.0 之间。
pip install sentencepiece protobuf "datasets>=3.4.1,<4.0.0" "huggingface_hub>=0.34.0" hf_transfer pip install -r requirements.txt
对于 Colab 或特定环境,你可能还需要:
pip install --no-deps bitsandbytes accelerate xformers peft trl triton cut_cross_entropy unsloth_zoo pip install --no-deps unsloth
orpheus/ ├── orpheus_sft_unsloth.py # Full training + inference script ├── inference.py # Standalone inference script ├── requirements.txt # Python dependencies ├── README.md # This file ├── lora_model/ # Saved LoRA adapters (after training) └── generated_audio/ # Generated audio outputs
在你的数据集上微调模型:
python orpheus_sft_unsloth.py
训练脚本会:
lora_model/训练参数:
用微调后的模型从文本生成语音:
python inference.py
或作为模块使用:
from inference import OrpheusInference # Initialize the model tts = OrpheusInference( model_path="unsloth/orpheus-3b-0.1-ft", lora_path="lora_model" # Optional: load fine-tuned adapters ) # Generate speech with emotion tags prompts = [ "Hey there my name is Elise, <giggles> and I'm a speech generation model.", "I missed you <laugh> so much! It's been way too long.", "This is absolutely amazing <gasp> I can't believe it worked!" ] audio_files = tts.generate( prompts=prompts, output_dir="generated_audio", temperature=0.6, top_p=0.95, max_new_tokens=1200 ) print(f"Generated {len(audio_files)} audio files")
Orpheus 支持特殊的情感/表达标签,用以生成更富表现力、更自然的语音:
支持的标签:
<laugh>、<giggles>、<chuckle><sigh>、<gasp><yawn>、<cough>、<sniffle>、<groan>用法:
prompts = [ "Hey there <giggles> welcome to my channel!", "I missed you <laugh> so much!", "That's so beautiful <sigh> it brings back memories.", "I'm so tired <yawn> after working all day.", "This is incredible <gasp> I can't believe my eyes!" ] audio_files = tts.generate(prompts=prompts)
工作原理:
<tag>对于多说话人模型,指定说话人名字:
tts.generate( prompts=["This is a test <laugh> with emotion."], voice="speaker_name" # Specify the speaker )
训练脚本要求数据集具有如下结构:
单说话人:
text:要朗读的文本audio:音频文件,含 array 与 sampling_rate 字段多说话人:
source:说话人标识text:要朗读的文本audio:音频文件,含 array 与 sampling_rate 字段示例数据集:MrDragonFox/Elise
生成的音频文件以 WAV 格式保存到 generated_audio/ 目录:
output_0.wav、output_1.wav 等典型显存需求:
1. 数据集版本错误:
Ensure datasets>=3.4.1,<4.0.0 is installed pip install "datasets>=3.4.1,<4.0.0" --force-reinstall
2. CUDA 显存不足:
max_new_tokensload_in_4bit=True3. 多 GPU 问题:
CUDA_VISIBLE_DEVICES=0 仅使用一块 GPUper_device_train_batch_size >1 可能报错FastLanguageModel.for_inference(model)load_in_4bit=True 开启 4-bit 量化temperature(0.4-0.8)与 top_p(0.9-0.95)参数max_new_tokens(约每 7 个 token 对应 1 个音频帧)本项目使用的模型与库遵循它们各自的许可证:
如果你在研究中使用了本代码,请引用:
@misc{orpheus-tts-unsloth, title={Orpheus TTS Fine-tuning with Unsloth}, author={Unsloth AI Team}, year={2024}, url={https://github.com/unslothai/unsloth} }
欢迎贡献!请随时提交 issue 或 pull request。