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

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -344
app.py DELETED
@@ -1,344 +0,0 @@
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()