Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
from langdetect import detect
|
3 |
from gtts import gTTS
|
4 |
-
import
|
5 |
|
6 |
-
# Define
|
7 |
lang_map = {
|
8 |
'en': 'English',
|
9 |
'es': 'Spanish',
|
@@ -27,38 +27,57 @@ lang_map = {
|
|
27 |
}
|
28 |
|
29 |
def identify_and_pronounce(name, selected_lang):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
if not name or name.strip() == "":
|
31 |
return "Please enter a name.", None
|
32 |
-
|
33 |
-
#
|
34 |
try:
|
35 |
detected_lang = detect(name)
|
36 |
except Exception as e:
|
37 |
return f"Error detecting language: {str(e)}", None
|
38 |
-
|
39 |
-
#
|
40 |
detected_lang_name = lang_map.get(detected_lang, 'English (default)')
|
|
|
41 |
detected_lang_code = detected_lang if detected_lang in lang_map else 'en'
|
42 |
|
43 |
-
# Use
|
44 |
if selected_lang != "Auto" and selected_lang in lang_map.values():
|
45 |
lang_name = selected_lang
|
|
|
46 |
lang_code = [code for code, name in lang_map.items() if name == selected_lang][0]
|
47 |
else:
|
48 |
lang_name = detected_lang_name
|
49 |
lang_code = detected_lang_code
|
50 |
|
51 |
-
# Generate pronunciation
|
52 |
try:
|
53 |
tts = gTTS(text=name, lang=lang_code, slow=False)
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
except Exception as e:
|
58 |
return f"Error generating pronunciation: {str(e)}", None
|
59 |
|
60 |
-
#
|
61 |
-
language_options = ["Auto"] + list(lang_map.values())
|
|
|
|
|
|
|
62 |
interface = gr.Interface(
|
63 |
fn=identify_and_pronounce,
|
64 |
inputs=[
|
@@ -67,11 +86,13 @@ interface = gr.Interface(
|
|
67 |
],
|
68 |
outputs=[
|
69 |
gr.Textbox(label="Language Info"),
|
70 |
-
gr.Audio(label="Pronunciation")
|
71 |
],
|
72 |
title="Name Language Detector and Pronouncer",
|
73 |
-
description="Enter a name to detect its language and hear it pronounced.
|
|
|
74 |
)
|
75 |
|
76 |
-
# Launch the app
|
77 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
from langdetect import detect
|
3 |
from gtts import gTTS
|
4 |
+
import tempfile
|
5 |
|
6 |
+
# Define a mapping from language codes to language names.
|
7 |
lang_map = {
|
8 |
'en': 'English',
|
9 |
'es': 'Spanish',
|
|
|
27 |
}
|
28 |
|
29 |
def identify_and_pronounce(name, selected_lang):
|
30 |
+
"""
|
31 |
+
This function detects the language of the given name and generates its pronunciation using gTTS.
|
32 |
+
|
33 |
+
Parameters:
|
34 |
+
name (str): The name input provided by the user.
|
35 |
+
selected_lang (str): Either 'Auto' (to use detected language) or a specific language name to override.
|
36 |
+
|
37 |
+
Returns:
|
38 |
+
tuple: A status message string and the path to the generated audio file.
|
39 |
+
"""
|
40 |
+
# Check for empty or whitespace-only name input.
|
41 |
if not name or name.strip() == "":
|
42 |
return "Please enter a name.", None
|
43 |
+
|
44 |
+
# Attempt to detect the language of the input name.
|
45 |
try:
|
46 |
detected_lang = detect(name)
|
47 |
except Exception as e:
|
48 |
return f"Error detecting language: {str(e)}", None
|
49 |
+
|
50 |
+
# Get the human-readable language name for the detected code.
|
51 |
detected_lang_name = lang_map.get(detected_lang, 'English (default)')
|
52 |
+
# Use English as default if detected language code is not in our mapping.
|
53 |
detected_lang_code = detected_lang if detected_lang in lang_map else 'en'
|
54 |
|
55 |
+
# Use the provided override language if it's not "Auto"
|
56 |
if selected_lang != "Auto" and selected_lang in lang_map.values():
|
57 |
lang_name = selected_lang
|
58 |
+
# Get the corresponding language code for the selected language.
|
59 |
lang_code = [code for code, name in lang_map.items() if name == selected_lang][0]
|
60 |
else:
|
61 |
lang_name = detected_lang_name
|
62 |
lang_code = detected_lang_code
|
63 |
|
64 |
+
# Generate pronunciation audio using gTTS.
|
65 |
try:
|
66 |
tts = gTTS(text=name, lang=lang_code, slow=False)
|
67 |
+
# Create a temporary file in the allowed directory.
|
68 |
+
# delete=False ensures the file persists after closing so that Gradio can serve it.
|
69 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
70 |
+
tts.save(temp_file.name) # Save the TTS output to the temporary file.
|
71 |
+
temp_file.close() # Close the file handle.
|
72 |
+
return f"Detected language: {detected_lang_name}\nPronounced in: {lang_name}", temp_file.name
|
73 |
except Exception as e:
|
74 |
return f"Error generating pronunciation: {str(e)}", None
|
75 |
|
76 |
+
# Prepare language options for the dropdown input.
|
77 |
+
language_options = ["Auto"] + list(lang_map.values())
|
78 |
+
|
79 |
+
# Create a Gradio Interface.
|
80 |
+
# Note: Specify `type="filepath"` for gr.Audio so that it expects a file path.
|
81 |
interface = gr.Interface(
|
82 |
fn=identify_and_pronounce,
|
83 |
inputs=[
|
|
|
86 |
],
|
87 |
outputs=[
|
88 |
gr.Textbox(label="Language Info"),
|
89 |
+
gr.Audio(label="Pronunciation", type="filepath")
|
90 |
],
|
91 |
title="Name Language Detector and Pronouncer",
|
92 |
+
description=("Enter a name to detect its language and hear it pronounced. "
|
93 |
+
"Optionally, select a language to override the default.")
|
94 |
)
|
95 |
|
96 |
+
# Launch the app.
|
97 |
+
# Bind to 0.0.0.0 so that HF Spaces can properly route requests to your app.
|
98 |
+
interface.launch(server_name="0.0.0.0")
|