| """
|
| Hugging Face Spaces应用程序入口文件
|
| """
|
| import gradio as gr
|
| from api import tts_api
|
| import asyncio
|
|
|
| def create_app():
|
| """创建Gradio应用"""
|
| with gr.Blocks(title="Edge TTS 文本转语音") as app:
|
| gr.Markdown("#🎙 Edge TTS 文本转语音工具")
|
| gr.Markdown("基于Microsoft Edge TTS的高质量文本转语音服务")
|
|
|
| with gr.Tab("文本配音"):
|
| with gr.Row():
|
| with gr.Column(scale=2):
|
| text_input = gr.TextArea(
|
| label="📝 输入文本",
|
| placeholder="在此输入您要转换为语音的文本...",
|
| lines=12
|
| )
|
|
|
| with gr.Row():
|
| voice_selection = gr.Dropdown(
|
| choices=tts_api.get_available_voices(),
|
| value="zh-CN-XiaoxiaoNeural",
|
| label="🗣️ 选择语音"
|
| )
|
|
|
| with gr.Row():
|
| rate_slider = gr.Slider(
|
| minimum=-50, maximum=50, value=0, step=1,
|
| label="⏩ 语速调整 (%)"
|
| )
|
|
|
| pitch_slider = gr.Slider(
|
| minimum=-50, maximum=50, value=0, step=1,
|
| label="🎵音调整 (Hz)"
|
| )
|
|
|
| generate_btn = gr.Button("🔊 生成语音", variant="primary")
|
|
|
| with gr.Column(scale=1):
|
| audio_output = gr.Audio(label="🎧 生成的语音", type="filepath")
|
| status_output = gr.Textbox(label="📊 状态信息", interactive=False)
|
|
|
|
|
| async def generate_speech(text, voice, rate, pitch):
|
| if not text.strip():
|
| return None, "请输入要转换的文本"
|
|
|
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
|
| output_path = temp_file.name
|
|
|
| try:
|
| result = await tts_api.text_to_speech(text, voice, rate, pitch, output_path)
|
| if result:
|
| return result, "语音生成成功"
|
| else:
|
| return None, "语音生成失败"
|
| except Exception as e:
|
| return None, f"生成语音时出错: {str(e)}"
|
|
|
| generate_btn.click(
|
| fn=lambda text, voice, rate, pitch: asyncio.run(
|
| generate_speech(text, voice, rate, pitch)
|
| ),
|
| inputs=[text_input, voice_selection, rate_slider, pitch_slider],
|
| outputs=[audio_output, status_output]
|
| )
|
|
|
| return app
|
|
|
| if __name__ == "__main__":
|
| import tempfile
|
| app = create_app()
|
| app.launch() |