Update app.py
Browse files
app.py
CHANGED
|
@@ -4,42 +4,34 @@ import asyncio
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 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=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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 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
|
| 30 |
-
# Pass default values for rate and pitch
|
| 31 |
-
audio, warning = asyncio.run(text_to_speech(text
|
| 32 |
return audio, warning
|
| 33 |
|
| 34 |
# Create Gradio application
|
| 35 |
async def create_demo():
|
| 36 |
-
voices = await get_voices()
|
| 37 |
-
|
| 38 |
demo = gr.Interface(
|
| 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"),
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Default voice to be used
|
| 8 |
+
DEFAULT_VOICE = "en-US-AndrewMultilingualNeural"
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Text-to-speech function
|
| 11 |
+
async def text_to_speech(text, voice=DEFAULT_VOICE, rate=0, pitch=0):
|
| 12 |
if not text.strip():
|
| 13 |
return None, gr.Warning("Please enter text to convert.")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
rate_str = f"{rate:+d}%"
|
| 16 |
+
pitch_str = f"{pitch:+d}Hz"
|
| 17 |
+
communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
|
|
|
|
| 18 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 19 |
tmp_path = tmp_file.name
|
| 20 |
await communicate.save(tmp_path)
|
| 21 |
return tmp_path, None
|
| 22 |
|
| 23 |
# Gradio interface function
|
| 24 |
+
def tts_interface(text):
|
| 25 |
+
# Pass the default voice and default values for rate and pitch
|
| 26 |
+
audio, warning = asyncio.run(text_to_speech(text))
|
| 27 |
return audio, warning
|
| 28 |
|
| 29 |
# Create Gradio application
|
| 30 |
async def create_demo():
|
|
|
|
|
|
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=tts_interface,
|
| 33 |
inputs=[
|
| 34 |
+
gr.Textbox(label="Input Text", lines=5)
|
|
|
|
| 35 |
],
|
| 36 |
outputs=[
|
| 37 |
gr.Audio(label="Generated Audio", type="filepath"),
|