Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -4,65 +4,69 @@ from tiny_tts import TinyTTS
|
|
| 4 |
|
| 5 |
# Download required NLTK data for g2p-en
|
| 6 |
try:
|
| 7 |
-
nltk.download('averaged_perceptron_tagger_eng')
|
| 8 |
-
nltk.download('averaged_perceptron_tagger')
|
| 9 |
-
nltk.download('cmudict')
|
| 10 |
except Exception as e:
|
| 11 |
print(f"NLTK download warning: {e}")
|
| 12 |
|
| 13 |
-
# Initialize the model (auto-downloads if needed)
|
| 14 |
print("Initializing TinyTTS...")
|
| 15 |
tts = TinyTTS()
|
| 16 |
print("Model loaded successfully!")
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
output_path = "output.wav"
|
| 20 |
try:
|
| 21 |
-
tts.speak(text, output_path=output_path, speaker=
|
| 22 |
return output_path
|
| 23 |
except Exception as e:
|
| 24 |
return f"Error: {e}"
|
| 25 |
|
| 26 |
-
# Define available speakers
|
| 27 |
-
# Define available speakers
|
| 28 |
-
available_speakers = list(tts.model.SPK2ID.keys()) if hasattr(tts.model, "SPK2ID") else ["female"]
|
| 29 |
|
| 30 |
# Create Gradio interface
|
| 31 |
with gr.Blocks(title="TinyTTS Demo", theme=gr.themes.Soft()) as app:
|
| 32 |
gr.Markdown("# 🗣️ TinyTTS")
|
| 33 |
-
gr.Markdown(
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
with gr.Row():
|
| 37 |
with gr.Column():
|
| 38 |
text_input = gr.Textbox(
|
| 39 |
-
label="Input Text",
|
| 40 |
-
placeholder="Enter English text here...",
|
| 41 |
value="The weather is nice today, and I feel very relaxed.",
|
| 42 |
lines=4
|
| 43 |
)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
-
submit_btn = gr.Button("Synthesize Speech", variant="primary")
|
| 50 |
-
|
| 51 |
with gr.Column():
|
| 52 |
audio_output = gr.Audio(label="Output Audio", type="filepath")
|
| 53 |
-
|
| 54 |
# Example prompts
|
| 55 |
gr.Examples(
|
| 56 |
examples=[
|
| 57 |
-
["The weather is nice today, and I feel very relaxed.",
|
| 58 |
-
["TinyTTS has only
|
|
|
|
|
|
|
| 59 |
],
|
| 60 |
-
inputs=[text_input,
|
| 61 |
)
|
| 62 |
-
|
| 63 |
submit_btn.click(
|
| 64 |
fn=synthesize_audio,
|
| 65 |
-
inputs=[text_input,
|
| 66 |
outputs=audio_output
|
| 67 |
)
|
| 68 |
|
|
|
|
| 4 |
|
| 5 |
# Download required NLTK data for g2p-en
|
| 6 |
try:
|
| 7 |
+
nltk.download('averaged_perceptron_tagger_eng', quiet=True)
|
| 8 |
+
nltk.download('averaged_perceptron_tagger', quiet=True)
|
| 9 |
+
nltk.download('cmudict', quiet=True)
|
| 10 |
except Exception as e:
|
| 11 |
print(f"NLTK download warning: {e}")
|
| 12 |
|
| 13 |
+
# Initialize the model (auto-downloads from HF Hub if needed)
|
| 14 |
print("Initializing TinyTTS...")
|
| 15 |
tts = TinyTTS()
|
| 16 |
print("Model loaded successfully!")
|
| 17 |
|
| 18 |
+
|
| 19 |
+
def synthesize_audio(text, speed):
|
| 20 |
output_path = "output.wav"
|
| 21 |
try:
|
| 22 |
+
tts.speak(text, output_path=output_path, speaker="MALE", speed=speed)
|
| 23 |
return output_path
|
| 24 |
except Exception as e:
|
| 25 |
return f"Error: {e}"
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Create Gradio interface
|
| 29 |
with gr.Blocks(title="TinyTTS Demo", theme=gr.themes.Soft()) as app:
|
| 30 |
gr.Markdown("# 🗣️ TinyTTS")
|
| 31 |
+
gr.Markdown(
|
| 32 |
+
"**Ultra-lightweight English Text-to-Speech — only 1.6M parameters, ~3.4 MB ONNX**\n\n"
|
| 33 |
+
"Synthesizes high-quality 44.1kHz audio **~53× faster** than real-time on CPU."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
with gr.Row():
|
| 37 |
with gr.Column():
|
| 38 |
text_input = gr.Textbox(
|
| 39 |
+
label="Input Text",
|
| 40 |
+
placeholder="Enter English text here...",
|
| 41 |
value="The weather is nice today, and I feel very relaxed.",
|
| 42 |
lines=4
|
| 43 |
)
|
| 44 |
+
speed_slider = gr.Slider(
|
| 45 |
+
minimum=0.5,
|
| 46 |
+
maximum=2.0,
|
| 47 |
+
value=1.0,
|
| 48 |
+
step=0.1,
|
| 49 |
+
label="Speed (1.0 = normal, >1 = faster, <1 = slower)"
|
| 50 |
)
|
| 51 |
+
submit_btn = gr.Button("🔊 Synthesize Speech", variant="primary")
|
| 52 |
+
|
| 53 |
with gr.Column():
|
| 54 |
audio_output = gr.Audio(label="Output Audio", type="filepath")
|
| 55 |
+
|
| 56 |
# Example prompts
|
| 57 |
gr.Examples(
|
| 58 |
examples=[
|
| 59 |
+
["The weather is nice today, and I feel very relaxed.", 1.0],
|
| 60 |
+
["TinyTTS has only one point six million parameters, making it extremely fast on CPUs.", 1.0],
|
| 61 |
+
["This is a speed test. Speaking at one and a half times the normal rate.", 1.5],
|
| 62 |
+
["Slow and steady wins the race. Let me speak more carefully.", 0.7],
|
| 63 |
],
|
| 64 |
+
inputs=[text_input, speed_slider],
|
| 65 |
)
|
| 66 |
+
|
| 67 |
submit_btn.click(
|
| 68 |
fn=synthesize_audio,
|
| 69 |
+
inputs=[text_input, speed_slider],
|
| 70 |
outputs=audio_output
|
| 71 |
)
|
| 72 |
|