Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,120 +1,35 @@
|
|
1 |
import streamlit as st
|
2 |
-
import re
|
3 |
from googletrans import Translator
|
4 |
-
import logging
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
.stButton > button {
|
22 |
-
background-color: #007fbf;
|
23 |
-
color: white;
|
24 |
-
border-radius: 20px;
|
25 |
-
border: none;
|
26 |
-
}
|
27 |
-
.stTextInput > div > div > input {
|
28 |
-
background-color: #2c3947;
|
29 |
-
color: white;
|
30 |
-
border: 1px solid #007fbf;
|
31 |
-
border-radius: 20px;
|
32 |
-
}
|
33 |
-
.footer {
|
34 |
-
text-align: center;
|
35 |
-
font-size: 14px;
|
36 |
-
margin-top: 20px;
|
37 |
-
color: #999;
|
38 |
-
}
|
39 |
-
</style>
|
40 |
-
""",
|
41 |
-
unsafe_allow_html=True
|
42 |
-
)
|
43 |
|
44 |
-
|
45 |
-
st.write("Upload your SRT file and choose the target language to translate.")
|
46 |
|
47 |
-
|
48 |
-
uploaded_file = st.file_uploader("Choose an SRT file", type="srt")
|
49 |
|
50 |
-
|
51 |
-
content = uploaded_file.read().decode('utf-8')
|
52 |
-
lines = content.split('\n')
|
53 |
-
|
54 |
-
# Extract text without timestamps
|
55 |
-
def clean_and_extract(lines):
|
56 |
-
cleaned_lines = []
|
57 |
-
temp_lines = []
|
58 |
-
for line in lines:
|
59 |
-
if re.match(r'^\d+$', line) or re.match(r'^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}$', line):
|
60 |
-
continue
|
61 |
-
if line == '':
|
62 |
-
if temp_lines:
|
63 |
-
cleaned_text = '\n'.join(temp_lines)
|
64 |
-
cleaned_lines.append(cleaned_text)
|
65 |
-
temp_lines = []
|
66 |
-
else:
|
67 |
-
temp_lines.append(line)
|
68 |
-
if temp_lines:
|
69 |
-
cleaned_text = '\n'.join(temp_lines)
|
70 |
-
cleaned_lines.append(cleaned_text)
|
71 |
-
return cleaned_lines
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
for text in texts:
|
81 |
-
try:
|
82 |
-
translated = translator.translate(text, dest=target_language)
|
83 |
-
translated_texts.append(translated.text)
|
84 |
-
except Exception as e:
|
85 |
-
st.error(f"Translation error: {e}")
|
86 |
-
|
87 |
-
# Reformat SRT with translated text
|
88 |
-
new_lines = []
|
89 |
-
text_iter = iter(translated_texts)
|
90 |
-
for line in lines:
|
91 |
-
if re.match(r'^\d+$', line):
|
92 |
-
new_lines.append(line)
|
93 |
-
elif re.match(r'^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}$', line):
|
94 |
-
new_lines.append(line)
|
95 |
-
try:
|
96 |
-
new_lines.append(next(text_iter))
|
97 |
-
except StopIteration:
|
98 |
-
break
|
99 |
-
elif line == '':
|
100 |
-
new_lines.append(line)
|
101 |
-
|
102 |
-
new_content = '\n'.join(new_lines)
|
103 |
-
|
104 |
-
# Download Button
|
105 |
-
st.download_button(
|
106 |
-
label="Download Translated SRT File",
|
107 |
-
data=new_content,
|
108 |
-
file_name="translated_subtitles.srt",
|
109 |
-
mime="text/plain"
|
110 |
-
)
|
111 |
-
|
112 |
-
# Footer
|
113 |
-
st.markdown(
|
114 |
-
"""
|
115 |
-
<div class="footer">
|
116 |
-
Designed by <a href="https://github.com/kayhgng" style="color: #007fbf; text-decoration: none;">alikay_h</a>
|
117 |
-
</div>
|
118 |
-
""",
|
119 |
-
unsafe_allow_html=True
|
120 |
-
)
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from googletrans import Translator
|
|
|
3 |
|
4 |
+
def translate_subtitles(input_file):
|
5 |
+
nums = ['0','1', '2', '3', '4', '5', '6', '7', '8', '9']
|
6 |
+
translator = Translator()
|
7 |
+
output_lines = []
|
8 |
|
9 |
+
with open(input_file, "r", encoding="utf-8") as f:
|
10 |
+
sub_lines = f.readlines()
|
11 |
|
12 |
+
for line_en in sub_lines:
|
13 |
+
if line_en[0] in nums:
|
14 |
+
output_lines.append(line_en)
|
15 |
+
elif line_en.strip() == '':
|
16 |
+
output_lines.append('\n')
|
17 |
+
else:
|
18 |
+
if line_en.strip():
|
19 |
+
line_fa = translator.translate(line_en, src='en', dest='fa')
|
20 |
+
output_lines.append(line_fa.text + '\n')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
return output_lines
|
|
|
23 |
|
24 |
+
st.title("ترجمه زیرنویس با استفاده از Google Translate")
|
|
|
25 |
|
26 |
+
uploaded_file = st.file_uploader("فایل زیرنویس خود را بارگذاری کنید", type=["srt", "txt"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
if uploaded_file is not None:
|
29 |
+
output_lines = translate_subtitles(uploaded_file)
|
30 |
+
st.write("ترجمه شده:")
|
31 |
+
for line in output_lines:
|
32 |
+
st.write(line, end='')
|
33 |
|
34 |
+
output_text = "".join(output_lines)
|
35 |
+
st.download_button(label="دانلود فایل ترجمه شده", data=output_text, file_name="زیرنویس_ترجمهشده.srt", mime='text/srt')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|