File size: 2,112 Bytes
96b4aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from transformers import pipeline
from datasets import Dataset
import streamlit as st
import torch

# Set the background color and layout with set_page_config
st.set_page_config(
    page_title="English to Tawra Translator",
    page_icon=":repeat:",
    layout="wide",
)

# Streamlit app setup
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:")

# Text input
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)

# Define your model from Hugging Face
model_directory = "repleeka/eng-taw-nmt"

device = 0 if torch.cuda.is_available() else -1
translation_pipeline = pipeline(
    task="translation",
    model="repleeka/eng-taw-nmt",
    tokenizer="repleeka/eng-taw-nmt",
    device=device
)

# Translate button
if st.button("Translate", key="translate_button"):
    if text_input:
        with st.spinner("Translating... Please wait"):
            # Prepare data for translation
            sentences = [text_input]
            data = Dataset.from_dict({"text": sentences})

            # Apply translation
            try:
                results = data.map(lambda x: {"translation": translation_pipeline(x["text"])})
                result = results[0]["translation"][0]['translation_text']
                
                # Capitalize the first letter of the result
                result = result.capitalize()

                # Display translation result with custom styling
                st.markdown("#### Translated text:")
                st.markdown(f'<h2 class="result-text">{result}</2>', unsafe_allow_html=True)
                # st.markdown(result)
                
            except Exception as e:
                st.error(f"Translation error: {e}")
    else:
        st.warning("Please enter text to translate.")

# Clear input button
if st.button("Clear Input"):
    st.session_state.text_input = ""