Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langdetect import detect
|
3 |
+
from gtts import gTTS
|
4 |
+
import os
|
5 |
+
|
6 |
+
def identify_and_pronounce(name):
|
7 |
+
if not name or name.strip() == "":
|
8 |
+
return "Please enter a name.", None
|
9 |
+
|
10 |
+
# Detect the language of the name
|
11 |
+
try:
|
12 |
+
lang = detect(name)
|
13 |
+
except Exception as e:
|
14 |
+
return f"Error detecting language: {str(e)}", None
|
15 |
+
|
16 |
+
# Map detected language code to a human-readable name and gTTS-compatible code
|
17 |
+
lang_map = {
|
18 |
+
'en': 'English',
|
19 |
+
'es': 'Spanish',
|
20 |
+
'fr': 'French',
|
21 |
+
'de': 'German',
|
22 |
+
'it': 'Italian',
|
23 |
+
'pt': 'Portuguese',
|
24 |
+
'nl': 'Dutch',
|
25 |
+
'ru': 'Russian',
|
26 |
+
'zh-cn': 'Chinese (Simplified)',
|
27 |
+
'ja': 'Japanese',
|
28 |
+
'ko': 'Korean',
|
29 |
+
'pl': 'Polish'
|
30 |
+
}
|
31 |
+
|
32 |
+
# Default to English if language not in map
|
33 |
+
lang_name = lang_map.get(lang, 'English (default)')
|
34 |
+
lang_code = lang if lang in lang_map else 'en' # Use detected code if supported, else 'en'
|
35 |
+
|
36 |
+
# Generate pronunciation
|
37 |
+
try:
|
38 |
+
tts = gTTS(text=name, lang=lang_code, slow=False)
|
39 |
+
audio_file = "output.mp3"
|
40 |
+
tts.save(audio_file)
|
41 |
+
return f"Detected language: {lang_name}", audio_file
|
42 |
+
except Exception as e:
|
43 |
+
return f"Error generating pronunciation: {str(e)}", None
|
44 |
+
|
45 |
+
# Gradio interface
|
46 |
+
interface = gr.Interface(
|
47 |
+
fn=identify_and_pronounce,
|
48 |
+
inputs=gr.Textbox(label="Enter a name"),
|
49 |
+
outputs=[
|
50 |
+
gr.Textbox(label="Language Detection"),
|
51 |
+
gr.Audio(label="Pronunciation")
|
52 |
+
],
|
53 |
+
title="Name Language Detector and Pronouncer",
|
54 |
+
description="Enter a name to detect its language and hear it pronounced."
|
55 |
+
)
|
56 |
+
|
57 |
+
# Launch the app
|
58 |
+
interface.launch()
|