Update app.py
Browse files
app.py
CHANGED
|
@@ -137,6 +137,78 @@ def combine_word_segments(words_timestamp, max_words_per_subtitle=8, min_silence
|
|
| 137 |
|
| 138 |
return before_translate
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
def convert_time_to_srt_format(seconds):
|
| 142 |
""" Convert seconds to SRT time format (HH:MM:SS,ms) """
|
|
@@ -162,15 +234,20 @@ def write_subtitles_to_file(subtitles, filename="subtitles.srt"):
|
|
| 162 |
# Write the text and speaker information
|
| 163 |
f.write(f"{entry['text']}\n\n")
|
| 164 |
|
| 165 |
-
|
|
|
|
|
|
|
| 166 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 167 |
for i, word_info in enumerate(words_timestamp, start=1):
|
| 168 |
start_time = convert_time_to_srt_format(word_info['start'])
|
| 169 |
end_time = convert_time_to_srt_format(word_info['end'])
|
| 170 |
word=word_info['word']
|
| 171 |
-
word =
|
|
|
|
|
|
|
| 172 |
srt_file.write(f"{i}\n{start_time} --> {end_time}\n{word}\n\n")
|
| 173 |
|
|
|
|
| 174 |
def generate_srt_from_sentences(sentence_timestamp, srt_path="default_subtitle.srt"):
|
| 175 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 176 |
for index, sentence in enumerate(sentence_timestamp):
|
|
@@ -214,9 +291,9 @@ def whisper_subtitle(uploaded_file,Source_Language,max_words_per_subtitle=8):
|
|
| 214 |
del faster_whisper_model
|
| 215 |
gc.collect()
|
| 216 |
torch.cuda.empty_cache()
|
| 217 |
-
|
| 218 |
word_segments=combine_word_segments(words_timestamp, max_words_per_subtitle=max_words_per_subtitle, min_silence_between_words=0.5)
|
| 219 |
-
|
| 220 |
#setup srt file names
|
| 221 |
base_name = os.path.basename(uploaded_file).rsplit('.', 1)[0][:30]
|
| 222 |
save_name = f"{subtitle_folder}/{base_name}_{src_lang}.srt"
|
|
@@ -224,22 +301,24 @@ def whisper_subtitle(uploaded_file,Source_Language,max_words_per_subtitle=8):
|
|
| 224 |
original_txt_name=original_srt_name.replace(".srt",".txt")
|
| 225 |
word_level_srt_name=original_srt_name.replace(".srt","_word_level.srt")
|
| 226 |
customize_srt_name=original_srt_name.replace(".srt","_customize.srt")
|
|
|
|
| 227 |
|
| 228 |
generate_srt_from_sentences(sentence_timestamp, srt_path=original_srt_name)
|
| 229 |
word_level_srt(words_timestamp, srt_path=word_level_srt_name)
|
|
|
|
| 230 |
write_subtitles_to_file(word_segments, filename=customize_srt_name)
|
| 231 |
with open(original_txt_name, 'w', encoding='utf-8') as f1:
|
| 232 |
f1.write(text)
|
| 233 |
-
return original_srt_name,customize_srt_name,word_level_srt_name,original_txt_name
|
| 234 |
|
| 235 |
#@title Using Gradio Interface
|
| 236 |
def subtitle_maker(Audio_or_Video_File,Source_Language,max_words_per_subtitle):
|
| 237 |
try:
|
| 238 |
-
default_srt_path,customize_srt_path,word_level_srt_path,text_path=whisper_subtitle(Audio_or_Video_File,Source_Language,max_words_per_subtitle=max_words_per_subtitle)
|
| 239 |
except Exception as e:
|
| 240 |
print(f"Error in whisper_subtitle: {e}")
|
| 241 |
-
default_srt_path,customize_srt_path,word_level_srt_path,text_path=None,None,None,None
|
| 242 |
-
return default_srt_path,customize_srt_path,word_level_srt_path,text_path
|
| 243 |
|
| 244 |
|
| 245 |
|
|
@@ -262,6 +341,7 @@ available_language=language_dict.keys()
|
|
| 262 |
source_lang_list.extend(available_language)
|
| 263 |
|
| 264 |
|
|
|
|
| 265 |
@click.command()
|
| 266 |
@click.option("--debug", is_flag=True, default=False, help="Enable debug mode.")
|
| 267 |
@click.option("--share", is_flag=True, default=False, help="Enable sharing of the interface.")
|
|
@@ -279,6 +359,7 @@ def main(debug, share):
|
|
| 279 |
gr.File(label="Default SRT File", show_label=True),
|
| 280 |
gr.File(label="Customize SRT File", show_label=True),
|
| 281 |
gr.File(label="Word Level SRT File", show_label=True),
|
|
|
|
| 282 |
gr.File(label="Text File", show_label=True)
|
| 283 |
]
|
| 284 |
|
|
@@ -288,4 +369,4 @@ def main(debug, share):
|
|
| 288 |
# Launch Gradio with command-line options
|
| 289 |
demo.queue().launch(debug=debug, share=share)
|
| 290 |
if __name__ == "__main__":
|
| 291 |
-
main()
|
|
|
|
| 137 |
|
| 138 |
return before_translate
|
| 139 |
|
| 140 |
+
def custom_word_segments(words_timestamp, min_silence_between_words=0.3, max_characters_per_subtitle=17):
|
| 141 |
+
before_translate = []
|
| 142 |
+
id = 1
|
| 143 |
+
text = ""
|
| 144 |
+
start = None
|
| 145 |
+
end = None
|
| 146 |
+
last_end_time = None
|
| 147 |
+
|
| 148 |
+
i = 0
|
| 149 |
+
while i < len(words_timestamp):
|
| 150 |
+
word = words_timestamp[i]['word']
|
| 151 |
+
word_start = words_timestamp[i]['start']
|
| 152 |
+
word_end = words_timestamp[i]['end']
|
| 153 |
+
|
| 154 |
+
# Look ahead to check if the next word (i+1) starts with a hyphen
|
| 155 |
+
if i + 1 < len(words_timestamp) and words_timestamp[i + 1]['word'].startswith("-"):
|
| 156 |
+
# Combine the current word and the next word (i, i+1) if next word starts with a hyphen
|
| 157 |
+
combined_text = word + words_timestamp[i + 1]['word'][:] # Skip the hyphen and combine
|
| 158 |
+
combined_start_time = word_start
|
| 159 |
+
combined_end_time = words_timestamp[i + 1]['end']
|
| 160 |
+
|
| 161 |
+
i += 1 # Skip the next word (i+1) since it has been combined
|
| 162 |
+
|
| 163 |
+
# Look ahead for the next non-hyphenated word, check further if needed (i+2, i+3, etc.)
|
| 164 |
+
while i + 1 < len(words_timestamp) and words_timestamp[i + 1]['word'].startswith("-"):
|
| 165 |
+
combined_text += words_timestamp[i + 1]['word'][:] # Add word excluding hyphen
|
| 166 |
+
combined_end_time = words_timestamp[i + 1]['end']
|
| 167 |
+
i += 1 # Skip the next hyphenated word
|
| 168 |
+
|
| 169 |
+
else:
|
| 170 |
+
# No hyphen at the next word, just take the current word
|
| 171 |
+
combined_text = word
|
| 172 |
+
combined_start_time = word_start
|
| 173 |
+
combined_end_time = word_end
|
| 174 |
+
|
| 175 |
+
# Check if the combined text exceeds the maximum character limit
|
| 176 |
+
if len(text) + len(combined_text) > max_characters_per_subtitle:
|
| 177 |
+
# If accumulated text is non-empty, store it as a subtitle
|
| 178 |
+
if text:
|
| 179 |
+
before_translate.append({
|
| 180 |
+
"word": text.strip(),
|
| 181 |
+
"start": start,
|
| 182 |
+
"end": end
|
| 183 |
+
})
|
| 184 |
+
id += 1
|
| 185 |
+
# Start a new subtitle with the combined text
|
| 186 |
+
text = combined_text
|
| 187 |
+
start = combined_start_time
|
| 188 |
+
else:
|
| 189 |
+
# Accumulate text
|
| 190 |
+
if not text:
|
| 191 |
+
start = combined_start_time
|
| 192 |
+
text += " " + combined_text
|
| 193 |
+
|
| 194 |
+
# Update the end timestamp
|
| 195 |
+
end = combined_end_time
|
| 196 |
+
last_end_time = end
|
| 197 |
+
|
| 198 |
+
# Move to the next word
|
| 199 |
+
i += 1
|
| 200 |
+
|
| 201 |
+
# Add the final subtitle segment if text is not empty
|
| 202 |
+
if text:
|
| 203 |
+
before_translate.append({
|
| 204 |
+
"word": text.strip(),
|
| 205 |
+
"start": start,
|
| 206 |
+
"end": end
|
| 207 |
+
})
|
| 208 |
+
|
| 209 |
+
return before_translate
|
| 210 |
+
|
| 211 |
+
|
| 212 |
|
| 213 |
def convert_time_to_srt_format(seconds):
|
| 214 |
""" Convert seconds to SRT time format (HH:MM:SS,ms) """
|
|
|
|
| 234 |
# Write the text and speaker information
|
| 235 |
f.write(f"{entry['text']}\n\n")
|
| 236 |
|
| 237 |
+
|
| 238 |
+
def word_level_srt(words_timestamp, srt_path="world_level_subtitle.srt",shorts=False):
|
| 239 |
+
punctuation_pattern = re.compile(r'[.,!?;:"\–—_~^+*|]')
|
| 240 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 241 |
for i, word_info in enumerate(words_timestamp, start=1):
|
| 242 |
start_time = convert_time_to_srt_format(word_info['start'])
|
| 243 |
end_time = convert_time_to_srt_format(word_info['end'])
|
| 244 |
word=word_info['word']
|
| 245 |
+
word =re.sub(punctuation_pattern, '', word)
|
| 246 |
+
if shorts==False:
|
| 247 |
+
word=word.replace("-","")
|
| 248 |
srt_file.write(f"{i}\n{start_time} --> {end_time}\n{word}\n\n")
|
| 249 |
|
| 250 |
+
|
| 251 |
def generate_srt_from_sentences(sentence_timestamp, srt_path="default_subtitle.srt"):
|
| 252 |
with open(srt_path, 'w', encoding='utf-8') as srt_file:
|
| 253 |
for index, sentence in enumerate(sentence_timestamp):
|
|
|
|
| 291 |
del faster_whisper_model
|
| 292 |
gc.collect()
|
| 293 |
torch.cuda.empty_cache()
|
| 294 |
+
|
| 295 |
word_segments=combine_word_segments(words_timestamp, max_words_per_subtitle=max_words_per_subtitle, min_silence_between_words=0.5)
|
| 296 |
+
shorts_segments=custom_word_segments(words_timestamp, min_silence_between_words=0.3, max_characters_per_subtitle=17)
|
| 297 |
#setup srt file names
|
| 298 |
base_name = os.path.basename(uploaded_file).rsplit('.', 1)[0][:30]
|
| 299 |
save_name = f"{subtitle_folder}/{base_name}_{src_lang}.srt"
|
|
|
|
| 301 |
original_txt_name=original_srt_name.replace(".srt",".txt")
|
| 302 |
word_level_srt_name=original_srt_name.replace(".srt","_word_level.srt")
|
| 303 |
customize_srt_name=original_srt_name.replace(".srt","_customize.srt")
|
| 304 |
+
shorts_srt_name=original_srt_name.replace(".srt","_shorts.srt")
|
| 305 |
|
| 306 |
generate_srt_from_sentences(sentence_timestamp, srt_path=original_srt_name)
|
| 307 |
word_level_srt(words_timestamp, srt_path=word_level_srt_name)
|
| 308 |
+
word_level_srt(shorts_segments, srt_path=shorts_srt_name,shorts=True)
|
| 309 |
write_subtitles_to_file(word_segments, filename=customize_srt_name)
|
| 310 |
with open(original_txt_name, 'w', encoding='utf-8') as f1:
|
| 311 |
f1.write(text)
|
| 312 |
+
return original_srt_name,customize_srt_name,word_level_srt_name,shorts_srt_name,original_txt_name
|
| 313 |
|
| 314 |
#@title Using Gradio Interface
|
| 315 |
def subtitle_maker(Audio_or_Video_File,Source_Language,max_words_per_subtitle):
|
| 316 |
try:
|
| 317 |
+
default_srt_path,customize_srt_path,word_level_srt_path,shorts_srt_name,text_path=whisper_subtitle(Audio_or_Video_File,Source_Language,max_words_per_subtitle=max_words_per_subtitle)
|
| 318 |
except Exception as e:
|
| 319 |
print(f"Error in whisper_subtitle: {e}")
|
| 320 |
+
default_srt_path,customize_srt_path,word_level_srt_path,shorts_srt_name,text_path=None,None,None,None,None
|
| 321 |
+
return default_srt_path,customize_srt_path,word_level_srt_path,shorts_srt_name,text_path
|
| 322 |
|
| 323 |
|
| 324 |
|
|
|
|
| 341 |
source_lang_list.extend(available_language)
|
| 342 |
|
| 343 |
|
| 344 |
+
|
| 345 |
@click.command()
|
| 346 |
@click.option("--debug", is_flag=True, default=False, help="Enable debug mode.")
|
| 347 |
@click.option("--share", is_flag=True, default=False, help="Enable sharing of the interface.")
|
|
|
|
| 359 |
gr.File(label="Default SRT File", show_label=True),
|
| 360 |
gr.File(label="Customize SRT File", show_label=True),
|
| 361 |
gr.File(label="Word Level SRT File", show_label=True),
|
| 362 |
+
gr.File(label="SRT File For Shorts", show_label=True),
|
| 363 |
gr.File(label="Text File", show_label=True)
|
| 364 |
]
|
| 365 |
|
|
|
|
| 369 |
# Launch Gradio with command-line options
|
| 370 |
demo.queue().launch(debug=debug, share=share)
|
| 371 |
if __name__ == "__main__":
|
| 372 |
+
main()
|