File size: 1,441 Bytes
9d96850 31ed4a8 9d96850 31ed4a8 9d96850 31ed4a8 9d96850 |
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 33 34 35 36 37 38 |
import streamlit as st
from googletrans import Translator, LANGUAGES
# Streamlit app
st.title("Text Translator")
# Input text
user_text = st.text_area("Enter the text you want to translate:")
# Source language selection
st.subheader("Select the source language of the input text:")
source_language_options = {lang: code for code, lang in LANGUAGES.items()}
selected_source_language = st.selectbox("Select a language:", list(source_language_options.keys()))
# Target language selection
st.subheader("Select the target language for translation:")
target_language_options = {lang: code for code, lang in LANGUAGES.items()}
selected_target_language = st.selectbox("Select a language:", list(target_language_options.keys()))
# Initialize the translator
translator = Translator()
# Translate the text when the user clicks the "Translate" button
if st.button("Translate"):
try:
# Translate the input text from the source language to the target language
translated = translator.translate(
user_text,
src=source_language_options[selected_source_language],
dest=target_language_options[selected_target_language]
)
translated_text = translated.text
translated_language = LANGUAGES[translated.dest]
st.success(f"Translated text ({translated_language}): {translated_text}")
except Exception as e:
st.error(f"Translation failed: {str(e)}")
|