| import streamlit as st |
| import torch |
| from transformers import pipeline |
| from datasets import Dataset |
|
|
| |
| st.set_page_config( |
| page_title="English to Tawra Translator", |
| page_icon=":repeat:", |
| layout="wide", |
| ) |
|
|
| |
| st.markdown(""" |
| <style> |
| .result-text { |
| color: #1E88E5; |
| background-color: #f0f2f6; |
| padding: 20px; |
| border-radius: 10px; |
| border-left: 5px solid #1E88E5; |
| } |
| </style> |
| """, unsafe_allow_html=True) |
|
|
| |
| @st.cache_resource |
| def load_translator(): |
| model_id = "repleeka/eng-taw-nmt" |
| |
| device = 0 if torch.cuda.is_available() else -1 |
| return pipeline( |
| task="translation", |
| model=model_id, |
| tokenizer=model_id, |
| device=device |
| ) |
|
|
| translation_pipeline = load_translator() |
|
|
| |
| st.title(":repeat: English to Tawra Translator") |
| st.markdown("Welcome to the English to Tawra Translator. :sparkles: Simply enter your text in English, and get the translation in Tawra instantly! :thumbsup:") |
|
|
| |
| if 'text_input' not in st.session_state: |
| st.session_state.text_input = "" |
|
|
| def clear_text(): |
| st.session_state.text_input = "" |
|
|
| |
| text_input = st.text_area( |
| "Enter English text to translate", |
| height=150, |
| value=st.session_state.text_input, |
| key="current_input" |
| ) |
|
|
| |
| st.session_state.text_input = text_input |
|
|
| col1, col2 = st.columns([1, 10]) |
|
|
| with col1: |
| translate_clicked = st.button("Translate", type="primary") |
|
|
| with col2: |
| |
| if st.button("Clear Input"): |
| clear_text() |
| st.rerun() |
|
|
| |
| if translate_clicked: |
| if text_input.strip(): |
| with st.spinner("Translating... Please wait"): |
| try: |
| |
| sentences = [text_input] |
| data = Dataset.from_dict({"text": sentences}) |
|
|
| |
| |
| |
| results = data.map(lambda x: {"translation": translation_pipeline(x["text"])}) |
| |
| |
| |
| raw_result = results[0]["translation"][0]['translation_text'] |
| |
| |
| final_result = raw_result.strip().capitalize() |
|
|
| |
| st.markdown("---") |
| st.markdown("#### Translated text:") |
| |
| st.markdown(f'<div class="result-text"><h2>{final_result}</h2></div>', unsafe_allow_html=True) |
| |
| except Exception as e: |
| st.error(f"Translation error: {e}") |
| |
| |
| else: |
| st.warning("Please enter some text to translate.") |