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