QwenTTS 预设音色

复制代码
import torch
import soundfile as sf
from qwen_tts import Qwen3TTSModel
import os

# ============ 1. 设置代理 (如果需要) ============
os.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'
os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:7890'

# ============ 2. 定义你要保存模型的【指定文件夹】路径 ============
# 使用绝对路径,例如在D盘创建一个`my_tts_models`文件夹
TARGET_MODEL_DIR = r"D:\AI\short_video\tts\Qwen3-TTS-0.6B-CustomVoice"
# 下载目录和导入目录都是要一个啊
# 我是用的github下载的 目录结构不变 git clone https://huggingface.co/Qwen/Qwen3-TTS-12Hz-0.6B-CustomVoic.git
# ============ 3. 加载模型 (关键修正) ============
model = Qwen3TTSModel.from_pretrained(
    r"D:\AI\short_video\tts\Qwen3-TTS-12Hz-0.6B-CustomVoice",  # 1. 必须用0.6B 2. 必须用CustomVoice
    cache_dir=TARGET_MODEL_DIR,               # 关键参数:指定下载目录
    device_map="cuda",
    # dtype=torch.float16, # 报错就关掉
    # attn_implementation="flash_attention_2", # 如果没装flash-attn,先注释掉
)
# 强制清理GPU缓存并验证
import torch
torch.cuda.empty_cache()
print(f"当前显存占用: {torch.cuda.memory_allocated(0)/1024**3:.2f} GB")

# 检查输入数据
print("模型加载完成,准备生成...")
# ============ 4. 生成语音 (使用预设音色的纯TTS) ============
wavs, sr = model.generate_custom_voice(
    text="沈逸琛遭养子沈寒轩陷害被送精神病院,还被顶替作家身份。沈家逼他替婚石宕村姑娘,殊不知对方是首富千金叶婉晨。二人联手,在作家论坛戳穿沈寒轩真面目,又在招标会曝光其罪行与私生子身份。最终恶人落网,沈逸琛复仇成功,与叶婉晨终成眷属。",
    language="Chinese",
    speaker="Uncle_Fu",  # 使用预设音色
    instruct="用解说文案的语气",       # 纯TTS,指令留空。或者直接省略此参数。
)

# ============ 5. 将生成的音频保存到【指定输出文件夹】 ============
TARGET_AUDIO_DIR = r"D:\AI\short_video\tts"        # 定义你的音频输出文件夹
os.makedirs(TARGET_AUDIO_DIR, exist_ok=True)  # 确保文件夹存在
output_path = os.path.join(TARGET_AUDIO_DIR, "output_custom_voice.wav")

sf.write(output_path, wavs[0], sr)
print(f"✅ 语音生成成功!模型已下载/加载至:{TARGET_MODEL_DIR}")
print(f"✅ 音频文件已保存至:{output_path}")
Speaker Voice Description Native language
Vivian Bright, slightly edgy young female voice. Chinese
Serena Warm, gentle young female voice. Chinese
Uncle_Fu Seasoned male voice with a low, mellow timbre. Chinese
Dylan Youthful Beijing male voice with a clear, natural timbre. Chinese (Beijing Dialect)
Eric Lively Chengdu male voice with a slightly husky brightness. Chinese (Sichuan Dialect)
Ryan Dynamic male voice with strong rhythmic drive. English
Aiden Sunny American male voice with a clear midrange. English
Ono_Anna Playful Japanese female voice with a light, nimble timbre. Japanese
Sohee Warm Korean female voice with rich emotion. Korean

pip install accelerate>=0.20.0

pip install qwen-tts --no-deps

--no-deps 参数是关键!它会跳过自动安装依赖包

相关推荐
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿1 天前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780512 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng82 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi2 天前
Chapter 2 - Python中的变量和简单的数据类型
python