File size: 3,549 Bytes
7ebcf95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import streamlit as st
import re
from googletrans import Translator
import logging

# Suppress Streamlit warnings
logging.getLogger('streamlit').setLevel(logging.ERROR)

# Initialize Translator
translator = Translator()

# Custom CSS for Azure-themed UI
st.markdown(
    """

    <style>

    .stApp {

        background-color: #15202b;

        color: white;

        font-family: Arial, sans-serif;

    }

    .stButton > button {

        background-color: #007fbf;

        color: white;

        border-radius: 20px;

        border: none;

    }

    .stTextInput > div > div > input {

        background-color: #2c3947;

        color: white;

        border: 1px solid #007fbf;

        border-radius: 20px;

    }

    .footer {

        text-align: center;

        font-size: 14px;

        margin-top: 20px;

        color: #999;

    }

    </style>

    """,
    unsafe_allow_html=True
)

st.title("SRT Subtitle Translator")
st.write("Upload your SRT file and choose the target language to translate.")

# File Upload
uploaded_file = st.file_uploader("Choose an SRT file", type="srt")

if uploaded_file is not None:
    content = uploaded_file.read().decode('utf-8')
    lines = content.split('\n')

    # Extract text without timestamps
    def clean_and_extract(lines):
        cleaned_lines = []
        temp_lines = []
        for line in lines:
            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):
                continue
            if line == '':
                if temp_lines:
                    cleaned_text = '\n'.join(temp_lines)
                    cleaned_lines.append(cleaned_text)
                temp_lines = []
            else:
                temp_lines.append(line)
        if temp_lines:
            cleaned_text = '\n'.join(temp_lines)
            cleaned_lines.append(cleaned_text)
        return cleaned_lines

    texts = clean_and_extract(lines)

    # Choose Target Language
    target_language = st.text_input("Enter target language code (e.g., 'en', 'fr'):").lower()
    
    if st.button("Translate"):
        translated_texts = []
        for text in texts:
            try:
                translated = translator.translate(text, dest=target_language)
                translated_texts.append(translated.text)
            except Exception as e:
                st.error(f"Translation error: {e}")

        # Reformat SRT with translated text
        new_lines = []
        text_iter = iter(translated_texts)
        for line in lines:
            if re.match(r'^\d+$', line):
                new_lines.append(line)
            elif re.match(r'^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}$', line):
                new_lines.append(line)
                try:
                    new_lines.append(next(text_iter))
                except StopIteration:
                    break
            elif line == '':
                new_lines.append(line)

        new_content = '\n'.join(new_lines)

        # Download Button
        st.download_button(
            label="Download Translated SRT File",
            data=new_content,
            file_name="translated_subtitles.srt",
            mime="text/plain"
        )

# Footer
st.markdown(
    """

    <div class="footer">

        Designed by <a href="https://github.com/kayhgng" style="color: #007fbf; text-decoration: none;">alikay_h</a>

    </div>

    """,
    unsafe_allow_html=True
)