File size: 1,702 Bytes
44356f5 |
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 |
import gradio as gr
from langdetect import detect
from gtts import gTTS
import os
def identify_and_pronounce(name):
if not name or name.strip() == "":
return "Please enter a name.", None
# Detect the language of the name
try:
lang = detect(name)
except Exception as e:
return f"Error detecting language: {str(e)}", None
# Map detected language code to a human-readable name and gTTS-compatible code
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'
}
# Default to English if language not in map
lang_name = lang_map.get(lang, 'English (default)')
lang_code = lang if lang in lang_map else 'en' # Use detected code if supported, else 'en'
# Generate pronunciation
try:
tts = gTTS(text=name, lang=lang_code, slow=False)
audio_file = "output.mp3"
tts.save(audio_file)
return f"Detected language: {lang_name}", audio_file
except Exception as e:
return f"Error generating pronunciation: {str(e)}", None
# Gradio interface
interface = gr.Interface(
fn=identify_and_pronounce,
inputs=gr.Textbox(label="Enter a name"),
outputs=[
gr.Textbox(label="Language Detection"),
gr.Audio(label="Pronunciation")
],
title="Name Language Detector and Pronouncer",
description="Enter a name to detect its language and hear it pronounced."
)
# Launch the app
interface.launch() |