Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,17 +1,15 @@
|
|
| 1 |
-
import json
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
-
import threading
|
| 5 |
import time
|
|
|
|
|
|
|
| 6 |
|
| 7 |
from huggingface_hub import snapshot_download
|
|
|
|
| 8 |
|
| 9 |
-
import warnings
|
| 10 |
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 11 |
warnings.filterwarnings("ignore", category=UserWarning)
|
| 12 |
|
| 13 |
-
import gradio as gr
|
| 14 |
-
|
| 15 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 16 |
sys.path.append(current_dir)
|
| 17 |
sys.path.append(os.path.join(current_dir, "indextts"))
|
|
@@ -20,35 +18,36 @@ from indextts.infer import IndexTTS
|
|
| 20 |
from tools.i18n.i18n import I18nAuto
|
| 21 |
|
| 22 |
# =======================
|
| 23 |
-
#
|
| 24 |
# =======================
|
| 25 |
MODEL_DIR = "checkpoints"
|
| 26 |
snapshot_download("IndexTeam/IndexTTS-1.5", local_dir=MODEL_DIR)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"bigvgan_generator.pth",
|
| 34 |
-
"bpe.model",
|
| 35 |
-
"gpt.pth",
|
| 36 |
-
"config.yaml",
|
| 37 |
-
]:
|
| 38 |
-
file_path = os.path.join(MODEL_DIR, file)
|
| 39 |
-
if not os.path.exists(file_path):
|
| 40 |
-
print(f"Required file {file_path} does not exist. Please download it.")
|
| 41 |
sys.exit(1)
|
| 42 |
|
| 43 |
-
# =======================
|
| 44 |
-
# 初始化模型
|
| 45 |
-
# =======================
|
| 46 |
i18n = I18nAuto(language="zh_CN")
|
| 47 |
tts = IndexTTS(model_dir=MODEL_DIR, cfg_path=os.path.join(MODEL_DIR, "config.yaml"))
|
| 48 |
|
| 49 |
os.makedirs("outputs/tasks", exist_ok=True)
|
| 50 |
os.makedirs("prompts", exist_ok=True)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# =======================
|
| 53 |
# 核心生成函数
|
| 54 |
# =======================
|
|
@@ -59,17 +58,6 @@ def gen_single(prompt, text, infer_mode,
|
|
| 59 |
max_mel_tokens=600):
|
| 60 |
output_path = os.path.join("outputs", f"spk_{int(time.time())}.wav")
|
| 61 |
print(">> start inference...")
|
| 62 |
-
|
| 63 |
-
# 将进度回调打印到终端
|
| 64 |
-
class ProgressPrinter:
|
| 65 |
-
def __init__(self):
|
| 66 |
-
self.last = time.time()
|
| 67 |
-
def __call__(self, progress_value):
|
| 68 |
-
now = time.time()
|
| 69 |
-
if now - self.last > 0.5: # 每0.5秒打印一次
|
| 70 |
-
print(f">> progress: {progress_value*100:.1f}%")
|
| 71 |
-
self.last = now
|
| 72 |
-
|
| 73 |
tts.gr_progress = ProgressPrinter()
|
| 74 |
|
| 75 |
kwargs = {
|
|
@@ -108,7 +96,7 @@ def gen_single(prompt, text, infer_mode,
|
|
| 108 |
with gr.Blocks(title="IndexTTS Demo") as demo:
|
| 109 |
gr.HTML('''
|
| 110 |
<h2><center>IndexTTS: 工业级可控零样本文本转语音系统</h2>
|
| 111 |
-
<p align="center">(简化版,
|
| 112 |
''')
|
| 113 |
|
| 114 |
with gr.Row():
|
|
@@ -119,9 +107,22 @@ with gr.Blocks(title="IndexTTS Demo") as demo:
|
|
| 119 |
gen_button = gr.Button("生成语音")
|
| 120 |
output_audio = gr.Audio(label="生成结果", visible=True, key="output_audio")
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
gen_button.click(
|
| 123 |
gen_single,
|
| 124 |
-
inputs=[prompt_audio, input_text, infer_mode
|
|
|
|
|
|
|
| 125 |
outputs=[output_audio]
|
| 126 |
)
|
| 127 |
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
| 3 |
import time
|
| 4 |
+
import threading
|
| 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"))
|
|
|
|
| 18 |
from tools.i18n.i18n import I18nAuto
|
| 19 |
|
| 20 |
# =======================
|
| 21 |
+
# 模型初始化
|
| 22 |
# =======================
|
| 23 |
MODEL_DIR = "checkpoints"
|
| 24 |
snapshot_download("IndexTeam/IndexTTS-1.5", local_dir=MODEL_DIR)
|
| 25 |
|
| 26 |
+
required_files = ["bigvgan_generator.pth", "bpe.model", "gpt.pth", "config.yaml"]
|
| 27 |
+
for f in required_files:
|
| 28 |
+
path = os.path.join(MODEL_DIR, f)
|
| 29 |
+
if not os.path.exists(path):
|
| 30 |
+
print(f"Required file {path} not found.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
os.makedirs("outputs/tasks", exist_ok=True)
|
| 37 |
os.makedirs("prompts", exist_ok=True)
|
| 38 |
|
| 39 |
+
# =======================
|
| 40 |
+
# 终端进度回调
|
| 41 |
+
# =======================
|
| 42 |
+
class ProgressPrinter:
|
| 43 |
+
def __init__(self):
|
| 44 |
+
self.last = time.time()
|
| 45 |
+
def __call__(self, progress_value):
|
| 46 |
+
now = time.time()
|
| 47 |
+
if now - self.last > 0.5:
|
| 48 |
+
print(f">> progress: {progress_value*100:.1f}%")
|
| 49 |
+
self.last = now
|
| 50 |
+
|
| 51 |
# =======================
|
| 52 |
# 核心生成函数
|
| 53 |
# =======================
|
|
|
|
| 58 |
max_mel_tokens=600):
|
| 59 |
output_path = os.path.join("outputs", f"spk_{int(time.time())}.wav")
|
| 60 |
print(">> start inference...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
tts.gr_progress = ProgressPrinter()
|
| 62 |
|
| 63 |
kwargs = {
|
|
|
|
| 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():
|
|
|
|
| 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 |
|