|
import streamlit as st |
|
from googletrans import Translator, LANGUAGES |
|
|
|
|
|
st.title("Text Translator") |
|
|
|
|
|
user_text = st.text_area("Enter the text you want to translate:") |
|
|
|
|
|
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())) |
|
|
|
|
|
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())) |
|
|
|
|
|
translator = Translator() |
|
|
|
|
|
if st.button("Translate"): |
|
try: |
|
|
|
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)}") |
|
|