File size: 4,081 Bytes
891f160 e656608 da34b5e 891f160 d60f4d4 8040f65 891f160 e656608 8040f65 da34b5e 8040f65 da34b5e d60f4d4 da34b5e 8040f65 da34b5e d60f4d4 da34b5e d60f4d4 da34b5e d60f4d4 da34b5e d60f4d4 da34b5e d60f4d4 da34b5e d60f4d4 da34b5e 8040f65 891f160 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import streamlit as st
import importlib
from io import BytesIO
st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="centered")
def main():
translation = importlib.import_module("translation")
lang_detect = importlib.import_module("lang_detect")
audio_processor = importlib.import_module("audio_processor")
st.markdown("<h1 style='text-align: center; color: #2E86C1;'>Multilingual Translator</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
col1, col2 = st.columns([1, 1])
with col1:
input_type = st.radio("Input", ["Text", "File Upload"], horizontal=True, key="input_type")
user_text = st.text_area("Enter or paste text", height=150, key="text_input").strip() if input_type == "Text" else ""
if input_type == "File Upload":
doc_file = st.file_uploader("Upload TXT File", type=["txt"], key="doc_input", accept_multiple_files=False)
user_text = doc_file.read().decode("utf-8").strip() if doc_file else ""
if user_text:
detected_options = lang_detect.detect_language(user_text) if len(user_text) >= 10 else [("English", 1.0, "English")]
source_lang = detected_options[0][0] if detected_options else "English"
native_lang_map = {
"English": "English", "French": "Français", "Spanish": "Español",
"German": "Deutsch", "Hindi": "हिन्दी", "Chinese": "中文",
"Arabic": "العربية", "Russian": "Русский", "Japanese": "日本語"
}
source_options = ["Detect"] + [native_lang_map.get(lang, lang) for lang, _, _ in detected_options] + list(native_lang_map.values())
target_options = list(native_lang_map.values())
source_lang_display = st.selectbox("From", source_options, index=0, key="source_lang").replace("Detect", native_lang_map.get(source_lang, source_lang))
source_lang = next((k for k, v in native_lang_map.items() if v == source_lang_display), source_lang)
target_lang_display = st.selectbox("To", target_options, index=target_options.index("हिन्दी") if "हिन्दी" in target_options else 0, key="target_lang")
target_lang = next((k for k, v in native_lang_map.items() if v == target_lang_display), "Hindi")
if st.button("Translate", key="translate_btn"):
try:
translated_text = translation.translate(user_text, source_lang, target_lang)
st.session_state.translated_text = translated_text
except Exception as e:
st.error(f"Translation failed: {str(e)}. Using input as fallback.")
st.session_state.translated_text = user_text
with col2:
if "translated_text" in st.session_state and st.session_state.translated_text:
st.write("Translation:")
st.write(st.session_state.translated_text)
output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, key="output_option")
if output_option == "Audio":
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
if audio and audio.getbuffer().nbytes > 0:
st.audio(audio, format="audio/mp3")
st.success("Audio playing.")
else:
st.error("Audio generation failed. Try English or French.")
st.markdown("""
<p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>
Developed by: Krishna Prakash <a href='https://www.linkedin.com/in/krishnaprakash-profile/' target='_blank'>
<img src='https://img.icons8.com/ios-filled/30/0077b5/linkedin.png' alt='LinkedIn' style='vertical-align: middle; margin: 0 5px;'/>
</a>
</p>
""", unsafe_allow_html=True)
if __name__ == "__main__":
main() |