Spaces:
Running
Running
capradeepgujaran
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,31 +6,70 @@ from gtts.lang import tts_langs
|
|
6 |
# Get available languages for Google TTS
|
7 |
google_langs = tts_langs()
|
8 |
|
9 |
-
#
|
10 |
google_lang_descriptions = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
"en": "English",
|
|
|
12 |
"es": "Spanish",
|
|
|
|
|
13 |
"fr": "French",
|
14 |
-
"
|
|
|
|
|
|
|
|
|
|
|
15 |
"it": "Italian",
|
16 |
"ja": "Japanese",
|
|
|
|
|
17 |
"ko": "Korean",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
"pt": "Portuguese",
|
19 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
}
|
21 |
|
22 |
-
# Define voice options
|
23 |
google_voice_options = {
|
24 |
"en": [("Australia", "com.au"), ("Canada", "ca"), ("United Kingdom", "co.uk"),
|
25 |
("United States", "com"), ("India", "co.in"), ("Ireland", "ie"), ("South Africa", "co.za")],
|
26 |
-
|
27 |
-
"fr": [("France", "fr"), ("Canada", "ca")],
|
28 |
-
"de": [("Germany", "de"), ("Austria", "at")],
|
29 |
-
"it": [("Italy", "it"), ("United States", "com")],
|
30 |
-
"ja": [("Japan", "jp")],
|
31 |
-
"ko": [("Korea", "com")],
|
32 |
-
"pt": [("Brazil", "com.br"), ("Portugal", "pt")],
|
33 |
-
"zh": [("China", "cn"), ("Taiwan", "tw")]
|
34 |
}
|
35 |
|
36 |
def google_tts(text, lang, tld):
|
@@ -41,7 +80,7 @@ def google_tts(text, lang, tld):
|
|
41 |
tts.save(temp_audio.name)
|
42 |
temp_audio_path = temp_audio.name
|
43 |
|
44 |
-
return temp_audio_path, f"Speech generated with Google TTS using {google_lang_descriptions
|
45 |
except Exception as e:
|
46 |
return None, f"Error in Google TTS speech generation: {str(e)}"
|
47 |
|
@@ -52,8 +91,8 @@ with gr.Blocks() as iface:
|
|
52 |
text_input = gr.Textbox(label="Enter text for speech generation")
|
53 |
|
54 |
# Create dropdown for descriptive language options
|
55 |
-
google_lang_input = gr.Dropdown([google_lang_descriptions[key] for key in
|
56 |
-
google_voice_input = gr.Dropdown([x[0] for x in google_voice_options
|
57 |
|
58 |
speech_button = gr.Button("Generate Speech")
|
59 |
speech_output = gr.Audio(label="Generated Speech")
|
@@ -62,15 +101,15 @@ with gr.Blocks() as iface:
|
|
62 |
def generate_speech(text, google_lang_desc, google_voice_desc):
|
63 |
# Convert descriptive language back to its code
|
64 |
google_lang_code = [key for key, value in google_lang_descriptions.items() if value == google_lang_desc][0]
|
65 |
-
# Find the tld (country code) based on the description
|
66 |
-
google_voice_tld = dict(google_voice_options[
|
67 |
return google_tts(text, google_lang_code, google_voice_tld)
|
68 |
|
69 |
def update_google_voice_options(lang_desc):
|
70 |
# Convert descriptive language back to its code
|
71 |
google_lang_code = [key for key, value in google_lang_descriptions.items() if value == lang_desc][0]
|
72 |
-
# Update the voice dropdown with corresponding voice options
|
73 |
-
return gr.Dropdown(choices=[x[0] for x in google_voice_options[
|
74 |
|
75 |
speech_button.click(generate_speech,
|
76 |
inputs=[text_input, google_lang_input, google_voice_input],
|
|
|
6 |
# Get available languages for Google TTS
|
7 |
google_langs = tts_langs()
|
8 |
|
9 |
+
# Create descriptive names for all supported languages
|
10 |
google_lang_descriptions = {
|
11 |
+
"af": "Afrikaans",
|
12 |
+
"ar": "Arabic",
|
13 |
+
"bn": "Bengali",
|
14 |
+
"bs": "Bosnian",
|
15 |
+
"ca": "Catalan",
|
16 |
+
"cs": "Czech",
|
17 |
+
"cy": "Welsh",
|
18 |
+
"da": "Danish",
|
19 |
+
"de": "German",
|
20 |
+
"el": "Greek",
|
21 |
"en": "English",
|
22 |
+
"eo": "Esperanto",
|
23 |
"es": "Spanish",
|
24 |
+
"et": "Estonian",
|
25 |
+
"fi": "Finnish",
|
26 |
"fr": "French",
|
27 |
+
"gu": "Gujarati",
|
28 |
+
"hi": "Hindi",
|
29 |
+
"hr": "Croatian",
|
30 |
+
"hu": "Hungarian",
|
31 |
+
"id": "Indonesian",
|
32 |
+
"is": "Icelandic",
|
33 |
"it": "Italian",
|
34 |
"ja": "Japanese",
|
35 |
+
"jw": "Javanese",
|
36 |
+
"kn": "Kannada",
|
37 |
"ko": "Korean",
|
38 |
+
"la": "Latin",
|
39 |
+
"lv": "Latvian",
|
40 |
+
"ml": "Malayalam",
|
41 |
+
"mr": "Marathi",
|
42 |
+
"my": "Myanmar",
|
43 |
+
"ne": "Nepali",
|
44 |
+
"nl": "Dutch",
|
45 |
+
"no": "Norwegian",
|
46 |
+
"pl": "Polish",
|
47 |
"pt": "Portuguese",
|
48 |
+
"ro": "Romanian",
|
49 |
+
"ru": "Russian",
|
50 |
+
"si": "Sinhala",
|
51 |
+
"sk": "Slovak",
|
52 |
+
"sq": "Albanian",
|
53 |
+
"sr": "Serbian",
|
54 |
+
"su": "Sundanese",
|
55 |
+
"sv": "Swedish",
|
56 |
+
"sw": "Swahili",
|
57 |
+
"ta": "Tamil",
|
58 |
+
"te": "Telugu",
|
59 |
+
"th": "Thai",
|
60 |
+
"tr": "Turkish",
|
61 |
+
"uk": "Ukrainian",
|
62 |
+
"ur": "Urdu",
|
63 |
+
"vi": "Vietnamese",
|
64 |
+
"zh-CN": "Chinese (Simplified)",
|
65 |
+
"zh-TW": "Chinese (Traditional)"
|
66 |
}
|
67 |
|
68 |
+
# Define voice options for English, as an example (others can be added as needed)
|
69 |
google_voice_options = {
|
70 |
"en": [("Australia", "com.au"), ("Canada", "ca"), ("United Kingdom", "co.uk"),
|
71 |
("United States", "com"), ("India", "co.in"), ("Ireland", "ie"), ("South Africa", "co.za")],
|
72 |
+
# Add voice options for other languages if applicable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
}
|
74 |
|
75 |
def google_tts(text, lang, tld):
|
|
|
80 |
tts.save(temp_audio.name)
|
81 |
temp_audio_path = temp_audio.name
|
82 |
|
83 |
+
return temp_audio_path, f"Speech generated with Google TTS using {google_lang_descriptions.get(lang, lang)} language and {tld} voice variant"
|
84 |
except Exception as e:
|
85 |
return None, f"Error in Google TTS speech generation: {str(e)}"
|
86 |
|
|
|
91 |
text_input = gr.Textbox(label="Enter text for speech generation")
|
92 |
|
93 |
# Create dropdown for descriptive language options
|
94 |
+
google_lang_input = gr.Dropdown([google_lang_descriptions[key] for key in google_langs.keys()], label="Select Language", value="English")
|
95 |
+
google_voice_input = gr.Dropdown([x[0] for x in google_voice_options.get("en", [])], label="Select Voice Variant", value="United States")
|
96 |
|
97 |
speech_button = gr.Button("Generate Speech")
|
98 |
speech_output = gr.Audio(label="Generated Speech")
|
|
|
101 |
def generate_speech(text, google_lang_desc, google_voice_desc):
|
102 |
# Convert descriptive language back to its code
|
103 |
google_lang_code = [key for key, value in google_lang_descriptions.items() if value == google_lang_desc][0]
|
104 |
+
# Find the tld (country code) based on the description (if applicable)
|
105 |
+
google_voice_tld = dict(google_voice_options.get(google_lang_code, [("Default", "")])).get(google_voice_desc, "")
|
106 |
return google_tts(text, google_lang_code, google_voice_tld)
|
107 |
|
108 |
def update_google_voice_options(lang_desc):
|
109 |
# Convert descriptive language back to its code
|
110 |
google_lang_code = [key for key, value in google_lang_descriptions.items() if value == lang_desc][0]
|
111 |
+
# Update the voice dropdown with corresponding voice options (if available)
|
112 |
+
return gr.Dropdown(choices=[x[0] for x in google_voice_options.get(google_lang_code, [("Default", "")])], value="Default")
|
113 |
|
114 |
speech_button.click(generate_speech,
|
115 |
inputs=[text_input, google_lang_input, google_voice_input],
|