Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,11 @@ import os
|
|
5 |
import tempfile
|
6 |
|
7 |
def text_to_speech(text, language, pitch):
|
|
|
|
|
|
|
|
|
|
|
8 |
tts = gTTS(text=text, lang=language, slow=False)
|
9 |
|
10 |
# Save to a temporary file
|
@@ -22,28 +27,32 @@ def text_to_speech(text, language, pitch):
|
|
22 |
return output_file
|
23 |
|
24 |
def gradio_tts_interface(text, language, pitch):
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
-
iface = gr.Blocks(
|
29 |
|
30 |
with iface:
|
31 |
-
gr.Markdown("# Text-to-Speech Demo")
|
32 |
|
33 |
with gr.Row():
|
34 |
with gr.Column():
|
35 |
text_input = gr.Textbox(label="Enter text to convert to speech", lines=3)
|
36 |
-
language_input = gr.Dropdown(["en", "fr", "es", "de", "it"
|
37 |
pitch_input = gr.Slider(minimum=0.5, maximum=2.0, value=1.0, step=0.1, label="Pitch (0.5 for lower/male, 1.0 for normal, 2.0 for higher/female)")
|
38 |
submit_button = gr.Button("Convert to Speech")
|
39 |
|
40 |
with gr.Column():
|
41 |
audio_output = gr.Audio(label="Generated Speech")
|
|
|
42 |
|
43 |
submit_button.click(
|
44 |
fn=gradio_tts_interface,
|
45 |
inputs=[text_input, language_input, pitch_input],
|
46 |
-
outputs=audio_output
|
47 |
)
|
48 |
|
49 |
iface.launch()
|
|
|
5 |
import tempfile
|
6 |
|
7 |
def text_to_speech(text, language, pitch):
|
8 |
+
if not text:
|
9 |
+
raise ValueError("Please enter some text to convert to speech.")
|
10 |
+
|
11 |
+
language = language or "en" # Default to English if no language is selected
|
12 |
+
|
13 |
tts = gTTS(text=text, lang=language, slow=False)
|
14 |
|
15 |
# Save to a temporary file
|
|
|
27 |
return output_file
|
28 |
|
29 |
def gradio_tts_interface(text, language, pitch):
|
30 |
+
try:
|
31 |
+
audio_file = text_to_speech(text, language, pitch)
|
32 |
+
return audio_file, None # Return audio file and no error message
|
33 |
+
except Exception as e:
|
34 |
+
return None, str(e) # Return no audio file and the error message
|
35 |
|
36 |
+
iface = gr.Blocks()
|
37 |
|
38 |
with iface:
|
39 |
+
gr.Markdown("# Text-to-Speech Demo with Pitch Control")
|
40 |
|
41 |
with gr.Row():
|
42 |
with gr.Column():
|
43 |
text_input = gr.Textbox(label="Enter text to convert to speech", lines=3)
|
44 |
+
language_input = gr.Dropdown(["en", "fr", "es", "de", "it"], label="Select Language", value="en")
|
45 |
pitch_input = gr.Slider(minimum=0.5, maximum=2.0, value=1.0, step=0.1, label="Pitch (0.5 for lower/male, 1.0 for normal, 2.0 for higher/female)")
|
46 |
submit_button = gr.Button("Convert to Speech")
|
47 |
|
48 |
with gr.Column():
|
49 |
audio_output = gr.Audio(label="Generated Speech")
|
50 |
+
error_output = gr.Textbox(label="Error (if any)", visible=True)
|
51 |
|
52 |
submit_button.click(
|
53 |
fn=gradio_tts_interface,
|
54 |
inputs=[text_input, language_input, pitch_input],
|
55 |
+
outputs=[audio_output, error_output]
|
56 |
)
|
57 |
|
58 |
iface.launch()
|