File size: 1,231 Bytes
9d96850 e257972 9d96850 12c8a54 31ed4a8 e257972 9d96850 e257972 9d96850 12c8a54 e257972 12c8a54 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import streamlit as st
from translate import Translator, LANGUAGES
# Streamlit app
st.title("Text Translator")
# Input text
user_text = st.text_area("Enter the text you want to translate:")
# Target language selection with a unique key
st.subheader("Select the target language for translation:")
target_language = st.selectbox("Select a target language:", [lang for lang in LANGUAGES.values()])
# Get the language code for the selected target language
selected_target_language_code = next(code for code, lang in LANGUAGES.items() if lang == target_language)
# Initialize the translator
translator = Translator(to_lang=selected_target_language_code)
# Translate the text when the user clicks the "Translate" button
if st.button("Translate"):
try:
# Translate the input text to the selected target language
translated_text = translator.translate(user_text)
# Get the full name of the translated language
translated_language = next(lang for code, lang in LANGUAGES.items() if code == selected_target_language_code)
st.success(f"Translated text ({translated_language}): {translated_text}")
except Exception as e:
st.error(f"Translation failed: {str(e)}")
|