|
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 |
|
|
|
|
|
try: |
|
lang = detect(name) |
|
except Exception as e: |
|
return f"Error detecting language: {str(e)}", None |
|
|
|
|
|
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' |
|
} |
|
|
|
|
|
lang_name = lang_map.get(lang, 'English (default)') |
|
lang_code = lang if lang in lang_map else 'en' |
|
|
|
|
|
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 |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
interface.launch() |