File size: 4,458 Bytes
737c030
e656608
da34b5e
891f160
ec946c9
aa9d2fe
 
ff5aa1c
 
 
 
 
8040f65
891f160
ff5aa1c
 
 
 
e656608
ff5aa1c
 
9878bca
8040f65
ff5aa1c
 
 
9878bca
f6b831a
 
538b10c
f6b831a
bcf090f
9878bca
f6b831a
 
 
9878bca
f6b831a
 
ff5aa1c
 
538b10c
ff5aa1c
bcf090f
f6b831a
 
ff5aa1c
 
 
1549aa1
 
9878bca
14a91a0
538b10c
 
9878bca
 
 
460acea
9878bca
bcf090f
 
1549aa1
 
 
538b10c
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
71
72
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][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
            source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "hi") if source_lang != "Auto-detect" else "auto"
            source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
            st.selectbox("Source", options=source_options, index=0 if source_lang == "Auto-detect" else source_options.index(f"{LANGUAGES[source_lang_code][0]} ({source_lang})"), key="source_lang")
            input_text = st.text_area("", height=300, key="input_text", label_visibility="hidden")
            input_type = st.radio("", ["Text", "File"], horizontal=True, label_visibility="hidden")
            if input_type == "File":
                uploaded_file = st.file_uploader("", type=["txt", "docx", "pdf"], key="file_input", label_visibility="hidden")
                if uploaded_file and uploaded_file.size < 1024*1024:  # 1 MB limit
                    st.session_state.input_text = uploaded_file.read().decode("utf-8").strip()
                elif uploaded_file and uploaded_file.size >= 1024*1024:
                    st.error("File size must be less than 1 MB")
            st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,))
        with col2:
            st.selectbox("Target", options=[f"{v[0]} ({v[1]})" 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=300, key="output_text", disabled=True, label_visibility="hidden")
                if st.button("πŸ”Š", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False):
                    pass

    except Exception as e:
        st.error(f"App error: {e}")

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.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
        target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang.split(" (")[0]), "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")
        translated_text = translation.translate(text, source_lang_code, target_lang)
        st.session_state.translated_text = translated_text 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.split(" (")[0]), "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()