Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ 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="
|
8 |
|
9 |
# Import LANGUAGES from translation.py
|
10 |
try:
|
@@ -37,6 +37,7 @@ 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 |
|
@@ -51,7 +52,7 @@ def main():
|
|
51 |
st.markdown("<h1 style='text-align: center; color: #4285F4;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
52 |
st.markdown("<p style='text-align: center; color: #666;'>Effortless Multilingual Translation</p>", unsafe_allow_html=True)
|
53 |
|
54 |
-
# Custom CSS to hide size limit and show only file types
|
55 |
st.markdown(
|
56 |
"""
|
57 |
<style>
|
@@ -59,45 +60,47 @@ def main():
|
|
59 |
display: none !important;
|
60 |
}
|
61 |
.stFileUploader label {
|
62 |
-
display:
|
63 |
-
color: #666 !important;
|
64 |
-
font-size: 0.8em !important;
|
65 |
-
}
|
66 |
-
.stFileUploader label::after {
|
67 |
-
content: 'TXT, DOCX, PDF' !important;
|
68 |
}
|
69 |
.stFileUploader [data-testid="stFileUploaderDropzone"] {
|
|
|
|
|
|
|
70 |
font-size: 0 !important;
|
71 |
}
|
72 |
.stFileUploader [data-testid="stFileUploaderDropzone"]::after {
|
73 |
-
content: 'Drag and drop
|
74 |
font-size: 1em !important;
|
75 |
color: #666 !important;
|
76 |
}
|
77 |
.stFileUploader [data-testid="stFileUploaderButton"] {
|
78 |
font-size: 1em !important;
|
79 |
color: #1E90FF !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
}
|
81 |
</style>
|
82 |
""",
|
83 |
unsafe_allow_html=True
|
84 |
)
|
85 |
|
86 |
-
# Language and Input/Output Layout with
|
87 |
-
left_col, right_col = st.columns([
|
88 |
with left_col:
|
89 |
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")]
|
90 |
source_language = detected_options[0][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
|
91 |
-
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_language), "
|
92 |
source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
|
93 |
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")
|
|
|
94 |
user_input_text = st.text_area("Input Text", height=200, key="user_input_text", placeholder="Enter text here", label_visibility="hidden")
|
95 |
input_type = st.radio("Input Type", ["Text", "File"], horizontal=True, label_visibility="hidden", key="input_type")
|
96 |
if input_type == "File":
|
97 |
st.file_uploader("Upload File", type=["txt", "docx", "pdf"], key="file_input", on_change=on_file_upload, label_visibility="hidden")
|
98 |
-
if st.session_state.get("file_input") and st.session_state.get("file_input").size >= 1024*1024:
|
99 |
-
st.error("File size must be less than 1 MB")
|
100 |
-
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation_module, language_detector, audio_processor_module))
|
101 |
with right_col:
|
102 |
source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
103 |
target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display]
|
@@ -108,15 +111,14 @@ def main():
|
|
108 |
if st.button("π", key="audio_btn", on_click=play_audio, args=(audio_processor_module,), help="Play audio", use_container_width=False):
|
109 |
pass
|
110 |
# Footer
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
<
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
""", unsafe_allow_html=True)
|
120 |
|
121 |
except Exception as e:
|
122 |
st.error(f"App error: {e}")
|
@@ -130,12 +132,15 @@ def trigger_translation(translation_module, language_detector, audio_processor_m
|
|
130 |
target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang
|
131 |
if source_lang == "Auto-detect":
|
132 |
detected_options = language_detector.detect_language(user_input_text)
|
133 |
-
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "en")
|
134 |
else:
|
135 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "en")
|
136 |
target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "hi")
|
137 |
translated_text = translation_module.translate(user_input_text, source_lang_code, target_lang_code)
|
138 |
-
|
|
|
|
|
|
|
139 |
|
140 |
# Function to handle audio playback
|
141 |
def play_audio(audio_processor_module):
|
|
|
4 |
import docx
|
5 |
from PyPDF2 import PdfReader
|
6 |
|
7 |
+
st.set_page_config(page_title="Multilingual Translator", page_icon="π", layout="wide")
|
8 |
|
9 |
# Import LANGUAGES from translation.py
|
10 |
try:
|
|
|
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 |
+
st.success(f"File '{uploaded_file.name}' uploaded successfully!")
|
41 |
elif uploaded_file and uploaded_file.size >= 1024*1024:
|
42 |
st.error("File size must be less than 1 MB")
|
43 |
|
|
|
52 |
st.markdown("<h1 style='text-align: center; color: #4285F4;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
53 |
st.markdown("<p style='text-align: center; color: #666;'>Effortless Multilingual Translation</p>", unsafe_allow_html=True)
|
54 |
|
55 |
+
# Custom CSS to hide size limit and white box, show only file types
|
56 |
st.markdown(
|
57 |
"""
|
58 |
<style>
|
|
|
60 |
display: none !important;
|
61 |
}
|
62 |
.stFileUploader label {
|
63 |
+
display: none !important;
|
|
|
|
|
|
|
|
|
|
|
64 |
}
|
65 |
.stFileUploader [data-testid="stFileUploaderDropzone"] {
|
66 |
+
border: 2px dashed #ccc !important;
|
67 |
+
padding: 10px !important;
|
68 |
+
text-align: center !important;
|
69 |
font-size: 0 !important;
|
70 |
}
|
71 |
.stFileUploader [data-testid="stFileUploaderDropzone"]::after {
|
72 |
+
content: 'Drag and drop TXT, DOCX, or PDF here or ' !important;
|
73 |
font-size: 1em !important;
|
74 |
color: #666 !important;
|
75 |
}
|
76 |
.stFileUploader [data-testid="stFileUploaderButton"] {
|
77 |
font-size: 1em !important;
|
78 |
color: #1E90FF !important;
|
79 |
+
background: none !important;
|
80 |
+
border: none !important;
|
81 |
+
padding: 0 !important;
|
82 |
+
}
|
83 |
+
.stFileUploader [data-testid="stFileUploader"] {
|
84 |
+
background: none !important;
|
85 |
}
|
86 |
</style>
|
87 |
""",
|
88 |
unsafe_allow_html=True
|
89 |
)
|
90 |
|
91 |
+
# Language and Input/Output Layout with symmetric columns
|
92 |
+
left_col, right_col = st.columns([1, 1]) # Equal width for symmetric layout
|
93 |
with left_col:
|
94 |
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")]
|
95 |
source_language = detected_options[0][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
|
96 |
+
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_language), "en") if source_language != "Auto-detect" else "auto"
|
97 |
source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
|
98 |
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")
|
99 |
+
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation_module, language_detector, audio_processor_module))
|
100 |
user_input_text = st.text_area("Input Text", height=200, key="user_input_text", placeholder="Enter text here", label_visibility="hidden")
|
101 |
input_type = st.radio("Input Type", ["Text", "File"], horizontal=True, label_visibility="hidden", key="input_type")
|
102 |
if input_type == "File":
|
103 |
st.file_uploader("Upload File", type=["txt", "docx", "pdf"], key="file_input", on_change=on_file_upload, label_visibility="hidden")
|
|
|
|
|
|
|
104 |
with right_col:
|
105 |
source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
106 |
target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display]
|
|
|
111 |
if st.button("π", key="audio_btn", on_click=play_audio, args=(audio_processor_module,), help="Play audio", use_container_width=False):
|
112 |
pass
|
113 |
# Footer
|
114 |
+
st.markdown("""
|
115 |
+
<p style="font-size: small; color: grey; text-align: center;">
|
116 |
+
Developed By: Krishna Prakash
|
117 |
+
<a href="https://www.linkedin.com/in/krishnaprakash-profile/" target="_blank">
|
118 |
+
<img src="https://img.icons8.com/ios-filled/30/0077b5/linkedin.png" alt="LinkedIn" style="vertical-align: middle; margin: 0 5px;"/>
|
119 |
+
</a>
|
120 |
+
</p>
|
121 |
+
""", unsafe_allow_html=True)
|
|
|
122 |
|
123 |
except Exception as e:
|
124 |
st.error(f"App error: {e}")
|
|
|
132 |
target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang
|
133 |
if source_lang == "Auto-detect":
|
134 |
detected_options = language_detector.detect_language(user_input_text)
|
135 |
+
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "en")
|
136 |
else:
|
137 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "en")
|
138 |
target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "hi")
|
139 |
translated_text = translation_module.translate(user_input_text, source_lang_code, target_lang_code)
|
140 |
+
if translated_text and len(translated_text.split()) > 2: # Basic validation
|
141 |
+
st.session_state.translated_text = translated_text
|
142 |
+
else:
|
143 |
+
st.session_state.translated_text = user_input_text
|
144 |
|
145 |
# Function to handle audio playback
|
146 |
def play_audio(audio_processor_module):
|