import streamlit as st
import re
# Function to load model
@st.cache(allow_output_mutation=True)
def load_model():
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("abdulwaheed1/english-to-urdu-translation-mbart")
model = AutoModelForSeq2SeqLM.from_pretrained("abdulwaheed1/english-to-urdu-translation-mbart")
return tokenizer, model
# Custom CSS to style the GUI
st.markdown(
"""
""",
unsafe_allow_html=True,
)
# Function to preprocess text
def preprocess_text(text):
# Convert text to lowercase
text = text.lower()
# Remove HTML tags
text = re.sub(r'<[^>]*>', '', text)
# Remove special characters and extra spaces
text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
text = re.sub(r'\s+', ' ', text).strip()
return text
# Create or load model
tokenizer, model = load_model()
# Creating a container for the main content
with st.container():
st.title("English to Urdu Translation")
# Display input field for English text
st.subheader("Enter English Text:")
english_text = st.text_area("", height=200)
# Translate button
if st.button("Translate"):
if english_text:
# Preprocess text
english_text = preprocess_text(english_text)
# Tokenize input text
inputs = tokenizer(english_text, return_tensors="pt", max_length=1024, truncation=True)
# Generate translation
translation_ids = model.generate(**inputs)
translation = tokenizer.batch_decode(translation_ids, skip_special_tokens=True)[0]
# Display translated text
st.subheader("Translation:")
st.text_area("", value=translation, height=200)
else:
st.warning("Please enter some text to translate.")
# Footer
st.markdown(
"""
""",
unsafe_allow_html=True,
)