| from transformers import pipeline
|
| from datasets import Dataset
|
| import streamlit as st
|
| import torch
|
|
|
|
|
| st.set_page_config(
|
| page_title="English to Nyishi Translator",
|
| page_icon=":repeat:",
|
| layout="wide",
|
| )
|
|
|
|
|
| st.title(":repeat: English to Nyishi Translator")
|
| st.markdown("Welcome to the English to Nyishi Translator. :sparkles: Simply enter your text in English, and get the translation in Nyishi instantly! :thumbsup:")
|
|
|
|
|
| if 'text_input' not in st.session_state:
|
| st.session_state.text_input = ""
|
| text_input = st.text_area("Enter English text to translate", height=150, value=st.session_state.text_input)
|
|
|
|
|
| model_directory = "repleeka/eng-nyi-nmt"
|
|
|
| device = 0 if torch.cuda.is_available() else -1
|
| translation_pipeline = pipeline(
|
| task="translation",
|
| model="repleeka/eng-nyi-nmt",
|
| tokenizer="repleeka/eng-nyi-nmt",
|
| device=device
|
| )
|
|
|
|
|
| if st.button("Translate", key="translate_button"):
|
| if text_input:
|
| with st.spinner("Translating... Please wait"):
|
|
|
| sentences = [text_input]
|
| data = Dataset.from_dict({"text": sentences})
|
|
|
|
|
| try:
|
| results = data.map(lambda x: {"translation": translation_pipeline(x["text"])})
|
| result = results[0]["translation"][0]['translation_text']
|
|
|
|
|
| result = result.capitalize()
|
|
|
|
|
| st.markdown("#### Translated text:")
|
| st.markdown(f'<h2 class="result-text">{result}</2>', unsafe_allow_html=True)
|
|
|
|
|
| except Exception as e:
|
| st.error(f"Translation error: {e}")
|
| else:
|
| st.warning("Please enter text to translate.")
|
|
|
|
|
| if st.button("Clear Input"):
|
| st.session_state.text_input = "" |