Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langdetect import detect
|
| 3 |
+
from gtts import gTTS
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Define lang_map in global scope
|
| 7 |
+
lang_map = {
|
| 8 |
+
'en': 'English',
|
| 9 |
+
'es': 'Spanish',
|
| 10 |
+
'fr': 'French',
|
| 11 |
+
'de': 'German',
|
| 12 |
+
'it': 'Italian',
|
| 13 |
+
'pt': 'Portuguese',
|
| 14 |
+
'nl': 'Dutch',
|
| 15 |
+
'ru': 'Russian',
|
| 16 |
+
'zh-cn': 'Chinese (Simplified)',
|
| 17 |
+
'ja': 'Japanese',
|
| 18 |
+
'ko': 'Korean',
|
| 19 |
+
'pl': 'Polish',
|
| 20 |
+
'uk': 'Ukrainian',
|
| 21 |
+
'sk': 'Slovak',
|
| 22 |
+
'lt': 'Lithuanian',
|
| 23 |
+
'cs': 'Czech',
|
| 24 |
+
'sr': 'Serbian',
|
| 25 |
+
'hr': 'Croatian',
|
| 26 |
+
'hi': 'Hindi'
|
| 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 |
+
# Detect the language of the name
|
| 34 |
+
try:
|
| 35 |
+
detected_lang = detect(name)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error detecting language: {str(e)}", None
|
| 38 |
+
|
| 39 |
+
# If detected language isn't in map, use English as default but allow override
|
| 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 selected language if provided and not "Auto", otherwise use detected language
|
| 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 |
+
audio_file = "output.mp3"
|
| 55 |
+
tts.save(audio_file)
|
| 56 |
+
return f"Detected language: {detected_lang_name}\nPronounced in: {lang_name}", audio_file
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return f"Error generating pronunciation: {str(e)}", None
|
| 59 |
+
|
| 60 |
+
# Gradio interface
|
| 61 |
+
language_options = ["Auto"] + list(lang_map.values()) # Include "Auto" as default option
|
| 62 |
+
interface = gr.Interface(
|
| 63 |
+
fn=identify_and_pronounce,
|
| 64 |
+
inputs=[
|
| 65 |
+
gr.Textbox(label="Enter a name", value="Tomasz Durzyński"),
|
| 66 |
+
gr.Dropdown(choices=language_options, label="Select Language (Auto uses detection)", value="Auto")
|
| 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. Optionally, select a language to override the default."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Launch the app
|
| 77 |
+
interface.launch()
|