Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,12 @@ import tempfile
|
|
| 5 |
import os
|
| 6 |
from moviepy.editor import AudioFileClip
|
| 7 |
|
| 8 |
-
#
|
| 9 |
async def get_voices():
|
| 10 |
voices = await edge_tts.list_voices()
|
| 11 |
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
| 12 |
|
| 13 |
-
#
|
| 14 |
async def text_to_speech(text, voice, rate, pitch):
|
| 15 |
if not text.strip():
|
| 16 |
return None, gr.Warning("Please enter the text to convert.")
|
|
@@ -26,8 +26,8 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
| 26 |
await communicate.save(tmp_path)
|
| 27 |
return tmp_path, None
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
def generate_srt(words, audio_duration, srt_path
|
| 31 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 32 |
segment_duration = audio_duration / (len(words) // 5) # Average duration for each 5 words
|
| 33 |
current_time = 0
|
|
@@ -56,7 +56,7 @@ def format_srt_time(seconds):
|
|
| 56 |
seconds %= 60
|
| 57 |
return f"{hours:02}:{minutes:02}:{seconds:02},{millis:03}"
|
| 58 |
|
| 59 |
-
#
|
| 60 |
async def text_to_audio_and_srt(text, voice, rate, pitch):
|
| 61 |
audio_path, warning = await text_to_speech(text, voice, rate, pitch)
|
| 62 |
if warning:
|
|
@@ -73,12 +73,12 @@ async def text_to_audio_and_srt(text, voice, rate, pitch):
|
|
| 73 |
|
| 74 |
return audio_path, srt_path, None
|
| 75 |
|
| 76 |
-
# Gradio
|
| 77 |
def tts_interface(text, voice, rate, pitch):
|
| 78 |
audio_path, srt_path, warning = asyncio.run(text_to_audio_and_srt(text, voice, rate, pitch))
|
| 79 |
return audio_path, srt_path, warning
|
| 80 |
|
| 81 |
-
#
|
| 82 |
async def create_demo():
|
| 83 |
voices = await get_voices()
|
| 84 |
|
|
@@ -113,7 +113,7 @@ async def create_demo():
|
|
| 113 |
|
| 114 |
return demo
|
| 115 |
|
| 116 |
-
#
|
| 117 |
if __name__ == "__main__":
|
| 118 |
demo = asyncio.run(create_demo())
|
| 119 |
demo.launch()
|
|
|
|
| 5 |
import os
|
| 6 |
from moviepy.editor import AudioFileClip
|
| 7 |
|
| 8 |
+
# Get all available voices
|
| 9 |
async def get_voices():
|
| 10 |
voices = await edge_tts.list_voices()
|
| 11 |
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
| 12 |
|
| 13 |
+
# Text to speech functionality
|
| 14 |
async def text_to_speech(text, voice, rate, pitch):
|
| 15 |
if not text.strip():
|
| 16 |
return None, gr.Warning("Please enter the text to convert.")
|
|
|
|
| 26 |
await communicate.save(tmp_path)
|
| 27 |
return tmp_path, None
|
| 28 |
|
| 29 |
+
# Generate SRT file with 2 lines of subtitles, each having up to 5 words
|
| 30 |
+
def generate_srt(words, audio_duration, srt_path):
|
| 31 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 32 |
segment_duration = audio_duration / (len(words) // 5) # Average duration for each 5 words
|
| 33 |
current_time = 0
|
|
|
|
| 56 |
seconds %= 60
|
| 57 |
return f"{hours:02}:{minutes:02}:{seconds:02},{millis:03}"
|
| 58 |
|
| 59 |
+
# Text to audio and SRT functionality
|
| 60 |
async def text_to_audio_and_srt(text, voice, rate, pitch):
|
| 61 |
audio_path, warning = await text_to_speech(text, voice, rate, pitch)
|
| 62 |
if warning:
|
|
|
|
| 73 |
|
| 74 |
return audio_path, srt_path, None
|
| 75 |
|
| 76 |
+
# Gradio interface function
|
| 77 |
def tts_interface(text, voice, rate, pitch):
|
| 78 |
audio_path, srt_path, warning = asyncio.run(text_to_audio_and_srt(text, voice, rate, pitch))
|
| 79 |
return audio_path, srt_path, warning
|
| 80 |
|
| 81 |
+
# Create Gradio app
|
| 82 |
async def create_demo():
|
| 83 |
voices = await get_voices()
|
| 84 |
|
|
|
|
| 113 |
|
| 114 |
return demo
|
| 115 |
|
| 116 |
+
# Run the app
|
| 117 |
if __name__ == "__main__":
|
| 118 |
demo = asyncio.run(create_demo())
|
| 119 |
demo.launch()
|