Renderlib-dev's picture
Update app.py
62c042a verified
Raw
History Blame Contribute Delete
4.94 kB
import gradio as gr
from supertonic import TTS
import os
import numpy as np
import tempfile
import spaces # ZeroGPU के लिए अनिवार्य
import torch
# GPU डिटेक्ट करना और TTS को GPU पर लोड करना
device = "cuda" if torch.cuda.is_available() else "cpu"
try:
# अगर Supertonic लाइब्रेरी device पैरामीटर सपोर्ट करती है
tts = TTS(auto_download=True, device=device)
except TypeError:
# अगर यह डिफ़ॉल्ट रूप से ONNX का उपयोग कर रही है
tts = TTS(auto_download=True)
print("Warning: Initialization using default backend.")
except Exception as e:
print(f"Error initializing TTS: {e}")
VOICES = ["M1", "M2", "M3", "M4", "M5", "F1", "F2", "F3", "F4", "F5"]
LANGUAGES = {
"English": "en", "Korean": "ko", "Japanese": "ja", "Arabic": "ar",
"Bulgarian": "bg", "Czech": "cs", "Danish": "da", "German": "de",
"Greek": "el", "Spanish": "es", "Estonian": "et", "Finnish": "fi",
"French": "fr", "Hindi": "hi", "Croatian": "hr", "Hungarian": "hu",
"Indonesian": "id", "Italian": "it", "Lithuanian": "lt", "Latvian": "lv",
"Dutch": "nl", "Polish": "pl", "Portuguese": "pt", "Romanian": "ro",
"Russian": "ru", "Slovak": "sk", "Slovenian": "sl", "Swedish": "sv",
"Turkish": "tr", "Ukrainian": "uk", "Vietnamese": "vi"
}
@spaces.GPU # यह लाइन जनरेशन के समय ZeroGPU को एक्टिवेट करेगी
def generate_speech(text, voice, language_name):
if not text.strip():
raise gr.Error("Please enter some text.")
try:
lang_code = LANGUAGES[language_name]
style = tts.get_voice_style(voice_name=voice)
wav, duration = tts.synthesize(text, voice_style=style, lang=lang_code)
# ERROR FIX: हर ऑडियो के लिए एक यूनीक टेम्परेरी फाइल बनाना
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
output_path = temp_file.name
tts.save_audio(wav, output_path)
readable_duration = float(duration)
return output_path, f"Generation Successful! \nDuration: {readable_duration:.2f}s"
except Exception as e:
raise gr.Error(f"Generation failed: {str(e)}")
# Gradio Interface
with gr.Blocks(theme='soft', title="Supertonic 3 TTS (GPU Accelerated)") as demo:
gr.Markdown("# 🎙️ Supertonic 3: Multilingual TTS")
gr.Markdown("An on-device, lightweight Text-to-Speech system by **Supertone**. Accelerated via **ZeroGPU**.")
with gr.Row():
with gr.Column(scale=1):
input_text = gr.Textbox(
label="Input Text",
placeholder="Type your message here...",
value="The train delay was announced at 4:45 PM <breath> due to track maintenance.",
lines=4
)
with gr.Row():
voice_opt = gr.Dropdown(
choices=VOICES,
value="M1",
label="Voice Style"
)
lang_opt = gr.Dropdown(
choices=sorted(list(LANGUAGES.keys())),
value="English",
label="Language"
)
btn = gr.Button("Synthesize Speech", variant="primary")
with gr.Column(scale=1):
audio_output = gr.Audio(label="Synthesized Audio", type="filepath")
status_box = gr.Textbox(label="Status", interactive=False)
gr.HTML("""
<div style="margin-top: 20px; text-align: center;">
<p>Supported Expression Tags: <code>&lt;laugh&gt;</code>, <code>&lt;breath&gt;</code>, <code>&lt;sigh&gt;</code></p>
<a href="https://huggingface.co/Supertone/supertonic-3">
<img src="https://img.shields.io/badge/Model-Supertone%20Supertonic--3-blue?logo=huggingface" alt="Model Page">
</a>
</div>
""")
# Setup Examples
gr.Examples(
examples=[
["Hello! This is a test of the Supertonic 3 system running locally.", "M1", "English"],
["こんにちは、これは日本語の音声合成のテストです。", "F1", "Japanese"],
["¡Hola! Esta es una prueba de voz en español.", "F3", "Spanish"],
["C'est un plaisir de vous rencontrer.", "M4", "French"]
],
inputs=[input_text, voice_opt, lang_opt]
)
btn.click(
fn=generate_speech,
inputs=[input_text, voice_opt, lang_opt],
outputs=[audio_output, status_box]
)
if __name__ == "__main__":
demo.launch()