File size: 5,990 Bytes
737c030
e656608
da34b5e
35cccff
 
891f160
ec946c9
aa9d2fe
 
ff5aa1c
 
 
 
 
8040f65
35cccff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
891f160
ff5aa1c
 
 
 
e656608
ff5aa1c
 
9878bca
8040f65
ff5aa1c
 
 
9878bca
f6b831a
 
538b10c
f6b831a
bcf090f
9878bca
f6b831a
 
 
35cccff
f6b831a
 
ff5aa1c
 
35cccff
 
 
ff5aa1c
bcf090f
f6b831a
 
35cccff
 
 
ff5aa1c
 
 
1549aa1
 
9878bca
14a91a0
538b10c
35cccff
9878bca
 
 
460acea
9878bca
35cccff
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import streamlit as st
import importlib
from io import BytesIO
import docx
from PyPDF2 import PdfReader

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 extract_text_from_file(file):
    try:
        if file.type == "application/pdf":
            pdf_reader = PdfReader(file)
            text = ""
            for page in pdf_reader.pages:
                text += page.extract_text() or ""
            return text.encode().decode('utf-8', errors='ignore').strip()
        elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
            doc = docx.Document(file)
            text = "\n".join([para.text for para in doc.paragraphs])
            return text.encode().decode('utf-8', errors='ignore').strip()
        elif file.type == "text/plain":
            return file.read().decode('utf-8', errors='ignore').strip()
        return ""
    except Exception:
        return ""

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 = extract_text_from_file(uploaded_file)
                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:
            source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
            target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display]
            st.selectbox("Target", options=target_options, index=target_options.index(f"{LANGUAGES['en'][0]} ({LANGUAGES['en'][1]})") if "English" not in source_lang_display else 0, 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
        # Footer
        if "translated_text" in st.session_state:
            st.markdown("<p style='text-align: center; color: #666;'>Developed by Krishna Prakash <a href='https://www.linkedin.com/in/krishna-prakash-123456' target='_blank'>LinkedIn</a></p>", unsafe_allow_html=True)

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