Spaces:
Running
Running
| import gradio as gr | |
| from gtts import gTTS | |
| import tempfile | |
| import os | |
| # Define a simple dictionary of available languages for gTTS | |
| # We can't use the massive list from tts_voice.py because gTTS uses simple language codes. | |
| gtts_languages = { | |
| "English": "en", | |
| "Mandarin Chinese": "zh-cn", | |
| "French": "fr", | |
| "German": "de", | |
| "Spanish": "es", | |
| "Japanese": "ja", | |
| "Korean": "ko", | |
| "Russian": "ru" | |
| } | |
| # The main function is now SYNCHRONOUS (no 'async') | |
| def text_to_speech_gtts(text, language_name, slow_speed): # ADDED slow_speed | |
| # 1. Input Validation | |
| if not text or not text.strip(): | |
| return "ERROR: Input text cannot be empty.", None | |
| try: | |
| # Get the language code from the name | |
| lang_code = gtts_languages[language_name] | |
| # Create a temporary file path | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: | |
| tmp_path = tmp_file.name | |
| # Initialize gTTS object - USE slow_speed from input | |
| tts = gTTS(text=text, lang=lang_code, slow=slow_speed) | |
| # Save the audio file (synchronous operation) [cite: 4] | |
| tts.save(tmp_path) | |
| return "Speech synthesis complete: {}".format(text), tmp_path | |
| except Exception as e: | |
| # Handle all gTTS-related errors (e.g., language not supported, network failure) | |
| return (f"ERROR: Failed to generate audio. Details: {str(e)}", None) | |
| # --- Modern Gradio Component Syntax (Translated UI) --- | |
| input_text = gr.Textbox(lines=5, label="Input Text") | |
| speech_speed = gr.Radio( | |
| choices=[("Normal", False), ("Slow", True)], # Custom labels and corresponding boolean values | |
| value=False, # Default to Normal speed | |
| label="Speech Speed" | |
| ) | |
| output_text = gr.Textbox(label="Output Text") | |
| output_audio = gr.Audio( | |
| type="filepath", | |
| label="Generated Audio File", | |
| interactive=True # The file will still be available for download without 'live=False' | |
| ) | |
| default_language = "English" # Use English as the default language | |
| language = gr.Dropdown( | |
| choices=list(gtts_languages.keys()), | |
| value=default_language, | |
| label="Language" # Dropped 'Voice' because gTTS has no specific voice selection | |
| ) | |
| # --- Gradio Interface Definition --- | |
| interface = gr.Interface( | |
| fn=text_to_speech_gtts, # Use the new synchronous function | |
| inputs=[input_text, language, speech_speed], # ADDED speech_speed | |
| outputs=[output_text, output_audio], | |
| title="Google TTS Text-to-Speech (Simple Version)", | |
| description="Convert text into audio using the reliable Google Text-to-Speech service. (Max 100 characters for optimal stability)" | |
| ) | |
| # --- Standard Synchronous Launch Command --- | |
| if __name__ == "__main__": | |
| # Gradio runs synchronous functions reliably without extra configuration. | |
| interface.launch() |