Update app.py
Browse files
app.py
CHANGED
@@ -25,27 +25,28 @@ def main():
|
|
25 |
col1, col2 = st.columns(2)
|
26 |
with col1:
|
27 |
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")]
|
28 |
-
source_lang = detected_options[0][
|
29 |
-
source_lang_code = next((k for k, v in LANGUAGES.items() if v[
|
30 |
source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
|
31 |
-
st.selectbox("Source", options=source_options, index=0 if source_lang == "Auto-detect" else source_options.index(f"{
|
|
|
32 |
input_type = st.radio("", ["Text", "File"], horizontal=True, label_visibility="hidden")
|
33 |
-
if input_type == "
|
34 |
-
st.
|
35 |
-
|
36 |
-
uploaded_file = st.file_uploader("", type=["txt", "docx", "pdf"], key="file_input", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden", max_size=1024*1024) # 1 MB limit
|
37 |
-
if uploaded_file and uploaded_file.size <= 1024*1024:
|
38 |
st.session_state.input_text = uploaded_file.read().decode("utf-8").strip()
|
|
|
|
|
39 |
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,))
|
40 |
with col2:
|
41 |
st.selectbox("Target", options=[f"{v[0]} ({v[1]})" for v in LANGUAGES.values()], index=list(LANGUAGES.values()).index(LANGUAGES["en"]), key="target_lang")
|
42 |
if "translated_text" in st.session_state:
|
43 |
st.text_area("", value=st.session_state.translated_text, height=200, key="output_text", disabled=True, label_visibility="hidden")
|
44 |
-
st.button("🔊", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False)
|
|
|
45 |
|
46 |
except Exception as e:
|
47 |
st.error(f"App error: {e}")
|
48 |
-
st.stop()
|
49 |
|
50 |
def trigger_translation(translation, lang_detect, audio_processor):
|
51 |
text = st.session_state.get("input_text", "").strip()
|
|
|
25 |
col1, col2 = st.columns(2)
|
26 |
with col1:
|
27 |
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")]
|
28 |
+
source_lang = detected_options[0][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
|
29 |
+
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "hi") if source_lang != "Auto-detect" else "auto"
|
30 |
source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
|
31 |
+
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")
|
32 |
+
input_text = st.text_area("", height=200, key="input_text", label_visibility="hidden")
|
33 |
input_type = st.radio("", ["Text", "File"], horizontal=True, label_visibility="hidden")
|
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.read().decode("utf-8").strip()
|
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.selectbox("Target", options=[f"{v[0]} ({v[1]})" for v in LANGUAGES.values()], index=list(LANGUAGES.values()).index(LANGUAGES["en"]), key="target_lang")
|
43 |
if "translated_text" in st.session_state:
|
44 |
st.text_area("", value=st.session_state.translated_text, height=200, 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}")
|
|
|
50 |
|
51 |
def trigger_translation(translation, lang_detect, audio_processor):
|
52 |
text = st.session_state.get("input_text", "").strip()
|