Liangjiejie commited on
Commit
2650462
·
verified ·
1 Parent(s): be3ecfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +353 -332
app.py CHANGED
@@ -1,344 +1,365 @@
1
- import gradio as gr
2
- import subprocess
 
3
  import os
4
- import sys
5
- import yaml
6
- from pathlib import Path
7
  import time
8
- import threading
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
- while thread.is_alive():
197
- yield "\n".join(current_log + process_output_lines), None
198
- time.sleep(0.3) # 更新頻度
199
-
200
- # スレッド終了待ちとプロセス終了待ち
201
- thread.join()
202
- try:
203
- process.wait(timeout=12000) # 20分タイムアウト (モデルサイズにより調整)
204
- except subprocess.TimeoutExpired:
205
- current_log.extend(process_output_lines) # ここまでのログを追加
206
- current_log.append("\n❌ Error: Conversion process timed out after 20 minutes.")
207
- process.kill()
208
- yield "\n".join(current_log), None
209
- return # tryブロックを抜ける
210
-
211
- # 最終的なプロセス出力を取得
212
- final_stdout, final_stderr = process.communicate()
213
- if final_stdout:
214
- process_output_lines.extend(final_stdout.strip().split('\n'))
215
- if final_stderr:
216
- processed_stderr = []
217
- for line in final_stderr.strip().split('\n'):
218
- processed_line = f"stderr: {line.strip()}"
219
- if "warning" not in line.lower() and line.strip(): # 空行と警告以外
220
- processed_line = f"ERROR (stderr): {line.strip()}"
221
- processed_stderr.append(processed_line)
222
- if any(line.startswith("ERROR") for line in processed_stderr):
223
- process_output_lines.append("--- Errors/Warnings (stderr) ---")
224
- process_output_lines.extend(processed_stderr)
225
- process_output_lines.append("-----------------------------")
226
- elif processed_stderr: # 警告のみの場合
227
- process_output_lines.append("--- Warnings (stderr) ---")
228
- process_output_lines.extend(processed_stderr)
229
- process_output_lines.append("------------------------")
230
-
231
-
232
- # 全てのプロセスログをメインログに追加
233
- current_log.extend(process_output_lines)
234
- current_log.append("--- End Script Output ---")
235
- current_log.append("\n-------------------------------")
236
-
237
- # --- 結果の確認と出力ファイルのコピー ---
238
- if process.returncode == 0:
239
- current_log.append("✅ ONNX conversion command finished successfully.")
240
- # 期待されるONNXファイルパス (入力と同じディレクトリ内)
241
- expected_onnx_path_in_temp = temp_safetensors_path.with_suffix(".onnx")
242
-
243
- if expected_onnx_path_in_temp.exists():
244
- current_log.append(f" - Found converted ONNX file: {expected_onnx_path_in_temp.name}")
245
- # 一時ディレクトリから永続的な出力ディレクトリにコピー
246
- final_onnx_path = OUTPUT_DIR / expected_onnx_path_in_temp.name
247
- try:
248
- shutil.copy(expected_onnx_path_in_temp, final_onnx_path)
249
- current_log.append(f" - Copied ONNX file for download to: {final_onnx_path.relative_to(SCRIPT_DIR)}")
250
- onnx_output_path_str = str(final_onnx_path) # ダウンロード用ファイルパスを設定
251
- except Exception as e:
252
- current_log.append(f"❌ Error copying ONNX file to output directory: {e}")
253
- else:
254
- current_log.append(f"⚠️ Warning: Expected ONNX file not found at '{expected_onnx_path_in_temp.name}'. Check script output above.")
255
- else:
256
- current_log.append(f"❌ ONNX conversion command failed with return code {process.returncode}.")
257
- current_log.append(" Please check the logs above for errors (especially lines starting with 'ERROR').")
258
-
259
- # 一時ディレクトリが自動で削除される前に最終結果をyield
260
- yield "\n".join(current_log), onnx_output_path_str
261
-
262
- except FileNotFoundError as e:
263
- current_log.append(f"\n❌ Error: A required command or file was not found: {e.filename}. Check Dockerfile setup and PATH.")
264
- current_log.append(f"{e}")
265
- yield "\n".join(current_log), None
266
- except Exception as e:
267
- current_log.append(f"\n❌ An unexpected error occurred: {e}")
268
- import traceback
269
- current_log.append(traceback.format_exc())
270
- yield "\n".join(current_log), None
271
- finally:
272
- # ガベージコレクション
273
- gc.collect()
274
- print("Conversion function finished.") # サーバーログ用
275
- # 最後のyieldでUIを最終状態に更新
276
- # yield "\n".join(current_log), onnx_output_path_str # tryブロック内で既に返している
277
-
278
-
279
- # --- Gradio Interface ---
280
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
281
- gr.Markdown("# Style-Bert-VITS2 Safetensors to ONNX Converter")
282
- gr.Markdown(
283
- "Upload your model's `.safetensors`, `config.json`, and `style_vectors.npy` files. "
284
- "The application will convert the model to ONNX format, and you can download the resulting `.onnx` file."
285
- )
286
- gr.Markdown(
287
- "_(Environment setup is handled automatically when this Space starts.)_"
 
 
 
 
 
288
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290
  with gr.Row():
291
- with gr.Column(scale=1):
292
- gr.Markdown("### 1. Upload Model Files")
293
- safetensors_upload = gr.File(
294
- label="Safetensors Model (.safetensors)",
295
- file_types=[".safetensors"],
296
  )
297
- config_upload = gr.File(
298
- label="Config File (config.json)",
299
- file_types=[".json"],
 
 
 
300
  )
301
- style_vectors_upload = gr.File(
302
- label="Style Vectors (style_vectors.npy)",
303
- file_types=[".npy"],
 
 
 
304
  )
305
-
306
- convert_button = gr.Button("2. Convert to ONNX", variant="primary", elem_id="convert_button")
307
-
308
- gr.Markdown("---")
309
- gr.Markdown("### 3. Download Result")
310
- onnx_download = gr.File(
311
- label="ONNX Model (.onnx)",
312
- interactive=False, # 出力専用
313
  )
314
- gr.Markdown(
315
- "**Note:** Conversion can take **several minutes** (5-20+ min depending on model size and hardware). "
316
- "Please be patient. The log on the right shows the progress."
 
 
 
 
317
  )
318
-
319
- with gr.Column(scale=2):
320
- output_log = gr.Textbox(
321
- label="Conversion Log",
322
- lines=30, # 高さをさらに増やす
323
- interactive=False,
324
- autoscroll=True,
325
- max_lines=2000 # ログが増える可能性
326
  )
327
-
328
- # ボタンクリック時のアクション設定
329
- convert_button.click(
330
- convert_safetensors_to_onnx_gradio,
331
- inputs=[safetensors_upload, config_upload, style_vectors_upload],
332
- outputs=[output_log, onnx_download] # ログとダウンロードファイルの2つを出力
333
- )
334
-
335
- # --- アプリの起動 ---
336
- if __name__ == "__main__":
337
- # Style-Bert-VITS2 へのパスを追加
338
- add_sbv2_to_path()
339
- # 出力ディレクトリ作成 (存在確認含む)
340
- OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
341
- print(f"Output directory: {OUTPUT_DIR.resolve()}")
342
-
343
- # Gradioアプリを起動
344
- demo.queue().launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import datetime
3
+ import logging
4
  import os
 
 
 
5
  import time
6
+ import traceback
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)