Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,13 @@ import pandas as pd
|
|
3 |
import pysrt
|
4 |
from transformers import MarianMTModel, MarianTokenizer
|
5 |
import tempfile
|
|
|
6 |
from io import BytesIO
|
7 |
import requests
|
8 |
|
9 |
def fetch_languages(url):
|
10 |
response = requests.get(url)
|
11 |
if response.status_code == 200:
|
12 |
-
csv_content = response.content.decode('utf-8')
|
13 |
df = pd.read_csv(BytesIO(response.content), delimiter="|", skiprows=2, header=None).dropna(axis=1, how='all')
|
14 |
df.columns = ['ISO 639-1', 'ISO 639-2', 'Language Name', 'Native Name']
|
15 |
df['ISO 639-1'] = df['ISO 639-1'].str.strip()
|
@@ -39,17 +39,17 @@ 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_text = "Operation in progress.
|
43 |
-
|
44 |
-
|
45 |
for idx, sub in enumerate(subs):
|
46 |
translated_text = translate_text(sub.text, source_language_code, target_language_code)
|
47 |
translated_sub = pysrt.SubRipItem(index=idx+1, start=sub.start, end=sub.end, text=translated_text)
|
48 |
translated_subs.append(translated_sub)
|
49 |
-
|
|
|
50 |
|
51 |
translated_file = pysrt.SubRipFile(items=translated_subs)
|
52 |
-
progress_bar.empty() # Optionally clear the progress bar after completion
|
53 |
return translated_file
|
54 |
|
55 |
st.title("SRT Translation")
|
@@ -70,19 +70,20 @@ file_input = st.file_uploader("π Upload SRT File", type=["srt"], accept_multi
|
|
70 |
if file_input is not None and source_language_code and target_language_code:
|
71 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".srt") as temp_file:
|
72 |
temp_file.write(file_input.getvalue())
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
# Convert translated SRT to a string, then encode to bytes for download
|
78 |
-
buffer = BytesIO()
|
79 |
-
translated_srt.save(buffer, encoding='utf-8')
|
80 |
-
buffer.seek(0)
|
81 |
-
translated_srt_bytes = buffer.getvalue()
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
st.download_button(
|
84 |
label="β¬οΈ Download Translated SRT",
|
85 |
data=translated_srt_bytes,
|
86 |
file_name="translated_subtitles.srt",
|
87 |
mime="text/plain",
|
88 |
-
)
|
|
|
3 |
import pysrt
|
4 |
from transformers import MarianMTModel, MarianTokenizer
|
5 |
import tempfile
|
6 |
+
import os
|
7 |
from io import BytesIO
|
8 |
import requests
|
9 |
|
10 |
def fetch_languages(url):
|
11 |
response = requests.get(url)
|
12 |
if response.status_code == 200:
|
|
|
13 |
df = pd.read_csv(BytesIO(response.content), delimiter="|", skiprows=2, header=None).dropna(axis=1, how='all')
|
14 |
df.columns = ['ISO 639-1', 'ISO 639-2', 'Language Name', 'Native Name']
|
15 |
df['ISO 639-1'] = df['ISO 639-1'].str.strip()
|
|
|
39 |
subs = pysrt.open(input_file)
|
40 |
total_subs = len(subs)
|
41 |
translated_subs = []
|
42 |
+
progress_text = "Operation in progress. Please wait."
|
43 |
+
st.progress(0, text=progress_text) # Initialize the progress bar placeholder
|
44 |
+
|
45 |
for idx, sub in enumerate(subs):
|
46 |
translated_text = translate_text(sub.text, source_language_code, target_language_code)
|
47 |
translated_sub = pysrt.SubRipItem(index=idx+1, start=sub.start, end=sub.end, text=translated_text)
|
48 |
translated_subs.append(translated_sub)
|
49 |
+
progress_percent = int(((idx + 1) / total_subs) * 100)
|
50 |
+
st.progress(progress_percent) # Update progress bar
|
51 |
|
52 |
translated_file = pysrt.SubRipFile(items=translated_subs)
|
|
|
53 |
return translated_file
|
54 |
|
55 |
st.title("SRT Translation")
|
|
|
70 |
if file_input is not None and source_language_code and target_language_code:
|
71 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".srt") as temp_file:
|
72 |
temp_file.write(file_input.getvalue())
|
73 |
+
temp_file.flush()
|
74 |
+
translated_srt = translate_srt(temp_file.name, source_language_code, target_language_code)
|
75 |
+
os.unlink(temp_file.name) # Delete the temp file
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
# Save the translated subtitles to a temporary file and then read it into BytesIO
|
78 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".srt") as temp_file:
|
79 |
+
translated_srt.save(temp_file.name, encoding='utf-8')
|
80 |
+
temp_file.seek(0)
|
81 |
+
translated_srt_bytes = open(temp_file.name, 'rb').read()
|
82 |
+
os.unlink(temp_file.name) # Delete the temp file after reading
|
83 |
+
|
84 |
st.download_button(
|
85 |
label="β¬οΈ Download Translated SRT",
|
86 |
data=translated_srt_bytes,
|
87 |
file_name="translated_subtitles.srt",
|
88 |
mime="text/plain",
|
89 |
+
)
|