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 rD:\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( rD:\AI\short_video\tts\Qwen3-TTS-12Hz-0.6B-CustomVoice, # 1. 必须用0.6B 2. 必须用CustomVoice cache_dirTARGET_MODEL_DIR, # 关键参数指定下载目录 device_mapcuda, # dtypetorch.float16, # 报错就关掉 # attn_implementationflash_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沈逸琛遭养子沈寒轩陷害被送精神病院还被顶替作家身份。沈家逼他替婚石宕村姑娘殊不知对方是首富千金叶婉晨。二人联手在作家论坛戳穿沈寒轩真面目又在招标会曝光其罪行与私生子身份。最终恶人落网沈逸琛复仇成功与叶婉晨终成眷属。, languageChinese, speakerUncle_Fu, # 使用预设音色 instruct用解说文案的语气, # 纯TTS指令留空。或者直接省略此参数。 ) # 5. 将生成的音频保存到【指定输出文件夹】 TARGET_AUDIO_DIR rD:\AI\short_video\tts # 定义你的音频输出文件夹 os.makedirs(TARGET_AUDIO_DIR, exist_okTrue) # 确保文件夹存在 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})SpeakerVoice DescriptionNative languageVivianBright, slightly edgy young female voice.ChineseSerenaWarm, gentle young female voice.ChineseUncle_FuSeasoned male voice with a low, mellow timbre.ChineseDylanYouthful Beijing male voice with a clear, natural timbre.Chinese (Beijing Dialect)EricLively Chengdu male voice with a slightly husky brightness.Chinese (Sichuan Dialect)RyanDynamic male voice with strong rhythmic drive.EnglishAidenSunny American male voice with a clear midrange.EnglishOno_AnnaPlayful Japanese female voice with a light, nimble timbre.JapaneseSoheeWarm Korean female voice with rich emotion.Korean