Spaces:
Sleeping
Sleeping
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') |