Nehal07 commited on
Commit
31ed4a8
1 Parent(s): 53b9f27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -7,12 +7,15 @@ st.title("Text Translator")
7
  # Input text
8
  user_text = st.text_area("Enter the text you want to translate:")
9
 
10
- # Language selection
11
- st.subheader("Select the target language for translation:")
 
 
12
 
13
- # Create a list of language codes and names for selection
14
- language_options = {lang: code for code, lang in LANGUAGES.items()}
15
- selected_language = st.selectbox("Select a language:", list(language_options.keys()))
 
16
 
17
  # Initialize the translator
18
  translator = Translator()
@@ -20,8 +23,15 @@ translator = Translator()
20
  # Translate the text when the user clicks the "Translate" button
21
  if st.button("Translate"):
22
  try:
23
- # Translate the text to the selected language
24
- translated_text = translator.translate(user_text, dest=language_options[selected_language]).text
25
- st.success(f"Translated text: {translated_text}")
 
 
 
 
 
 
 
26
  except Exception as e:
27
  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
+ # Source language selection
11
+ st.subheader("Select the source language of the input text:")
12
+ source_language_options = {lang: code for code, lang in LANGUAGES.items()}
13
+ selected_source_language = st.selectbox("Select a language:", list(source_language_options.keys()))
14
 
15
+ # Target language selection
16
+ st.subheader("Select the target language for translation:")
17
+ target_language_options = {lang: code for code, lang in LANGUAGES.items()}
18
+ selected_target_language = st.selectbox("Select a language:", list(target_language_options.keys()))
19
 
20
  # Initialize the translator
21
  translator = Translator()
 
23
  # Translate the text when the user clicks the "Translate" button
24
  if st.button("Translate"):
25
  try:
26
+ # Translate the input text from the source language to the target language
27
+ translated = translator.translate(
28
+ user_text,
29
+ src=source_language_options[selected_source_language],
30
+ dest=target_language_options[selected_target_language]
31
+ )
32
+ translated_text = translated.text
33
+ translated_language = LANGUAGES[translated.dest]
34
+
35
+ st.success(f"Translated text ({translated_language}): {translated_text}")
36
  except Exception as e:
37
  st.error(f"Translation failed: {str(e)}")