Views
No views yet
language field is omitted, v1.5 may improve some languages and regress slightly on others compared with 1.0. When the language is specified, v1.5 is stronger than 1.0 on almost all supported languages. Set the tag when building the user message, for example processor.build_user_message(text=text_fr, language="French")."[pause 3.2s]". For example, 我今天学习了一首中国的古诗,它的名字是[pause 3.2s]静夜思! inserts an explicit 3.2s pause before 静夜思.| Language | Code | Flag | Language | Code | Flag | Language | Code | Flag |
|---|---|---|---|---|---|---|---|---|
| Chinese | zh | 🇨🇳 | Cantonese | yue | 🇭🇰 | English | en | 🇺🇸 |
| Arabic | ar | 🇸🇦 | Czech | cs | 🇨🇿 | Danish | da | 🇩🇰 |
| Dutch | nl | 🇳🇱 | Finnish | fi | 🇫🇮 | French | fr | 🇫🇷 |
| German | de | 🇩🇪 | Greek | el | 🇬🇷 | Hebrew | he | 🇮🇱 |
| Hindi | hi | 🇮🇳 | Hungarian | hu | 🇭🇺 | Italian | it | 🇮🇹 |
| Japanese | ja | 🇯🇵 | Korean | ko | 🇰🇷 | Macedonian | mk | 🇲🇰 |
| Malay | ms | 🇲🇾 | Persian (Farsi) | fa | 🇮🇷 | Polish | pl | 🇵🇱 |
| Portuguese | pt | 🇵🇹 | Romanian | ro | 🇷🇴 | Russian | ru | 🇷🇺 |
| Spanish | es | 🇪🇸 | Swahili | sw | 🇹🇿 | Swedish | sv | 🇸🇪 |
| Tagalog | tl | 🇵🇭 | Thai | th | 🇹🇭 | Turkish | tr | 🇹🇷 |
| Vietnamese | vi | 🇻🇳 |
1conda create -n moss-tts python=3.12 -y
2conda activate moss-tts1git clone https://github.com/OpenMOSS/MOSS-TTS.git
2cd MOSS-TTS
3pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e .pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e ".[flash-attn]"MAX_JOBS=4 pip install --extra-index-url https://download.pytorch.org/whl/cu128 -e ".[flash-attn]"pyproject.toml, which currently pins torch==2.9.1+cu128 and torchaudio==2.9.1+cu128.torch.float16 or torch.bfloat16.Tip: MOSS-TTS-v1.5 uses the same generation API as the 1.0 MossTTSDelay-8B checkpoint. For multilingual inputs, setlanguagewhenever the language is known.
generate interface for rapid usage. The examples below cover:[pause X.Ys]1from pathlib import Path
2import importlib.util
3import torch
4import torchaudio
5from transformers import AutoModel, AutoProcessor
6# Disable the broken cuDNN SDPA backend
7torch.backends.cuda.enable_cudnn_sdp(False)
8# Keep these enabled as fallbacks
9torch.backends.cuda.enable_flash_sdp(True)
10torch.backends.cuda.enable_mem_efficient_sdp(True)
11torch.backends.cuda.enable_math_sdp(True)
12
13
14pretrained_model_name_or_path = "OpenMOSS-Team/MOSS-TTS-v1.5"
15device = "cuda" if torch.cuda.is_available() else "cpu"
16dtype = torch.bfloat16 if device == "cuda" else torch.float32
17
18def resolve_attn_implementation() -> str:
19 # Prefer FlashAttention 2 when package + device conditions are met.
20 if (
21 device == "cuda"
22 and importlib.util.find_spec("flash_attn") is not None
23 and dtype in {torch.float16, torch.bfloat16}
24 ):
25 major, _ = torch.cuda.get_device_capability()
26 if major >= 8:
27 return "flash_attention_2"
28
29 # CUDA fallback: use PyTorch SDPA kernels.
30 if device == "cuda":
31 return "sdpa"
32
33 # CPU fallback.
34 return "eager"
35
36
37attn_implementation = resolve_attn_implementation()
38print(f"[INFO] Using attn_implementation={attn_implementation}")
39
40processor = AutoProcessor.from_pretrained(
41 pretrained_model_name_or_path,
42 trust_remote_code=True,
43)
44processor.audio_tokenizer = processor.audio_tokenizer.to(device)
45
46text_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祝你永远带着笑容向前走。"
47text_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."
48text_3 = "nin2 hao3,qing3 wen4 nin2 lai2 zi4 na3 zuo4 cheng2 shi4?"
49text_4 = "nin2 hao3,qing4 wen3 nin2 lai2 zi4 na4 zuo3 cheng4 shi3?"
50text_5 = "您好,请问您来自哪 zuo4 cheng2 shi4?"
51text_6 = "/həloʊ, meɪ aɪ æsk wɪtʃ sɪti juː ɑːr frʌm?/"
52text_7 = "Bonjour, je voudrais essayer une voix française naturelle et stable."
53text_8 = "我今天学习了一首中国的古诗,它的名字是[pause 3.2s]静夜思!"
54
55# Use audio from ./assets/audio to avoid downloading from the cloud.
56ref_audio_1 = "https://speech-demo.oss-cn-shanghai.aliyuncs.com/moss_tts_demo/tts_readme_demo/reference_zh.wav"
57ref_audio_2 = "https://speech-demo.oss-cn-shanghai.aliyuncs.com/moss_tts_demo/tts_readme_demo/reference_en.m4a"
58
59conversations = [
60 # Direct TTS (no reference). Language tags are recommended in v1.5.
61 [processor.build_user_message(text=text_1)],
62 [processor.build_user_message(text=text_2)],
63 # Direct TTS (no reference). For languages other than Chinese and English, it is recommended to use language tags.
64 [processor.build_user_message(text=text_7, language="French")],
65 # Pinyin or IPA input
66 [processor.build_user_message(text=text_3)],
67 [processor.build_user_message(text=text_4)],
68 [processor.build_user_message(text=text_5)],
69 [processor.build_user_message(text=text_6)],
70 # Explicit pause control. Use [pause X.Ys], such as [pause 3.2s].
71 [processor.build_user_message(text=text_8)],
72 # Voice cloning (with reference)
73 [processor.build_user_message(text=text_1, reference=[ref_audio_1])],
74 [processor.build_user_message(text=text_2, reference=[ref_audio_2])],
75 # Duration control
76 [processor.build_user_message(text=text_2, tokens=325)],
77 [processor.build_user_message(text=text_2, tokens=600)],
78]
79
80model = AutoModel.from_pretrained(
81 pretrained_model_name_or_path,
82 trust_remote_code=True,
83 # If FlashAttention 2 is installed, you can set attn_implementation="flash_attention_2"
84 attn_implementation=attn_implementation,
85 torch_dtype=dtype,
86).to(device)
87model.eval()
88
89batch_size = 1
90
91save_dir = Path("inference_root")
92save_dir.mkdir(exist_ok=True, parents=True)
93sample_idx = 0
94with torch.no_grad():
95 for start in range(0, len(conversations), batch_size):
96 batch_conversations = conversations[start : start + batch_size]
97 batch = processor(batch_conversations, mode="generation")
98 input_ids = batch["input_ids"].to(device)
99 attention_mask = batch["attention_mask"].to(device)
100
101 outputs = model.generate(
102 input_ids=input_ids,
103 attention_mask=attention_mask,
104 max_new_tokens=4096,
105 )
106
107 for message in processor.decode(outputs):
108 audio = message.audio_codes_list[0]
109 out_path = save_dir / f"sample{sample_idx}.wav"
110 sample_idx += 1
111 torchaudio.save(out_path, audio.unsqueeze(0), processor.model_config.sampling_rate)
112UserMessage and AssistantMessage fields, generation hyperparameters, Pinyin/IPA preprocessing examples, and evaluation results, see the MOSS-TTS 1.0 README.