Amooec commited on
Commit
474a7b2
·
verified ·
1 Parent(s): 8899a97

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pysrt
3
+ from transformers import MarianMTModel, MarianTokenizer
4
+
5
+ # تابع برای ترجمه متون
6
+ def translate_text(text, model, tokenizer):
7
+ translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True))
8
+ translated_text = [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
9
+ return translated_text[0]
10
+
11
+ # بارگذاری مدل و توکنایزر
12
+ model_name = 'Helsinki-NLP/opus-mt-en-fa'
13
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
14
+ model = MarianMTModel.from_pretrained(model_name)
15
+
16
+ st.title("SRT Translator: English to Persian")
17
+ st.write("Upload an SRT file to translate it from English to Persian.")
18
+
19
+ uploaded_file = st.file_uploader("Choose an SRT file", type="srt")
20
+
21
+ if uploaded_file is not None:
22
+ subs = pysrt.from_string(uploaded_file.read().decode('utf-8'))
23
+ for sub in subs:
24
+ sub.text = translate_text(sub.text, model, tokenizer)
25
+
26
+ output_srt = subs.to_string()
27
+ st.download_button(
28
+ label="Download Translated SRT",
29
+ data=output_srt,
30
+ file_name="translated.srt",
31
+ mime="text/srt"
32
+ )
33
+ st.text_area("Translated SRT Preview", output_srt, height=300)