Update app.py
Browse files
app.py
CHANGED
@@ -10,15 +10,15 @@ async def get_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
|
@@ -26,13 +26,12 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
26 |
return tmp_path, None
|
27 |
|
28 |
# Gradio interface function
|
29 |
-
def tts_interface(text, voice
|
30 |
-
|
|
|
31 |
return audio, warning
|
32 |
|
33 |
# Create Gradio application
|
34 |
-
import gradio as gr
|
35 |
-
|
36 |
async def create_demo():
|
37 |
voices = await get_voices()
|
38 |
|
@@ -40,9 +39,7 @@ async def create_demo():
|
|
40 |
fn=tts_interface,
|
41 |
inputs=[
|
42 |
gr.Textbox(label="Input Text", lines=5),
|
43 |
-
gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value="")
|
44 |
-
gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
|
45 |
-
gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
|
46 |
],
|
47 |
outputs=[
|
48 |
gr.Audio(label="Generated Audio", type="filepath"),
|
@@ -57,4 +54,4 @@ async def create_demo():
|
|
57 |
# Run the application
|
58 |
if __name__ == "__main__":
|
59 |
demo = asyncio.run(create_demo())
|
60 |
-
demo.launch()
|
|
|
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=0, pitch=0):
|
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}%" # Default to 0% for rate
|
21 |
+
pitch_str = f"{pitch:+d}Hz" # Default to 0Hz for pitch
|
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
|
|
|
26 |
return tmp_path, None
|
27 |
|
28 |
# Gradio interface function
|
29 |
+
def tts_interface(text, voice):
|
30 |
+
# Pass default values for rate and pitch
|
31 |
+
audio, warning = asyncio.run(text_to_speech(text, voice, rate=0, pitch=0))
|
32 |
return audio, warning
|
33 |
|
34 |
# Create Gradio application
|
|
|
|
|
35 |
async def create_demo():
|
36 |
voices = await get_voices()
|
37 |
|
|
|
39 |
fn=tts_interface,
|
40 |
inputs=[
|
41 |
gr.Textbox(label="Input Text", lines=5),
|
42 |
+
gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value="")
|
|
|
|
|
43 |
],
|
44 |
outputs=[
|
45 |
gr.Audio(label="Generated Audio", type="filepath"),
|
|
|
54 |
# Run the application
|
55 |
if __name__ == "__main__":
|
56 |
demo = asyncio.run(create_demo())
|
57 |
+
demo.launch()
|