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