File size: 1,266 Bytes
d96ca2a
 
 
2fbfcba
 
 
 
d96ca2a
2fbfcba
 
d96ca2a
2fbfcba
 
 
 
 
 
 
 
 
d96ca2a
2fbfcba
d96ca2a
2fbfcba
d96ca2a
2fbfcba
d96ca2a
2fbfcba
 
 
 
 
d96ca2a
2fbfcba
 
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
import streamlit as st
from googletrans import Translator

def translate_subtitles(input_file):
    nums = ['0','1', '2', '3', '4', '5', '6', '7', '8', '9']
    translator = Translator()
    output_lines = []

    with open(input_file, "r", encoding="utf-8") as f:
        sub_lines = f.readlines()

    for line_en in sub_lines:
        if line_en[0] in nums:
            output_lines.append(line_en)
        elif line_en.strip() == '':
            output_lines.append('\n')
        else:
            if line_en.strip():
                line_fa = translator.translate(line_en, src='en', dest='fa')
                output_lines.append(line_fa.text + '\n')

    return output_lines

st.title("ترجمه زیرنویس با استفاده از Google Translate")

uploaded_file = st.file_uploader("فایل زیرنویس خود را بارگذاری کنید", type=["srt", "txt"])

if uploaded_file is not None:
    output_lines = translate_subtitles(uploaded_file)
    st.write("ترجمه شده:")
    for line in output_lines:
        st.write(line, end='')
    
    output_text = "".join(output_lines)
    st.download_button(label="دانلود فایل ترجمه شده", data=output_text, file_name="زیرنویس_ترجمه‌شده.srt", mime='text/srt')