OpenMOSS/MOSS-TTS-v1.5
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

MOSS-TTS 系列


    

MOSS-TTS-v1.5

MOSS-TTS-v1.5 是 MOSS-TTS 1.0 的延续版本。它保留了 1.0 版本的主要功能,包括零样本声音克隆、长文本语音生成、 token 级时长控制、拼音/国际音标(IPA)发音控制、多语言合成以及代码切换。有关 1.0 版本的完整功能介绍、输入格式、解码超参数和评估表格,请参阅 MOSS-TTS 1.0 README。

与 MOSS-TTS 1.0 相比,v1.5 主要在以下方面进行了改进:

  • 更强的带语言标签多语言合成:当省略 language 字段时,v1.5 对部分语言的合成效果可能有所提升,对另一些语言则可能略有退步。但当明确指定语言后,v1.5 在几乎所有支持的语言上表现均优于 1.0。构建用户消息时可设置标签,例如 processor.build_user_message(text=text_fr, language="French")。
  • 更稳定的声音克隆:v1.5 提高了说话人相似度,降低了克隆差异,使多次生成的结果更加一致。
  • 更优的长参考、短文本克隆:对于参考音频远长于目标文本的场景,v1.5 处理起来比 1.0 更加可靠。
  • 更稳定的标点韵律跟随:v1.5 能更严格地遵循由标点符号引导的停顿,尤其是在长句中。
  • 显式停顿控制:v1.5 支持如 "[pause 3.2s]" 这样的内联停顿标记。例如,我今天学习了一首中国的古诗,它的名字是[pause 3.2s]静夜思! 会在 静夜思 之前插入一个 3.2 秒的显式停顿。

支持语言

MOSS-TTS-v1.5 当前支持31种语言。它保留了 MOSS-TTS 1.0 支持的20种语言,并通过多语言持续训练扩展了更多语言,包括粤语、荷兰语、芬兰语、印地语、马其顿语、马来语、罗马尼亚语、斯瓦希里语、他加禄语、泰语和越南语。

语言代码旗帜语言代码旗帜语言代码旗帜
Chinesezh🇨🇳Cantoneseyue🇭🇰Englishen🇺🇸
Arabicar🇸🇦Czechcs🇨🇿Danishda🇩🇰
Dutchnl🇳🇱Finnishfi🇫🇮Frenchfr🇫🇷
Germande🇩🇪Greekel🇬🇷Hebrewhe🇮🇱
Hindihi🇮🇳Hungarianhu🇭🇺Italianit🇮🇹
Japaneseja🇯🇵Koreanko🇰🇷Macedonianmk🇲🇰
Malayms🇲🇾Persian (Farsi)fa🇮🇷Polishpl🇵🇱
Portuguesept🇵🇹Romanianro🇷🇴Russianru🇷🇺
Spanishes🇪🇸Swahilisw🇹🇿Swedishsv🇸🇪
Tagalogtl🇵🇭Thaith🇹🇭Turkishtr🇹🇷
Vietnamesevi🇻🇳

快速开始

环境设置

为避免依赖冲突,我们建议使用一个干净、独立的 Python 环境,并安装Transformers 5.0.0。

conda create -n moss-tts python=3.12 -y
conda activate moss-tts

安装所有必需的依赖项:

git clone https://github.com/OpenMOSS/MOSS-TTS.git
cd MOSS-TTS
pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e .

(可选)安装 FlashAttention 2

若您的硬件支持,可安装 FlashAttention 2 以获得更快的速度和更低的 GPU 内存占用。

pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e ".[flash-attn]"

如果您的机器内存有限但 CPU 核心较多,可以限制构建并行度:

MAX_JOBS=4 pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e ".[flash-attn]"

注意事项:

  • 依赖项在 pyproject.toml 中管理,目前固定为 torch==2.9.1+cu128 和 torchaudio==2.9.1+cu128。
  • 如果 FlashAttention 2 在您的机器上构建失败,您可以跳过它并使用默认的注意力后端。
  • FlashAttention 2 仅在受支持的 GPU 上可用,通常与 torch.float16 或 torch.bfloat16 配合使用。

基本用法

提示:MOSS-TTS-v1.5 使用与 1.0 MossTTSDelay-8B checkpoint 相同的生成 API。对于多语言输入,当已知语言时,请设置 language。

MOSS-TTS 提供了便捷的 generate 接口以便快速使用。以下示例涵盖:

  1. 直接生成(中文/英文/带语言标签的多语言文本/拼音/IPA)
  2. 声音克隆
  3. 时长控制
  4. 使用 [pause X.Ys] 进行显式停顿控制
from pathlib import Path
import importlib.util
import torch
import torchaudio
from transformers import AutoModel, AutoProcessor
# Disable the broken cuDNN SDPA backend
torch.backends.cuda.enable_cudnn_sdp(False)
# Keep these enabled as fallbacks
torch.backends.cuda.enable_flash_sdp(True)
torch.backends.cuda.enable_mem_efficient_sdp(True)
torch.backends.cuda.enable_math_sdp(True)


pretrained_model_name_or_path = "OpenMOSS-Team/MOSS-TTS-v1.5"
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.bfloat16 if device == "cuda" else torch.float32

def resolve_attn_implementation() -> str:
    # Prefer FlashAttention 2 when package + device conditions are met.
    if (
        device == "cuda"
        and importlib.util.find_spec("flash_attn") is not None
        and dtype in {torch.float16, torch.bfloat16}
    ):
        major, _ = torch.cuda.get_device_capability()
        if major >= 8:
            return "flash_attention_2"

    # CUDA fallback: use PyTorch SDPA kernels.
    if device == "cuda":
        return "sdpa"

    # CPU fallback.
    return "eager"


attn_implementation = resolve_attn_implementation()
print(f"[INFO] Using attn_implementation={attn_implementation}")

processor = AutoProcessor.from_pretrained(
    pretrained_model_name_or_path,
    trust_remote_code=True,
)
processor.audio_tokenizer = processor.audio_tokenizer.to(device)

text_1 = "亲爱的你,\n你好呀。\n\n今天,我想用最认真、最温柔的声音,对你说一些重要的话。\n这些话,像一颗小小的星星,希望能在你的心里慢慢发光。\n\n首先,我想祝你——\n每天都能平平安安、快快乐乐。\n\n希望你早上醒来的时候,\n窗外有光,屋子里很安静,\n你的心是轻轻的,没有着急,也没有害怕。\n\n希望你吃饭的时候胃口很好,\n走路的时候脚步稳稳,\n晚上睡觉的时候,能做一个又一个甜甜的梦。\n\n我希望你能一直保持好奇心。\n对世界充满问题,\n对天空、星星、花草、书本和故事感兴趣。\n当你问“为什么”的时候,\n希望总有人愿意认真地听你说话。\n\n我也希望你学会温柔。\n温柔地对待朋友,\n温柔地对待小动物,\n也温柔地对待自己。\n\n如果有一天你犯了错,\n请不要太快责怪自己,\n因为每一个认真成长的人,\n都会在路上慢慢学会更好的方法。\n\n愿你拥有勇气。\n当你站在陌生的地方时,\n当你第一次举手发言时,\n当你遇到困难、感到害怕的时候,\n希望你能轻轻地告诉自己:\n“我可以试一试。”\n\n就算没有一次成功,也没有关系。\n失败不是坏事,\n它只是告诉你,你正在努力。\n\n我希望你学会分享快乐。\n把开心的事情告诉别人,\n把笑声送给身边的人,\n因为快乐被分享的时候,\n会变得更大、更亮。\n\n如果有一天你感到难过,\n我希望你知道——\n难过并不丢脸,\n哭泣也不是软弱。\n\n愿你能找到一个安全的地方,\n慢慢把心里的话说出来,\n然后再一次抬起头,看见希望。\n\n我还希望你能拥有梦想。\n这个梦想也许很大,\n也许很小,\n也许现在还说不清楚。\n\n没关系。\n梦想会和你一起长大,\n在时间里慢慢变得清楚。\n\n最后,我想送你一个最最重要的祝福:\n\n愿你被世界温柔对待,\n也愿你成为一个温柔的人。\n\n愿你的每一天,\n都值得被记住,\n都值得被珍惜。\n\n亲爱的你,\n请记住,\n你是独一无二的,\n你已经很棒了,\n而你的未来,\n一定会慢慢变得闪闪发光。\n\n祝你健康、勇敢、幸福,\n祝你永远带着笑容向前走。"
text_2 = "We stand on the threshold of the AI era.\nArtificial intelligence is no longer just a concept in laboratories, but is entering every industry, every creative endeavor, and every decision. It has learned to see, hear, speak, and think, and is beginning to become an extension of human capabilities. AI is not about replacing humans, but about amplifying human creativity, making knowledge more equitable, more efficient, and allowing imagination to reach further. A new era, jointly shaped by humans and intelligent systems, has arrived."
text_3 = "nin2 hao3,qing3 wen4 nin2 lai2 zi4 na3 zuo4 cheng2 shi4?"
text_4 = "nin2 hao3,qing4 wen3 nin2 lai2 zi4 na4 zuo3 cheng4 shi3?"
text_5 = "您好,请问您来自哪 zuo4 cheng2 shi4?"
text_6 = "/həloʊ, meɪ aɪ æsk wɪtʃ sɪti juː ɑːr frʌm?/"
text_7 = "Bonjour, je voudrais essayer une voix française naturelle et stable."
text_8 = "我今天学习了一首中国的古诗,它的名字是[pause 3.2s]静夜思!"

# Use audio from ./assets/audio to avoid downloading from the cloud.
ref_audio_1 = "https://speech-demo.oss-cn-shanghai.aliyuncs.com/moss_tts_demo/tts_readme_demo/reference_zh.wav"
ref_audio_2 = "https://speech-demo.oss-cn-shanghai.aliyuncs.com/moss_tts_demo/tts_readme_demo/reference_en.m4a"

conversations = [
    # Direct TTS (no reference). Language tags are recommended in v1.5.
    [processor.build_user_message(text=text_1)],
    [processor.build_user_message(text=text_2)],
    # Direct TTS (no reference). For languages ​​other than Chinese and English, it is recommended to use language tags.
    [processor.build_user_message(text=text_7, language="French")],
    # Pinyin or IPA input
    [processor.build_user_message(text=text_3)],
    [processor.build_user_message(text=text_4)],
    [processor.build_user_message(text=text_5)],
    [processor.build_user_message(text=text_6)],
    # Explicit pause control. Use [pause X.Ys], such as [pause 3.2s].
    [processor.build_user_message(text=text_8)],
    # Voice cloning (with reference)
    [processor.build_user_message(text=text_1, reference=[ref_audio_1])],
    [processor.build_user_message(text=text_2, reference=[ref_audio_2])],
    # Duration control
    [processor.build_user_message(text=text_2, tokens=325)],
    [processor.build_user_message(text=text_2, tokens=600)],
]

model = AutoModel.from_pretrained(
    pretrained_model_name_or_path,
    trust_remote_code=True,
    # If FlashAttention 2 is installed, you can set attn_implementation="flash_attention_2"
    attn_implementation=attn_implementation,
    torch_dtype=dtype,
).to(device)
model.eval()

batch_size = 1

save_dir = Path("inference_root")
save_dir.mkdir(exist_ok=True, parents=True)
sample_idx = 0
with torch.no_grad():
    for start in range(0, len(conversations), batch_size):
        batch_conversations = conversations[start : start + batch_size]
        batch = processor(batch_conversations, mode="generation")
        input_ids = batch["input_ids"].to(device)
        attention_mask = batch["attention_mask"].to(device)

        outputs = model.generate(
            input_ids=input_ids,
            attention_mask=attention_mask,
            max_new_tokens=4096,
        )

        for message in processor.decode(outputs):
            audio = message.audio_codes_list[0]
            out_path = save_dir / f"sample{sample_idx}.wav"
            sample_idx += 1
            torchaudio.save(out_path, audio.unsqueeze(0), processor.model_config.sampling_rate)

更多用法

MOSS-TTS-v1.5 与 MOSS-TTS 1.0 保持 API 兼容。关于音频前缀续接、详细的 UserMessage 和 AssistantMessage 字段说明、生成超参数、拼音/IPA 预处理示例以及评估结果,请参见 MOSS-TTS 1.0 README。