Update app.py
Browse files
app.py
CHANGED
@@ -13,36 +13,39 @@ except ImportError as e:
|
|
13 |
st.error(f"Failed to import translation module: {e}")
|
14 |
st.stop()
|
15 |
|
16 |
-
|
|
|
17 |
try:
|
18 |
-
if
|
19 |
-
pdf_reader = PdfReader(
|
20 |
text = ""
|
21 |
for page in pdf_reader.pages:
|
22 |
text += page.extract_text() or ""
|
23 |
return text.encode().decode('utf-8', errors='ignore').strip()
|
24 |
-
elif
|
25 |
-
doc = docx.Document(
|
26 |
text = "\n".join([para.text for para in doc.paragraphs])
|
27 |
return text.encode().decode('utf-8', errors='ignore').strip()
|
28 |
-
elif
|
29 |
-
return
|
30 |
return ""
|
31 |
except Exception:
|
32 |
return ""
|
33 |
|
|
|
34 |
def on_file_upload():
|
35 |
uploaded_file = st.session_state.file_input
|
36 |
if uploaded_file and uploaded_file.size < 1024*1024:
|
37 |
-
st.session_state.
|
38 |
elif uploaded_file and uploaded_file.size >= 1024*1024:
|
39 |
st.error("File size must be less than 1 MB")
|
40 |
|
|
|
41 |
def main():
|
42 |
try:
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
|
47 |
# Header
|
48 |
st.markdown("<h1 style='text-align: center; color: #4285F4;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
@@ -61,36 +64,34 @@ def main():
|
|
61 |
font-size: 0.8em;
|
62 |
}
|
63 |
.stFileUploader label::after {
|
64 |
-
content:
|
65 |
}
|
66 |
</style>
|
67 |
""",
|
68 |
unsafe_allow_html=True
|
69 |
)
|
70 |
|
71 |
-
# Language and Input/Output Layout
|
72 |
-
|
73 |
-
with
|
74 |
-
detected_options =
|
75 |
-
|
76 |
-
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] ==
|
77 |
source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
|
78 |
-
st.selectbox("Source Language", options=source_options, index=0 if
|
79 |
-
|
80 |
input_type = st.radio("Input Type", ["Text", "File"], horizontal=True, label_visibility="hidden", key="input_type")
|
81 |
if input_type == "File":
|
82 |
st.file_uploader("Upload File", type=["txt", "docx", "pdf"], key="file_input", on_change=on_file_upload, label_visibility="hidden")
|
83 |
if st.session_state.get("file_input") and st.session_state.get("file_input").size >= 1024*1024:
|
84 |
st.error("File size must be less than 1 MB")
|
85 |
-
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(
|
86 |
-
with
|
87 |
source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
88 |
target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display]
|
89 |
st.selectbox("Target Language", 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")
|
90 |
if "translated_text" in st.session_state:
|
91 |
st.text_area("Output Text", value=st.session_state.translated_text, height=400, key="output_text", disabled=True, label_visibility="hidden")
|
92 |
-
if st.button("🔊", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False):
|
93 |
-
pass
|
94 |
# Footer
|
95 |
if "translated_text" in st.session_state:
|
96 |
st.markdown("""
|
@@ -101,30 +102,35 @@ def main():
|
|
101 |
</a>
|
102 |
</p>
|
103 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
104 |
|
105 |
except Exception as e:
|
106 |
st.error(f"App error: {e}")
|
107 |
|
108 |
-
|
109 |
-
|
|
|
110 |
if text:
|
111 |
source_lang = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
112 |
target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang
|
113 |
if source_lang == "Auto-detect":
|
114 |
-
detected_options =
|
115 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "hi")
|
116 |
else:
|
117 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "hi")
|
118 |
target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "en")
|
119 |
-
translated_text =
|
120 |
st.session_state.translated_text = translated_text or text
|
121 |
|
122 |
-
|
|
|
123 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
124 |
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang.split(" (")[0]), "en")
|
125 |
-
|
126 |
-
if
|
127 |
-
st.audio(
|
128 |
|
129 |
if __name__ == "__main__":
|
130 |
main()
|
|
|
13 |
st.error(f"Failed to import translation module: {e}")
|
14 |
st.stop()
|
15 |
|
16 |
+
# Function to extract text from uploaded files (PDF, DOCX, TXT)
|
17 |
+
def extract_text_from_file(uploaded_file):
|
18 |
try:
|
19 |
+
if uploaded_file.type == "application/pdf":
|
20 |
+
pdf_reader = PdfReader(uploaded_file)
|
21 |
text = ""
|
22 |
for page in pdf_reader.pages:
|
23 |
text += page.extract_text() or ""
|
24 |
return text.encode().decode('utf-8', errors='ignore').strip()
|
25 |
+
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
26 |
+
doc = docx.Document(uploaded_file)
|
27 |
text = "\n".join([para.text for para in doc.paragraphs])
|
28 |
return text.encode().decode('utf-8', errors='ignore').strip()
|
29 |
+
elif uploaded_file.type == "text/plain":
|
30 |
+
return uploaded_file.read().decode('utf-8', errors='ignore').strip()
|
31 |
return ""
|
32 |
except Exception:
|
33 |
return ""
|
34 |
|
35 |
+
# Callback to update input text when file is uploaded
|
36 |
def on_file_upload():
|
37 |
uploaded_file = st.session_state.file_input
|
38 |
if uploaded_file and uploaded_file.size < 1024*1024:
|
39 |
+
st.session_state.user_input_text = extract_text_from_file(uploaded_file)
|
40 |
elif uploaded_file and uploaded_file.size >= 1024*1024:
|
41 |
st.error("File size must be less than 1 MB")
|
42 |
|
43 |
+
# Main application function
|
44 |
def main():
|
45 |
try:
|
46 |
+
translation_module = importlib.import_module("translation")
|
47 |
+
language_detector = importlib.import_module("lang_detect")
|
48 |
+
audio_processor_module = importlib.import_module("audio_processor")
|
49 |
|
50 |
# Header
|
51 |
st.markdown("<h1 style='text-align: center; color: #4285F4;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
|
|
64 |
font-size: 0.8em;
|
65 |
}
|
66 |
.stFileUploader label::after {
|
67 |
+
content: 'Limit 1MB per file • TXT, DOCX, PDF';
|
68 |
}
|
69 |
</style>
|
70 |
""",
|
71 |
unsafe_allow_html=True
|
72 |
)
|
73 |
|
74 |
+
# Language and Input/Output Layout with symmetric columns
|
75 |
+
left_col, right_col = st.columns([1, 1]) # Equal width for symmetric layout
|
76 |
+
with left_col:
|
77 |
+
detected_options = language_detector.detect_language(st.session_state.get("user_input_text", "")) if st.session_state.get("user_input_text", "").strip() else [("Auto-detect", 1.0, "Auto-detect")]
|
78 |
+
source_language = detected_options[0][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
|
79 |
+
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_language), "hi") if source_language != "Auto-detect" else "auto"
|
80 |
source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
|
81 |
+
st.selectbox("Source Language", options=source_options, index=0 if source_language == "Auto-detect" else source_options.index(f"{LANGUAGES[source_lang_code][0]} ({source_language})"), key="source_lang")
|
82 |
+
user_input_text = st.text_area("Input Text", height=400, key="user_input_text", placeholder="Enter text here", label_visibility="hidden")
|
83 |
input_type = st.radio("Input Type", ["Text", "File"], horizontal=True, label_visibility="hidden", key="input_type")
|
84 |
if input_type == "File":
|
85 |
st.file_uploader("Upload File", type=["txt", "docx", "pdf"], key="file_input", on_change=on_file_upload, label_visibility="hidden")
|
86 |
if st.session_state.get("file_input") and st.session_state.get("file_input").size >= 1024*1024:
|
87 |
st.error("File size must be less than 1 MB")
|
88 |
+
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation_module, language_detector, audio_processor_module))
|
89 |
+
with right_col:
|
90 |
source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
91 |
target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display]
|
92 |
st.selectbox("Target Language", 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")
|
93 |
if "translated_text" in st.session_state:
|
94 |
st.text_area("Output Text", value=st.session_state.translated_text, height=400, key="output_text", disabled=True, label_visibility="hidden")
|
|
|
|
|
95 |
# Footer
|
96 |
if "translated_text" in st.session_state:
|
97 |
st.markdown("""
|
|
|
102 |
</a>
|
103 |
</p>
|
104 |
""", unsafe_allow_html=True)
|
105 |
+
# Play audio button and playback below output
|
106 |
+
if st.button("🔊", key="audio_btn", on_click=play_audio, args=(audio_processor_module,), help="Play audio", use_container_width=False):
|
107 |
+
pass
|
108 |
|
109 |
except Exception as e:
|
110 |
st.error(f"App error: {e}")
|
111 |
|
112 |
+
# Function to trigger translation process
|
113 |
+
def trigger_translation(translation_module, language_detector, audio_processor_module):
|
114 |
+
text = st.session_state.get("user_input_text", "").strip()
|
115 |
if text:
|
116 |
source_lang = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
117 |
target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang
|
118 |
if source_lang == "Auto-detect":
|
119 |
+
detected_options = language_detector.detect_language(text)
|
120 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "hi")
|
121 |
else:
|
122 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "hi")
|
123 |
target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "en")
|
124 |
+
translated_text = translation_module.translate(text, source_lang_code, target_lang_code)
|
125 |
st.session_state.translated_text = translated_text or text
|
126 |
|
127 |
+
# Function to handle audio playback
|
128 |
+
def play_audio(audio_processor_module):
|
129 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
130 |
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang.split(" (")[0]), "en")
|
131 |
+
audio_data = audio_processor_module.text_to_speech(st.session_state.translated_text, target_lang)
|
132 |
+
if audio_data and audio_data.getbuffer().nbytes > 0:
|
133 |
+
st.audio(audio_data, format="audio/mp3")
|
134 |
|
135 |
if __name__ == "__main__":
|
136 |
main()
|