Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,344 +1,365 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
|
|
|
| 3 |
import os
|
| 4 |
-
import sys
|
| 5 |
-
import yaml
|
| 6 |
-
from pathlib import Path
|
| 7 |
import time
|
| 8 |
-
import
|
| 9 |
-
import tempfile
|
| 10 |
-
import shutil
|
| 11 |
-
import gc # ガベージコレクション用
|
| 12 |
-
|
| 13 |
-
# --- 定数 ---
|
| 14 |
-
# Dockerfile内でクローンされるパスに合わせる
|
| 15 |
-
SCRIPT_DIR = Path(__file__).parent
|
| 16 |
-
SBV2_REPO_PATH = SCRIPT_DIR / "Style-Bert-VITS2"
|
| 17 |
-
# ダウンロード用ファイルの一時置き場 (コンテナ内に作成)
|
| 18 |
-
OUTPUT_DIR = SCRIPT_DIR / "outputs"
|
| 19 |
-
|
| 20 |
-
# --- ヘルパー関数 ---
|
| 21 |
-
def add_sbv2_to_path():
|
| 22 |
-
"""Style-Bert-VITS2リポジトリのパスを sys.path に追加"""
|
| 23 |
-
repo_path_str = str(SBV2_REPO_PATH.resolve())
|
| 24 |
-
if SBV2_REPO_PATH.exists() and repo_path_str not in sys.path:
|
| 25 |
-
sys.path.insert(0, repo_path_str)
|
| 26 |
-
print(f"Added {repo_path_str} to sys.path")
|
| 27 |
-
elif not SBV2_REPO_PATH.exists():
|
| 28 |
-
print(f"Warning: Style-Bert-VITS2 repository not found at {SBV2_REPO_PATH}")
|
| 29 |
-
|
| 30 |
-
def stream_process_output(process, log_list):
|
| 31 |
-
"""サブプロセスの標準出力/エラーをリアルタイムでリストに追加"""
|
| 32 |
-
try:
|
| 33 |
-
if process.stdout:
|
| 34 |
-
for line in iter(process.stdout.readline, ''):
|
| 35 |
-
log_list.append(line.strip()) # 余分な改行を削除
|
| 36 |
-
if process.stderr:
|
| 37 |
-
for line in iter(process.stderr.readline, ''):
|
| 38 |
-
processed_line = f"stderr: {line.strip()}"
|
| 39 |
-
# 警告はそのまま、他はエラーとして強調 (任意)
|
| 40 |
-
if "warning" not in line.lower():
|
| 41 |
-
processed_line = f"ERROR (stderr): {line.strip()}"
|
| 42 |
-
log_list.append(processed_line)
|
| 43 |
-
except Exception as e:
|
| 44 |
-
log_list.append(f"Error reading process stream: {e}")
|
| 45 |
-
|
| 46 |
-
# --- Gradio アプリのバックエンド関数 ---
|
| 47 |
-
def convert_safetensors_to_onnx_gradio(
|
| 48 |
-
safetensors_file_obj,
|
| 49 |
-
config_file_obj,
|
| 50 |
-
style_vectors_file_obj
|
| 51 |
-
): # gr.Progress は削除
|
| 52 |
-
"""
|
| 53 |
-
アップロードされたSafetensors, config.json, style_vectors.npy を使って
|
| 54 |
-
ONNXに変換し、結果をダウンロード可能にする。
|
| 55 |
-
"""
|
| 56 |
-
log = ["Starting ONNX conversion..."]
|
| 57 |
-
# 初期状態ではダウンロードファイルは空
|
| 58 |
-
yield "\n".join(log), None
|
| 59 |
-
|
| 60 |
-
# --- ファイルアップロードの検証 ---
|
| 61 |
-
if safetensors_file_obj is None:
|
| 62 |
-
log.append("❌ Error: Safetensors file is missing. Please upload the .safetensors file.")
|
| 63 |
-
yield "\n".join(log), None
|
| 64 |
-
return
|
| 65 |
-
if config_file_obj is None:
|
| 66 |
-
log.append("❌ Error: config.json file is missing. Please upload the config.json file.")
|
| 67 |
-
yield "\n".join(log), None
|
| 68 |
-
return
|
| 69 |
-
if style_vectors_file_obj is None:
|
| 70 |
-
log.append("❌ Error: style_vectors.npy file is missing. Please upload the style_vectors.npy file.")
|
| 71 |
-
yield "\n".join(log), None
|
| 72 |
-
return
|
| 73 |
-
|
| 74 |
-
# --- Style-Bert-VITS2 パスの確認 ---
|
| 75 |
-
add_sbv2_to_path()
|
| 76 |
-
if not SBV2_REPO_PATH.exists():
|
| 77 |
-
log.append(f"❌ Error: Style-Bert-VITS2 repository not found at {SBV2_REPO_PATH}. Check Space build logs.")
|
| 78 |
-
yield "\n".join(log), None
|
| 79 |
-
return
|
| 80 |
-
|
| 81 |
-
# --- 出力ディレクトリ作成 ---
|
| 82 |
-
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 83 |
-
|
| 84 |
-
onnx_output_path_str = None # 最終的なONNXファイルパス (文字列)
|
| 85 |
-
current_log = log[:] # ログリストをコピー
|
| 86 |
-
|
| 87 |
-
try:
|
| 88 |
-
# --- 一時ディレクトリを作成して処理 ---
|
| 89 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
| 90 |
-
temp_dir_path = Path(temp_dir)
|
| 91 |
-
current_log.append(f"📁 Created temporary directory: {temp_dir_path}")
|
| 92 |
-
yield "\n".join(current_log), None # UI更新
|
| 93 |
-
|
| 94 |
-
# --- SBV2が期待するディレクトリ構造を一時ディレクトリ内に作成 ---
|
| 95 |
-
# モデル名を .safetensors ファイル名から取得 (拡張子なし)
|
| 96 |
-
safetensors_filename = Path(safetensors_file_obj.name).name
|
| 97 |
-
if not safetensors_filename.lower().endswith(".safetensors"):
|
| 98 |
-
current_log.append(f"❌ Error: Invalid safetensors filename: {safetensors_filename}")
|
| 99 |
-
yield "\n".join(current_log), None
|
| 100 |
-
return
|
| 101 |
-
model_name = Path(safetensors_filename).stem # 拡張子を除いた部分
|
| 102 |
-
|
| 103 |
-
# assets_root を一時ディレクトリ自体にする
|
| 104 |
-
assets_root = temp_dir_path
|
| 105 |
-
# assets_root の下に model_name のディレクトリを作成
|
| 106 |
-
model_dir_in_temp = assets_root / model_name
|
| 107 |
-
model_dir_in_temp.mkdir(exist_ok=True)
|
| 108 |
-
current_log.append(f" - Created model directory: {model_dir_in_temp.relative_to(assets_root)}")
|
| 109 |
-
yield "\n".join(current_log), None
|
| 110 |
-
|
| 111 |
-
# --- 3つのファイルを model_dir_in_temp にコピー ---
|
| 112 |
-
files_to_copy = {
|
| 113 |
-
"safetensors": safetensors_file_obj,
|
| 114 |
-
"config.json": config_file_obj,
|
| 115 |
-
"style_vectors.npy": style_vectors_file_obj,
|
| 116 |
-
}
|
| 117 |
-
copied_paths = {}
|
| 118 |
-
|
| 119 |
-
for file_key, file_obj in files_to_copy.items():
|
| 120 |
-
original_filename = Path(file_obj.name).name
|
| 121 |
-
# ファイル名の基本的な検証 (サニタイズはより厳密に行うことも可能)
|
| 122 |
-
if "/" in original_filename or "\\" in original_filename or ".." in original_filename:
|
| 123 |
-
current_log.append(f"❌ Error: Invalid characters found in filename: {original_filename}")
|
| 124 |
-
yield "\n".join(current_log), None
|
| 125 |
-
return # tryブロックを抜ける
|
| 126 |
-
# 期待されるファイル名と一致しているか確認 (config と style_vectors)
|
| 127 |
-
if file_key == "config.json" and original_filename.lower() != "config.json":
|
| 128 |
-
current_log.append(f"⚠️ Warning: Uploaded JSON file name is '{original_filename}', expected 'config.json'. Using uploaded name.")
|
| 129 |
-
if file_key == "style_vectors.npy" and original_filename.lower() != "style_vectors.npy":
|
| 130 |
-
current_log.append(f"⚠️ Warning: Uploaded NPY file name is '{original_filename}', expected 'style_vectors.npy'. Using uploaded name.")
|
| 131 |
-
|
| 132 |
-
destination_path = model_dir_in_temp / original_filename
|
| 133 |
-
try:
|
| 134 |
-
shutil.copy(file_obj.name, destination_path)
|
| 135 |
-
current_log.append(f" - Copied '{original_filename}' to model directory.")
|
| 136 |
-
# .safetensorsファイルのパスを保存しておく
|
| 137 |
-
if file_key == "safetensors":
|
| 138 |
-
copied_paths["safetensors"] = destination_path
|
| 139 |
-
except Exception as e:
|
| 140 |
-
current_log.append(f"❌ Error copying file '{original_filename}': {e}")
|
| 141 |
-
yield "\n".join(current_log), None
|
| 142 |
-
return # tryブロックを抜ける
|
| 143 |
-
yield "\n".join(current_log), None # 各ファイルコピー後にUI更新
|
| 144 |
-
|
| 145 |
-
# safetensorsファイルがコピーされたか確認
|
| 146 |
-
temp_safetensors_path = copied_paths.get("safetensors")
|
| 147 |
-
if not temp_safetensors_path:
|
| 148 |
-
current_log.append("❌ Error: Failed to locate the copied safetensors file in the temporary directory.")
|
| 149 |
-
yield "\n".join(current_log), None
|
| 150 |
-
return
|
| 151 |
-
|
| 152 |
-
current_log.append(f"✅ All required files copied to temporary model directory.")
|
| 153 |
-
current_log.append(f" - Using temporary assets_root: {assets_root}")
|
| 154 |
-
yield "\n".join(current_log), None
|
| 155 |
-
|
| 156 |
-
# --- paths.yml を一時的に設定 ---
|
| 157 |
-
config_path = SBV2_REPO_PATH / "configs" / "paths.yml"
|
| 158 |
-
config_path.parent.mkdir(parents=True, exist_ok=True)
|
| 159 |
-
# dataset_root は今回は使わないが設定はしておく (assets_rootと同じ場所)
|
| 160 |
-
paths_config = {"dataset_root": str(assets_root.resolve()), "assets_root": str(assets_root.resolve())}
|
| 161 |
-
with open(config_path, "w", encoding="utf-8") as f:
|
| 162 |
-
yaml.dump(paths_config, f)
|
| 163 |
-
current_log.append(f" - Saved temporary paths config to {config_path}")
|
| 164 |
-
yield "\n".join(current_log), None
|
| 165 |
-
|
| 166 |
-
# --- ONNX変換スクリプト実行 ---
|
| 167 |
-
current_log.append(f"\n🚀 Starting ONNX conversion script for model '{model_name}'...")
|
| 168 |
-
convert_script = SBV2_REPO_PATH / "convert_onnx.py"
|
| 169 |
-
if not convert_script.exists():
|
| 170 |
-
current_log.append(f"❌ Error: convert_onnx.py not found at '{convert_script}'. Check repository setup.")
|
| 171 |
-
yield "\n".join(current_log), None
|
| 172 |
-
return # tryブロックを抜ける
|
| 173 |
-
|
| 174 |
-
python_executable = sys.executable
|
| 175 |
-
command = [
|
| 176 |
-
python_executable,
|
| 177 |
-
str(convert_script.resolve()),
|
| 178 |
-
"--model",
|
| 179 |
-
str(temp_safetensors_path.resolve()) # 一時ディレクトリ内の .safetensors パス
|
| 180 |
-
]
|
| 181 |
-
current_log.append(f"\n Running command: {' '.join(command)}")
|
| 182 |
-
yield "\n".join(current_log), None
|
| 183 |
-
|
| 184 |
-
process_env = os.environ.copy()
|
| 185 |
-
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
| 186 |
-
text=True, encoding='utf-8', errors='replace',
|
| 187 |
-
cwd=SBV2_REPO_PATH, # スクリプトの場所で実行
|
| 188 |
-
env=process_env)
|
| 189 |
-
|
| 190 |
-
# ログ出力用リスト (スレッドと共有)
|
| 191 |
-
process_output_lines = ["\n--- Conversion Script Output ---"]
|
| 192 |
-
thread = threading.Thread(target=stream_process_output, args=(process, process_output_lines))
|
| 193 |
-
thread.start()
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
|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|
| 296 |
)
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
| 300 |
)
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
| 304 |
)
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
interactive=False, # 出力専用
|
| 313 |
)
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
)
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
label="
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
max_lines=2000 # ログが増える可能性
|
| 326 |
)
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import datetime
|
| 3 |
+
import logging
|
| 4 |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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
import edge_tts
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import librosa
|
| 11 |
+
import torch
|
| 12 |
+
from fairseq import checkpoint_utils
|
| 13 |
+
|
| 14 |
+
from config import Config
|
| 15 |
+
from lib.infer_pack.models import (
|
| 16 |
+
SynthesizerTrnMs256NSFsid,
|
| 17 |
+
SynthesizerTrnMs256NSFsid_nono,
|
| 18 |
+
SynthesizerTrnMs768NSFsid,
|
| 19 |
+
SynthesizerTrnMs768NSFsid_nono,
|
| 20 |
+
)
|
| 21 |
+
from rmvpe import RMVPE
|
| 22 |
+
from vc_infer_pipeline import VC
|
| 23 |
+
|
| 24 |
+
logging.getLogger("fairseq").setLevel(logging.WARNING)
|
| 25 |
+
logging.getLogger("numba").setLevel(logging.WARNING)
|
| 26 |
+
logging.getLogger("markdown_it").setLevel(logging.WARNING)
|
| 27 |
+
logging.getLogger("urllib3").setLevel(logging.WARNING)
|
| 28 |
+
logging.getLogger("matplotlib").setLevel(logging.WARNING)
|
| 29 |
+
|
| 30 |
+
limitation = os.getenv("SYSTEM") == "spaces"
|
| 31 |
+
|
| 32 |
+
config = Config()
|
| 33 |
+
|
| 34 |
+
edge_output_filename = "edge_output.mp3"
|
| 35 |
+
tts_voice_list = asyncio.get_event_loop().run_until_complete(edge_tts.list_voices())
|
| 36 |
+
tts_voices = [f"{v['ShortName']}-{v['Gender']}" for v in tts_voice_list]
|
| 37 |
+
|
| 38 |
+
model_root = "weights"
|
| 39 |
+
models = [
|
| 40 |
+
d for d in os.listdir(model_root) if os.path.isdir(os.path.join(model_root, d))
|
| 41 |
+
]
|
| 42 |
+
if len(models) == 0:
|
| 43 |
+
raise ValueError("No model found in `weights` folder")
|
| 44 |
+
models.sort()
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def model_data(model_name):
|
| 48 |
+
# global n_spk, tgt_sr, net_g, vc, cpt, version, index_file
|
| 49 |
+
pth_files = [
|
| 50 |
+
os.path.join(model_root, model_name, f)
|
| 51 |
+
for f in os.listdir(os.path.join(model_root, model_name))
|
| 52 |
+
if f.endswith(".pth")
|
| 53 |
+
]
|
| 54 |
+
if len(pth_files) == 0:
|
| 55 |
+
raise ValueError(f"No pth file found in {model_root}/{model_name}")
|
| 56 |
+
pth_path = pth_files[0]
|
| 57 |
+
print(f"Loading {pth_path}")
|
| 58 |
+
cpt = torch.load(pth_path, map_location="cpu")
|
| 59 |
+
tgt_sr = cpt["config"][-1]
|
| 60 |
+
cpt["config"][-3] = cpt["weight"]["emb_g.weight"].shape[0] # n_spk
|
| 61 |
+
if_f0 = cpt.get("f0", 1)
|
| 62 |
+
version = cpt.get("version", "v1")
|
| 63 |
+
if version == "v1":
|
| 64 |
+
if if_f0 == 1:
|
| 65 |
+
net_g = SynthesizerTrnMs256NSFsid(*cpt["config"], is_half=config.is_half)
|
| 66 |
+
else:
|
| 67 |
+
net_g = SynthesizerTrnMs256NSFsid_nono(*cpt["config"])
|
| 68 |
+
elif version == "v2":
|
| 69 |
+
if if_f0 == 1:
|
| 70 |
+
net_g = SynthesizerTrnMs768NSFsid(*cpt["config"], is_half=config.is_half)
|
| 71 |
+
else:
|
| 72 |
+
net_g = SynthesizerTrnMs768NSFsid_nono(*cpt["config"])
|
| 73 |
+
else:
|
| 74 |
+
raise ValueError("Unknown version")
|
| 75 |
+
del net_g.enc_q
|
| 76 |
+
net_g.load_state_dict(cpt["weight"], strict=False)
|
| 77 |
+
print("Model loaded")
|
| 78 |
+
net_g.eval().to(config.device)
|
| 79 |
+
if config.is_half:
|
| 80 |
+
net_g = net_g.half()
|
| 81 |
+
else:
|
| 82 |
+
net_g = net_g.float()
|
| 83 |
+
vc = VC(tgt_sr, config)
|
| 84 |
+
# n_spk = cpt["config"][-3]
|
| 85 |
+
|
| 86 |
+
index_files = [
|
| 87 |
+
os.path.join(model_root, model_name, f)
|
| 88 |
+
for f in os.listdir(os.path.join(model_root, model_name))
|
| 89 |
+
if f.endswith(".index")
|
| 90 |
+
]
|
| 91 |
+
if len(index_files) == 0:
|
| 92 |
+
print("No index file found")
|
| 93 |
+
index_file = ""
|
| 94 |
+
else:
|
| 95 |
+
index_file = index_files[0]
|
| 96 |
+
print(f"Index file found: {index_file}")
|
| 97 |
+
|
| 98 |
+
return tgt_sr, net_g, vc, version, index_file, if_f0
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def load_hubert():
|
| 102 |
+
global hubert_model
|
| 103 |
+
models, _, _ = checkpoint_utils.load_model_ensemble_and_task(
|
| 104 |
+
["hubert_base.pt"],
|
| 105 |
+
suffix="",
|
| 106 |
)
|
| 107 |
+
hubert_model = models[0]
|
| 108 |
+
hubert_model = hubert_model.to(config.device)
|
| 109 |
+
if config.is_half:
|
| 110 |
+
hubert_model = hubert_model.half()
|
| 111 |
+
else:
|
| 112 |
+
hubert_model = hubert_model.float()
|
| 113 |
+
return hubert_model.eval()
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
print("Loading hubert model...")
|
| 117 |
+
hubert_model = load_hubert()
|
| 118 |
+
print("Hubert model loaded.")
|
| 119 |
+
|
| 120 |
+
print("Loading rmvpe model...")
|
| 121 |
+
rmvpe_model = RMVPE("rmvpe.pt", config.is_half, config.device)
|
| 122 |
+
print("rmvpe model loaded.")
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def tts(
|
| 126 |
+
model_name,
|
| 127 |
+
speed,
|
| 128 |
+
tts_text,
|
| 129 |
+
tts_voice,
|
| 130 |
+
f0_up_key,
|
| 131 |
+
f0_method,
|
| 132 |
+
index_rate,
|
| 133 |
+
protect,
|
| 134 |
+
filter_radius=3,
|
| 135 |
+
resample_sr=0,
|
| 136 |
+
rms_mix_rate=0.25,
|
| 137 |
+
):
|
| 138 |
+
print("------------------")
|
| 139 |
+
print(datetime.datetime.now())
|
| 140 |
+
print("tts_text:")
|
| 141 |
+
print(tts_text)
|
| 142 |
+
print(f"tts_voice: {tts_voice}")
|
| 143 |
+
print(f"Model name: {model_name}")
|
| 144 |
+
print(f"F0: {f0_method}, Key: {f0_up_key}, Index: {index_rate}, Protect: {protect}")
|
| 145 |
+
try:
|
| 146 |
+
if limitation and len(tts_text) > 280:
|
| 147 |
+
print("Error: Text too long")
|
| 148 |
+
return (
|
| 149 |
+
f"Text characters should be at most 280 in this huggingface space, but got {len(tts_text)} characters.",
|
| 150 |
+
None,
|
| 151 |
+
None,
|
| 152 |
+
)
|
| 153 |
+
tgt_sr, net_g, vc, version, index_file, if_f0 = model_data(model_name)
|
| 154 |
+
t0 = time.time()
|
| 155 |
+
if speed >= 0:
|
| 156 |
+
speed_str = f"+{speed}%"
|
| 157 |
+
else:
|
| 158 |
+
speed_str = f"{speed}%"
|
| 159 |
+
asyncio.run(
|
| 160 |
+
edge_tts.Communicate(
|
| 161 |
+
tts_text, "-".join(tts_voice.split("-")[:-1]), rate=speed_str
|
| 162 |
+
).save(edge_output_filename)
|
| 163 |
+
)
|
| 164 |
+
t1 = time.time()
|
| 165 |
+
edge_time = t1 - t0
|
| 166 |
+
audio, sr = librosa.load(edge_output_filename, sr=16000, mono=True)
|
| 167 |
+
duration = len(audio) / sr
|
| 168 |
+
print(f"Audio duration: {duration}s")
|
| 169 |
+
if limitation and duration >= 20:
|
| 170 |
+
print("Error: Audio too long")
|
| 171 |
+
return (
|
| 172 |
+
f"Audio should be less than 20 seconds in this huggingface space, but got {duration}s.",
|
| 173 |
+
edge_output_filename,
|
| 174 |
+
None,
|
| 175 |
+
)
|
| 176 |
|
| 177 |
+
f0_up_key = int(f0_up_key)
|
| 178 |
+
|
| 179 |
+
if not hubert_model:
|
| 180 |
+
load_hubert()
|
| 181 |
+
if f0_method == "rmvpe":
|
| 182 |
+
vc.model_rmvpe = rmvpe_model
|
| 183 |
+
times = [0, 0, 0]
|
| 184 |
+
audio_opt = vc.pipeline(
|
| 185 |
+
hubert_model,
|
| 186 |
+
net_g,
|
| 187 |
+
0,
|
| 188 |
+
audio,
|
| 189 |
+
edge_output_filename,
|
| 190 |
+
times,
|
| 191 |
+
f0_up_key,
|
| 192 |
+
f0_method,
|
| 193 |
+
index_file,
|
| 194 |
+
# file_big_npy,
|
| 195 |
+
index_rate,
|
| 196 |
+
if_f0,
|
| 197 |
+
filter_radius,
|
| 198 |
+
tgt_sr,
|
| 199 |
+
resample_sr,
|
| 200 |
+
rms_mix_rate,
|
| 201 |
+
version,
|
| 202 |
+
protect,
|
| 203 |
+
None,
|
| 204 |
+
)
|
| 205 |
+
if tgt_sr != resample_sr >= 16000:
|
| 206 |
+
tgt_sr = resample_sr
|
| 207 |
+
info = f"Success. Time: edge-tts: {edge_time}s, npy: {times[0]}s, f0: {times[1]}s, infer: {times[2]}s"
|
| 208 |
+
print(info)
|
| 209 |
+
return (
|
| 210 |
+
info,
|
| 211 |
+
edge_output_filename,
|
| 212 |
+
(tgt_sr, audio_opt),
|
| 213 |
+
)
|
| 214 |
+
except EOFError:
|
| 215 |
+
info = (
|
| 216 |
+
"It seems that the edge-tts output is not valid. "
|
| 217 |
+
"This may occur when the input text and the speaker do not match. "
|
| 218 |
+
"For example, maybe you entered Japanese (without alphabets) text but chose non-Japanese speaker?"
|
| 219 |
+
)
|
| 220 |
+
print(info)
|
| 221 |
+
return info, None, None
|
| 222 |
+
except:
|
| 223 |
+
info = traceback.format_exc()
|
| 224 |
+
print(info)
|
| 225 |
+
return info, None, None
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
initial_md = """
|
| 229 |
+
# RVC text-to-speech webui
|
| 230 |
+
|
| 231 |
+
This is a text-to-speech webui of RVC models.
|
| 232 |
+
|
| 233 |
+
Input text ➡[(edge-tts)](https://github.com/rany2/edge-tts)➡ Speech mp3 file ➡[(RVC)](https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI)➡ Final output
|
| 234 |
+
"""
|
| 235 |
+
|
| 236 |
+
app = gr.Blocks()
|
| 237 |
+
with app:
|
| 238 |
+
gr.Markdown(initial_md)
|
| 239 |
with gr.Row():
|
| 240 |
+
with gr.Column():
|
| 241 |
+
model_name = gr.Dropdown(label="Model", choices=models, value=models[0])
|
| 242 |
+
f0_key_up = gr.Number(
|
| 243 |
+
label="Transpose (the best value depends on the models and speakers)",
|
| 244 |
+
value=0,
|
| 245 |
)
|
| 246 |
+
with gr.Column():
|
| 247 |
+
f0_method = gr.Radio(
|
| 248 |
+
label="Pitch extraction method (pm: very fast, low quality, rmvpe: a little slow, high quality)",
|
| 249 |
+
choices=["pm", "rmvpe"], # harvest and crepe is too slow
|
| 250 |
+
value="rmvpe",
|
| 251 |
+
interactive=True,
|
| 252 |
)
|
| 253 |
+
index_rate = gr.Slider(
|
| 254 |
+
minimum=0,
|
| 255 |
+
maximum=1,
|
| 256 |
+
label="Index rate",
|
| 257 |
+
value=1,
|
| 258 |
+
interactive=True,
|
| 259 |
)
|
| 260 |
+
protect0 = gr.Slider(
|
| 261 |
+
minimum=0,
|
| 262 |
+
maximum=0.5,
|
| 263 |
+
label="Protect",
|
| 264 |
+
value=0.33,
|
| 265 |
+
step=0.01,
|
| 266 |
+
interactive=True,
|
|
|
|
| 267 |
)
|
| 268 |
+
with gr.Row():
|
| 269 |
+
with gr.Column():
|
| 270 |
+
tts_voice = gr.Dropdown(
|
| 271 |
+
label="Edge-tts speaker (format: language-Country-Name-Gender)",
|
| 272 |
+
choices=tts_voices,
|
| 273 |
+
allow_custom_value=False,
|
| 274 |
+
value="ja-JP-NanamiNeural-Female",
|
| 275 |
)
|
| 276 |
+
speed = gr.Slider(
|
| 277 |
+
minimum=-100,
|
| 278 |
+
maximum=100,
|
| 279 |
+
label="Speech speed (%)",
|
| 280 |
+
value=0,
|
| 281 |
+
step=10,
|
| 282 |
+
interactive=True,
|
|
|
|
| 283 |
)
|
| 284 |
+
tts_text = gr.Textbox(label="Input Text", value="これは日本語テキストから音声への変換デモです。")
|
| 285 |
+
with gr.Column():
|
| 286 |
+
but0 = gr.Button("Convert", variant="primary")
|
| 287 |
+
info_text = gr.Textbox(label="Output info")
|
| 288 |
+
with gr.Column():
|
| 289 |
+
edge_tts_output = gr.Audio(label="Edge Voice", type="filepath")
|
| 290 |
+
tts_output = gr.Audio(label="Result")
|
| 291 |
+
but0.click(
|
| 292 |
+
tts,
|
| 293 |
+
[
|
| 294 |
+
model_name,
|
| 295 |
+
speed,
|
| 296 |
+
tts_text,
|
| 297 |
+
tts_voice,
|
| 298 |
+
f0_key_up,
|
| 299 |
+
f0_method,
|
| 300 |
+
index_rate,
|
| 301 |
+
protect0,
|
| 302 |
+
],
|
| 303 |
+
[info_text, edge_tts_output, tts_output],
|
| 304 |
+
)
|
| 305 |
+
with gr.Row():
|
| 306 |
+
examples = gr.Examples(
|
| 307 |
+
examples_per_page=100,
|
| 308 |
+
examples=[
|
| 309 |
+
["これは日本語テキストから音声への変換デモです。", "ja-JP-NanamiNeural-Female"],
|
| 310 |
+
[
|
| 311 |
+
"This is an English text to speech conversation demo.",
|
| 312 |
+
"en-US-AriaNeural-Female",
|
| 313 |
+
],
|
| 314 |
+
["这是一个中文文本到语音的转换演示。", "zh-CN-XiaoxiaoNeural-Female"],
|
| 315 |
+
["한국어 텍스트에서 음성으로 변환하는 데모입니다.", "ko-KR-SunHiNeural-Female"],
|
| 316 |
+
[
|
| 317 |
+
"Il s'agit d'une démo de conversion du texte français à la parole.",
|
| 318 |
+
"fr-FR-DeniseNeural-Female",
|
| 319 |
+
],
|
| 320 |
+
[
|
| 321 |
+
"Dies ist eine Demo zur Umwandlung von Deutsch in Sprache.",
|
| 322 |
+
"de-DE-AmalaNeural-Female",
|
| 323 |
+
],
|
| 324 |
+
[
|
| 325 |
+
"Tämä on suomenkielinen tekstistä puheeksi -esittely.",
|
| 326 |
+
"fi-FI-NooraNeural-Female",
|
| 327 |
+
],
|
| 328 |
+
[
|
| 329 |
+
"Это демонстрационный пример преобразования русского текста в речь.",
|
| 330 |
+
"ru-RU-SvetlanaNeural-Female",
|
| 331 |
+
],
|
| 332 |
+
[
|
| 333 |
+
"Αυτή είναι μια επίδειξη μετατροπής ελληνικού κειμένου σε ομιλία.",
|
| 334 |
+
"el-GR-AthinaNeural-Female",
|
| 335 |
+
],
|
| 336 |
+
[
|
| 337 |
+
"Esta es una demostración de conversión de texto a voz en español.",
|
| 338 |
+
"es-ES-ElviraNeural-Female",
|
| 339 |
+
],
|
| 340 |
+
[
|
| 341 |
+
"Questa è una dimostrazione di sintesi vocale in italiano.",
|
| 342 |
+
"it-IT-ElsaNeural-Female",
|
| 343 |
+
],
|
| 344 |
+
[
|
| 345 |
+
"Esta é uma demonstração de conversão de texto em fala em português.",
|
| 346 |
+
"pt-PT-RaquelNeural-Female",
|
| 347 |
+
],
|
| 348 |
+
[
|
| 349 |
+
"Це демонстрація тексту до мовлення українською мовою.",
|
| 350 |
+
"uk-UA-PolinaNeural-Female",
|
| 351 |
+
],
|
| 352 |
+
[
|
| 353 |
+
"هذا عرض توضيحي عربي لتحويل النص إلى كلام.",
|
| 354 |
+
"ar-EG-SalmaNeural-Female",
|
| 355 |
+
],
|
| 356 |
+
[
|
| 357 |
+
"இது தமிழ் உரையிலிருந்து பேச்சு மாற்ற டெமோ.",
|
| 358 |
+
"ta-IN-PallaviNeural-Female",
|
| 359 |
+
],
|
| 360 |
+
],
|
| 361 |
+
inputs=[tts_text, tts_voice],
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
app.launch(inbrowser=True)
|