Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,33 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from TTS.api import TTS
|
| 3 |
import tempfile
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
# ---
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Initialize TTS with the chosen model.
|
| 12 |
-
# This model is known to be stable and CPU-compatible.
|
| 13 |
-
# The '.to("cpu")' ensures it runs on the free hardware tier.
|
| 14 |
-
tts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False).to("cpu")
|
| 15 |
-
# For Tacotron2, we typically use the name of the dataset as the speaker
|
| 16 |
-
VOICES = ["ljspeech"]
|
| 17 |
-
DEFAULT_VOICE = VOICES[0]
|
| 18 |
-
except Exception as e:
|
| 19 |
-
tts_model = None
|
| 20 |
-
VOICES = ["Model Load Error"]
|
| 21 |
-
DEFAULT_VOICE = VOICES[0]
|
| 22 |
-
# Print the error to the logs, but continue with the Gradio interface
|
| 23 |
-
print(f"Error loading TTS model (likely gruut failure or missing dependency): {e}", file=sys.stderr)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def text_to_speech_coqui(text, speaker_name):
|
| 28 |
# 1. Input Validation
|
| 29 |
-
if not tts_model:
|
| 30 |
-
return "ERROR: TTS Model failed to load at startup. Check Space logs.", None
|
| 31 |
if not text or not text.strip():
|
| 32 |
return "ERROR: Input text cannot be empty.", None
|
| 33 |
|
| 34 |
try:
|
|
|
|
|
|
|
|
|
|
| 35 |
# Create a temporary file path
|
| 36 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".
|
| 37 |
tmp_path = tmp_file.name
|
| 38 |
|
| 39 |
-
# Generate audio file
|
| 40 |
-
|
| 41 |
-
text=text,
|
| 42 |
-
speaker=speaker_name,
|
| 43 |
-
file_path=tmp_path
|
| 44 |
-
)
|
| 45 |
|
| 46 |
-
return "
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
# Handle all generation errors
|
|
@@ -51,25 +35,27 @@ def text_to_speech_coqui(text, speaker_name):
|
|
| 51 |
|
| 52 |
|
| 53 |
# --- Gradio UI Definition ---
|
| 54 |
-
input_text = gr.Textbox(lines=5, label="
|
| 55 |
-
output_text = gr.Textbox(label="
|
| 56 |
-
output_audio = gr.Audio(type="filepath", label="
|
| 57 |
|
| 58 |
language = gr.Dropdown(
|
| 59 |
-
choices=
|
| 60 |
-
value=
|
| 61 |
-
label="
|
| 62 |
)
|
| 63 |
|
| 64 |
|
| 65 |
# --- Gradio Interface Definition and Launch ---
|
|
|
|
| 66 |
interface = gr.Interface(
|
| 67 |
-
fn=
|
| 68 |
inputs=[input_text, language],
|
| 69 |
outputs=[output_text, output_audio],
|
| 70 |
-
title="
|
| 71 |
-
description="
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
-
|
|
|
|
|
|
| 1 |
+
from tts_voice import tts_order_voice
|
| 2 |
+
import edge_tts
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import tempfile
|
| 5 |
+
import anyio
|
| 6 |
+
import asyncio
|
| 7 |
|
| 8 |
+
# --- Voice Data ---
|
| 9 |
+
# Uses the dictionary loaded from your tts_voice.py file
|
| 10 |
+
language_dict = tts_order_voice
|
| 11 |
+
DEFAULT_LANGUAGE = list(language_dict.keys())[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# --- Core TTS Function (Asynchronous) ---
|
| 14 |
+
async def text_to_speech_edge(text, language_code):
|
|
|
|
| 15 |
# 1. Input Validation
|
|
|
|
|
|
|
| 16 |
if not text or not text.strip():
|
| 17 |
return "ERROR: Input text cannot be empty.", None
|
| 18 |
|
| 19 |
try:
|
| 20 |
+
voice = language_dict[language_code]
|
| 21 |
+
communicate = edge_tts.Communicate(text, voice)
|
| 22 |
+
|
| 23 |
# Create a temporary file path
|
| 24 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 25 |
tmp_path = tmp_file.name
|
| 26 |
|
| 27 |
+
# Generate audio file
|
| 28 |
+
await communicate.save(tmp_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
return f"语音合成完成:{text}", tmp_path
|
| 31 |
|
| 32 |
except Exception as e:
|
| 33 |
# Handle all generation errors
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
# --- Gradio UI Definition ---
|
| 38 |
+
input_text = gr.Textbox(lines=5, label="输入文本")
|
| 39 |
+
output_text = gr.Textbox(label="输出文本")
|
| 40 |
+
output_audio = gr.Audio(type="filepath", label="导出文件")
|
| 41 |
|
| 42 |
language = gr.Dropdown(
|
| 43 |
+
choices=list(language_dict.keys()),
|
| 44 |
+
value=DEFAULT_LANGUAGE,
|
| 45 |
+
label="语言"
|
| 46 |
)
|
| 47 |
|
| 48 |
|
| 49 |
# --- Gradio Interface Definition and Launch ---
|
| 50 |
+
# Note: Since the core function is async, we use asyncio for launch
|
| 51 |
interface = gr.Interface(
|
| 52 |
+
fn=text_to_speech_edge,
|
| 53 |
inputs=[input_text, language],
|
| 54 |
outputs=[output_text, output_audio],
|
| 55 |
+
title="Edge TTS 文字转语音 (Cloud API)",
|
| 56 |
+
description="使用微软 Edge TTS 提供的云服务,稳定且无需本地模型加载。"
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
# Ensure anyio is used to run the async Gradio app
|
| 61 |
+
anyio.run(interface.launch, backend="asyncio")
|