Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
# Mapping target languages to specific Hugging Face models | |
MODEL_MAPPING = { | |
"French": "Helsinki-NLP/opus-mt-en-fr", | |
"Spanish": "Helsinki-NLP/opus-mt-en-es", | |
"German": "Helsinki-NLP/opus-mt-en-de", | |
"Italian": "Helsinki-NLP/opus-mt-en-it", | |
"Chinese": "Helsinki-NLP/opus-mt-en-zh", | |
"Arabic": "Helsinki-NLP/opus-mt-en-ar", | |
"Hindi": "Helsinki-NLP/opus-mt-en-hi", | |
"Urdu": "Helsinki-NLP/opus-mt-en-ur", | |
} | |
# Function to set background gradient and fix text visibility | |
def set_background(): | |
st.markdown( | |
""" | |
<style> | |
/* Set full-page gradient background */ | |
.stApp { | |
background: linear-gradient(135deg, #ff0080, #ff8c00); | |
color: white; | |
} | |
/* Style headers */ | |
h1, h2, h3, h4 { | |
color: white; | |
} | |
/* Style sidebar */ | |
.css-1d391kg { | |
background-color: rgba(255, 255, 255, 0.2) !important; | |
} | |
/* Style text area with better visibility */ | |
.stTextArea textarea { | |
background-color: rgba(255, 255, 255, 0.9) !important; | |
color: black !important; | |
font-weight: bold; | |
} | |
/* Style selectbox */ | |
.stSelectbox div { | |
background-color: rgba(255, 255, 255, 0.9) !important; | |
color: black !important; | |
} | |
/* Style text input fields */ | |
input { | |
background-color: rgba(255, 255, 255, 0.9) !important; | |
color: black !important; | |
font-weight: bold; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True | |
) | |
# Apply background | |
set_background() | |
# App title and description | |
st.title("π Multilingual Translation App") | |
st.markdown("### Translate English text into multiple languages using AI.") | |
st.write( | |
""" | |
This app allows you to translate English text into multiple languages using AI-powered translation models. | |
Select your target language and enter your text to get an accurate translation. | |
**How to Use:** | |
1. Select a target language from the sidebar. | |
2. Enter the text you want to translate. | |
3. Click the "Translate" button to generate the translation. | |
""" | |
) | |
# Sidebar for language selection | |
selected_language = st.sidebar.selectbox("π Select Target Language", list(MODEL_MAPPING.keys())) | |
# User input | |
user_input = st.text_area("βοΈ Enter text in English:") | |
# Translation logic | |
if st.button("Translate β¨"): | |
if user_input.strip(): | |
model_name = MODEL_MAPPING[selected_language] | |
translator = pipeline("translation", model=model_name) | |
translated_text = translator(user_input)[0]['translation_text'] | |
st.success(f"β **Translated Text ({selected_language}):**") | |
st.write(f"π {translated_text}") | |
else: | |
st.warning("β οΈ Please enter some text to translate.") | |