Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
-
import tempfile, os, zipfile
|
4 |
|
5 |
translator_cache = {}
|
6 |
|
@@ -21,14 +21,17 @@ def get_translator(src_lang, tgt_lang):
|
|
21 |
return translator_cache[key]
|
22 |
|
23 |
def translate_text(text, src, tgt):
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
32 |
|
33 |
def parse_srt(srt_text):
|
34 |
blocks = srt_text.strip().split("\n\n")
|
@@ -47,15 +50,16 @@ def reassemble_srt(subtitles):
|
|
47 |
return "\n\n".join(f"{idx}\n{ts}\n{txt}" for idx, ts, txt in subtitles)
|
48 |
|
49 |
def process_file(file_obj, src_lang, tgt_lang, output_dir):
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
subtitles = parse_srt(raw_text)
|
52 |
|
53 |
translated_subs = []
|
54 |
for idx, ts, txt in subtitles:
|
55 |
-
|
56 |
-
translated = translate_text(txt, src_lang, tgt_lang)
|
57 |
-
except Exception as e:
|
58 |
-
translated = f"[Error: {e}]"
|
59 |
translated_subs.append((idx, ts, translated))
|
60 |
|
61 |
output_path = os.path.join(output_dir, os.path.basename(file_obj.name))
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import tempfile, os, zipfile
|
4 |
|
5 |
translator_cache = {}
|
6 |
|
|
|
21 |
return translator_cache[key]
|
22 |
|
23 |
def translate_text(text, src, tgt):
|
24 |
+
try:
|
25 |
+
if (src, tgt) in MODEL_MAP:
|
26 |
+
translator = get_translator(src, tgt)
|
27 |
+
return translator(text, max_length=512)[0]["translation_text"]
|
28 |
+
elif (src == "zh" and tgt == "ja") or (src == "ja" and tgt == "zh"):
|
29 |
+
mid = translate_text(text, src, "en")
|
30 |
+
return translate_text(mid, "en", tgt)
|
31 |
+
else:
|
32 |
+
return f"[Unsupported language pair: {src}→{tgt}]"
|
33 |
+
except Exception as e:
|
34 |
+
return f"[Translation error: {e}]"
|
35 |
|
36 |
def parse_srt(srt_text):
|
37 |
blocks = srt_text.strip().split("\n\n")
|
|
|
50 |
return "\n\n".join(f"{idx}\n{ts}\n{txt}" for idx, ts, txt in subtitles)
|
51 |
|
52 |
def process_file(file_obj, src_lang, tgt_lang, output_dir):
|
53 |
+
try:
|
54 |
+
raw_text = file_obj.read().decode("utf-8")
|
55 |
+
except:
|
56 |
+
raw_text = file_obj.read().decode("utf-8", errors="ignore")
|
57 |
+
|
58 |
subtitles = parse_srt(raw_text)
|
59 |
|
60 |
translated_subs = []
|
61 |
for idx, ts, txt in subtitles:
|
62 |
+
translated = translate_text(txt, src_lang, tgt_lang)
|
|
|
|
|
|
|
63 |
translated_subs.append((idx, ts, translated))
|
64 |
|
65 |
output_path = os.path.join(output_dir, os.path.basename(file_obj.name))
|