Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,36 +17,41 @@ gtts_languages = {
|
|
| 17 |
}
|
| 18 |
|
| 19 |
# The main function is now SYNCHRONOUS (no 'async')
|
| 20 |
-
def text_to_speech_gtts(text, language_name):
|
| 21 |
# 1. Input Validation
|
| 22 |
if not text or not text.strip():
|
| 23 |
return "ERROR: Input text cannot be empty.", None
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
# Get the language code from the name
|
| 27 |
lang_code = gtts_languages[language_name]
|
| 28 |
|
| 29 |
# Create a temporary file path
|
| 30 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 31 |
tmp_path = tmp_file.name
|
| 32 |
|
| 33 |
-
# Initialize gTTS object
|
| 34 |
-
tts = gTTS(text=text, lang=lang_code, slow=
|
| 35 |
|
| 36 |
-
# Save the audio file (synchronous operation)
|
| 37 |
tts.save(tmp_path)
|
| 38 |
|
| 39 |
return "Speech synthesis complete: {}".format(text), tmp_path
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
# Handle all gTTS-related errors (e.g., language not supported, network failure)
|
| 43 |
-
return f"ERROR: Failed to generate audio. Details: {str(e)}", None
|
| 44 |
|
| 45 |
|
| 46 |
# --- Modern Gradio Component Syntax (Translated UI) ---
|
| 47 |
input_text = gr.Textbox(lines=5, label="Input Text")
|
| 48 |
output_text = gr.Textbox(label="Output Text")
|
| 49 |
-
output_audio = gr.Audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
default_language = "English" # Use English as the default language
|
| 52 |
language = gr.Dropdown(
|
|
@@ -59,12 +64,17 @@ language = gr.Dropdown(
|
|
| 59 |
# --- Gradio Interface Definition ---
|
| 60 |
interface = gr.Interface(
|
| 61 |
fn=text_to_speech_gtts, # Use the new synchronous function
|
| 62 |
-
inputs=[input_text, language],
|
| 63 |
-
outputs=[output_text, output_audio],
|
| 64 |
title="Google TTS Text-to-Speech (Simple Version)",
|
| 65 |
description="Convert text into audio using the reliable Google Text-to-Speech service. (Max 100 characters for optimal stability)"
|
| 66 |
)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# --- Standard Synchronous Launch Command ---
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
# The main function is now SYNCHRONOUS (no 'async')
|
| 20 |
+
def text_to_speech_gtts(text, language_name, slow_speed): # ADDED slow_speed
|
| 21 |
# 1. Input Validation
|
| 22 |
if not text or not text.strip():
|
| 23 |
return "ERROR: Input text cannot be empty.", None
|
| 24 |
|
| 25 |
try:
|
| 26 |
+
# Get the language code from the name
|
| 27 |
lang_code = gtts_languages[language_name]
|
| 28 |
|
| 29 |
# Create a temporary file path
|
| 30 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 31 |
tmp_path = tmp_file.name
|
| 32 |
|
| 33 |
+
# Initialize gTTS object - USE slow_speed from input
|
| 34 |
+
# tts = gTTS(text=text, lang=lang_code, slow=slow_speed)
|
| 35 |
|
| 36 |
+
# Save the audio file (synchronous operation) [cite: 4]
|
| 37 |
tts.save(tmp_path)
|
| 38 |
|
| 39 |
return "Speech synthesis complete: {}".format(text), tmp_path
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
# Handle all gTTS-related errors (e.g., language not supported, network failure)
|
| 43 |
+
return f"ERROR: Failed to generate audio. Details: {str(e)}", None [cite: 5]
|
| 44 |
|
| 45 |
|
| 46 |
# --- Modern Gradio Component Syntax (Translated UI) ---
|
| 47 |
input_text = gr.Textbox(lines=5, label="Input Text")
|
| 48 |
output_text = gr.Textbox(label="Output Text")
|
| 49 |
+
output_audio = gr.Audio(
|
| 50 |
+
type="filepath",
|
| 51 |
+
label="Generated Audio File",
|
| 52 |
+
interactive=True, # Make the component interactive
|
| 53 |
+
live=False # This ensures the file is available for download after generation
|
| 54 |
+
)
|
| 55 |
|
| 56 |
default_language = "English" # Use English as the default language
|
| 57 |
language = gr.Dropdown(
|
|
|
|
| 64 |
# --- Gradio Interface Definition ---
|
| 65 |
interface = gr.Interface(
|
| 66 |
fn=text_to_speech_gtts, # Use the new synchronous function
|
| 67 |
+
inputs=[input_text, language, speech_speed], # ADDED speech_speed
|
| 68 |
+
outputs=[output_text, output_audio],
|
| 69 |
title="Google TTS Text-to-Speech (Simple Version)",
|
| 70 |
description="Convert text into audio using the reliable Google Text-to-Speech service. (Max 100 characters for optimal stability)"
|
| 71 |
)
|
| 72 |
|
| 73 |
+
speech_speed = gr.Radio(
|
| 74 |
+
choices=[("Normal", False), ("Slow", True)], # Custom labels and corresponding boolean values
|
| 75 |
+
value=False, # Default to Normal speed
|
| 76 |
+
label="Speech Speed"
|
| 77 |
+
)
|
| 78 |
|
| 79 |
# --- Standard Synchronous Launch Command ---
|
| 80 |
if __name__ == "__main__":
|