Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from TTS.api import TTS
|
| 3 |
import tempfile
|
|
|
|
| 4 |
|
| 5 |
# Load multilingual TTS model (supports English and Arabic, with voice options)
|
| 6 |
model_name = "tts_models/multilingual/multi-dataset/your_tts"
|
|
@@ -9,26 +10,86 @@ tts = TTS(model_name)
|
|
| 9 |
# Get available speakers from model
|
| 10 |
available_speakers = tts.speakers
|
| 11 |
|
| 12 |
-
def text_to_speech(text, language, speaker_name):
|
| 13 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Save to temporary WAV file
|
| 15 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
| 16 |
-
|
|
|
|
| 17 |
return f.name
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from TTS.api import TTS
|
| 3 |
import tempfile
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
# Load multilingual TTS model (supports English and Arabic, with voice options)
|
| 7 |
model_name = "tts_models/multilingual/multi-dataset/your_tts"
|
|
|
|
| 10 |
# Get available speakers from model
|
| 11 |
available_speakers = tts.speakers
|
| 12 |
|
| 13 |
+
def text_to_speech(text, language, speaker_name, speed, pitch):
|
| 14 |
try:
|
| 15 |
+
# Validate inputs
|
| 16 |
+
if not text.strip():
|
| 17 |
+
raise ValueError("Please enter some text")
|
| 18 |
+
|
| 19 |
+
# Create parameters dictionary with optional parameters
|
| 20 |
+
params = {
|
| 21 |
+
"text": text,
|
| 22 |
+
"speaker": speaker_name,
|
| 23 |
+
"language": language,
|
| 24 |
+
"file_path": None # We'll handle the file ourselves
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Add optional parameters if they're not default values
|
| 28 |
+
if speed != 1.0:
|
| 29 |
+
params["speed"] = speed
|
| 30 |
+
if pitch != 1.0:
|
| 31 |
+
params["pitch"] = pitch
|
| 32 |
+
|
| 33 |
# Save to temporary WAV file
|
| 34 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
|
| 35 |
+
params["file_path"] = f.name
|
| 36 |
+
tts.tts_to_file(**params)
|
| 37 |
return f.name
|
| 38 |
+
|
| 39 |
except Exception as e:
|
| 40 |
+
raise gr.Error(f"Error generating speech: {str(e)}")
|
| 41 |
|
| 42 |
+
def create_download_link(audio_file):
|
| 43 |
+
if audio_file is None or not os.path.exists(audio_file):
|
| 44 |
+
return None
|
| 45 |
+
return gr.DownloadButton(label="Download Audio", value=audio_file)
|
| 46 |
+
|
| 47 |
+
with gr.Blocks(title="Enhanced TTS App") as app:
|
| 48 |
+
gr.Markdown("# Enhanced English & Arabic Text-to-Speech")
|
| 49 |
+
gr.Markdown("Type your text, adjust settings, and generate speech!")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column():
|
| 53 |
+
text_input = gr.Textbox(label="Enter text (English or Arabic)", lines=5)
|
| 54 |
+
language = gr.Dropdown(choices=["en", "ar"], label="Language", value="en")
|
| 55 |
+
speaker = gr.Dropdown(choices=available_speakers, label="Voice (Male/Female)")
|
| 56 |
+
|
| 57 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 58 |
+
speed = gr.Slider(minimum=0.5, maximum=2.0, value=1.0, step=0.1,
|
| 59 |
+
label="Speed (1.0 = normal)")
|
| 60 |
+
pitch = gr.Slider(minimum=0.5, maximum=2.0, value=1.0, step=0.1,
|
| 61 |
+
label="Pitch (1.0 = normal)")
|
| 62 |
+
|
| 63 |
+
generate_btn = gr.Button("Generate Speech", variant="primary")
|
| 64 |
+
|
| 65 |
+
with gr.Column():
|
| 66 |
+
audio_output = gr.Audio(label="Generated Audio", type="filepath")
|
| 67 |
+
download_section = gr.Group(visible=False)
|
| 68 |
+
|
| 69 |
+
# Set up interactivity
|
| 70 |
+
generate_btn.click(
|
| 71 |
+
fn=text_to_speech,
|
| 72 |
+
inputs=[text_input, language, speaker, speed, pitch],
|
| 73 |
+
outputs=audio_output
|
| 74 |
+
).then(
|
| 75 |
+
fn=lambda: gr.Group(visible=True),
|
| 76 |
+
outputs=download_section
|
| 77 |
+
).then(
|
| 78 |
+
fn=create_download_link,
|
| 79 |
+
inputs=audio_output,
|
| 80 |
+
outputs=download_section
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
gr.Examples(
|
| 84 |
+
examples=[
|
| 85 |
+
["Hello, welcome to our text-to-speech application!", "en", available_speakers[0], 1.0, 1.0],
|
| 86 |
+
["مرحبا بكم في تطبيق تحويل النص إلى كلام", "ar", available_speakers[-1], 1.0, 1.0]
|
| 87 |
+
],
|
| 88 |
+
inputs=[text_input, language, speaker, speed, pitch],
|
| 89 |
+
outputs=audio_output,
|
| 90 |
+
fn=text_to_speech,
|
| 91 |
+
cache_examples=True
|
| 92 |
+
)
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
+
app.launch()
|