Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from tiny_tts import TinyTTS
|
| 3 |
+
|
| 4 |
+
# Initialize the model (auto-downloads if needed)
|
| 5 |
+
print("Initializing TinyTTS...")
|
| 6 |
+
tts = TinyTTS()
|
| 7 |
+
print("Model loaded successfully!")
|
| 8 |
+
|
| 9 |
+
def synthesize_audio(text, speaker):
|
| 10 |
+
output_path = "output.wav"
|
| 11 |
+
try:
|
| 12 |
+
tts.speak(text, output_path=output_path, speaker=speaker)
|
| 13 |
+
return output_path
|
| 14 |
+
except Exception as e:
|
| 15 |
+
return f"Error: {e}"
|
| 16 |
+
|
| 17 |
+
# Define available speakers
|
| 18 |
+
# TinyTTS supports 'LJ' and potentially others based on the checkpoint
|
| 19 |
+
available_speakers = list(tts.model.SPK2ID.keys()) if hasattr(tts.model, "SPK2ID") else ["LJ", "female", "male"]
|
| 20 |
+
|
| 21 |
+
# Create Gradio interface
|
| 22 |
+
with gr.Blocks(title="TinyTTS Demo", theme=gr.themes.Soft()) as app:
|
| 23 |
+
gr.Markdown("# 🗣️ TinyTTS")
|
| 24 |
+
gr.Markdown("**Ultra-lightweight English Text-to-Speech (~9M parameters, ~20MB on disk)**")
|
| 25 |
+
gr.Markdown("This space runs on CPU efficiently and synthesizes high-quality audio faster than real-time.")
|
| 26 |
+
|
| 27 |
+
with gr.Row():
|
| 28 |
+
with gr.Column():
|
| 29 |
+
text_input = gr.Textbox(
|
| 30 |
+
label="Input Text",
|
| 31 |
+
placeholder="Enter English text here...",
|
| 32 |
+
value="The weather is nice today, and I feel very relaxed.",
|
| 33 |
+
lines=4
|
| 34 |
+
)
|
| 35 |
+
speaker_dropdown = gr.Dropdown(
|
| 36 |
+
choices=available_speakers,
|
| 37 |
+
value="female" if "female" in available_speakers else available_speakers[0],
|
| 38 |
+
label="Speaker"
|
| 39 |
+
)
|
| 40 |
+
submit_btn = gr.Button("Synthesize Speech", variant="primary")
|
| 41 |
+
|
| 42 |
+
with gr.Column():
|
| 43 |
+
audio_output = gr.Audio(label="Output Audio", type="filepath")
|
| 44 |
+
|
| 45 |
+
# Example prompts
|
| 46 |
+
gr.Examples(
|
| 47 |
+
examples=[
|
| 48 |
+
["The weather is nice today, and I feel very relaxed.", "female"],
|
| 49 |
+
["Hello there! I am an ultra-lightweight text to speech system.", "LJ"],
|
| 50 |
+
["TinyTTS has only nine million parameters, making it extremely fast on CPUs.", "female"],
|
| 51 |
+
],
|
| 52 |
+
inputs=[text_input, speaker_dropdown],
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
submit_btn.click(
|
| 56 |
+
fn=synthesize_audio,
|
| 57 |
+
inputs=[text_input, speaker_dropdown],
|
| 58 |
+
outputs=audio_output
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
app.launch(server_name="0.0.0.0")
|