Spaces:
Running on Zero
Running on Zero
app.py
CHANGED
|
@@ -193,35 +193,6 @@ def _audio_to_tuple(audio):
|
|
| 193 |
return None
|
| 194 |
|
| 195 |
|
| 196 |
-
def split_text(text, max_len=100):
|
| 197 |
-
"""Split text into segments of approximately max_len characters."""
|
| 198 |
-
import re
|
| 199 |
-
# Split by common sentence delimiters, keeping the delimiter
|
| 200 |
-
segments = re.split(r'([。!?;.!?;\n])', text)
|
| 201 |
-
|
| 202 |
-
parts = []
|
| 203 |
-
current_part = ""
|
| 204 |
-
|
| 205 |
-
for i in range(0, len(segments), 2):
|
| 206 |
-
chunk = segments[i]
|
| 207 |
-
punct = segments[i+1] if i+1 < len(segments) else ""
|
| 208 |
-
full_chunk = chunk + punct
|
| 209 |
-
|
| 210 |
-
if len(current_part) + len(full_chunk) > max_len and current_part:
|
| 211 |
-
parts.append(current_part.strip())
|
| 212 |
-
current_part = full_chunk
|
| 213 |
-
else:
|
| 214 |
-
current_part += full_chunk
|
| 215 |
-
|
| 216 |
-
if current_part:
|
| 217 |
-
# If the last part is still too long, force split it
|
| 218 |
-
while len(current_part) > max_len:
|
| 219 |
-
parts.append(current_part[:max_len].strip())
|
| 220 |
-
current_part = current_part[max_len:]
|
| 221 |
-
if current_part.strip():
|
| 222 |
-
parts.append(current_part.strip())
|
| 223 |
-
|
| 224 |
-
return [p for p in parts if p]
|
| 225 |
|
| 226 |
|
| 227 |
@spaces.GPU
|
|
@@ -351,7 +322,7 @@ def extract_voice_clone_prompt(ref_audio,ref_text,use_xvector_only):
|
|
| 351 |
|
| 352 |
|
| 353 |
def generate_voice_design(text, language, voice_description, progress=gr.Progress(track_tqdm=True)):
|
| 354 |
-
"""Generate speech using Voice Design model (1.7B only)
|
| 355 |
if not text or not text.strip():
|
| 356 |
return None, "错误:文本不能为空。"
|
| 357 |
if not voice_description or not voice_description.strip():
|
|
@@ -359,27 +330,16 @@ def generate_voice_design(text, language, voice_description, progress=gr.Progres
|
|
| 359 |
|
| 360 |
logger.info(f"开始 Voice Design 生成任务。语言: {language}, 文本长度: {len(text)}, 描述: {voice_description}")
|
| 361 |
try:
|
| 362 |
-
|
| 363 |
-
logger.info(
|
| 364 |
-
|
| 365 |
-
sr = 24000
|
| 366 |
-
|
| 367 |
-
for i, part in enumerate(progress.tqdm(text_parts, desc="正在生成分段")):
|
| 368 |
-
logger.info(f"正在处理第 {i+1}/{len(text_parts)} 段文本...")
|
| 369 |
-
wav, current_sr = infer_voice_design(part, language, voice_description)
|
| 370 |
-
all_wavs.append(wav)
|
| 371 |
-
sr = current_sr
|
| 372 |
-
|
| 373 |
-
combined_wav = np.concatenate(all_wavs)
|
| 374 |
-
logger.info("Voice Design 生成任务完成,正在合并音频...")
|
| 375 |
-
return (sr, combined_wav), "语音设计生成成功!"
|
| 376 |
except Exception as e:
|
| 377 |
logger.error(f"Voice Design 生成失败: {str(e)}", exc_info=True)
|
| 378 |
return None, f"错误: {type(e).__name__}: {e}"
|
| 379 |
|
| 380 |
|
| 381 |
def generate_voice_clone(ref_audio, ref_text, target_text, language, use_xvector_only, model_size, progress=gr.Progress(track_tqdm=True)):
|
| 382 |
-
"""Generate speech using Base (Voice Clone) model
|
| 383 |
if not target_text or not target_text.strip():
|
| 384 |
return None, "错误:目标文本不能为空。"
|
| 385 |
|
|
@@ -392,20 +352,9 @@ def generate_voice_clone(ref_audio, ref_text, target_text, language, use_xvector
|
|
| 392 |
|
| 393 |
logger.info(f"开始 Voice Clone 生成任务。模型大小: {model_size}, 语言: {language}, 目标文本长度: {len(target_text)}, 仅使用 x-vector: {use_xvector_only}")
|
| 394 |
try:
|
| 395 |
-
|
| 396 |
-
logger.info(
|
| 397 |
-
|
| 398 |
-
sr = 24000
|
| 399 |
-
|
| 400 |
-
for i, part in enumerate(progress.tqdm(text_parts, desc="正在生成分段")):
|
| 401 |
-
logger.info(f"正在处理第 {i+1}/{len(text_parts)} 段文本...")
|
| 402 |
-
wav, current_sr = infer_voice_clone( part, language,audio_tuple,ref_text,use_xvector_only)
|
| 403 |
-
all_wavs.append(wav)
|
| 404 |
-
sr = current_sr
|
| 405 |
-
|
| 406 |
-
combined_wav = np.concatenate(all_wavs)
|
| 407 |
-
logger.info("Voice Clone 生成任务完成,正在合并音频...")
|
| 408 |
-
return (sr, combined_wav), "语音克隆生成成功!"
|
| 409 |
except Exception as e:
|
| 410 |
logger.error(f"Voice Clone 生成失败: {str(e)}", exc_info=True)
|
| 411 |
return None, f"错误: {type(e).__name__}: {e}"
|
|
@@ -420,21 +369,9 @@ def generate_voice_clone_from_prompt_file(prompt_file_path, target_text, languag
|
|
| 420 |
|
| 421 |
logger.info(f"开始 Voice Clone 生成任务(使用特征文件)。语言: {language}, 目标文本长度: {len(target_text)}, 特征文件: {prompt_file_path}")
|
| 422 |
try:
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
all_wavs = []
|
| 427 |
-
sr = 24000
|
| 428 |
-
|
| 429 |
-
for i, part in enumerate(progress.tqdm(text_parts, desc="正在生成分段")):
|
| 430 |
-
logger.info(f"正在处理第 {i+1}/{len(text_parts)} 段文本...")
|
| 431 |
-
wav, current_sr = infer_voice_clone_from_prompt(part, language, prompt_file_path)
|
| 432 |
-
all_wavs.append(wav)
|
| 433 |
-
sr = current_sr
|
| 434 |
-
|
| 435 |
-
combined_wav = np.concatenate(all_wavs)
|
| 436 |
-
logger.info("Voice Clone 生成任务完成,正在合并音频...")
|
| 437 |
-
return (sr, combined_wav), "语音克隆生成成功(使用特征文件)!"
|
| 438 |
except Exception as e:
|
| 439 |
logger.error(f"Voice Clone 生成失败: {str(e)}", exc_info=True)
|
| 440 |
return None, f"错误: {type(e).__name__}: {e}"
|
|
|
|
| 193 |
return None
|
| 194 |
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
|
| 198 |
@spaces.GPU
|
|
|
|
| 322 |
|
| 323 |
|
| 324 |
def generate_voice_design(text, language, voice_description, progress=gr.Progress(track_tqdm=True)):
|
| 325 |
+
"""Generate speech using Voice Design model (1.7B only)."""
|
| 326 |
if not text or not text.strip():
|
| 327 |
return None, "错误:文本不能为空。"
|
| 328 |
if not voice_description or not voice_description.strip():
|
|
|
|
| 330 |
|
| 331 |
logger.info(f"开始 Voice Design 生成任务。语言: {language}, 文本长度: {len(text)}, 描述: {voice_description}")
|
| 332 |
try:
|
| 333 |
+
wav, sr = infer_voice_design(text.strip(), language, voice_description)
|
| 334 |
+
logger.info("Voice Design 生成任务完成...")
|
| 335 |
+
return (sr, wav), "语音设计生成成功!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
except Exception as e:
|
| 337 |
logger.error(f"Voice Design 生成失败: {str(e)}", exc_info=True)
|
| 338 |
return None, f"错误: {type(e).__name__}: {e}"
|
| 339 |
|
| 340 |
|
| 341 |
def generate_voice_clone(ref_audio, ref_text, target_text, language, use_xvector_only, model_size, progress=gr.Progress(track_tqdm=True)):
|
| 342 |
+
"""Generate speech using Base (Voice Clone) model."""
|
| 343 |
if not target_text or not target_text.strip():
|
| 344 |
return None, "错误:目标文本不能为空。"
|
| 345 |
|
|
|
|
| 352 |
|
| 353 |
logger.info(f"开始 Voice Clone 生成任务。模型大小: {model_size}, 语言: {language}, 目标文本长度: {len(target_text)}, 仅使用 x-vector: {use_xvector_only}")
|
| 354 |
try:
|
| 355 |
+
wav, sr = infer_voice_clone(target_text.strip(), language, audio_tuple, ref_text, use_xvector_only)
|
| 356 |
+
logger.info("Voice Clone 生成任务完成...")
|
| 357 |
+
return (sr, wav), "语音克隆生成成功!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
except Exception as e:
|
| 359 |
logger.error(f"Voice Clone 生成失败: {str(e)}", exc_info=True)
|
| 360 |
return None, f"错误: {type(e).__name__}: {e}"
|
|
|
|
| 369 |
|
| 370 |
logger.info(f"开始 Voice Clone 生成任务(使用特征文件)。语言: {language}, 目标文本长度: {len(target_text)}, 特征文件: {prompt_file_path}")
|
| 371 |
try:
|
| 372 |
+
wav, sr = infer_voice_clone_from_prompt(target_text.strip(), language, prompt_file_path)
|
| 373 |
+
logger.info("Voice Clone 生成任务完成...")
|
| 374 |
+
return (sr, wav), "语音克隆生成成功(使用特征文件)!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
except Exception as e:
|
| 376 |
logger.error(f"Voice Clone 生成失败: {str(e)}", exc_info=True)
|
| 377 |
return None, f"错误: {type(e).__name__}: {e}"
|