Upload 2 files
Browse files- gradio_app.py +50 -32
- requirements.txt +1 -1
gradio_app.py
CHANGED
|
@@ -5,44 +5,62 @@ import os
|
|
| 5 |
|
| 6 |
# 定义生成音频的异步函数
|
| 7 |
async def generate_audio(text, voice, rate, volume):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# 定义Gradio界面的主函数
|
| 18 |
def text_to_speech(text, voice, rate, volume):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# 异步获取所有可用语音
|
| 28 |
def get_voices():
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# 获取所有可用语音
|
| 48 |
voice_options = get_voices()
|
|
|
|
| 5 |
|
| 6 |
# 定义生成音频的异步函数
|
| 7 |
async def generate_audio(text, voice, rate, volume):
|
| 8 |
+
try:
|
| 9 |
+
# 创建输出文件路径
|
| 10 |
+
output_path = "output.mp3"
|
| 11 |
+
|
| 12 |
+
# 使用edge-tts生成音频
|
| 13 |
+
communicate = edge_tts.Communicate(text, voice, rate=rate, volume=volume)
|
| 14 |
+
await communicate.save(output_path)
|
| 15 |
+
|
| 16 |
+
return output_path
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"生成音频失败: {e}")
|
| 19 |
+
raise gr.Error(f"生成音频失败: {str(e)}")
|
| 20 |
|
| 21 |
# 定义Gradio界面的主函数
|
| 22 |
def text_to_speech(text, voice, rate, volume):
|
| 23 |
+
try:
|
| 24 |
+
# 调用异步函数生成音频
|
| 25 |
+
loop = asyncio.new_event_loop()
|
| 26 |
+
asyncio.set_event_loop(loop)
|
| 27 |
+
output_path = loop.run_until_complete(generate_audio(text, voice, rate, volume))
|
| 28 |
+
loop.close()
|
| 29 |
+
|
| 30 |
+
return output_path
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"文本转语音失败: {e}")
|
| 33 |
+
raise gr.Error(f"文本转语音失败: {str(e)}")
|
| 34 |
|
| 35 |
# 异步获取所有可用语音
|
| 36 |
def get_voices():
|
| 37 |
+
try:
|
| 38 |
+
loop = asyncio.new_event_loop()
|
| 39 |
+
asyncio.set_event_loop(loop)
|
| 40 |
+
voices = loop.run_until_complete(edge_tts.list_voices())
|
| 41 |
+
loop.close()
|
| 42 |
+
|
| 43 |
+
# 过滤出支持的语音,并按语言分组
|
| 44 |
+
voice_options = []
|
| 45 |
+
for voice in voices:
|
| 46 |
+
short_name = voice.get("ShortName", "")
|
| 47 |
+
friendly_name = voice.get("FriendlyName", short_name)
|
| 48 |
+
locale = voice.get("Locale", "")
|
| 49 |
+
voice_options.append((f"{friendly_name} ({locale})", short_name))
|
| 50 |
+
|
| 51 |
+
# 按友好名称排序
|
| 52 |
+
voice_options.sort(key=lambda x: x[0])
|
| 53 |
+
|
| 54 |
+
return voice_options
|
| 55 |
+
except Exception as e:
|
| 56 |
+
# 如果获取语音列表失败,返回默认的语音列表
|
| 57 |
+
print(f"获取语音列表失败: {e}")
|
| 58 |
+
return [
|
| 59 |
+
("中文女声 (zh-CN)", "zh-CN-YunxiNeural"),
|
| 60 |
+
("中文男声 (zh-CN)", "zh-CN-YunxiNeural"),
|
| 61 |
+
("英文女声 (en-US)", "en-US-JennyNeural"),
|
| 62 |
+
("英文男声 (en-US)", "en-US-GuyNeural")
|
| 63 |
+
]
|
| 64 |
|
| 65 |
# 获取所有可用语音
|
| 66 |
voice_options = get_voices()
|
requirements.txt
CHANGED
|
@@ -2,4 +2,4 @@ edge-tts
|
|
| 2 |
fastapi
|
| 3 |
uvicorn
|
| 4 |
python-multipart
|
| 5 |
-
gradio
|
|
|
|
| 2 |
fastapi
|
| 3 |
uvicorn
|
| 4 |
python-multipart
|
| 5 |
+
gradio>=3.0.0,<4.0.0
|