Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from supertonic import TTS
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Initialize TTS
|
| 7 |
+
try:
|
| 8 |
+
tts = TTS(auto_download=True)
|
| 9 |
+
except Exception as e:
|
| 10 |
+
print(f"Error initializing TTS: {e}")
|
| 11 |
+
|
| 12 |
+
VOICES = ["M1", "M2", "M3", "M4", "M5", "F1", "F2", "F3", "F4", "F5"]
|
| 13 |
+
|
| 14 |
+
LANGUAGES = {
|
| 15 |
+
"English": "en", "Korean": "ko", "Japanese": "ja", "Arabic": "ar",
|
| 16 |
+
"Bulgarian": "bg", "Czech": "cs", "Danish": "da", "German": "de",
|
| 17 |
+
"Greek": "el", "Spanish": "es", "Estonian": "et", "Finnish": "fi",
|
| 18 |
+
"French": "fr", "Hindi": "hi", "Croatian": "hr", "Hungarian": "hu",
|
| 19 |
+
"Indonesian": "id", "Italian": "it", "Lithuanian": "lt", "Latvian": "lv",
|
| 20 |
+
"Dutch": "nl", "Polish": "pl", "Portuguese": "pt", "Romanian": "ro",
|
| 21 |
+
"Russian": "ru", "Slovak": "sk", "Slovenian": "sl", "Swedish": "sv",
|
| 22 |
+
"Turkish": "tr", "Ukrainian": "uk", "Vietnamese": "vi"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
def generate_speech(text, voice, language_name):
|
| 26 |
+
if not text.strip():
|
| 27 |
+
raise gr.Error("Please enter some text.")
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
lang_code = LANGUAGES[language_name]
|
| 31 |
+
style = tts.get_voice_style(voice_name=voice)
|
| 32 |
+
wav, duration = tts.synthesize(text, voice_style=style, lang=lang_code)
|
| 33 |
+
|
| 34 |
+
# ИСПРАВЛЕНИЕ: Создаем уникальный временный файл для каждого запроса
|
| 35 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 36 |
+
output_path = tmp.name
|
| 37 |
+
|
| 38 |
+
tts.save_audio(wav, output_path)
|
| 39 |
+
readable_duration = float(duration)
|
| 40 |
+
|
| 41 |
+
return output_path, f"Generation Successful! \nDuration: {readable_duration:.2f}s"
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
raise gr.Error(f"Generation failed: {str(e)}")
|
| 45 |
+
|
| 46 |
+
with gr.Blocks(theme='soft', title="Supertonic 3 TTS") as demo:
|
| 47 |
+
gr.Markdown("# 🎙️ Supertonic 3: Multilingual TTS API")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column(scale=1):
|
| 51 |
+
input_text = gr.Textbox(label="Input Text", lines=4, value="Привет, как дела?")
|
| 52 |
+
with gr.Row():
|
| 53 |
+
voice_opt = gr.Dropdown(choices=VOICES, value="M2", label="Voice Style")
|
| 54 |
+
lang_opt = gr.Dropdown(choices=sorted(list(LANGUAGES.keys())), value="Russian", label="Language")
|
| 55 |
+
btn = gr.Button("Synthesize Speech", variant="primary")
|
| 56 |
+
|
| 57 |
+
with gr.Column(scale=1):
|
| 58 |
+
audio_output = gr.Audio(label="Synthesized Audio", type="filepath")
|
| 59 |
+
status_box = gr.Textbox(label="Status", interactive=False)
|
| 60 |
+
|
| 61 |
+
btn.click(
|
| 62 |
+
fn=generate_speech,
|
| 63 |
+
inputs=[input_text, voice_opt, lang_opt],
|
| 64 |
+
outputs=[audio_output, status_box]
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
# ВАЖНО для Hugging Face Spaces:
|
| 69 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|