File size: 2,190 Bytes
24437ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
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