Spaces:
Sleeping
Sleeping
import asyncio | |
import edge_tts | |
ing import GradioComponent to match huggingface space conventions | |
import gradio as gr | |
# Fetch available voices once at startup | |
df_voices = asyncio.run(edge_tts.list_voices()) | |
voice_names = [v["Name"] for v in df_voices] | |
async def generate_tts(text: str, voice: str, rate: int, pitch: int): | |
# Edge TTS parameters expect strings | |
rate_str = f"{rate}%" | |
pitch_str = f"{pitch}Hz" | |
communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str) | |
# Stream audio to memory | |
audio_chunks = [] | |
async for chunk in communicate.stream(): | |
audio_chunks.append(chunk) | |
return b"".join(audio_chunks) | |
# Synchronous wrapper for Gradio | |
def tts(text, voice, rate, pitch): | |
audio = asyncio.run(generate_tts(text, voice, rate, pitch)) | |
return ("output.mp3", audio) | |
# Gradio UI | |
def main(): | |
with gr.Blocks() as demo: | |
gr.Markdown("## Edge TTS Text-to-Speech Converter") | |
with gr.Row(): | |
text_input = gr.Textbox(label="Input Text", lines=4, placeholder="Enter text to convert...") | |
voice_selector = gr.Dropdown(label="Voice Model", choices=voice_names, value=voice_names[0]) | |
with gr.Row(): | |
rate_slider = gr.Slider(label="Speaking Rate (%)", minimum=10, maximum=200, step=1, value=100) | |
pitch_slider = gr.Slider(label="Pitch (Hz)", minimum=-20, maximum=20, step=1, value=0) | |
output_audio = gr.Audio(label="Generated Audio", type="file") | |
generate_btn = gr.Button("Convert to Speech") | |
generate_btn.click(fn=tts, | |
inputs=[text_input, voice_selector, rate_slider, pitch_slider], | |
outputs=output_audio) | |
return demo | |
if __name__ == "__main__": | |
main().launch() |