tdurzynski commited on
Commit
ad9605e
·
verified ·
1 Parent(s): 44356f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -3,13 +3,13 @@ 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
 
@@ -26,32 +26,51 @@ def identify_and_pronounce(name):
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
 
3
  from gtts import gTTS
4
  import os
5
 
6
+ def identify_and_pronounce(name, selected_lang):
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
+ detected_lang = detect(name)
13
  except Exception as e:
14
  return f"Error detecting language: {str(e)}", None
15
 
 
26
  'zh-cn': 'Chinese (Simplified)',
27
  'ja': 'Japanese',
28
  'ko': 'Korean',
29
+ 'pl': 'Polish',
30
+ 'uk': 'Ukrainian',
31
+ 'sk': 'Slovak',
32
+ 'lt': 'Lithuanian',
33
+ 'cs': 'Czech',
34
+ 'sr': 'Serbian',
35
+ 'hr': 'Croatian',
36
+ 'hi': 'Hindi'
37
  }
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"),
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