import streamlit as st import importlib from io import BytesIO st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="centered") # Import LANGUAGES from translation.py try: from translation import LANGUAGES except ImportError as e: st.error(f"Failed to import translation module: {e}") st.stop() def main(): try: translation = importlib.import_module("translation") lang_detect = importlib.import_module("lang_detect") audio_processor = importlib.import_module("audio_processor") # Header st.markdown("
Translate text like Google Translate
", unsafe_allow_html=True) # Language and Input/Output Layout col1, col2 = st.columns(2) with col1: detected_options = lang_detect.detect_language(st.session_state.get("input_text", "")) if st.session_state.get("input_text", "").strip() else [("Auto-detect", 1.0, "Auto-detect")] source_lang = detected_options[0][0] if detected_options[0][0] != "Auto-detect" else "Auto-detect" source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "hi") if source_lang != "Auto-detect" else "auto" source_options = ["Auto-detect"] + [v[0] for v in LANGUAGES.values()] st.selectbox("Source", options=source_options, index=0 if source_lang == "Auto-detect" else source_options.index(source_lang), key="source_lang") input_type = st.radio("", ["Text", "File"], horizontal=True, label_visibility="hidden") if input_type == "Text": st.text_area("", height=200, key="input_text", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden") else: uploaded_file = st.file_uploader("", type=["txt", "docx", "pdf"], key="file_input", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden") if uploaded_file: st.session_state.input_text = uploaded_file.read().decode("utf-8").strip() st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,)) with col2: st.selectbox("Target", options=[v[0] for v in LANGUAGES.values()], index=list(LANGUAGES.values()).index(LANGUAGES["en"]), key="target_lang") if "translated_text" in st.session_state: st.text_area("", value=st.session_state.translated_text, height=200, key="output_text", disabled=True, label_visibility="hidden") st.button("🔊", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False) except Exception as e: st.error(f"App error: {e}") st.stop() def trigger_translation(translation, lang_detect, audio_processor): text = st.session_state.get("input_text", "").strip() if text: source_lang = st.session_state.source_lang target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "en") if source_lang == "Auto-detect": detected_options = lang_detect.detect_language(text) source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "hi") else: source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "hi") st.session_state.translated_text = translation.translate(text, source_lang_code, target_lang) or text def play_audio(audio_processor): if "translated_text" in st.session_state and st.session_state.translated_text: target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "en") 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") if __name__ == "__main__": main()