Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from TTS.api import TTS
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# Initialize TTS
|
| 7 |
-
model_name = "tts_models/en/vctk/vits"
|
| 8 |
tts = TTS(model_name)
|
| 9 |
|
| 10 |
-
# Custom speaker labels
|
| 11 |
speaker_labels = {
|
| 12 |
"p225": "Male, Young Adult",
|
| 13 |
"p226": "Female, Middle-Aged",
|
|
@@ -21,7 +21,7 @@ speaker_labels = {
|
|
| 21 |
"p234": "Female, Gentle Storyteller"
|
| 22 |
}
|
| 23 |
|
| 24 |
-
#
|
| 25 |
available_speakers = [spk for spk in tts.speakers if spk in speaker_labels]
|
| 26 |
|
| 27 |
def text_to_speech(text, speaker_name, speed, pitch):
|
|
@@ -29,106 +29,102 @@ def text_to_speech(text, speaker_name, speed, pitch):
|
|
| 29 |
if not text.strip():
|
| 30 |
raise ValueError("Please enter some text")
|
| 31 |
|
| 32 |
-
#
|
| 33 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
| 34 |
-
# Generate with adjusted speed and pitch
|
| 35 |
tts.tts_to_file(
|
| 36 |
text=text,
|
| 37 |
speaker=speaker_name,
|
| 38 |
file_path=f.name,
|
| 39 |
-
speed=speed
|
| 40 |
-
# Note: Some models may not support pitch directly
|
| 41 |
-
# We'll use a workaround for pitch adjustment
|
| 42 |
)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
raise gr.Error(f"Error generating speech: {str(e)}")
|
| 60 |
|
| 61 |
def create_download_link(audio_file):
|
| 62 |
-
if audio_file
|
| 63 |
-
return
|
| 64 |
-
return gr.
|
| 65 |
|
| 66 |
with gr.Blocks(title="Storytelling TTS App") as app:
|
| 67 |
-
gr.Markdown("# Professional Storytelling Text-to-Speech")
|
| 68 |
-
gr.Markdown("
|
| 69 |
-
|
| 70 |
with gr.Row():
|
| 71 |
with gr.Column():
|
| 72 |
-
text_input = gr.Textbox(
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
speaker = gr.Dropdown(
|
| 76 |
choices=available_speakers,
|
| 77 |
label="Narrator Voice",
|
| 78 |
-
value="p227",
|
| 79 |
format_func=lambda x: speaker_labels[x]
|
| 80 |
)
|
| 81 |
-
|
| 82 |
-
with gr.Accordion("Voice Adjustment", open=True):
|
| 83 |
speed = gr.Slider(
|
| 84 |
-
minimum=0.5, maximum=2.0,
|
| 85 |
value=1.0, step=0.1,
|
| 86 |
label="Speaking Rate",
|
| 87 |
-
info="1.0 = normal
|
| 88 |
)
|
| 89 |
pitch = gr.Slider(
|
| 90 |
-
minimum=-5.0, maximum=5.0,
|
| 91 |
value=0.0, step=0.5,
|
| 92 |
-
label="Pitch
|
| 93 |
-
info="0 = normal, positive
|
| 94 |
)
|
| 95 |
-
|
| 96 |
-
generate_btn = gr.Button("Generate Narration", variant="primary")
|
| 97 |
-
|
| 98 |
with gr.Column():
|
| 99 |
audio_output = gr.Audio(
|
| 100 |
-
label="Generated Narration",
|
| 101 |
type="filepath",
|
| 102 |
elem_classes=["output-audio"]
|
| 103 |
)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
# Set up interactivity
|
| 118 |
generate_btn.click(
|
| 119 |
fn=text_to_speech,
|
| 120 |
inputs=[text_input, speaker, speed, pitch],
|
| 121 |
outputs=audio_output
|
| 122 |
-
).then(
|
| 123 |
-
fn=lambda: gr.Group(visible=True),
|
| 124 |
-
outputs=download_section
|
| 125 |
).then(
|
| 126 |
fn=create_download_link,
|
| 127 |
inputs=audio_output,
|
| 128 |
-
outputs=
|
| 129 |
)
|
| 130 |
-
|
| 131 |
-
# Storytelling examples
|
| 132 |
gr.Examples(
|
| 133 |
examples=[
|
| 134 |
["The old man sat by the fireplace, his eyes twinkling with memories of adventures past.", "p227", 0.9, 0.0],
|
|
@@ -142,7 +138,6 @@ with gr.Blocks(title="Storytelling TTS App") as app:
|
|
| 142 |
)
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
| 145 |
-
# Install sox for pitch adjustment if not available
|
| 146 |
try:
|
| 147 |
import sox
|
| 148 |
except ImportError:
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from TTS.api import TTS
|
| 3 |
import tempfile
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Initialize TTS
|
| 7 |
+
model_name = "tts_models/en/vctk/vits"
|
| 8 |
tts = TTS(model_name)
|
| 9 |
|
| 10 |
+
# Custom speaker labels
|
| 11 |
speaker_labels = {
|
| 12 |
"p225": "Male, Young Adult",
|
| 13 |
"p226": "Female, Middle-Aged",
|
|
|
|
| 21 |
"p234": "Female, Gentle Storyteller"
|
| 22 |
}
|
| 23 |
|
| 24 |
+
# Filter available speakers
|
| 25 |
available_speakers = [spk for spk in tts.speakers if spk in speaker_labels]
|
| 26 |
|
| 27 |
def text_to_speech(text, speaker_name, speed, pitch):
|
|
|
|
| 29 |
if not text.strip():
|
| 30 |
raise ValueError("Please enter some text")
|
| 31 |
|
| 32 |
+
# Generate temporary file
|
| 33 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
|
|
|
| 34 |
tts.tts_to_file(
|
| 35 |
text=text,
|
| 36 |
speaker=speaker_name,
|
| 37 |
file_path=f.name,
|
| 38 |
+
speed=speed
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
+
output_path = f.name
|
| 41 |
+
|
| 42 |
+
# Adjust pitch using sox if needed
|
| 43 |
+
if pitch != 0.0:
|
| 44 |
+
try:
|
| 45 |
+
import sox
|
| 46 |
+
tfm = sox.Transformer()
|
| 47 |
+
tfm.pitch(pitch)
|
| 48 |
+
adjusted_file = output_path + "_adjusted.wav"
|
| 49 |
+
tfm.build_file(output_path, adjusted_file)
|
| 50 |
+
os.replace(adjusted_file, output_path)
|
| 51 |
+
except ImportError:
|
| 52 |
+
print("Sox not installed; skipping pitch adjustment.")
|
| 53 |
+
|
| 54 |
+
return output_path
|
| 55 |
+
|
| 56 |
except Exception as e:
|
| 57 |
raise gr.Error(f"Error generating speech: {str(e)}")
|
| 58 |
|
| 59 |
def create_download_link(audio_file):
|
| 60 |
+
if audio_file and os.path.exists(audio_file):
|
| 61 |
+
return gr.update(visible=True, value=audio_file)
|
| 62 |
+
return gr.update(visible=False)
|
| 63 |
|
| 64 |
with gr.Blocks(title="Storytelling TTS App") as app:
|
| 65 |
+
gr.Markdown("# 🎙️ Professional Storytelling Text-to-Speech")
|
| 66 |
+
gr.Markdown("Convert your text into narrated audio using expressive voices. Ideal for audiobooks, storytelling, and podcast narration.")
|
| 67 |
+
|
| 68 |
with gr.Row():
|
| 69 |
with gr.Column():
|
| 70 |
+
text_input = gr.Textbox(
|
| 71 |
+
label="Enter your story text",
|
| 72 |
+
lines=8,
|
| 73 |
+
placeholder="Once upon a time..."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
speaker = gr.Dropdown(
|
| 77 |
choices=available_speakers,
|
| 78 |
label="Narrator Voice",
|
| 79 |
+
value="p227",
|
| 80 |
format_func=lambda x: speaker_labels[x]
|
| 81 |
)
|
| 82 |
+
|
| 83 |
+
with gr.Accordion("🎛️ Voice Adjustment", open=True):
|
| 84 |
speed = gr.Slider(
|
| 85 |
+
minimum=0.5, maximum=2.0,
|
| 86 |
value=1.0, step=0.1,
|
| 87 |
label="Speaking Rate",
|
| 88 |
+
info="1.0 = normal speed"
|
| 89 |
)
|
| 90 |
pitch = gr.Slider(
|
| 91 |
+
minimum=-5.0, maximum=5.0,
|
| 92 |
value=0.0, step=0.5,
|
| 93 |
+
label="Pitch Shift (in semitones)",
|
| 94 |
+
info="0 = normal, positive = higher pitch"
|
| 95 |
)
|
| 96 |
+
|
| 97 |
+
generate_btn = gr.Button("🎧 Generate Narration", variant="primary")
|
| 98 |
+
|
| 99 |
with gr.Column():
|
| 100 |
audio_output = gr.Audio(
|
| 101 |
+
label="Generated Narration",
|
| 102 |
type="filepath",
|
| 103 |
elem_classes=["output-audio"]
|
| 104 |
)
|
| 105 |
+
download_button = gr.DownloadButton(
|
| 106 |
+
label="Download Audio", visible=False
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
with gr.Accordion("🎤 Preview Narrator Voices (Samples Coming Soon)", open=False):
|
| 110 |
+
gr.Markdown("Previews will be available here once sample audios are added.")
|
| 111 |
+
for speaker_id in available_speakers[:3]:
|
| 112 |
+
gr.Audio(
|
| 113 |
+
value=None,
|
| 114 |
+
label=speaker_labels[speaker_id],
|
| 115 |
+
visible=False # Set to True and provide file path or URL to enable
|
| 116 |
+
)
|
| 117 |
+
|
|
|
|
| 118 |
generate_btn.click(
|
| 119 |
fn=text_to_speech,
|
| 120 |
inputs=[text_input, speaker, speed, pitch],
|
| 121 |
outputs=audio_output
|
|
|
|
|
|
|
|
|
|
| 122 |
).then(
|
| 123 |
fn=create_download_link,
|
| 124 |
inputs=audio_output,
|
| 125 |
+
outputs=download_button
|
| 126 |
)
|
| 127 |
+
|
|
|
|
| 128 |
gr.Examples(
|
| 129 |
examples=[
|
| 130 |
["The old man sat by the fireplace, his eyes twinkling with memories of adventures past.", "p227", 0.9, 0.0],
|
|
|
|
| 138 |
)
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
|
|
|
| 141 |
try:
|
| 142 |
import sox
|
| 143 |
except ImportError:
|