Update app.py
Browse files
app.py
CHANGED
@@ -5,45 +5,49 @@ from translate import Translator
|
|
5 |
st.title("Text Translator")
|
6 |
|
7 |
# Input text
|
8 |
-
user_text = st.
|
9 |
|
10 |
# Define a dictionary of language codes and their full names
|
11 |
language_names = {
|
12 |
-
'Afrikaans': '
|
13 |
-
'
|
14 |
-
'
|
15 |
-
'
|
16 |
-
'
|
17 |
-
'
|
18 |
-
'
|
19 |
-
'
|
20 |
-
'
|
21 |
-
'
|
22 |
-
'
|
23 |
-
'
|
24 |
-
'
|
25 |
-
'
|
26 |
-
'
|
27 |
-
'
|
28 |
-
'Tamil': 'Tamil', 'Tatar': 'Tatar', 'Telugu': 'Telugu', 'Thai': 'Thai', 'Turkish': 'Turkish', 'Turkmen': 'Turkmen', 'Ukrainian': 'Ukrainian',
|
29 |
-
'Urdu': 'Urdu', 'Uyghur': 'Uyghur', 'Uzbek': 'Uzbek', 'Vietnamese': 'Vietnamese', 'Welsh': 'Welsh', 'Xhosa': 'Xhosa',
|
30 |
-
'Yiddish': 'Yiddish', 'Yoruba': 'Yoruba', 'Zulu': 'Zulu'
|
31 |
}
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
# Target language selection with a unique key
|
34 |
st.subheader("Select the target language for translation:")
|
35 |
selected_target_language = st.selectbox("Select a target language:", list(language_names.keys()))
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
if st.button("Translate"):
|
42 |
-
try:
|
43 |
-
# Translate the input text to the selected target language
|
44 |
-
translated_text = translator.translate(user_text)
|
45 |
-
|
46 |
-
# Display the translated text in black text color
|
47 |
-
st.markdown(f'<span style="color:black;">{translated_text}</span>', unsafe_allow_html=True)
|
48 |
-
except Exception as e:
|
49 |
-
st.error(f"Translation failed: {str(e)}")
|
|
|
5 |
st.title("Text Translator")
|
6 |
|
7 |
# Input text
|
8 |
+
user_text = st.text_input("Enter the text you want to translate:")
|
9 |
|
10 |
# Define a dictionary of language codes and their full names
|
11 |
language_names = {
|
12 |
+
'Afrikaans': 'af', 'Albanian': 'sq', 'Amharic': 'am', 'Arabic': 'ar', 'Armenian': 'hy', 'Azerbaijani': 'az', 'Basque': 'eu',
|
13 |
+
'Belarusian': 'be', 'Bengali': 'bn', 'Bosnian': 'bs', 'Bulgarian': 'bg', 'Catalan': 'ca', 'Cebuano': 'ceb', 'Chichewa': 'ny',
|
14 |
+
'Chinese (Simplified)': 'zh-cn', 'Corsican': 'co', 'Croatian': 'hr', 'Czech': 'cs', 'Danish': 'da', 'Dutch': 'nl', 'English': 'en',
|
15 |
+
'Esperanto': 'eo', 'Estonian': 'et', 'Filipino': 'tl', 'Finnish': 'fi', 'French': 'fr', 'Frisian': 'fy', 'Galician': 'gl',
|
16 |
+
'Georgian': 'ka', 'German': 'de', 'Greek': 'el', 'Gujarati': 'gu', 'Haitian Creole': 'ht', 'Hausa': 'ha', 'Hawaiian': 'haw',
|
17 |
+
'Hebrew': 'iw', 'Hindi': 'hi', 'Hmong': 'hmn', 'Hungarian': 'hu', 'Icelandic': 'is', 'Igbo': 'ig', 'Indonesian': 'id',
|
18 |
+
'Irish': 'ga', 'Italian': 'it', 'Japanese': 'ja', 'Javanese': 'jw', 'Kannada': 'kn', 'Kazakh': 'kk', 'Khmer': 'km',
|
19 |
+
'Korean': 'ko', 'Kurdish': 'ku', 'Kyrgyz': 'ky', 'Lao': 'lo', 'Latin': 'la', 'Latvian': 'lv', 'Lithuanian': 'lt',
|
20 |
+
'Luxembourgish': 'lb', 'Macedonian': 'mk', 'Malagasy': 'mg', 'Malay': 'ms', 'Malayalam': 'ml', 'Maltese': 'mt', 'Maori': 'mi',
|
21 |
+
'Marathi': 'mr', 'Mongolian': 'mn', 'Myanmar (Burmese)': 'my', 'Nepali': 'ne', 'Norwegian': 'no', 'Odia': 'or',
|
22 |
+
'Pashto': 'ps', 'Persian': 'fa', 'Polish': 'pl', 'Portuguese': 'pt', 'Punjabi': 'pa', 'Romanian': 'ro', 'Russian': 'ru',
|
23 |
+
'Samoan': 'sm', 'Scots Gaelic': 'gd', 'Serbian': 'sr', 'Sesotho': 'st', 'Shona': 'sn', 'Sindhi': 'sd', 'Sinhala': 'si',
|
24 |
+
'Slovak': 'sk', 'Slovenian': 'sl', 'Somali': 'so', 'Spanish': 'es', 'Sundanese': 'su', 'Swahili': 'sw', 'Swedish': 'sv',
|
25 |
+
'Tajik': 'tg', 'Tamil': 'ta', 'Tatar': 'tt', 'Telugu': 'te', 'Thai': 'th', 'Turkish': 'tr', 'Turkmen': 'tk', 'Ukrainian': 'uk',
|
26 |
+
'Urdu': 'ur', 'Uyghur': 'ug', 'Uzbek': 'uz', 'Vietnamese': 'vi', 'Welsh': 'cy', 'Xhosa': 'xh', 'Yiddish': 'yi', 'Yoruba': 'yo',
|
27 |
+
'Zulu': 'zu'
|
|
|
|
|
|
|
28 |
}
|
29 |
|
30 |
+
# Define a dictionary of reverse language codes to language names
|
31 |
+
reverse_language_names = {code: lang for lang, code in language_names.items()}
|
32 |
+
|
33 |
+
# Function to translate text
|
34 |
+
def translate_text(text, target_language_code):
|
35 |
+
try:
|
36 |
+
# Initialize the translator
|
37 |
+
translator = Translator(to_lang=target_language_code)
|
38 |
+
|
39 |
+
# Translate the text to the selected target language
|
40 |
+
translated_text = translator.translate(text)
|
41 |
+
|
42 |
+
return translated_text
|
43 |
+
except Exception as e:
|
44 |
+
return f"Translation failed: {str(e)}"
|
45 |
+
|
46 |
# Target language selection with a unique key
|
47 |
st.subheader("Select the target language for translation:")
|
48 |
selected_target_language = st.selectbox("Select a target language:", list(language_names.keys()))
|
49 |
|
50 |
+
# Display the translated text as the user types
|
51 |
+
if user_text:
|
52 |
+
translated_text = translate_text(user_text, language_names[selected_target_language])
|
53 |
+
st.markdown(f'<span style="color:black;">{translated_text}</span>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|