Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import pandas as pd
|
|
3 |
import gradio as gr
|
4 |
from transformers import MarianMTModel, MarianTokenizer
|
5 |
import io
|
|
|
6 |
|
7 |
# Fetch and parse language options
|
8 |
url = "https://huggingface.co/Lenylvt/LanguageISO/resolve/main/iso.md"
|
@@ -35,21 +36,23 @@ def translate_text(text, source_language_code, target_language_code):
|
|
35 |
|
36 |
return translated_text
|
37 |
|
38 |
-
def translate_srt(
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
50 |
output_path = "/mnt/data/translated_srt.srt"
|
51 |
with open(output_path, "w", encoding="utf-8") as file:
|
52 |
-
file.write(
|
|
|
53 |
return output_path
|
54 |
|
55 |
source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")
|
|
|
3 |
import gradio as gr
|
4 |
from transformers import MarianMTModel, MarianTokenizer
|
5 |
import io
|
6 |
+
import pysrt
|
7 |
|
8 |
# Fetch and parse language options
|
9 |
url = "https://huggingface.co/Lenylvt/LanguageISO/resolve/main/iso.md"
|
|
|
36 |
|
37 |
return translated_text
|
38 |
|
39 |
+
def translate_srt(file_content, source_language_code, target_language_code):
|
40 |
+
# Load the SRT content using pysrt and io.BytesIO
|
41 |
+
subs = pysrt.from_string(file_content.decode("utf-8"))
|
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 SRT content to a temporary file
|
52 |
output_path = "/mnt/data/translated_srt.srt"
|
53 |
with open(output_path, "w", encoding="utf-8") as file:
|
54 |
+
file.write(srt_content)
|
55 |
+
|
56 |
return output_path
|
57 |
|
58 |
source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")
|