File size: 4,581 Bytes
2b29150 af2bcdb 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 11b0635 8bda912 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
import gradio
print("Installed gradio version:", gradio.__version__)
import gradio_client
print("Installed gradio_client version:", gradio_client.__version__)
import gradio_client.utils as gr_utils
def patched_get_type(schema):
# Check for a boolean schema
if isinstance(schema, bool):
return "Any" # Return a default type; adjust as needed.
# Otherwise, run the original logic.
# Note: This example assumes you can call the original if needed,
# otherwise, you might need to inline the rest of the function.
if "const" in schema:
# (Original logic here; you can copy over the code from the package.)
# For demonstration, we'll simply return a placeholder:
return "ConstType"
# You need to add the remaining conditions based on the original implementation.
return "Unknown"
# Apply the monkey patch.
gr_utils.get_type = patched_get_type
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")
|