Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import edge_tts
|
3 |
+
import asyncio
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Get all available voices
|
8 |
+
async def get_voices():
|
9 |
+
voices = await edge_tts.list_voices()
|
10 |
+
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
11 |
+
|
12 |
+
# Text-to-speech function
|
13 |
+
async def text_to_speech(text, voice, rate, pitch):
|
14 |
+
if not text.strip():
|
15 |
+
return None, gr.Warning("Please enter text to convert.")
|
16 |
+
if not voice:
|
17 |
+
return None, gr.Warning("Please select a voice.")
|
18 |
+
|
19 |
+
voice_short_name = voice.split(" - ")[0]
|
20 |
+
rate_str = f"{rate:+d}%"
|
21 |
+
pitch_str = f"{pitch:+d}Hz"
|
22 |
+
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
23 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
24 |
+
tmp_path = tmp_file.name
|
25 |
+
await communicate.save(tmp_path)
|
26 |
+
return tmp_path, None
|
27 |
+
|
28 |
+
# Gradio interface function
|
29 |
+
def tts_interface(text, voice, rate, pitch):
|
30 |
+
audio, warning = asyncio.run(text_to_speech(text, voice, rate, pitch))
|
31 |
+
return audio, warning
|
32 |
+
|
33 |
+
# Create Gradio application
|
34 |
+
async def create_demo():
|
35 |
+
voices = await get_voices()
|
36 |
+
|
37 |
+
demo = gr.Interface(
|
38 |
+
fn=tts_interface,
|
39 |
+
inputs=[
|
40 |
+
gr.Textbox(label="Input Text", lines=5),
|
41 |
+
gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
|
42 |
+
gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
|
43 |
+
gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
|
44 |
+
],
|
45 |
+
outputs=[
|
46 |
+
gr.Audio(label="Generated Audio", type="filepath"),
|
47 |
+
gr.Markdown(label="Warning", visible=False)
|
48 |
+
],
|
49 |
+
title="Edge TTS Text-to-Speech",
|
50 |
+
description="Convert text to speech using Microsoft Edge TTS. Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.",
|
51 |
+
analytics_enabled=False,
|
52 |
+
allow_flagging=False
|
53 |
+
)
|
54 |
+
|
55 |
+
return demo
|
56 |
+
|
57 |
+
# Run the application
|
58 |
+
if __name__ == "__main__":
|
59 |
+
demo = asyncio.run(create_demo())
|
60 |
+
demo.launch()
|