import gradio as gr from langdetect import detect from gtts import gTTS import tempfile # Define a mapping from language codes to language names. lang_map = { 'en': 'English', 'es': 'Spanish', 'fr': 'French', 'de': 'German', 'it': 'Italian', 'pt': 'Portuguese', 'nl': 'Dutch', 'ru': 'Russian', 'zh-cn': 'Chinese (Simplified)', 'ja': 'Japanese', 'ko': 'Korean', 'pl': 'Polish', 'uk': 'Ukrainian', 'sk': 'Slovak', 'lt': 'Lithuanian', 'cs': 'Czech', 'sr': 'Serbian', 'hr': 'Croatian', 'hi': 'Hindi' } def identify_and_pronounce(name, selected_lang): """ This function detects the language of the given name and generates its pronunciation using gTTS. Parameters: name (str): The name input provided by the user. selected_lang (str): Either 'Auto' (to use detected language) or a specific language name to override. Returns: tuple: A status message string and the path to the generated audio file. """ # Check for empty or whitespace-only name input. if not name or name.strip() == "": return "Please enter a name.", None # Attempt to detect the language of the input name. try: detected_lang = detect(name) except Exception as e: return f"Error detecting language: {str(e)}", None # Get the human-readable language name for the detected code. detected_lang_name = lang_map.get(detected_lang, 'English (default)') # Use English as default if detected language code is not in our mapping. detected_lang_code = detected_lang if detected_lang in lang_map else 'en' # Use the provided override language if it's not "Auto" if selected_lang != "Auto" and selected_lang in lang_map.values(): lang_name = selected_lang # Get the corresponding language code for the selected language. lang_code = [code for code, name in lang_map.items() if name == selected_lang][0] else: lang_name = detected_lang_name lang_code = detected_lang_code # Generate pronunciation audio using gTTS. try: tts = gTTS(text=name, lang=lang_code, slow=False) # Create a temporary file in the allowed directory. # delete=False ensures the file persists after closing so that Gradio can serve it. temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") tts.save(temp_file.name) # Save the TTS output to the temporary file. temp_file.close() # Close the file handle. return f"Detected language: {detected_lang_name}\nPronounced in: {lang_name}", temp_file.name except Exception as e: return f"Error generating pronunciation: {str(e)}", None # Prepare language options for the dropdown input. language_options = ["Auto"] + list(lang_map.values()) # Create a Gradio Interface. # Note: Specify `type="filepath"` for gr.Audio so that it expects a file path. interface = gr.Interface( fn=identify_and_pronounce, inputs=[ gr.Textbox(label="Enter a name", value="Tomasz DurzyƄski"), gr.Dropdown(choices=language_options, label="Select Language (Auto uses detection)", value="Auto") ], outputs=[ gr.Textbox(label="Language Info"), gr.Audio(label="Pronunciation", type="filepath") ], title="Name Language Detector and Pronouncer", description=("Enter a name to detect its language and hear it pronounced. " "Optionally, select a language to override the default.") ) # Launch the app. # Bind to 0.0.0.0 so that HF Spaces can properly route requests to your app. interface.launch(server_name="0.0.0.0")