Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,19 @@ from gtts.lang import tts_langs
|
|
6 |
# Get available languages for Google TTS
|
7 |
google_langs = tts_langs()
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# Define voice options with descriptive names for each language
|
10 |
google_voice_options = {
|
11 |
"en": [("Australia", "com.au"), ("Canada", "ca"), ("United Kingdom", "co.uk"),
|
@@ -28,7 +41,7 @@ def google_tts(text, lang, tld):
|
|
28 |
tts.save(temp_audio.name)
|
29 |
temp_audio_path = temp_audio.name
|
30 |
|
31 |
-
return temp_audio_path, f"Speech generated with Google TTS using {lang} language and {tld} voice variant"
|
32 |
except Exception as e:
|
33 |
return None, f"Error in Google TTS speech generation: {str(e)}"
|
34 |
|
@@ -38,21 +51,26 @@ with gr.Blocks() as iface:
|
|
38 |
|
39 |
text_input = gr.Textbox(label="Enter text for speech generation")
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
|
45 |
speech_button = gr.Button("Generate Speech")
|
46 |
speech_output = gr.Audio(label="Generated Speech")
|
47 |
speech_message = gr.Textbox(label="Message")
|
48 |
|
49 |
-
def generate_speech(text,
|
|
|
|
|
50 |
# Find the tld (country code) based on the description
|
51 |
-
google_voice_tld = dict(google_voice_options[
|
52 |
-
return google_tts(text,
|
53 |
|
54 |
-
def update_google_voice_options(
|
55 |
-
|
|
|
|
|
|
|
56 |
|
57 |
speech_button.click(generate_speech,
|
58 |
inputs=[text_input, google_lang_input, google_voice_input],
|
|
|
6 |
# Get available languages for Google TTS
|
7 |
google_langs = tts_langs()
|
8 |
|
9 |
+
# Define descriptive names for languages
|
10 |
+
google_lang_descriptions = {
|
11 |
+
"en": "English",
|
12 |
+
"es": "Spanish",
|
13 |
+
"fr": "French",
|
14 |
+
"de": "German",
|
15 |
+
"it": "Italian",
|
16 |
+
"ja": "Japanese",
|
17 |
+
"ko": "Korean",
|
18 |
+
"pt": "Portuguese",
|
19 |
+
"zh": "Chinese"
|
20 |
+
}
|
21 |
+
|
22 |
# Define voice options with descriptive names for each language
|
23 |
google_voice_options = {
|
24 |
"en": [("Australia", "com.au"), ("Canada", "ca"), ("United Kingdom", "co.uk"),
|
|
|
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[lang]} language and {tld} voice variant"
|
45 |
except Exception as e:
|
46 |
return None, f"Error in Google TTS speech generation: {str(e)}"
|
47 |
|
|
|
51 |
|
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 google_lang_descriptions], label="Select Language", value="English")
|
56 |
+
google_voice_input = gr.Dropdown([x[0] for x in google_voice_options["en"]], label="Select Voice Variant", value="United States")
|
57 |
|
58 |
speech_button = gr.Button("Generate Speech")
|
59 |
speech_output = gr.Audio(label="Generated Speech")
|
60 |
speech_message = gr.Textbox(label="Message")
|
61 |
|
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[google_lang_code])[google_voice_desc]
|
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[google_lang_code]], value="United States")
|
74 |
|
75 |
speech_button.click(generate_speech,
|
76 |
inputs=[text_input, google_lang_input, google_voice_input],
|