Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import time
|
| 4 |
-
import
|
|
|
|
| 5 |
import warnings
|
| 6 |
|
| 7 |
from huggingface_hub import snapshot_download
|
| 8 |
-
import gradio as gr
|
| 9 |
|
| 10 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 11 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 12 |
|
|
|
|
| 13 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
sys.path.append(current_dir)
|
| 15 |
sys.path.append(os.path.join(current_dir, "indextts"))
|
|
@@ -17,48 +21,78 @@ sys.path.append(os.path.join(current_dir, "indextts"))
|
|
| 17 |
from indextts.infer import IndexTTS
|
| 18 |
from tools.i18n.i18n import I18nAuto
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
required_files = ["bigvgan_generator.pth", "bpe.model", "gpt.pth", "config.yaml"]
|
| 27 |
for f in required_files:
|
| 28 |
-
path = os.path.join(
|
| 29 |
if not os.path.exists(path):
|
| 30 |
-
|
| 31 |
-
sys.exit(1)
|
| 32 |
-
|
| 33 |
-
i18n = I18nAuto(language="zh_CN")
|
| 34 |
-
tts = IndexTTS(model_dir=MODEL_DIR, cfg_path=os.path.join(MODEL_DIR, "config.yaml"))
|
| 35 |
|
| 36 |
-
|
| 37 |
-
os.makedirs("prompts", exist_ok=True)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
#
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
kwargs = {
|
| 64 |
"do_sample": bool(do_sample),
|
|
@@ -71,66 +105,37 @@ def gen_single(prompt, text, infer_mode,
|
|
| 71 |
"max_mel_tokens": int(max_mel_tokens),
|
| 72 |
}
|
| 73 |
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
if infer_mode == "普通推理":
|
| 76 |
-
tts.
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
else:
|
| 81 |
-
tts.
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
-
print(
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
print(f">> wav file saved to: {output_path}")
|
| 91 |
return output_path
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
#
|
| 95 |
-
#
|
| 96 |
-
with gr.Blocks(title="IndexTTS Demo") as demo:
|
| 97 |
-
gr.HTML('''
|
| 98 |
-
<h2><center>IndexTTS: 工业级可控零样本文本转语音系统</h2>
|
| 99 |
-
<p align="center">(简化版,终端打印进度 + 高级参数可调)</p>
|
| 100 |
-
''')
|
| 101 |
-
|
| 102 |
-
with gr.Row():
|
| 103 |
-
prompt_audio = gr.Audio(label="参考音频", key="prompt_audio",
|
| 104 |
-
sources=["upload","microphone"], type="filepath")
|
| 105 |
-
input_text = gr.TextArea(label="文本", key="input_text", placeholder="请输入目标文本")
|
| 106 |
-
infer_mode = gr.Radio(choices=["普通推理", "批次推理"], label="推理模式", value="普通推理")
|
| 107 |
-
gen_button = gr.Button("生成语音")
|
| 108 |
-
output_audio = gr.Audio(label="生成结果", visible=True, key="output_audio")
|
| 109 |
-
|
| 110 |
-
with gr.Accordion("高级生成参数", open=False):
|
| 111 |
-
do_sample = gr.Checkbox(label="do_sample", value=True)
|
| 112 |
-
top_p = gr.Slider(label="top_p", minimum=0.0, maximum=1.0, value=0.9, step=0.01)
|
| 113 |
-
top_k = gr.Slider(label="top_k", minimum=0, maximum=100, value=50, step=1)
|
| 114 |
-
temperature = gr.Slider(label="temperature", minimum=0.1, maximum=2.0, value=1.0, step=0.1)
|
| 115 |
-
length_penalty = gr.Slider(label="length_penalty", minimum=-2.0, maximum=2.0, value=0.0, step=0.1)
|
| 116 |
-
num_beams = gr.Slider(label="num_beams", minimum=1, maximum=10, value=1, step=1)
|
| 117 |
-
repetition_penalty = gr.Slider(label="repetition_penalty", minimum=0.1, maximum=20.0, value=1.0, step=0.1)
|
| 118 |
-
max_mel_tokens = gr.Slider(label="max_mel_tokens", minimum=50, maximum=tts.cfg.gpt.max_mel_tokens,
|
| 119 |
-
value=600, step=10)
|
| 120 |
-
|
| 121 |
-
gen_button.click(
|
| 122 |
-
gen_single,
|
| 123 |
-
inputs=[prompt_audio, input_text, infer_mode,
|
| 124 |
-
120, 4, do_sample, top_p, top_k, temperature,
|
| 125 |
-
length_penalty, num_beams, repetition_penalty, max_mel_tokens],
|
| 126 |
-
outputs=[output_audio]
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
# =======================
|
| 130 |
-
# 启动
|
| 131 |
-
# =======================
|
| 132 |
-
def main():
|
| 133 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 134 |
-
|
| 135 |
if __name__ == "__main__":
|
| 136 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
import time
|
| 7 |
+
import json
|
| 8 |
+
import argparse
|
| 9 |
import warnings
|
| 10 |
|
| 11 |
from huggingface_hub import snapshot_download
|
|
|
|
| 12 |
|
| 13 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 14 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 15 |
|
| 16 |
+
# 添加本地模块路径
|
| 17 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
sys.path.append(current_dir)
|
| 19 |
sys.path.append(os.path.join(current_dir, "indextts"))
|
|
|
|
| 21 |
from indextts.infer import IndexTTS
|
| 22 |
from tools.i18n.i18n import I18nAuto
|
| 23 |
|
| 24 |
+
# ---------------------------
|
| 25 |
+
# 参数解析
|
| 26 |
+
# ---------------------------
|
| 27 |
+
parser = argparse.ArgumentParser(description="IndexTTS CLI")
|
| 28 |
+
parser.add_argument("--model_dir", type=str, default="checkpoints", help="模型目录")
|
| 29 |
+
parser.add_argument("--output_dir", type=str, default="outputs", help="音频输出目录")
|
| 30 |
+
parser.add_argument("--verbose", action="store_true", help="打印详细信息")
|
| 31 |
+
cmd_args = parser.parse_args()
|
| 32 |
+
|
| 33 |
+
os.makedirs(cmd_args.model_dir, exist_ok=True)
|
| 34 |
+
os.makedirs(cmd_args.output_dir, exist_ok=True)
|
| 35 |
+
|
| 36 |
+
# ---------------------------
|
| 37 |
+
# 下载模型(如果不存在)
|
| 38 |
+
# ---------------------------
|
| 39 |
+
print(">> 检查模型...")
|
| 40 |
+
snapshot_download(
|
| 41 |
+
repo_id="IndexTeam/IndexTTS-1.5",
|
| 42 |
+
local_dir=cmd_args.model_dir,
|
| 43 |
+
local_dir_use_symlinks=False,
|
| 44 |
+
)
|
| 45 |
required_files = ["bigvgan_generator.pth", "bpe.model", "gpt.pth", "config.yaml"]
|
| 46 |
for f in required_files:
|
| 47 |
+
path = os.path.join(cmd_args.model_dir, f)
|
| 48 |
if not os.path.exists(path):
|
| 49 |
+
raise FileNotFoundError(f"缺少模型文件: {path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
print(">> 模型文件检查完成")
|
|
|
|
| 52 |
|
| 53 |
+
# ---------------------------
|
| 54 |
+
# 初始化 TTS
|
| 55 |
+
# ---------------------------
|
| 56 |
+
i18n = I18nAuto(language="zh_CN")
|
| 57 |
+
tts = IndexTTS(model_dir=cmd_args.model_dir, cfg_path=os.path.join(cmd_args.model_dir, "config.yaml"))
|
| 58 |
+
|
| 59 |
+
# ---------------------------
|
| 60 |
+
# 语音生成函数
|
| 61 |
+
# ---------------------------
|
| 62 |
+
def generate_audio(
|
| 63 |
+
text,
|
| 64 |
+
output_path=None,
|
| 65 |
+
prompt_audio=None,
|
| 66 |
+
infer_mode="普通推理",
|
| 67 |
+
max_text_tokens_per_sentence=120,
|
| 68 |
+
sentences_bucket_max_size=4,
|
| 69 |
+
do_sample=True,
|
| 70 |
+
top_p=0.8,
|
| 71 |
+
top_k=30,
|
| 72 |
+
temperature=1.0,
|
| 73 |
+
length_penalty=0.0,
|
| 74 |
+
num_beams=3,
|
| 75 |
+
repetition_penalty=10.0,
|
| 76 |
+
max_mel_tokens=None,
|
| 77 |
+
verbose=True
|
| 78 |
+
):
|
| 79 |
+
"""
|
| 80 |
+
Args:
|
| 81 |
+
text: 要生成的文本
|
| 82 |
+
output_path: 输出 wav 文件路径
|
| 83 |
+
prompt_audio: 参考音频路径
|
| 84 |
+
infer_mode: "普通推理" 或 "批次推理"
|
| 85 |
+
max_text_tokens_per_sentence: 分句最大 token 数
|
| 86 |
+
sentences_bucket_max_size: 分句分桶最大容量(批次模式有效)
|
| 87 |
+
do_sample, top_p, top_k, temperature, length_penalty, num_beams, repetition_penalty, max_mel_tokens:
|
| 88 |
+
TTS 模型生成参数
|
| 89 |
+
verbose: 是否打印进度
|
| 90 |
+
"""
|
| 91 |
+
if output_path is None:
|
| 92 |
+
output_path = os.path.join(cmd_args.output_dir, f"spk_{int(time.time())}.wav")
|
| 93 |
+
|
| 94 |
+
if max_mel_tokens is None:
|
| 95 |
+
max_mel_tokens = tts.cfg.gpt.max_mel_tokens
|
| 96 |
|
| 97 |
kwargs = {
|
| 98 |
"do_sample": bool(do_sample),
|
|
|
|
| 105 |
"max_mel_tokens": int(max_mel_tokens),
|
| 106 |
}
|
| 107 |
|
| 108 |
+
print(">> start inference...")
|
| 109 |
+
start_time = time.time()
|
| 110 |
try:
|
| 111 |
if infer_mode == "普通推理":
|
| 112 |
+
tts.gr_progress = None
|
| 113 |
+
tts.infer(
|
| 114 |
+
prompt_audio, text, output_path,
|
| 115 |
+
verbose=verbose,
|
| 116 |
+
max_text_tokens_per_sentence=int(max_text_tokens_per_sentence),
|
| 117 |
+
**kwargs
|
| 118 |
+
)
|
| 119 |
else:
|
| 120 |
+
tts.gr_progress = None
|
| 121 |
+
tts.infer_fast(
|
| 122 |
+
prompt_audio, text, output_path,
|
| 123 |
+
verbose=verbose,
|
| 124 |
+
max_text_tokens_per_sentence=int(max_text_tokens_per_sentence),
|
| 125 |
+
sentences_bucket_max_size=int(sentences_bucket_max_size),
|
| 126 |
+
**kwargs
|
| 127 |
+
)
|
| 128 |
+
print(f">> wav file saved to: {output_path}")
|
| 129 |
+
print(f">> total time: {time.time() - start_time:.2f}s")
|
| 130 |
except Exception as e:
|
| 131 |
+
print(">> 生成失败:", e)
|
| 132 |
+
raise e
|
|
|
|
|
|
|
| 133 |
return output_path
|
| 134 |
|
| 135 |
+
# ---------------------------
|
| 136 |
+
# CLI 测试
|
| 137 |
+
# ---------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
if __name__ == "__main__":
|
| 139 |
+
sample_text = "你好,欢迎使用 IndexTTS。"
|
| 140 |
+
output_file = generate_audio(sample_text)
|
| 141 |
+
print(f">> 生成完成: {output_file}")
|