Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
import os
|
| 4 |
-
import pandas as pd # 仍保留,以备报告内容需要用到数据帧时使用
|
| 5 |
|
| 6 |
# --- TTS 模型函数 (CPU 友好) ---
|
| 7 |
|
|
@@ -17,29 +16,25 @@ def synthesize_espeak(text: str, lang: str = "en-us") -> str | None:
|
|
| 17 |
os.remove(output_file)
|
| 18 |
|
| 19 |
try:
|
| 20 |
-
# 运行 espeak-ng 的命令。--stdout 输出到标准输出,我们捕获它。
|
| 21 |
-
# 添加了 timeout 以防止无限期挂起
|
| 22 |
command = ["espeak-ng", f"-v{lang}", "--stdout", text]
|
| 23 |
|
| 24 |
process = subprocess.run(command, capture_output=True, check=True, timeout=10)
|
| 25 |
|
| 26 |
-
# 检查 espeak-ng 是否实际产生了音频输出
|
| 27 |
if not process.stdout:
|
| 28 |
gr.Warning("eSpeak-ng 没有为给定文本生成任何音频输出。请尝试不同的文本。")
|
| 29 |
print(f"eSpeak-ng 为文本 '{text}' 未产生输出。")
|
| 30 |
-
return None
|
| 31 |
-
|
| 32 |
-
# 将捕获到的标准输出(音频数据)写入 WAV 文件
|
| 33 |
with open(output_file, "wb") as f:
|
| 34 |
f.write(process.stdout)
|
| 35 |
|
| 36 |
print(f"eSpeak-ng 合成成功: {output_file}")
|
| 37 |
-
return output_file
|
| 38 |
|
| 39 |
except FileNotFoundError:
|
| 40 |
error_msg = "错误:未找到 espeak-ng。请确保它已安装在您 Space 的 Dockerfile 中,并且 Space 已重建。"
|
| 41 |
print(error_msg)
|
| 42 |
-
gr.Error(error_msg)
|
| 43 |
return None
|
| 44 |
except subprocess.CalledProcessError as e:
|
| 45 |
error_msg = f"eSpeak-ng 合成过程中出现错误。命令以代码 {e.returncode} 退出。错误输出:{e.stderr.decode()}"
|
|
@@ -64,93 +59,42 @@ def synthesize_api_tts(text: str) -> str | None:
|
|
| 64 |
对于此演示,它返回一个占位符音频文件。
|
| 65 |
"""
|
| 66 |
print(f"正在模拟 API TTS:'{text}'")
|
| 67 |
-
|
| 68 |
-
# --- 重要:请用您真实的 API 调用替换此处 ---
|
| 69 |
-
# --------------------------------------------------------
|
| 70 |
-
|
| 71 |
-
# 占位符:返回一个通用音频用于演示
|
| 72 |
-
# 在实际场景中,您会从 API 获取一个真实的音频文件。
|
| 73 |
return "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3" # 占位符 WAV/MP3
|
| 74 |
|
| 75 |
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
-
/* 自定义 CSS from 原始模板或添加您自己的 */
|
| 80 |
-
.markdown-text {
|
| 81 |
-
font-family: 'IBM Plex Sans', sans-serif;
|
| 82 |
-
color: #333;
|
| 83 |
-
line-height: 1.6;
|
| 84 |
-
}
|
| 85 |
-
h1, h2, h3, h4, h5, h6 {
|
| 86 |
-
color: #0056b3; /* 漂亮的蓝色 */
|
| 87 |
-
}
|
| 88 |
-
table {
|
| 89 |
-
width: 100%;
|
| 90 |
-
border-collapse: collapse;
|
| 91 |
-
margin-top: 15px;
|
| 92 |
-
}
|
| 93 |
-
th, td {
|
| 94 |
-
border: 1px solid #ddd;
|
| 95 |
-
padding: 8px;
|
| 96 |
-
text-align: left;
|
| 97 |
-
}
|
| 98 |
-
th {
|
| 99 |
-
background-color: #f2f2f2;
|
| 100 |
-
}
|
| 101 |
-
""") as demo:
|
| 102 |
-
gr.HTML("<h1 style='text-align: center; color: #0056b3;'>🎙️ 文本转音频模型对比实验</h1>")
|
| 103 |
-
gr.Markdown(
|
| 104 |
-
"欢迎来到我们的文本转音频模型对比实验空间。在这里,您可以对比不同模型在文本转音频任务上的表现。",
|
| 105 |
-
elem_classes="markdown-text"
|
| 106 |
-
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
elem_classes="markdown-text"
|
| 114 |
-
)
|
| 115 |
-
text_input = gr.Textbox(
|
| 116 |
-
label="输入您想要合成的文本",
|
| 117 |
-
lines=3,
|
| 118 |
-
placeholder="例如:您好,这是一个文本转音频的测试。"
|
| 119 |
-
)
|
| 120 |
-
with gr.Row():
|
| 121 |
-
espeak_button = gr.Button("🎤 合成 (eSpeak-ng)")
|
| 122 |
-
api_tts_button = gr.Button("🎧 合成 (API TTS 示例)")
|
| 123 |
-
|
| 124 |
-
with gr.Row():
|
| 125 |
-
espeak_output = gr.Audio(label="eSpeak-ng 输出", type="filepath")
|
| 126 |
-
api_tts_output = gr.Audio(label="API TTS 示例输出", type="filepath")
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
# 注意:为了让应用能够正常启动,我们暂时移除了 "Report" 选项卡。
|
| 150 |
-
# 如果应用能成功运行,说明问题出在报告内容上。
|
| 151 |
-
# 之后您可以尝试逐步添加报告内容,或简化其格式,以找出具体原因。
|
| 152 |
-
# 例如,可以先只放几段简单的文字,不包含表格或 mermaid 图。
|
| 153 |
|
| 154 |
# --- 启动 Gradio Demo ---
|
| 155 |
-
# 使用 queue() 是 Spaces 的良好实践
|
| 156 |
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
# --- TTS 模型函数 (CPU 友好) ---
|
| 6 |
|
|
|
|
| 16 |
os.remove(output_file)
|
| 17 |
|
| 18 |
try:
|
|
|
|
|
|
|
| 19 |
command = ["espeak-ng", f"-v{lang}", "--stdout", text]
|
| 20 |
|
| 21 |
process = subprocess.run(command, capture_output=True, check=True, timeout=10)
|
| 22 |
|
|
|
|
| 23 |
if not process.stdout:
|
| 24 |
gr.Warning("eSpeak-ng 没有为给定文本生成任何音频输出。请尝试不同的文本。")
|
| 25 |
print(f"eSpeak-ng 为文本 '{text}' 未产生输出。")
|
| 26 |
+
return None
|
| 27 |
+
|
|
|
|
| 28 |
with open(output_file, "wb") as f:
|
| 29 |
f.write(process.stdout)
|
| 30 |
|
| 31 |
print(f"eSpeak-ng 合成成功: {output_file}")
|
| 32 |
+
return output_file
|
| 33 |
|
| 34 |
except FileNotFoundError:
|
| 35 |
error_msg = "错误:未找到 espeak-ng。请确保它已安装在您 Space 的 Dockerfile 中,并且 Space 已重建。"
|
| 36 |
print(error_msg)
|
| 37 |
+
gr.Error(error_msg)
|
| 38 |
return None
|
| 39 |
except subprocess.CalledProcessError as e:
|
| 40 |
error_msg = f"eSpeak-ng 合成过程中出现错误。命令以代码 {e.returncode} 退出。错误输出:{e.stderr.decode()}"
|
|
|
|
| 59 |
对于此演示,它返回一个占位符音频文件。
|
| 60 |
"""
|
| 61 |
print(f"正在模拟 API TTS:'{text}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3" # 占位符 WAV/MP3
|
| 63 |
|
| 64 |
|
| 65 |
+
---
|
| 66 |
|
| 67 |
+
### **Gradio 界面 (最简化版本)**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
```python
|
| 70 |
+
with gr.Blocks() as demo: # 移除 css 参数
|
| 71 |
+
gr.Markdown("# 🎙️ 文本转音频模型对比实验") # 使用 Markdown 标题替代 gr.HTML
|
| 72 |
+
|
| 73 |
+
gr.Markdown("这是一个最简化的版本,用于排查启动问题。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
text_input = gr.Textbox(
|
| 76 |
+
label="输入您想要合成的文本",
|
| 77 |
+
lines=3,
|
| 78 |
+
placeholder="例如:您好,这是��个文本转音频的测试。"
|
| 79 |
+
)
|
| 80 |
+
with gr.Row():
|
| 81 |
+
espeak_button = gr.Button("🎤 合成 (eSpeak-ng)")
|
| 82 |
+
api_tts_button = gr.Button("🎧 合成 (API TTS 示例)")
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
espeak_output = gr.Audio(label="eSpeak-ng 输出", type="filepath")
|
| 86 |
+
api_tts_output = gr.Audio(label="API TTS 示例输出", type="filepath")
|
| 87 |
|
| 88 |
+
espeak_button.click(
|
| 89 |
+
synthesize_espeak,
|
| 90 |
+
inputs=[text_input],
|
| 91 |
+
outputs=[espeak_output]
|
| 92 |
+
)
|
| 93 |
+
api_tts_button.click(
|
| 94 |
+
synthesize_api_tts,
|
| 95 |
+
inputs=[text_input],
|
| 96 |
+
outputs=[api_tts_output]
|
| 97 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# --- 启动 Gradio Demo ---
|
|
|
|
| 100 |
demo.queue().launch()
|