Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import edge_tts | |
| import asyncio | |
| import os | |
| # Função para obter todas as vozes disponíveis | |
| async def get_voices(): | |
| voices = await edge_tts.list_voices() | |
| return voices | |
| # Função para filtrar vozes por idioma | |
| def filter_voices_by_language(voices, language): | |
| return [v for v in voices if v.locale.startswith(language)] | |
| # Função para gerar o áudio | |
| async def text_to_speech(text, voice): | |
| output_file = f"output_{hash(text)}.mp3" | |
| communicate = edge_tts.Communicate(text, voice) | |
| await communicate.save(output_file) | |
| return output_file | |
| # Função para processar a entrada e chamar o TTS | |
| def process_tts(text, voice): | |
| return asyncio.run(text_to_speech(text, voice)) | |
| # Função para atualizar as opções de voz com base no idioma selecionado | |
| def update_voice_options(language): | |
| voices = asyncio.run(get_voices()) | |
| language_codes = { | |
| "Português (Brasil)": "pt-BR", | |
| "Inglês": "en", | |
| "Espanhol": "es", | |
| "Francês": "fr", | |
| "Italiano": "it", | |
| "Multilíngue": "multi" | |
| } | |
| if language == "Multilíngue": | |
| filtered_voices = [v for v in voices if "multilingual" in v.properties.get("VoiceType", "").lower()] | |
| else: | |
| filtered_voices = filter_voices_by_language(voices, language_codes[language]) | |
| voice_options = [f"{v.short_name} ({v.gender})" for v in filtered_voices] | |
| return gr.Dropdown.update(choices=voice_options, value=voice_options[0] if voice_options else None) | |
| # Configurar a interface Gradio | |
| with gr.Blocks() as iface: | |
| gr.Markdown("# Conversor de Texto para Fala usando Azure TTS") | |
| gr.Markdown("Converta texto em fala usando as vozes da Azure em diferentes idiomas.") | |
| with gr.Row(): | |
| language = gr.Dropdown( | |
| ["Português (Brasil)", "Inglês", "Espanhol", "Francês", "Italiano", "Multilíngue"], | |
| label="Idioma", | |
| value="Português (Brasil)" | |
| ) | |
| voice = gr.Dropdown(label="Voz") | |
| text_input = gr.Textbox(label="Texto para converter em fala", lines=3) | |
| audio_output = gr.Audio(label="Áudio gerado") | |
| convert_button = gr.Button("Converter") | |
| language.change(update_voice_options, inputs=[language], outputs=[voice]) | |
| convert_button.click(process_tts, inputs=[text_input, voice], outputs=[audio_output]) | |
| # Inicializar as opções de voz | |
| iface.load(update_voice_options, inputs=[language], outputs=[voice]) | |
| # Iniciar a aplicação | |
| iface.launch() | |