File size: 3,666 Bytes
073d7e9
 
 
5110299
 
 
 
 
 
 
e1c9233
 
 
073d7e9
 
e1c9233
073d7e9
 
5110299
073d7e9
 
 
 
 
 
 
 
 
 
 
49769ff
4aa603a
 
 
49769ff
 
4aa603a
 
49769ff
4aa603a
 
49769ff
 
4aa603a
 
 
49769ff
 
 
 
 
 
 
 
 
 
 
 
 
 
4aa603a
 
 
49769ff
 
 
1d6f91e
 
 
5110299
1d6f91e
 
 
 
 
073d7e9
 
49769ff
4aa603a
073d7e9
1d6f91e
49769ff
073d7e9
1d6f91e
073d7e9
 
1d6f91e
 
 
5110299
 
1d6f91e
 
 
073d7e9
 
49769ff
1d6f91e
073d7e9
1d6f91e
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import streamlit as st
from transformers import MarianMTModel, MarianTokenizer

# Define available target languages and their Hugging Face model names
language_options = {
    "French": "Helsinki-NLP/opus-mt-en-fr",
    "German": "Helsinki-NLP/opus-mt-en-de",
    "Spanish": "Helsinki-NLP/opus-mt-en-es",
    "Italian": "Helsinki-NLP/opus-mt-en-it",
    "Dutch": "Helsinki-NLP/opus-mt-en-nl",
    "Urdu": "Helsinki-NLP/opus-mt-en-ur",
    "Punjabi": "Helsinki-NLP/opus-mt-en-pa",
    "Pashto": "Helsinki-NLP/opus-mt-en-ps", 
}


# Function to load the model based on the selected language
@st.cache_resource
def load_model(model_name):
    model = MarianMTModel.from_pretrained(model_name)
    tokenizer = MarianTokenizer.from_pretrained(model_name)
    return model, tokenizer

# Function to translate text
def translate_text(model, tokenizer, text):
    inputs = tokenizer.encode(text, return_tensors='pt', truncation=True, padding=True)
    translated = model.generate(inputs, max_length=512, num_beams=5, early_stopping=True)
    translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
    return translated_text

# Streamlit app layout with dark color scheme
st.markdown("""
    <style>
        body {
            background-color: #121212;
            color: #e0e0e0;
        }
        .stApp {
            background-color: #121212;
        }
        .css-1d391kg {
            background-color: #1f1f1f;
            color: #e0e0e0;
            border-radius: 10px;
            padding: 20px;
        }
        .stButton > button {
            background-color: #1f1f1f;
            color: #e0e0e0;
            border: 1px solid #333;
        }
        .stButton > button:hover {
            background-color: #333;
            border: 1px solid #444;
        }
        .stTextArea textarea {
            background-color: #1f1f1f;
            color: #e0e0e0;
            border: 1px solid #333;
        }
    </style>
""", unsafe_allow_html=True)

# App title and subtitle with dark theme
st.markdown("<h1 style='text-align: center; color: #e0e0e0;'>🌍 Polyglot Translator 🌍</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center; color: #b0b0b0; font-size: 20px;'>Empowering You to Speak Any Language!</p>", unsafe_allow_html=True)

# Sidebar for language selection and instructions
st.sidebar.title("Language Options")
language = st.sidebar.selectbox("Choose target language", list(language_options.keys()))

st.sidebar.markdown("### How to use")
st.sidebar.write("1. Enter English text in the box below.")
st.sidebar.write("2. Select the target language from the options.")
st.sidebar.write("3. Click **Translate** to get the result.")

# Input text
st.markdown("<h3 style='color: #e0e0e0;'>Enter text in English to translate:</h3>", unsafe_allow_html=True)
text = st.text_area("", height=150)

# Character count
st.write(f"<p style='color: #e0e0e0;'>Character count: {len(text)}</p>", unsafe_allow_html=True)

# Button to translate
if st.button("Translate"):
    if text:
        # Show a spinner during the translation process
        with st.spinner('Translating...'):
            # Load model and tokenizer based on selected language
            model_name = language_options[language]
            model, tokenizer = load_model(model_name)
            
            # Perform translation
            translated_text = translate_text(model, tokenizer, text)
        
        # Display the translation
        st.markdown(f"<h3 style='color: #e0e0e0;'>Translated Text ({language}):</h3>", unsafe_allow_html=True)
        st.success(translated_text)
    else:
        st.error("Please enter text to translate.")