Spaces:
Sleeping
Sleeping
File size: 3,576 Bytes
073d7e9 5110299 e1c9233 2aad4cf 073d7e9 5110299 073d7e9 2aad4cf 4aa603a 2aad4cf 49769ff 4aa603a 2aad4cf 4aa603a 2aad4cf 49769ff 4aa603a 49769ff 2aad4cf 49769ff 2aad4cf 49769ff 2aad4cf 49769ff 2aad4cf 49769ff 2aad4cf 4aa603a 2aad4cf 49769ff 1d6f91e 5110299 1d6f91e 073d7e9 49769ff 4aa603a 073d7e9 1d6f91e 2aad4cf |
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 |
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", # Punjabi language model
"Pashto": "Helsinki-NLP/opus-mt-en-ps", # Pashto language model
# Add more languages as needed
}
# 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 gradient theme
st.markdown("""
<style>
body {
background: linear-gradient(to right, #0f0c29, #302b63, #24243e); /* Dark gradient */
color: #e0e0e0;
}
.stApp {
background: linear-gradient(to right, #0f0c29, #302b63, #24243e); /* Dark gradient */
}
.css-1d391kg {
background: linear-gradient(to bottom, #141e30, #243b55); /* Dark gradient for text area */
color: #e0e0e0;
border-radius: 10px;
padding: 20px;
}
.stButton > button {
background: linear-gradient(to right, #2c3e50, #4ca1af); /* Gradient for buttons */
color: #e0e0e0;
border: 1px solid #444;
}
.stButton > button:hover {
background: linear-gradient(to right, #0f2027, #2c5364); /* Hover effect for buttons */
border: 1px solid #555;
}
.stTextArea textarea {
background: linear-gradient(to bottom, #141e30, #243b55); /* Dark gradient for text input */
color: #e0e0e0;
border: 1px solid #333;
}
.css-1r6slb0 {
background-color: #141e30 !important; /* Sidebar background */
color: #e0e0e0;
}
.css-1cpxqw2 a {
color: #e0e0e0 !important;
}
</style>
""", unsafe_allow_html=True)
# App title and subtitle with dark gradient 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: #e0
|