Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ 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 |
target_language_options = {lang: code for code, lang in LANGUAGES.items()}
|
13 |
selected_target_language = st.selectbox("Select a target language:", list(target_language_options.keys()), key="target_language")
|
@@ -17,17 +17,20 @@ translator = Translator()
|
|
17 |
|
18 |
# Translate the text when the user clicks the "Translate" button
|
19 |
if st.button("Translate"):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
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 |
target_language_options = {lang: code for code, lang in LANGUAGES.items()}
|
13 |
selected_target_language = st.selectbox("Select a target language:", list(target_language_options.keys()), key="target_language")
|
|
|
17 |
|
18 |
# Translate the text when the user clicks the "Translate" button
|
19 |
if st.button("Translate"):
|
20 |
+
try:
|
21 |
+
# Translate the input text to the target language
|
22 |
+
translated = translator.translate(
|
23 |
+
user_text,
|
24 |
+
dest=target_language_options[selected_target_language]
|
25 |
+
)
|
26 |
+
|
27 |
+
# Check if translation is not None
|
28 |
+
if translated is not None:
|
29 |
+
translated_text = translated.text
|
30 |
+
translated_language = LANGUAGES[translated.dest]
|
31 |
+
st.success(f"Translated text ({translated_language}): {translated_text}")
|
32 |
+
else:
|
33 |
+
st.error("Translation failed: Empty text")
|
34 |
+
|
35 |
+
except Exception as e:
|
36 |
+
st.error(f"Translation failed: {str(e)}")
|