asdaswadefswefr's picture
Update app.py
6d93611 verified
import gradio as gr
import edge_tts
import asyncio
import tempfile
# Função para listar as vozes disponíveis
async def get_voices():
voices = await edge_tts.list_voices()
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
# Função de texto para fala
async def text_to_speech(text, voice, rate, pitch):
if not text.strip():
return None, "Por favor, insira um texto para converter."
if not voice:
return None, "Por favor, selecione uma voz."
voice_short_name = voice.split(" - ")[0]
rate_str = f"{rate:+d}%"
pitch_str = f"{pitch:+d}Hz"
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
tmp_path = tmp_file.name
await communicate.save(tmp_path)
return tmp_path, None
# Função da interface Gradio
def tts_interface(text, voice, rate, pitch):
audio, warning = asyncio.run(text_to_speech(text, voice, rate, pitch))
return audio, warning
# Criar a aplicação Gradio
async def create_demo():
voices = await get_voices()
demo = gr.Interface(
fn=tts_interface,
inputs=[
gr.Textbox(label="Texto de Entrada", lines=5),
gr.Dropdown(choices=[""] + list(voices.keys()), label="Selecionar Voz", value=""),
gr.Slider(minimum=-50, maximum=50, value=0, label="Ajuste de Taxa de Fala (%)", step=1),
gr.Slider(minimum=-20, maximum=20, value=0, label="Ajuste de Tom (Hz)", step=1)
],
outputs=[
gr.Audio(label="Áudio Gerado", type="filepath"),
gr.Markdown(label="Aviso", visible=False)
],
title="Texto para Fala com Edge TTS",
description="Converta texto em fala usando Microsoft Edge TTS."
)
return demo
# Executar a aplicação
if __name__ == "__main__":
demo = asyncio.run(create_demo())
demo.launch()