Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,23 +36,23 @@ def translate_text(text, source_language_code, target_language_code):
|
|
| 36 |
|
| 37 |
return translated_text
|
| 38 |
|
| 39 |
-
def translate_srt(
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Translate each subtitle
|
| 44 |
for sub in subs:
|
| 45 |
translated_text = translate_text(sub.text, source_language_code, target_language_code)
|
| 46 |
sub.text = translated_text
|
| 47 |
-
|
| 48 |
-
# Convert the subtitles back to an SRT string
|
| 49 |
-
srt_content = '\n'.join(subs.to_string())
|
| 50 |
|
| 51 |
-
# Save the translated
|
| 52 |
output_path = "/mnt/data/translated_srt.srt"
|
| 53 |
with open(output_path, "w", encoding="utf-8") as file:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
return output_path
|
| 57 |
|
| 58 |
source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")
|
|
|
|
| 36 |
|
| 37 |
return translated_text
|
| 38 |
|
| 39 |
+
def translate_srt(file_info, source_language_code, target_language_code):
|
| 40 |
+
# Assuming file_info is a dictionary with 'content' holding the file's bytes
|
| 41 |
+
file_content = file_info['content'] # Correctly access the bytes content of the file
|
| 42 |
+
|
| 43 |
+
# Use pysrt to load subtitles from the file content
|
| 44 |
+
subs = pysrt.open(io.BytesIO(file_content))
|
| 45 |
|
| 46 |
# Translate each subtitle
|
| 47 |
for sub in subs:
|
| 48 |
translated_text = translate_text(sub.text, source_language_code, target_language_code)
|
| 49 |
sub.text = translated_text
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Save the translated subtitles to a temporary file
|
| 52 |
output_path = "/mnt/data/translated_srt.srt"
|
| 53 |
with open(output_path, "w", encoding="utf-8") as file:
|
| 54 |
+
subs.save(file, encoding='utf-8')
|
| 55 |
+
|
| 56 |
return output_path
|
| 57 |
|
| 58 |
source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")
|