File size: 4,357 Bytes
8575f8b
 
 
ee683a6
7e0701a
ee683a6
 
 
 
 
 
 
8575f8b
 
 
 
 
90190f3
 
 
b8f1021
90190f3
b8f1021
 
 
 
 
 
 
90190f3
b8f1021
 
 
90190f3
b8f1021
 
 
90190f3
b8f1021
90190f3
b8f1021
 
90190f3
 
 
 
 
 
 
 
 
 
b8f1021
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90190f3
b8f1021
 
 
 
90190f3
 
 
 
 
 
 
 
 
b8f1021
 
 
 
 
8575f8b
 
 
ee683a6
 
 
8575f8b
 
 
 
 
 
 
 
 
 
c05de1b
cd56b16
8575f8b
ee683a6
 
8575f8b
 
 
6daa488
 
 
 
 
 
8575f8b
6daa488
 
 
8575f8b
 
 
 
 
 
c05de1b
 
90190f3
b8f1021
c05de1b
5202cce
 
cd56b16
5202cce
cd56b16
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import streamlit as st
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer

# Load the model and tokenizer once at the start
@st.cache_resource
def load_model_and_tokenizer():
    model_name = "facebook/m2m100_418M"
    tokenizer = M2M100Tokenizer.from_pretrained(model_name)
    model = M2M100ForConditionalGeneration.from_pretrained(model_name)
    return model, tokenizer

model, tokenizer = load_model_and_tokenizer()

# Streamlit UI
st.title("English to Multiple Language Translator")
st.write("Translate English text into different languages using AI.")

# Sidebar for settings
st.sidebar.title("Settings")

# Dark mode toggle
dark_mode = st.sidebar.checkbox("🌙 Dark Mode")

# Custom CSS for dark and light mode
if dark_mode:
    st.markdown(
        """
        <style>
        body {
            background-color: #2e2e2e;
            color: #FFFFFF;
        }
        .stButton>button {
            background-color: #4CAF50;
            color: white;
        }
        .stTextInput>div>input {
            background-color: #4b4b4b;
            color: white;
            border: 1px solid #ccc;
        }
        .stSidebar {
            background-color: #383838;
        }
        .stSidebar h1, .stSidebar h2, .stSidebar h3, .stSidebar h4, .stSidebar h5, .stSidebar h6, .stSidebar .markdown-text-container {
            color: #FFFFFF;
        }
        .stSidebar a {
            color: #1f77b4;
        }
        .stSidebar .stCheckbox div {
            color: #FFFFFF;
        }
        </style>
        """,
        unsafe_allow_html=True,
    )
else:
    st.markdown(
        """
        <style>
        body {
            background-color: #FFFFFF;
            color: #000000;
        }
        .stButton>button {
            background-color: #1f77b4;
            color: white;
        }
        .stTextInput>div>input {
            background-color: #FFFFFF;
            color: black;
            border: 1px solid #ccc;
        }
        .stSidebar {
            background-color: #f0f2f6;
        }
        .stSidebar h1, .stSidebar h2, .stSidebar h3, .stSidebar h4, .stSidebar h5, .stSidebar h6, .stSidebar .markdown-text-container {
            color: #000000;
        }
        .stSidebar a {
            color: #1f77b4;
        }
        .stSidebar .stCheckbox div {
            color: #000000;
        }
        </style>
        """,
        unsafe_allow_html=True,
    )

# Input text
input_text = st.text_area("Enter English text:", value="")

# Sidebar for language selection
st.sidebar.subheader("Select Target Language")

language_options = {
    "French": "fr",
    "Spanish": "es",
    "German": "de",
    "Chinese": "zh",
    "Arabic": "ar",
    "Hindi": "hi",
    "Japanese": "ja",
    "Russian": "ru",
    "Portuguese": "pt",
    "Italian": "it",
    "Urdu": "ur"  # Added Urdu language
}

selected_language = st.sidebar.selectbox("Target Language", list(language_options.keys()))

if st.button("Translate"):
    if input_text:
        # Interactive loading state
        with st.spinner('Translating... Please wait...'):
            # Set target language
            target_language = language_options[selected_language]
            tokenizer.src_lang = "en"
            encoded_input = tokenizer(input_text, return_tensors="pt")

            # Generate translation
            generated_tokens = model.generate(**encoded_input, forced_bos_token_id=tokenizer.get_lang_id(target_language))
            translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]

        # Display translated text
        st.write(f"**Translated text ({selected_language}):**")
        st.write(translated_text)
    else:
        st.write("Please enter text to translate.")

# Contact information
st.sidebar.subheader("Contact Developer")
st.sidebar.write("Developer: [M.Abbas](https://www.linkedin.com/in/mohammad-abbas-dev/)")
st.sidebar.write("Gmail: [email protected]")

# WhatsApp bot contact
whatsapp_link = "https://wa.me/+923108295635?text=Hi, What query you have about our English to Multiple Language Translator AI app."

st.sidebar.markdown(f"""
    <a href="{whatsapp_link}" target="_blank">
        <img src="https://upload.wikimedia.org/wikipedia/commons/6/6b/WhatsApp.svg" alt="WhatsApp" style="width:50px;height:50px;">
    </a>
""", unsafe_allow_html=True)