Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
# Enable Zero GPU | |
os.environ["TOKENIZERS_PARALLELISM"] = "true" | |
model_name = "Mhassanen/nllb-200-600M-En-Ar" | |
tokenizer = AutoTokenizer.from_pretrained(model_name, src_lang="eng_Latn", tgt_lang="arz_Arab") | |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name) | |
def translate2(text): | |
import torch | |
# Ensure Zero GPU is enabled | |
torch.set_grad_enabled(False) | |
torch.set_num_threads(1) | |
inputs = tokenizer(text, return_tensors="pt", padding=True) | |
translated_tokens = model.generate(**inputs) | |
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True) | |
return translated_text[0] | |
st.set_page_config(page_title="English to Arabic Translation", page_icon="logo.png") | |
with st.sidebar: | |
st.image("logo.png", width=70) | |
st.markdown('<div style="position: absolute; left: 5px;"></div>', unsafe_allow_html=True) | |
st.markdown("# English to Arabic Translation") | |
st.markdown("---") | |
st.markdown("## About") | |
st.markdown(''' | |
- This App powered by [Mhassanen/nllb-200-600M-En-Ar](https://huggingface.co/Mhassanen/nllb-200-600M-En-Ar) Language model | |
''') | |
st.title("Try Now!") | |
text_to_translate = st.text_area("Enter text in English:") | |
if st.button("Translate"): | |
if text_to_translate: | |
with st.spinner("Translating..."): | |
translation = translate2(text_to_translate) | |
st.success("Translation completed!") | |
st.text_area("Translated text in Arabic:", translation, height=200) | |
else: | |
st.warning("Please enter some text to translate.") | |