|
import streamlit as st |
|
from translate import Translator |
|
|
|
|
|
def translate_text(text, from_lang, to_lang): |
|
translator = Translator(from_lang=from_lang, to_lang=to_lang) |
|
translation = translator.translate(text) |
|
return translation |
|
|
|
|
|
st.title('Multilingual Text Translator') |
|
|
|
st.write('Translate text from one language to another with this simple app.') |
|
|
|
from_lang = st.selectbox('Select the source language:', |
|
['English', 'Spanish', 'French', 'German', 'Chinese', 'Japanese', 'Hindi'], |
|
index=0) |
|
to_lang = st.selectbox('Select the target language:', |
|
['Spanish', 'English', 'French', 'German', 'Chinese', 'Japanese', 'Hindi'], |
|
index=1) |
|
|
|
lang_codes = { |
|
'English': 'en', |
|
'Spanish': 'es', |
|
'French': 'fr', |
|
'German': 'de', |
|
'Chinese': 'zh', |
|
'Japanese': 'ja', |
|
'Hindi': 'hi' |
|
} |
|
|
|
text = st.text_area('Enter the text you want to translate:') |
|
|
|
if st.button('Translate'): |
|
if text: |
|
from_lang_code = lang_codes[from_lang] |
|
to_lang_code = lang_codes[to_lang] |
|
translation = translate_text(text, from_lang_code, to_lang_code) |
|
st.success(f'Translation ({from_lang} to {to_lang}): {translation}') |
|
else: |
|
st.error('Please enter the text you want to translate.') |
|
|