Spaces:
Running
Running
fix: 整理简化代码
Browse files- src/gui_cloud.py +24 -34
src/gui_cloud.py
CHANGED
|
@@ -375,19 +375,9 @@ def validate_audio_upload(files) -> Tuple[bool, str, List[str]]:
|
|
| 375 |
else:
|
| 376 |
path = str(f)
|
| 377 |
|
| 378 |
-
logger.info(f"文件[{i}] 解析路径: {path}, 存在: {os.path.exists(path)}")
|
| 379 |
-
|
| 380 |
if path.lower().endswith(CloudConfig.AUDIO_EXTENSIONS):
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
size = os.path.getsize(path)
|
| 384 |
-
logger.info(f"文件[{i}] 大小: {size} bytes")
|
| 385 |
-
if size > 0:
|
| 386 |
-
valid_files.append(path)
|
| 387 |
-
else:
|
| 388 |
-
logger.warning(f"文件为空: {path}")
|
| 389 |
-
else:
|
| 390 |
-
logger.warning(f"文件不存在: {path}")
|
| 391 |
|
| 392 |
if not valid_files:
|
| 393 |
return False, f"未找到有效音频文件,支持格式: {', '.join(CloudConfig.AUDIO_EXTENSIONS)}", []
|
|
@@ -498,47 +488,47 @@ def process_make_voicebank(
|
|
| 498 |
os.makedirs(input_dir, exist_ok=True)
|
| 499 |
os.makedirs(bank_dir, exist_ok=True)
|
| 500 |
|
| 501 |
-
# 复制音频文件到输入目录
|
| 502 |
-
# 注意:Gradio 6.x 上传的文件可能是临时路径,需要正确处理
|
| 503 |
progress(0.05, desc="复制音频文件...")
|
| 504 |
copied_count = 0
|
|
|
|
|
|
|
| 505 |
for idx, src_path in enumerate(file_paths):
|
| 506 |
-
|
|
|
|
|
|
|
| 507 |
if not os.path.exists(src_path):
|
| 508 |
-
|
| 509 |
continue
|
| 510 |
|
| 511 |
-
# 检查源文件大小
|
| 512 |
src_size = os.path.getsize(src_path)
|
| 513 |
if src_size == 0:
|
| 514 |
-
|
| 515 |
continue
|
| 516 |
|
| 517 |
try:
|
| 518 |
-
#
|
| 519 |
-
original_name = os.path.basename(src_path)
|
| 520 |
_, ext = os.path.splitext(original_name)
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
# 生成安全的文件名(避免中文和空格导致的问题)
|
| 524 |
-
safe_name = f"audio_{idx:04d}{ext}"
|
| 525 |
dst_path = os.path.join(input_dir, safe_name)
|
| 526 |
|
| 527 |
-
# 使用 shutil.copy2 复制文件(保留元数据)
|
| 528 |
shutil.copy2(src_path, dst_path)
|
| 529 |
|
| 530 |
-
# 验证复制
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
log(f"
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
copied_count += 1
|
| 537 |
-
log(f" {original_name} ({src_size} bytes) -> {safe_name}")
|
| 538 |
except Exception as e:
|
| 539 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
|
| 541 |
if copied_count == 0:
|
|
|
|
| 542 |
return "❌ 无法访问上传的文件,请重新上传", "\n".join(logs), None, None
|
| 543 |
|
| 544 |
log(f"📋 已复制 {copied_count}/{len(file_paths)} 个文件到工作目录")
|
|
|
|
| 375 |
else:
|
| 376 |
path = str(f)
|
| 377 |
|
|
|
|
|
|
|
| 378 |
if path.lower().endswith(CloudConfig.AUDIO_EXTENSIONS):
|
| 379 |
+
valid_files.append(path)
|
| 380 |
+
logger.info(f"文件[{i}] 有效路径: {path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
if not valid_files:
|
| 383 |
return False, f"未找到有效音频文件,支持格式: {', '.join(CloudConfig.AUDIO_EXTENSIONS)}", []
|
|
|
|
| 488 |
os.makedirs(input_dir, exist_ok=True)
|
| 489 |
os.makedirs(bank_dir, exist_ok=True)
|
| 490 |
|
| 491 |
+
# 复制音频文件到输入目录(重命名为安全文件名)
|
|
|
|
| 492 |
progress(0.05, desc="复制音频文件...")
|
| 493 |
copied_count = 0
|
| 494 |
+
copy_errors = []
|
| 495 |
+
|
| 496 |
for idx, src_path in enumerate(file_paths):
|
| 497 |
+
original_name = os.path.basename(src_path)
|
| 498 |
+
|
| 499 |
+
# 检查源文件
|
| 500 |
if not os.path.exists(src_path):
|
| 501 |
+
copy_errors.append(f"{original_name}: 文件不存在")
|
| 502 |
continue
|
| 503 |
|
|
|
|
| 504 |
src_size = os.path.getsize(src_path)
|
| 505 |
if src_size == 0:
|
| 506 |
+
copy_errors.append(f"{original_name}: 文件为空")
|
| 507 |
continue
|
| 508 |
|
| 509 |
try:
|
| 510 |
+
# 生成安全的文件名
|
|
|
|
| 511 |
_, ext = os.path.splitext(original_name)
|
| 512 |
+
safe_name = f"audio_{idx:04d}{ext.lower()}"
|
|
|
|
|
|
|
|
|
|
| 513 |
dst_path = os.path.join(input_dir, safe_name)
|
| 514 |
|
|
|
|
| 515 |
shutil.copy2(src_path, dst_path)
|
| 516 |
|
| 517 |
+
# 验证复制结果
|
| 518 |
+
if os.path.getsize(dst_path) == src_size:
|
| 519 |
+
copied_count += 1
|
| 520 |
+
log(f" {original_name} ({src_size} bytes) -> {safe_name}")
|
| 521 |
+
else:
|
| 522 |
+
copy_errors.append(f"{original_name}: 复制不完整")
|
|
|
|
|
|
|
| 523 |
except Exception as e:
|
| 524 |
+
copy_errors.append(f"{original_name}: {e}")
|
| 525 |
+
|
| 526 |
+
if copy_errors:
|
| 527 |
+
for err in copy_errors:
|
| 528 |
+
log(f"⚠️ {err}")
|
| 529 |
|
| 530 |
if copied_count == 0:
|
| 531 |
+
decrement_concurrency()
|
| 532 |
return "❌ 无法访问上传的文件,请重新上传", "\n".join(logs), None, None
|
| 533 |
|
| 534 |
log(f"📋 已复制 {copied_count}/{len(file_paths)} 个文件到工作目录")
|