Spaces:
Sleeping
Sleeping
File size: 1,775 Bytes
1b41e6d a257b37 25681f6 1b41e6d 25681f6 100a302 25681f6 1b41e6d 25681f6 |
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 |
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() |