Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -44,22 +44,19 @@ async def text_to_speech(text, voice, rate, pitch, output_path):
|
|
44 |
# Generate SRT file with specified lines of subtitles
|
45 |
def generate_srt(words, audio_duration, srt_path, num_lines):
|
46 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
divisor = len(words) // (5 * num_lines)
|
51 |
-
if divisor == 0:
|
52 |
-
segment_duration = audio_duration # Use full duration as fallback
|
53 |
-
else:
|
54 |
-
segment_duration = audio_duration / divisor # Calculate duration per segment
|
55 |
|
|
|
56 |
for i in range(0, len(words), 5 * num_lines):
|
57 |
lines = []
|
58 |
for j in range(num_lines):
|
59 |
-
|
|
|
|
|
60 |
if line:
|
61 |
lines.append(line)
|
62 |
-
|
63 |
start_time = current_time
|
64 |
end_time = start_time + segment_duration
|
65 |
|
@@ -70,6 +67,7 @@ def generate_srt(words, audio_duration, srt_path, num_lines):
|
|
70 |
current_time += segment_duration
|
71 |
|
72 |
return srt_path
|
|
|
73 |
# def generate_srt(words, audio_duration, srt_path, num_lines):
|
74 |
# with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
75 |
# divisor = len(words) // (5 * num_lines)
|
@@ -138,8 +136,19 @@ async def text_to_audio_and_srt(text, voice, rate, pitch, num_lines, output_audi
|
|
138 |
|
139 |
# Gradio interface function
|
140 |
def tts_interface(text, voice, rate, pitch, num_lines, output_audio_path="output_audio.mp3", output_srt_path="output_subtitle.srt"):
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
# def tts_interface(text, voice, rate, pitch, num_lines):
|
144 |
# audio_path, srt_path, warning = asyncio.run(text_to_audio_and_srt(text, voice, rate, pitch, num_lines))
|
145 |
# return audio_path, srt_path, warning
|
|
|
44 |
# Generate SRT file with specified lines of subtitles
|
45 |
def generate_srt(words, audio_duration, srt_path, num_lines):
|
46 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
47 |
+
total_segments = max(len(words) // (5 * num_lines), 1) # Ensure at least one segment
|
48 |
+
segment_duration = audio_duration / total_segments
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
current_time = 0
|
51 |
for i in range(0, len(words), 5 * num_lines):
|
52 |
lines = []
|
53 |
for j in range(num_lines):
|
54 |
+
line_start = i + j * 5
|
55 |
+
line_end = line_start + 5
|
56 |
+
line = ' '.join(words[line_start:line_end])
|
57 |
if line:
|
58 |
lines.append(line)
|
59 |
+
|
60 |
start_time = current_time
|
61 |
end_time = start_time + segment_duration
|
62 |
|
|
|
67 |
current_time += segment_duration
|
68 |
|
69 |
return srt_path
|
70 |
+
|
71 |
# def generate_srt(words, audio_duration, srt_path, num_lines):
|
72 |
# with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
73 |
# divisor = len(words) // (5 * num_lines)
|
|
|
136 |
|
137 |
# Gradio interface function
|
138 |
def tts_interface(text, voice, rate, pitch, num_lines, output_audio_path="output_audio.mp3", output_srt_path="output_subtitle.srt"):
|
139 |
+
if not text.strip():
|
140 |
+
return None, None, gr.Warning("Text input cannot be empty.")
|
141 |
+
if num_lines <= 0:
|
142 |
+
return None, None, gr.Warning("Number of SRT lines must be greater than zero.")
|
143 |
+
|
144 |
+
try:
|
145 |
+
audio_path, srt_path, warning = asyncio.run(
|
146 |
+
text_to_audio_and_srt(text, voice, rate, pitch, num_lines, output_audio_path, output_srt_path)
|
147 |
+
)
|
148 |
+
return audio_path, srt_path, warning
|
149 |
+
except Exception as e:
|
150 |
+
return None, None, gr.Warning(f"An error occurred: {e}")
|
151 |
+
|
152 |
# def tts_interface(text, voice, rate, pitch, num_lines):
|
153 |
# audio_path, srt_path, warning = asyncio.run(text_to_audio_and_srt(text, voice, rate, pitch, num_lines))
|
154 |
# return audio_path, srt_path, warning
|