Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import MarianTokenizer, MarianMTModel
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
@st.cache(allow_output_mutation=True, suppress_st_warning=True)
|
| 5 |
+
|
| 6 |
+
def download_model():
|
| 7 |
+
model_name = f'Helsinki-NLP/opus-mt-en-ur'
|
| 8 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 9 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 10 |
+
return model, tokenizer
|
| 11 |
+
|
| 12 |
+
st.title('English to Urdu Translator')
|
| 13 |
+
st.markdown("[Developd By: Aziz Ahmad](https://www.linkedin.com/in/theazizahmad/)", unsafe_allow_html=True)
|
| 14 |
+
|
| 15 |
+
text = st.text_area("Enter Text:", value='', height=None, max_chars=None, key=None)
|
| 16 |
+
model, tokenizer = download_model()
|
| 17 |
+
|
| 18 |
+
if st.button('Translate to Urdu'):
|
| 19 |
+
if text == '':
|
| 20 |
+
st.write('Please enter English text for translation')
|
| 21 |
+
else:
|
| 22 |
+
|
| 23 |
+
# Tokenize the text
|
| 24 |
+
batch = tokenizer(text, return_tensors="pt", padding=True)
|
| 25 |
+
# tokenized text maximum allowed size of 512
|
| 26 |
+
batch["input_ids"] = batch["input_ids"][:, :512]
|
| 27 |
+
batch["attention_mask"] = batch["attention_mask"][:, :512]
|
| 28 |
+
translation_encoded = model.generate(**batch)
|
| 29 |
+
translation = tokenizer.batch_decode(translation_encoded, skip_special_tokens=True)
|
| 30 |
+
st.write('', str(translation).strip('][\''))
|
| 31 |
+
else: pass
|