756 字
4 分钟
在WSL中快速使用IndexTTS推理 | Python | TTS
这是一篇IndexTTS的快速上手教程,以Windows平台的WSL子系统上,拉取源代码,下载模型,通过uv运行推理脚本。
正在计算文章时效性...
查看修订历史
相关链接
前置环境
推荐使用WSL配合NVIDIA显卡。
# 更新系统软件源sudo apt update && sudo apt upgrade -y# 安装基础依赖库sudo apt install python3 git git-lfs curl wget build-essential nvidia-cuda-toolkit ffmpeg -y# 启用lfsgit lfs install
# 安装Python管理器uv# curl方式curl -LsSf https://astral.sh/uv/install.sh | sh# pip方式pip install -U uv
# 刷新环境变量source $HOME/.local/bin/env# 拉取源代码git clone https://github.com/index-tts/index-tts.gitgit lfs pull # 下载大文件# 进入目录,创建必要的文件夹cd index-ttsmkdir -p checkpoints inputs outputs模型下载
现在需要去魔搭社区或者HuggingFace中下载模型权重和配置文件,也即 config.yaml 和.pth / .pt到目录checkpoints中:
# huggingface# 设置镜像地址环境变量export HF_ENDPOINT=https://hf-mirror.com# 使用uv安装huggingface-cli下载模型到checkpoints目录uv tool install "huggingface-hub[cli,hf_xet]"hf download IndexTeam/IndexTTS-2 --local-dir=checkpoints
# ModelScopeuv tool install "modelscope"modelscope download --model IndexTeam/IndexTTS-2 --local_dir checkpoints安装依赖
# 安装全部依赖(`--all-extras`)(按需配置国内镜像源)uv sync --all-extras --default-index "https://mirrors.aliyun.com/pypi/simple"推理脚本
# 创建轮推脚本nano batch_tts.py脚本内容如下:
import osfrom indextts.infer_v2 import IndexTTS2import torchimport gc
# ==========================================# 1. 配置区域 (Configuration Area)# ==========================================
# 输入的txt文本文件路径 (每行一句需要生成的文本)INPUT_TXT = "inputs/text.txt"
# 输出的文件夹路径OUTPUT_DIR = "outputs"
# 输出文件名称序列的前缀 (如 "audio_" 会生成 "audio_1.wav", "audio_2.wav"等)OUTPUT_PREFIX = "audio_"
# 参考音频路径 (Voice Cloning Prompt)REF_AUDIO = "inputs/furina.wav"
# ==========================================# 2. 核心逻辑 (Core Logic)# ==========================================
def main(): # 检查输入文本是否存在 if not os.path.exists(INPUT_TXT): print(f"错误: 找不到输入文件 '{INPUT_TXT}'。请先创建该文本文件。") return
# 创建输出文件夹 (如果不存在) if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) print(f"已创建输出文件夹: {OUTPUT_DIR}")
# 初始化 IndexTTS2 模型 print("正在加载 IndexTTS2 模型...") # 注意: 可根据需要将 use_fp16 设置为 True 以节省显存并加快推理 tts = IndexTTS2( cfg_path="checkpoints/config.yaml", model_dir="checkpoints", use_fp16=True, use_accel=True, use_cuda_kernel=False, use_deepspeed=False ) print("模型加载完成!")
# 读取 txt 文本 with open(INPUT_TXT, "r", encoding="utf-8") as f: lines = f.readlines()
# 逐行生成 TTS 音频 count = 1 for line in lines: text = line.strip()
# 跳过空行 if not text: continue
output_filename = f"{OUTPUT_PREFIX}{count}.wav" output_path = os.path.join(OUTPUT_DIR, output_filename)
# 断点续传检查:如果目标音频文件已存在,则跳过 if os.path.exists(output_path): print(f"[{count}/{len(lines)}] 音频已存在,跳过生成: {output_filename}") count += 1 continue
print(f"[{count}/{len(lines)}] 正在生成: {output_filename}") print(f"文本内容: {text}")
# 调用推理接口生成音频 tts.infer( spk_audio_prompt=REF_AUDIO, text=text, output_path=output_path, verbose=False )
print(f"生成成功 -> {output_path}\n")
# 显存优化:每一句生成完后清理显存碎片,防止跑久了OOM爆显存 gc.collect() if torch.cuda.is_available(): torch.cuda.empty_cache()
count += 1
print("所有任务处理完毕!")
if __name__ == "__main__": main()在配置区域中inputs/furina.wav为参考音频,inputs/text.txt为推理文本,脚本将逐行逐步推理,文本格式如下:
你好,这里是RoL1n~Hello,world!在确保text.txt和furina.wav参考音频都配置好之后,输入运行命令即可:
uv run batch_tts.py当然也可以运行官方默认的webui:
uv run webui.py --accel --torch_compile --fp16发现错误或想要改进这篇文章?
文章修订历史 (7 次)
查看变更记录
2026-07-18 ef79680
CMS:Update Posts “2026-07-18-在wsl中快速使用indextts推理-python-tts”
2026-07-18 1902c2c
CMS:Update Posts “2026-07-18-在wsl中快速使用indextts推理-python-tts”
2026-07-18 376bcd3
CMS:Update Posts “2026-07-18-在wsl中快速使用indextts推理-python-tts”
2026-07-18 8484daa
CMS:Update Posts “2026-07-18-在wsl中快速使用indextts推理-python-tts”
2026-07-18 065a5d0
CMS:Update Posts “2026-07-18-在wsl中快速使用indextts推理-python-tts”
2026-07-18 c5d656a
CMS:Update Posts “2026-07-18-在wsl中快速使用indextts推理-python-tts”
2026-07-18 8edf112
CMS:Create Posts “2026-07-18-在wsl中快速使用indextts推理-python-tts”
在WSL中快速使用IndexTTS推理 | Python | TTS
https://blog.srprolin.top/posts/2026-07-18-在wsl中快速使用indextts推理-python-tts/