|
|
""" |
|
|
UI 工具模块 |
|
|
包含Gradio界面相关的辅助函数 |
|
|
""" |
|
|
|
|
|
import gradio as gr |
|
|
import logging |
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
def get_audio_duration(audio_path): |
|
|
"""获取音频时长""" |
|
|
try: |
|
|
import librosa |
|
|
y, sr = librosa.load(audio_path, sr=None) |
|
|
return len(y) / sr |
|
|
except Exception as e: |
|
|
logger.warning(f"获取音频时长失败: {e}") |
|
|
return 0 |
|
|
|
|
|
|
|
|
def clear_all(): |
|
|
"""清空所有输入和输出""" |
|
|
return "", None, "🔄 已清空所有内容" |
|
|
|
|
|
|
|
|
def load_example(text, character): |
|
|
"""加载示例""" |
|
|
return text, character, f"📝 已加载示例: {text[:20]}..." |
|
|
|
|
|
|
|
|
def create_tts_wrapper(tts_interface): |
|
|
"""创建TTS包装函数""" |
|
|
def tts_wrapper(text, character, progress=gr.Progress()): |
|
|
"""TTS包装函数""" |
|
|
if not text.strip(): |
|
|
return None, "❌ 请输入要合成的文本" |
|
|
|
|
|
progress(0.1, desc="准备模型...") |
|
|
|
|
|
|
|
|
if character != tts_interface.current_character: |
|
|
progress(0.3, desc=f"加载角色模型: {character}") |
|
|
status, error = tts_interface.load_character(character) |
|
|
if error: |
|
|
return None, f"❌ {error}" |
|
|
|
|
|
progress(0.5, desc="正在合成语音...") |
|
|
|
|
|
audio_path, error = tts_interface.synthesize_speech(text, character) |
|
|
|
|
|
progress(0.9, desc="完成处理...") |
|
|
|
|
|
if error: |
|
|
return None, f"❌ {error}" |
|
|
|
|
|
progress(1.0, desc="✅ 合成成功!") |
|
|
return audio_path, f"✅ 合成成功!音频长度: {get_audio_duration(audio_path):.1f}秒" |
|
|
|
|
|
return tts_wrapper |
|
|
|
|
|
|
|
|
def create_system_status_display(tts_interface): |
|
|
"""创建系统状态显示""" |
|
|
def get_system_status(): |
|
|
if not tts_interface.install_error: |
|
|
status_color = "🟢" |
|
|
status_text = "Genie TTS 运行正常" |
|
|
else: |
|
|
status_color = "🔴" |
|
|
status_text = f"Genie TTS 安装失败: {tts_interface.install_error[:100]}..." |
|
|
|
|
|
return f"{status_color} {status_text}" |
|
|
|
|
|
return get_system_status |