Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,23 @@
|
|
1 |
-
import
|
2 |
-
import pandas as pd
|
3 |
import gradio as gr
|
|
|
4 |
from transformers import MarianMTModel, MarianTokenizer
|
5 |
-
import
|
6 |
-
import pysrt
|
7 |
|
8 |
-
# Fetch and parse language options
|
9 |
url = "https://huggingface.co/Lenylvt/LanguageISO/resolve/main/iso.md"
|
10 |
-
|
11 |
-
df = pd.read_csv(io.StringIO(response.text), delimiter="|", skiprows=2, header=None).dropna(axis=1, how='all')
|
12 |
df.columns = ['ISO 639-1', 'ISO 639-2', 'Language Name', 'Native Name']
|
13 |
df['ISO 639-1'] = df['ISO 639-1'].str.strip()
|
14 |
|
15 |
# Prepare language options for the dropdown
|
16 |
-
language_options = [(row['ISO 639-1'], f"{row['ISO 639-1']}
|
17 |
|
18 |
def translate_text(text, source_language_code, target_language_code):
|
19 |
# Construct model name using ISO 639-1 codes
|
20 |
model_name = f"Helsinki-NLP/opus-mt-{source_language_code}-{target_language_code}"
|
21 |
|
22 |
-
# Check if source and target languages are the same
|
23 |
if source_language_code == target_language_code:
|
24 |
return "Translation between the same languages is not supported."
|
25 |
|
@@ -36,38 +34,30 @@ def translate_text(text, source_language_code, target_language_code):
|
|
36 |
|
37 |
return translated_text
|
38 |
|
39 |
-
def translate_srt(
|
40 |
-
#
|
41 |
-
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
|
46 |
# Translate each subtitle
|
47 |
-
for sub in subs:
|
48 |
translated_text = translate_text(sub.text, source_language_code, target_language_code)
|
49 |
-
|
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
|
57 |
|
58 |
source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")
|
59 |
target_language_dropdown = gr.Dropdown(choices=language_options, label="Target Language")
|
|
|
60 |
|
61 |
iface = gr.Interface(
|
62 |
fn=translate_srt,
|
63 |
-
inputs=[
|
64 |
-
|
65 |
-
source_language_dropdown,
|
66 |
-
target_language_dropdown
|
67 |
-
],
|
68 |
-
outputs=gr.File(label="Download Translated SRT File"),
|
69 |
title="SRT Translator",
|
70 |
-
description="Translate
|
71 |
)
|
72 |
|
73 |
iface.launch()
|
|
|
1 |
+
import pysrt
|
|
|
2 |
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
from transformers import MarianMTModel, MarianTokenizer
|
5 |
+
from tqdm import tqdm
|
|
|
6 |
|
7 |
+
# Fetch and parse language options from the provided URL
|
8 |
url = "https://huggingface.co/Lenylvt/LanguageISO/resolve/main/iso.md"
|
9 |
+
df = pd.read_csv(url, delimiter="|", skiprows=2, header=None).dropna(axis=1, how='all')
|
|
|
10 |
df.columns = ['ISO 639-1', 'ISO 639-2', 'Language Name', 'Native Name']
|
11 |
df['ISO 639-1'] = df['ISO 639-1'].str.strip()
|
12 |
|
13 |
# Prepare language options for the dropdown
|
14 |
+
language_options = [(row['ISO 639-1'], f"{row['ISO 639-1']}") for index, row in df.iterrows()]
|
15 |
|
16 |
def translate_text(text, source_language_code, target_language_code):
|
17 |
# Construct model name using ISO 639-1 codes
|
18 |
model_name = f"Helsinki-NLP/opus-mt-{source_language_code}-{target_language_code}"
|
19 |
|
20 |
+
# Check if source and target languages are the same, which is not supported for translation
|
21 |
if source_language_code == target_language_code:
|
22 |
return "Translation between the same languages is not supported."
|
23 |
|
|
|
34 |
|
35 |
return translated_text
|
36 |
|
37 |
+
def translate_srt(input_file, source_language_code, target_language_code):
|
38 |
+
# Load SRT file
|
39 |
+
subs = pysrt.open(input_file)
|
40 |
|
41 |
+
# Initialize an empty list to store translated subtitles
|
42 |
+
translated_subs = []
|
43 |
|
44 |
# Translate each subtitle
|
45 |
+
for sub in tqdm(subs, desc="Translating"):
|
46 |
translated_text = translate_text(sub.text, source_language_code, target_language_code)
|
47 |
+
translated_subs.append(translated_text)
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
return "\n".join(translated_subs)
|
50 |
|
51 |
source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")
|
52 |
target_language_dropdown = gr.Dropdown(choices=language_options, label="Target Language")
|
53 |
+
file_input = gr.inputs.File(label="Upload SRT File", type="text")
|
54 |
|
55 |
iface = gr.Interface(
|
56 |
fn=translate_srt,
|
57 |
+
inputs=[file_input, source_language_dropdown, target_language_dropdown],
|
58 |
+
outputs=gr.Textbox(label="Translated SRT"),
|
|
|
|
|
|
|
|
|
59 |
title="SRT Translator",
|
60 |
+
description="Translate subtitles from one language to another."
|
61 |
)
|
62 |
|
63 |
iface.launch()
|