Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,26 +2,34 @@ import asyncio
|
|
2 |
import os
|
3 |
import edge_tts
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
# Function to get available voices
|
7 |
async def get_voices():
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
# Function to convert text to speech
|
12 |
async def text_to_speech(text, voice, rate, pitch):
|
13 |
try:
|
14 |
-
|
|
|
|
|
|
|
15 |
voice_short_name = voice.split(" (")[0]
|
16 |
|
17 |
-
# Convert rate
|
18 |
rate_str = f"+{int(rate)}%" if rate >= 0 else f"{int(rate)}%"
|
19 |
|
20 |
-
# Convert pitch
|
21 |
pitch_str = f"+{int(pitch)}Hz" if pitch >= 0 else f"{int(pitch)}Hz"
|
22 |
|
23 |
-
# Generate unique output filename
|
24 |
-
|
|
|
25 |
|
26 |
# Initialize edge-tts communication
|
27 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
@@ -31,11 +39,11 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
31 |
|
32 |
# Check if file was created
|
33 |
if os.path.exists(output_file):
|
34 |
-
return output_file
|
35 |
else:
|
36 |
-
return "Error: Audio file was not generated."
|
37 |
except Exception as e:
|
38 |
-
return f"Error: {str(e)}"
|
39 |
|
40 |
# Gradio interface function
|
41 |
def create_gradio_interface():
|
@@ -43,40 +51,81 @@ def create_gradio_interface():
|
|
43 |
loop = asyncio.get_event_loop()
|
44 |
voices = loop.run_until_complete(get_voices())
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
# Define Gradio interface
|
47 |
-
with gr.Blocks(
|
48 |
-
gr.Markdown(
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
rate_slider = gr.Slider(minimum=-50, maximum=50, value=0, step=1, label="Rate (%)")
|
55 |
-
pitch_slider = gr.Slider(minimum=-200, maximum=200, value=0, step=10, label="Pitch (Hz)")
|
56 |
-
|
57 |
-
# Generate button
|
58 |
-
generate_button = gr.Button("Generate Audio")
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
# Button click event
|
65 |
async def on_generate(text, voice, rate, pitch):
|
66 |
-
|
67 |
-
|
68 |
-
if not voice:
|
69 |
-
return None, "Error: Please select a voice."
|
70 |
-
|
71 |
-
result = await text_to_speech(text, voice, rate, pitch)
|
72 |
-
if result.startswith("Error"):
|
73 |
-
return None, result
|
74 |
-
return result, "Audio generated successfully!"
|
75 |
|
76 |
generate_button.click(
|
77 |
fn=on_generate,
|
78 |
inputs=[text_input, voice_dropdown, rate_slider, pitch_slider],
|
79 |
-
outputs=[audio_output,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
)
|
81 |
|
82 |
return interface
|
@@ -84,4 +133,4 @@ def create_gradio_interface():
|
|
84 |
# Launch the interface
|
85 |
if __name__ == "__main__":
|
86 |
interface = create_gradio_interface()
|
87 |
-
interface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
2 |
import os
|
3 |
import edge_tts
|
4 |
import gradio as gr
|
5 |
+
from datetime import datetime
|
6 |
|
7 |
# Function to get available voices
|
8 |
async def get_voices():
|
9 |
+
try:
|
10 |
+
voices = await edge_tts.list_voices()
|
11 |
+
return sorted([f"{voice['ShortName']} ({voice['Gender']})" for voice in voices])
|
12 |
+
except Exception as e:
|
13 |
+
return [f"Error fetching voices: {str(e)}"]
|
14 |
|
15 |
# Function to convert text to speech
|
16 |
async def text_to_speech(text, voice, rate, pitch):
|
17 |
try:
|
18 |
+
if not text or not voice:
|
19 |
+
return None, "Error: Text and voice selection are required."
|
20 |
+
|
21 |
+
# Extract voice ShortName (e.g., "en-US-AvaNeural (Female)" -> "en-US-AvaNeural")
|
22 |
voice_short_name = voice.split(" (")[0]
|
23 |
|
24 |
+
# Convert rate to edge-tts format (e.g., 10 -> "+10%", -10 -> "-10%")
|
25 |
rate_str = f"+{int(rate)}%" if rate >= 0 else f"{int(rate)}%"
|
26 |
|
27 |
+
# Convert pitch to edge-tts format (e.g., 100 -> "+100Hz", -100 -> "-100Hz")
|
28 |
pitch_str = f"+{int(pitch)}Hz" if pitch >= 0 else f"{int(pitch)}Hz"
|
29 |
|
30 |
+
# Generate unique output filename with timestamp
|
31 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
32 |
+
output_file = f"output_{timestamp}.mp3"
|
33 |
|
34 |
# Initialize edge-tts communication
|
35 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
|
|
39 |
|
40 |
# Check if file was created
|
41 |
if os.path.exists(output_file):
|
42 |
+
return output_file, "Audio generated successfully!"
|
43 |
else:
|
44 |
+
return None, "Error: Audio file was not generated."
|
45 |
except Exception as e:
|
46 |
+
return None, f"Error: {str(e)}"
|
47 |
|
48 |
# Gradio interface function
|
49 |
def create_gradio_interface():
|
|
|
51 |
loop = asyncio.get_event_loop()
|
52 |
voices = loop.run_until_complete(get_voices())
|
53 |
|
54 |
+
# Custom CSS for a polished look
|
55 |
+
css = """
|
56 |
+
.gradio-container {background-color: #f5f7fa;}
|
57 |
+
.title {text-align: center; color: #2c3e50;}
|
58 |
+
.footer {text-align: center; color: #7f8c8d; font-size: 0.9em; margin-top: 20px;}
|
59 |
+
.button-primary {background-color: #3498db !important; color: white !important;}
|
60 |
+
.input-box {border-radius: 8px;}
|
61 |
+
"""
|
62 |
+
|
63 |
# Define Gradio interface
|
64 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as interface:
|
65 |
+
gr.Markdown(
|
66 |
+
"""
|
67 |
+
<h1 class='title'>Edge TTS Text-to-Speech</h1>
|
68 |
+
<p style='text-align: center;'>Convert text to speech with customizable voice, rate, and pitch.</p>
|
69 |
+
"""
|
70 |
+
)
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column(scale=2):
|
74 |
+
text_input = gr.Textbox(
|
75 |
+
label="Input Text",
|
76 |
+
placeholder="Enter the text you want to convert to speech...",
|
77 |
+
lines=5,
|
78 |
+
elem_classes="input-box"
|
79 |
+
)
|
80 |
+
voice_dropdown = gr.Dropdown(
|
81 |
+
choices=voices,
|
82 |
+
label="Voice Model",
|
83 |
+
value=voices[0] if voices else None,
|
84 |
+
allow_custom_value=False
|
85 |
+
)
|
86 |
+
rate_slider = gr.Slider(
|
87 |
+
minimum=-50,
|
88 |
+
maximum=50,
|
89 |
+
value=0,
|
90 |
+
step=1,
|
91 |
+
label="Speech Rate (%)",
|
92 |
+
info="Adjust the speed of the speech (±50%)"
|
93 |
+
)
|
94 |
+
pitch_slider = gr.Slider(
|
95 |
+
minimum=-200,
|
96 |
+
maximum=200,
|
97 |
+
value=0,
|
98 |
+
step=10,
|
99 |
+
label="Pitch (Hz)",
|
100 |
+
info="Adjust the pitch of the voice (±200Hz)"
|
101 |
+
)
|
102 |
+
generate_button = gr.Button("Generate Audio", variant="primary", elem_classes="button-primary")
|
103 |
+
|
104 |
+
with gr.Column(scale=1):
|
105 |
+
audio_output = gr.Audio(label="Generated Audio", interactive=False)
|
106 |
+
status_output = gr.Textbox(
|
107 |
+
label="Status",
|
108 |
+
interactive=False,
|
109 |
+
placeholder="Status messages will appear here..."
|
110 |
+
)
|
111 |
|
112 |
# Button click event
|
113 |
async def on_generate(text, voice, rate, pitch):
|
114 |
+
audio, status = await text_to_speech(text, voice, rate, pitch)
|
115 |
+
return audio, status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
generate_button.click(
|
118 |
fn=on_generate,
|
119 |
inputs=[text_input, voice_dropdown, rate_slider, pitch_slider],
|
120 |
+
outputs=[audio_output, status_output]
|
121 |
+
)
|
122 |
+
|
123 |
+
gr.Markdown(
|
124 |
+
"""
|
125 |
+
<p class='footer'>
|
126 |
+
Powered by Edge TTS and Gradio | Deployed on Hugging Face Spaces
|
127 |
+
</p>
|
128 |
+
"""
|
129 |
)
|
130 |
|
131 |
return interface
|
|
|
133 |
# Launch the interface
|
134 |
if __name__ == "__main__":
|
135 |
interface = create_gradio_interface()
|
136 |
+
interface.launch(server_name="0.0.0.0", server_port=7860, share=False)
|