Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import streamlit as st
|
2 |
import importlib
|
3 |
from io import BytesIO
|
|
|
|
|
4 |
|
5 |
st.set_page_config(page_title="Multilingual Translator", page_icon="π", layout="centered")
|
6 |
|
@@ -11,6 +13,24 @@ except ImportError as e:
|
|
11 |
st.error(f"Failed to import translation module: {e}")
|
12 |
st.stop()
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def main():
|
15 |
try:
|
16 |
translation = importlib.import_module("translation")
|
@@ -34,16 +54,21 @@ def main():
|
|
34 |
if input_type == "File":
|
35 |
uploaded_file = st.file_uploader("", type=["txt", "docx", "pdf"], key="file_input", label_visibility="hidden")
|
36 |
if uploaded_file and uploaded_file.size < 1024*1024: # 1 MB limit
|
37 |
-
st.session_state.input_text = uploaded_file
|
38 |
elif uploaded_file and uploaded_file.size >= 1024*1024:
|
39 |
st.error("File size must be less than 1 MB")
|
40 |
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,))
|
41 |
with col2:
|
42 |
-
st.
|
|
|
|
|
43 |
if "translated_text" in st.session_state:
|
44 |
st.text_area("", value=st.session_state.translated_text, height=300, key="output_text", disabled=True, label_visibility="hidden")
|
45 |
if st.button("π", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False):
|
46 |
pass
|
|
|
|
|
|
|
47 |
|
48 |
except Exception as e:
|
49 |
st.error(f"App error: {e}")
|
@@ -52,13 +77,14 @@ def trigger_translation(translation, lang_detect, audio_processor):
|
|
52 |
text = st.session_state.get("input_text", "").strip()
|
53 |
if text:
|
54 |
source_lang = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
55 |
-
target_lang =
|
56 |
if source_lang == "Auto-detect":
|
57 |
detected_options = lang_detect.detect_language(text)
|
58 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "hi")
|
59 |
else:
|
60 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "hi")
|
61 |
-
|
|
|
62 |
st.session_state.translated_text = translated_text or text
|
63 |
|
64 |
def play_audio(audio_processor):
|
|
|
1 |
import streamlit as st
|
2 |
import importlib
|
3 |
from io import BytesIO
|
4 |
+
import docx
|
5 |
+
from PyPDF2 import PdfReader
|
6 |
|
7 |
st.set_page_config(page_title="Multilingual Translator", page_icon="π", layout="centered")
|
8 |
|
|
|
13 |
st.error(f"Failed to import translation module: {e}")
|
14 |
st.stop()
|
15 |
|
16 |
+
def extract_text_from_file(file):
|
17 |
+
try:
|
18 |
+
if file.type == "application/pdf":
|
19 |
+
pdf_reader = PdfReader(file)
|
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 file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
25 |
+
doc = docx.Document(file)
|
26 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
27 |
+
return text.encode().decode('utf-8', errors='ignore').strip()
|
28 |
+
elif file.type == "text/plain":
|
29 |
+
return file.read().decode('utf-8', errors='ignore').strip()
|
30 |
+
return ""
|
31 |
+
except Exception:
|
32 |
+
return ""
|
33 |
+
|
34 |
def main():
|
35 |
try:
|
36 |
translation = importlib.import_module("translation")
|
|
|
54 |
if input_type == "File":
|
55 |
uploaded_file = st.file_uploader("", type=["txt", "docx", "pdf"], key="file_input", label_visibility="hidden")
|
56 |
if uploaded_file and uploaded_file.size < 1024*1024: # 1 MB limit
|
57 |
+
st.session_state.input_text = extract_text_from_file(uploaded_file)
|
58 |
elif uploaded_file and uploaded_file.size >= 1024*1024:
|
59 |
st.error("File size must be less than 1 MB")
|
60 |
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,))
|
61 |
with col2:
|
62 |
+
source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
63 |
+
target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display]
|
64 |
+
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")
|
65 |
if "translated_text" in st.session_state:
|
66 |
st.text_area("", value=st.session_state.translated_text, height=300, key="output_text", disabled=True, label_visibility="hidden")
|
67 |
if st.button("π", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False):
|
68 |
pass
|
69 |
+
# Footer
|
70 |
+
if "translated_text" in st.session_state:
|
71 |
+
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)
|
72 |
|
73 |
except Exception as e:
|
74 |
st.error(f"App error: {e}")
|
|
|
77 |
text = st.session_state.get("input_text", "").strip()
|
78 |
if text:
|
79 |
source_lang = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
80 |
+
target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang
|
81 |
if source_lang == "Auto-detect":
|
82 |
detected_options = lang_detect.detect_language(text)
|
83 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "hi")
|
84 |
else:
|
85 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "hi")
|
86 |
+
target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "en")
|
87 |
+
translated_text = translation.translate(text, source_lang_code, target_lang_code)
|
88 |
st.session_state.translated_text = translated_text or text
|
89 |
|
90 |
def play_audio(audio_processor):
|