File size: 4,205 Bytes
737c030
e656608
da34b5e
891f160
ec946c9
aa9d2fe
 
ff5aa1c
 
 
 
 
8040f65
891f160
ff5aa1c
 
 
 
e656608
ff5aa1c
 
9878bca
8040f65
ff5aa1c
 
 
9878bca
 
 
 
 
 
ff5aa1c
9878bca
ff5aa1c
9878bca
 
 
ff5aa1c
 
e8fb859
ff5aa1c
9878bca
 
ff5aa1c
 
 
 
1549aa1
 
9878bca
14a91a0
9878bca
e8fb859
9878bca
 
 
460acea
9878bca
 
1549aa1
 
 
e8fb859
1549aa1
 
14a91a0
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
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("<h1 style='text-align: center; color: #4285F4;'>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)

        # 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()