tdurzynski's picture
Create app.py
44356f5 verified
raw
history blame
1.7 kB
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()