Nehal07's picture
Update app.py
31ed4a8
raw
history blame
1.44 kB
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)}")