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 参数是关键!它会跳过自动安装依赖包

相关推荐
花酒锄作田10 小时前
Pydantic校验配置文件
python
hboot10 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi21 小时前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉
韩师傅2 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L2 天前
LangGraph的MessageState and HumanMessage
python