alikayh commited on
Commit
7ebcf95
·
verified ·
1 Parent(s): b16ed25

Upload translator_kayhgng.py

Browse files
Files changed (1) hide show
  1. translator_kayhgng.py +120 -0
translator_kayhgng.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import re
3
+ from googletrans import Translator
4
+ import logging
5
+
6
+ # Suppress Streamlit warnings
7
+ logging.getLogger('streamlit').setLevel(logging.ERROR)
8
+
9
+ # Initialize Translator
10
+ translator = Translator()
11
+
12
+ # Custom CSS for Azure-themed UI
13
+ st.markdown(
14
+ """
15
+ <style>
16
+ .stApp {
17
+ background-color: #15202b;
18
+ color: white;
19
+ font-family: Arial, sans-serif;
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
+ st.title("SRT Subtitle Translator")
45
+ st.write("Upload your SRT file and choose the target language to translate.")
46
+
47
+ # File Upload
48
+ uploaded_file = st.file_uploader("Choose an SRT file", type="srt")
49
+
50
+ if uploaded_file is not None:
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
+ texts = clean_and_extract(lines)
74
+
75
+ # Choose Target Language
76
+ target_language = st.text_input("Enter target language code (e.g., 'en', 'fr'):").lower()
77
+
78
+ if st.button("Translate"):
79
+ translated_texts = []
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
+ )