Nehal07 commited on
Commit
c2e4f74
1 Parent(s): 2d10fd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -33
app.py CHANGED
@@ -5,45 +5,49 @@ from translate import Translator
5
  st.title("Text Translator")
6
 
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
- 'Afrikaans': 'Afrikaans', 'Albanian': 'Albanian', 'Amharic': 'Amharic', 'Arabic': 'Arabic', 'Armenian': 'Armenian',
13
- 'Azerbaijani': 'Azerbaijani', 'Basque': 'Basque', 'Belarusian': 'Belarusian', 'Bengali': 'Bengali', 'Bosnian': 'Bosnian',
14
- 'Bulgarian': 'Bulgarian', 'Catalan': 'Catalan', 'Cebuano': 'Cebuano', 'Chichewa': 'Chichewa', 'Chinese (Simplified)': 'Chinese (Simplified)',
15
- 'Corsican': 'Corsican', 'Croatian': 'Croatian', 'Czech': 'Czech', 'Danish': 'Danish', 'Dutch': 'Dutch', 'English': 'English',
16
- 'Esperanto': 'Esperanto', 'Estonian': 'Estonian', 'Filipino': 'Filipino', 'Finnish': 'Finnish', 'French': 'French', 'Frisian': 'Frisian',
17
- 'Galician': 'Galician', 'Georgian': 'Georgian', 'German': 'German', 'Greek': 'Greek', 'Gujarati': 'Gujarati', 'Haitian Creole': 'Haitian Creole',
18
- 'Hausa': 'Hausa', 'Hawaiian': 'Hawaiian', 'Hebrew': 'Hebrew', 'Hindi': 'Hindi', 'Hmong': 'Hmong', 'Hungarian': 'Hungarian',
19
- 'Icelandic': 'Icelandic', 'Igbo': 'Igbo', 'Indonesian': 'Indonesian', 'Irish': 'Irish', 'Italian': 'Italian', 'Japanese': 'Japanese',
20
- 'Javanese': 'Javanese', 'Kannada': 'Kannada', 'Kazakh': 'Kazakh', 'Khmer': 'Khmer', 'Korean': 'Korean', 'Kurdish': 'Kurdish',
21
- 'Kyrgyz': 'Kyrgyz', 'Lao': 'Lao', 'Latin': 'Latin', 'Latvian': 'Latvian', 'Lithuanian': 'Lithuanian', 'Luxembourgish': 'Luxembourgish',
22
- 'Macedonian': 'Macedonian', 'Malagasy': 'Malagasy', 'Malay': 'Malay', 'Malayalam': 'Malayalam', 'Maltese': 'Maltese', 'Maori': 'Maori',
23
- 'Marathi': 'Marathi', 'Mongolian': 'Mongolian', 'Myanmar (Burmese)': 'Myanmar (Burmese)', 'Nepali': 'Nepali', 'Norwegian': 'Norwegian',
24
- 'Odia': 'Odia', 'Pashto': 'Pashto', 'Persian': 'Persian', 'Polish': 'Polish', 'Portuguese': 'Portuguese', 'Punjabi': 'Punjabi',
25
- 'Romanian': 'Romanian', 'Russian': 'Russian', 'Samoan': 'Samoan', 'Scots Gaelic': 'Scots Gaelic', 'Serbian': 'Serbian',
26
- 'Sesotho': 'Sesotho', 'Shona': 'Shona', 'Sindhi': 'Sindhi', 'Sinhala': 'Sinhala', 'Slovak': 'Slovak', 'Slovenian': 'Slovenian',
27
- 'Somali': 'Somali', 'Spanish': 'Spanish', 'Sundanese': 'Sundanese', 'Swahili': 'Swahili', 'Swedish': 'Swedish', 'Tajik': 'Tajik',
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
- # Initialize the translator
38
- translator = Translator(to_lang=selected_target_language)
39
-
40
- # Translate the text when the user clicks the "Translate" button
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)