File size: 1,817 Bytes
c1585be
4ce7c67
c1585be
 
 
 
4ce7c67
 
 
 
 
 
 
f9f1879
4ce7c67
c1585be
 
 
 
4ce7c67
 
 
c1585be
4ce7c67
c1585be
d88cd51
c1585be
4ce7c67
c1585be
4ce7c67
 
 
 
d88cd51
 
4ce7c67
 
d88cd51
 
 
c1585be
 
d88cd51
c1585be
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline  # 用pipeline简化调用,避免模型加载冲突
import soundfile as sf
import torch
import os

# 初始化超轻量中文TTS管道(模型体积<800MB,无依赖冲突)
device = 0 if torch.cuda.is_available() else -1  # CPU/GPU自动适配
tts = pipeline(
    "text-to-speech",
    model="suno/bark-small",  # 超轻量模型,支持中英文,体积仅700MB
    device=device
)

# 语音生成函数(极简逻辑,稳定无错)
def generate_speech(text):
    if not text.strip():
        return None, "错误:请输入有效文本!"
    
    # 生成语音(控制长度,避免内存溢出)
    text = text[:300]  # 限制300字内,适配免费配置
    audio_output = tts(text)["audio"]
    
    # 保存音频(采样率24000Hz,通用格式)
    output_path = "output.wav"
    sf.write(output_path, audio_output, samplerate=24000)
    
    return output_path, "语音生成成功!(无依赖冲突,稳定运行)"

# 极简界面(减少资源占用)
with gr.Blocks(title="无冲突TTS") as demo:
    gr.Markdown("# 🎤 免费中英双语TTS(无冲突版)")
    gr.Markdown("基于suno/bark-small模型(700MB),适配免费Space,无依赖冲突")
    
    text_input = gr.Textbox(
        label="输入文本(中英双语)",
        placeholder="请输入中文或英文文本(≤300字)...",
        lines=4
    )
    audio_output = gr.Audio(label="生成的语音", type="filepath")
    status_text = gr.Textbox(label="状态", interactive=False)
    
    generate_btn = gr.Button("🚀 开始生成", variant="primary")
    generate_btn.click(
        fn=generate_speech,
        inputs=text_input,
        outputs=[audio_output, status_text]
    )

if __name__ == "__main__":
    demo.launch()