Nehal07 commited on
Commit
37224ba
1 Parent(s): aba198b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
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
- try:
21
- # Translate the input text from English to the target language
22
- translated = translator.translate(
23
- user_text,
24
- src='en', # Source language is set to English
25
- dest=target_language_options[selected_target_language]
26
- )
27
-
28
- translated_text = translated.text
29
- translated_language = LANGUAGES[translated.dest]
30
-
31
- st.success(f"Translated text ({translated_language}): {translated_text}")
32
- except Exception as e:
33
- st.error(f"Translation failed: {str(e)}")
 
 
 
 
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)}")