VoiceOversV3 / app.py
capradeepgujaran's picture
Update app.py
c580d77 verified
raw
history blame
3.46 kB
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()
# Define descriptive names for languages
google_lang_descriptions = {
"en": "English",
"es": "Spanish",
"fr": "French",
"de": "German",
"it": "Italian",
"ja": "Japanese",
"ko": "Korean",
"pt": "Portuguese",
"zh": "Chinese"
}
# Define voice options with descriptive names for each language
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")],
"es": [("Spain", "com"), ("Latin America", "es"), ("Mexico", "com.mx")],
"fr": [("France", "fr"), ("Canada", "ca")],
"de": [("Germany", "de"), ("Austria", "at")],
"it": [("Italy", "it"), ("United States", "com")],
"ja": [("Japan", "jp")],
"ko": [("Korea", "com")],
"pt": [("Brazil", "com.br"), ("Portugal", "pt")],
"zh": [("China", "cn"), ("Taiwan", "tw")]
}
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[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_lang_descriptions], label="Select Language", value="English")
google_voice_input = gr.Dropdown([x[0] for x in google_voice_options["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
google_voice_tld = dict(google_voice_options[google_lang_code])[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
return gr.Dropdown(choices=[x[0] for x in google_voice_options[google_lang_code]], value="United States")
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()