Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -288,7 +288,44 @@ def clean_text(text):
|
|
| 288 |
text = re.sub(r"^```|```$", '', text)
|
| 289 |
text = re.sub(r'^srt', '', text, flags=re.MULTILINE)
|
| 290 |
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
def enhance_text(api_key, text):
|
| 293 |
url = "https://api.one-api.ir/chatbot/v1/gpt4o/"
|
| 294 |
|
|
@@ -380,9 +417,14 @@ def process_video(url, type):
|
|
| 380 |
subtitle_file = generate_subtitle_file(language=language, segments=segments, input_video_name=input_video_name)
|
| 381 |
source_language = "en"
|
| 382 |
target_language = "fa"
|
| 383 |
-
srt_string = read_srt_file(subtitle_file)
|
| 384 |
-
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
srt = read_srt_file("google_translate.srt")
|
| 387 |
return srt, input_video
|
| 388 |
|
|
|
|
| 288 |
text = re.sub(r"^```|```$", '', text)
|
| 289 |
text = re.sub(r'^srt', '', text, flags=re.MULTILINE)
|
| 290 |
return text
|
| 291 |
+
def split_srt_file(input_file, max_chars=3000):
|
| 292 |
+
# Read the contents of the SRT file
|
| 293 |
+
with open(input_file, 'r', encoding='utf-8') as file:
|
| 294 |
+
content = file.read()
|
| 295 |
|
| 296 |
+
# Split the content into individual subtitles
|
| 297 |
+
subtitles = content.strip().split('\n\n')
|
| 298 |
+
|
| 299 |
+
# Prepare to write the split files
|
| 300 |
+
output_files = []
|
| 301 |
+
current_file_content = ''
|
| 302 |
+
current_file_index = 1
|
| 303 |
+
|
| 304 |
+
for subtitle in subtitles:
|
| 305 |
+
# Check if adding this subtitle would exceed the character limit
|
| 306 |
+
if len(current_file_content) + len(subtitle) + 2 > max_chars: # +2 for \n\n
|
| 307 |
+
# Write the current file
|
| 308 |
+
output_file_name = f'split_{current_file_index}.srt'
|
| 309 |
+
with open(output_file_name, 'w', encoding='utf-8') as output_file:
|
| 310 |
+
output_file.write(current_file_content.strip())
|
| 311 |
+
output_files.append(output_file_name)
|
| 312 |
+
|
| 313 |
+
# Prepare for the next file
|
| 314 |
+
current_file_index += 1
|
| 315 |
+
current_file_content = subtitle + '\n\n'
|
| 316 |
+
else:
|
| 317 |
+
# If it fits, add the subtitle
|
| 318 |
+
current_file_content += subtitle + '\n\n'
|
| 319 |
+
|
| 320 |
+
# Write any remaining content to a new SRT file
|
| 321 |
+
if current_file_content:
|
| 322 |
+
output_file_name = f'split_{current_file_index}.srt'
|
| 323 |
+
with open(output_file_name, 'w', encoding='utf-8') as output_file:
|
| 324 |
+
output_file.write(current_file_content.strip())
|
| 325 |
+
output_files.append(output_file_name)
|
| 326 |
+
|
| 327 |
+
return output_files
|
| 328 |
+
|
| 329 |
def enhance_text(api_key, text):
|
| 330 |
url = "https://api.one-api.ir/chatbot/v1/gpt4o/"
|
| 331 |
|
|
|
|
| 417 |
subtitle_file = generate_subtitle_file(language=language, segments=segments, input_video_name=input_video_name)
|
| 418 |
source_language = "en"
|
| 419 |
target_language = "fa"
|
| 420 |
+
#srt_string = read_srt_file(subtitle_file)
|
| 421 |
+
srt_files=split_srt_file(subtitle_file)
|
| 422 |
+
for i in srt_files:
|
| 423 |
+
srt_string = read_srt_file(f"/content/{i}")
|
| 424 |
+
#google_translate = translate_text(api_key, source_language, target_language, srt_string)
|
| 425 |
+
google_translate = enhance_text(api_key, srt_string)
|
| 426 |
+
write_google(google_translate)
|
| 427 |
+
time.sleep(15)
|
| 428 |
srt = read_srt_file("google_translate.srt")
|
| 429 |
return srt, input_video
|
| 430 |
|