Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,12 +37,18 @@ def translate_text(text, source_language_code, target_language_code):
|
|
| 37 |
|
| 38 |
def translate_srt(input_file, source_language_code, target_language_code):
|
| 39 |
subs = pysrt.open(input_file)
|
|
|
|
| 40 |
translated_subs = []
|
|
|
|
|
|
|
| 41 |
for idx, sub in enumerate(subs):
|
| 42 |
translated_text = translate_text(sub.text, source_language_code, target_language_code)
|
| 43 |
translated_sub = pysrt.SubRipItem(index=idx+1, start=sub.start, end=sub.end, text=translated_text)
|
| 44 |
translated_subs.append(translated_sub)
|
|
|
|
|
|
|
| 45 |
translated_file = pysrt.SubRipFile(translated_subs)
|
|
|
|
| 46 |
return translated_file
|
| 47 |
|
| 48 |
st.title("SRT Translator")
|
|
@@ -61,15 +67,12 @@ if language_options:
|
|
| 61 |
file_input = st.file_uploader("Upload SRT File", type=["srt"])
|
| 62 |
|
| 63 |
if file_input is not None and source_language_code and target_language_code:
|
| 64 |
-
# Save the uploaded file to a temporary file
|
| 65 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".srt") as temp_file:
|
| 66 |
temp_file.write(file_input.getvalue())
|
| 67 |
temp_file_path = temp_file.name
|
| 68 |
|
| 69 |
-
# Now, temp_file_path contains the path to the temporary file which you can pass to translate_srt
|
| 70 |
translated_srt = translate_srt(temp_file_path, source_language_code, target_language_code)
|
| 71 |
|
| 72 |
-
# Proceed with the translation and file download preparation as before
|
| 73 |
buffer = BytesIO()
|
| 74 |
translated_srt.save(buffer, encoding='utf-8')
|
| 75 |
buffer.seek(0)
|
|
@@ -81,4 +84,4 @@ if file_input is not None and source_language_code and target_language_code:
|
|
| 81 |
data=translated_srt_bytes,
|
| 82 |
file_name="translated_subtitles.srt",
|
| 83 |
mime="text/plain",
|
| 84 |
-
)
|
|
|
|
| 37 |
|
| 38 |
def translate_srt(input_file, source_language_code, target_language_code):
|
| 39 |
subs = pysrt.open(input_file)
|
| 40 |
+
total_subs = len(subs)
|
| 41 |
translated_subs = []
|
| 42 |
+
progress_bar = st.progress(0) # Initialize the progress bar
|
| 43 |
+
|
| 44 |
for idx, sub in enumerate(subs):
|
| 45 |
translated_text = translate_text(sub.text, source_language_code, target_language_code)
|
| 46 |
translated_sub = pysrt.SubRipItem(index=idx+1, start=sub.start, end=sub.end, text=translated_text)
|
| 47 |
translated_subs.append(translated_sub)
|
| 48 |
+
progress_bar.progress((idx + 1) / total_subs) # Update progress bar
|
| 49 |
+
|
| 50 |
translated_file = pysrt.SubRipFile(translated_subs)
|
| 51 |
+
progress_bar.empty() # Optionally clear the progress bar after completion
|
| 52 |
return translated_file
|
| 53 |
|
| 54 |
st.title("SRT Translator")
|
|
|
|
| 67 |
file_input = st.file_uploader("Upload SRT File", type=["srt"])
|
| 68 |
|
| 69 |
if file_input is not None and source_language_code and target_language_code:
|
|
|
|
| 70 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".srt") as temp_file:
|
| 71 |
temp_file.write(file_input.getvalue())
|
| 72 |
temp_file_path = temp_file.name
|
| 73 |
|
|
|
|
| 74 |
translated_srt = translate_srt(temp_file_path, source_language_code, target_language_code)
|
| 75 |
|
|
|
|
| 76 |
buffer = BytesIO()
|
| 77 |
translated_srt.save(buffer, encoding='utf-8')
|
| 78 |
buffer.seek(0)
|
|
|
|
| 84 |
data=translated_srt_bytes,
|
| 85 |
file_name="translated_subtitles.srt",
|
| 86 |
mime="text/plain",
|
| 87 |
+
)
|