File size: 2,941 Bytes
a74e244
 
 
dc73446
 
 
 
 
 
 
 
 
 
a74e244
 
08fb67c
1b15e42
b0647fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08fb67c
b0647fe
08fb67c
 
 
 
 
 
 
 
 
 
 
 
 
 
b0647fe
 
 
 
 
4dd017f
b0647fe
1b15e42
 
b0647fe
1b15e42
b0647fe
 
 
 
 
 
 
 
 
 
 
 
a74e244
 
1b15e42
a74e244
 
1b15e42
a74e244
b0647fe
1b15e42
a74e244
dc73446
 
 
 
1b15e42
 
a74e244
1b15e42
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
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.")