import gradio as gr import tempfile from gtts import gTTS from gtts.lang import tts_langs # Get available languages for Google TTS google_langs = tts_langs() # Create descriptive names for all supported languages google_lang_descriptions = { "af": "Afrikaans", "ar": "Arabic", "bn": "Bengali", "bs": "Bosnian", "ca": "Catalan", "cs": "Czech", "cy": "Welsh", "da": "Danish", "de": "German", "el": "Greek", "en": "English", "eo": "Esperanto", "es": "Spanish", "et": "Estonian", "fi": "Finnish", "fr": "French", "gu": "Gujarati", "hi": "Hindi", "hr": "Croatian", "hu": "Hungarian", "id": "Indonesian", "is": "Icelandic", "it": "Italian", "ja": "Japanese", "jw": "Javanese", "kn": "Kannada", "ko": "Korean", "la": "Latin", "lv": "Latvian", "ml": "Malayalam", "mr": "Marathi", "my": "Myanmar", "ne": "Nepali", "nl": "Dutch", "no": "Norwegian", "pl": "Polish", "pt": "Portuguese", "ro": "Romanian", "ru": "Russian", "si": "Sinhala", "sk": "Slovak", "sq": "Albanian", "sr": "Serbian", "su": "Sundanese", "sv": "Swedish", "sw": "Swahili", "ta": "Tamil", "te": "Telugu", "th": "Thai", "tr": "Turkish", "uk": "Ukrainian", "ur": "Urdu", "vi": "Vietnamese", "zh-CN": "Chinese (Simplified)", "zh-TW": "Chinese (Traditional)" } # Add any missing languages dynamically from gtts.langs() and fallback to code itself if not in predefined list for lang_code in google_langs.keys(): if lang_code not in google_lang_descriptions: google_lang_descriptions[lang_code] = google_langs[lang_code] # Define voice options for English, as an example (others can be added as needed) google_voice_options = { "en": [("Australia", "com.au"), ("Canada", "ca"), ("United Kingdom", "co.uk"), ("United States", "com"), ("India", "co.in"), ("Ireland", "ie"), ("South Africa", "co.za")], # Add voice options for other languages if applicable } def google_tts(text, lang, tld=None): try: # Only include tld if it's provided (for languages like English) if tld: tts = gTTS(text=text, lang=lang, tld=tld, slow=False) else: tts = gTTS(text=text, lang=lang, slow=False) # No TLD for languages without specific variants with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio: tts.save(temp_audio.name) temp_audio_path = temp_audio.name return temp_audio_path, f"Speech generated with Google TTS using {google_lang_descriptions.get(lang, lang)} language" except Exception as e: return None, f"Error in Google TTS speech generation: {str(e)}" # Gradio interface with gr.Blocks() as iface: gr.Markdown("# Google TTS Tool") text_input = gr.Textbox(label="Enter text for speech generation") # Create dropdown for descriptive language options google_lang_input = gr.Dropdown([google_lang_descriptions[key] for key in google_langs.keys()], label="Select Language", value="English") google_voice_input = gr.Dropdown([x[0] for x in google_voice_options.get("en", [])], label="Select Voice Variant", value="United States") speech_button = gr.Button("Generate Speech") speech_output = gr.Audio(label="Generated Speech") speech_message = gr.Textbox(label="Message") def generate_speech(text, google_lang_desc, google_voice_desc): # Convert descriptive language back to its code google_lang_code = [key for key, value in google_lang_descriptions.items() if value == google_lang_desc][0] # Find the tld (country code) based on the description (if applicable) google_voice_tld = dict(google_voice_options.get(google_lang_code, [("Default", None)])).get(google_voice_desc, None) return google_tts(text, google_lang_code, google_voice_tld) def update_google_voice_options(lang_desc): # Convert descriptive language back to its code google_lang_code = [key for key, value in google_lang_descriptions.items() if value == lang_desc][0] # Update the voice dropdown with corresponding voice options (if available) return gr.Dropdown(choices=[x[0] for x in google_voice_options.get(google_lang_code, [("Default", None)])], value="Default") speech_button.click(generate_speech, inputs=[text_input, google_lang_input, google_voice_input], outputs=[speech_output, speech_message]) google_lang_input.change(update_google_voice_options, inputs=[google_lang_input], outputs=[google_voice_input]) iface.launch()