Update app.py
Browse files
app.py
CHANGED
@@ -23,10 +23,9 @@ def main():
|
|
23 |
detected_options = lang_detect.detect_language(st.session_state.get("user_text", "")) if st.session_state.get("user_text", "").strip() and len(st.session_state.get("user_text", "").strip()) >= 10 else [("English", 1.0, "English")]
|
24 |
source_lang = detected_options[0][0] if detected_options else "English"
|
25 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "en")
|
26 |
-
source_lang_display = st.selectbox("Source Language", [LANGUAGES[source_lang_code][0]] + [v[0] for v in LANGUAGES.values()], index=0, key="source_lang", help="Auto-detected, override if needed")
|
27 |
-
source_lang = next((k for k, v in LANGUAGES.items() if v[0] == source_lang_display), "en")
|
28 |
with col2:
|
29 |
-
target_lang_display = st.selectbox("Target Language", [v[0] for v in LANGUAGES.values()], index=list(LANGUAGES.values()).index(LANGUAGES["hi"]), key="target_lang")
|
30 |
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == target_lang_display), "hi")
|
31 |
|
32 |
# Input Section
|
@@ -34,27 +33,24 @@ def main():
|
|
34 |
st.session_state.user_text = ""
|
35 |
col_input = st.container()
|
36 |
with col_input:
|
37 |
-
input_type = st.radio("Input", ["Text", "File Upload"], horizontal=True, label_visibility="
|
38 |
if input_type == "Text":
|
39 |
-
user_text = st.text_area("", height=200, key="user_text", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,))
|
40 |
else:
|
41 |
-
user_text = st.file_uploader("
|
42 |
if user_text:
|
43 |
st.session_state.user_text = user_text.read().decode("utf-8").strip()
|
44 |
st.markdown(f"<small style='color: grey;'>Characters: {len(st.session_state.user_text)}/1000</small>", unsafe_allow_html=True)
|
45 |
-
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,))
|
46 |
|
47 |
# Output Section
|
48 |
col_output = st.container()
|
49 |
with col_output:
|
50 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
51 |
-
st.text_area("", value=st.session_state.translated_text, height=200, key="output_area", disabled=True)
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
with col_a:
|
56 |
-
if st.button("π", key="audio_btn", on_click=play_audio, args=(audio_processor,)):
|
57 |
-
pass # Handled by play_audio function
|
58 |
|
59 |
def trigger_translation(translation, lang_detect, audio_processor):
|
60 |
user_text = st.session_state.user_text.strip()
|
@@ -81,10 +77,6 @@ def trigger_translation(translation, lang_detect, audio_processor):
|
|
81 |
finally:
|
82 |
spinner.empty()
|
83 |
|
84 |
-
def copy_to_clipboard():
|
85 |
-
st.clipboard(st.session_state.translated_text)
|
86 |
-
st.success("Copied to clipboard!")
|
87 |
-
|
88 |
def play_audio(audio_processor):
|
89 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
90 |
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "hi")
|
|
|
23 |
detected_options = lang_detect.detect_language(st.session_state.get("user_text", "")) if st.session_state.get("user_text", "").strip() and len(st.session_state.get("user_text", "").strip()) >= 10 else [("English", 1.0, "English")]
|
24 |
source_lang = detected_options[0][0] if detected_options else "English"
|
25 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "en")
|
26 |
+
source_lang_display = st.selectbox("Source Language", options=[LANGUAGES[source_lang_code][0]] + [v[0] for v in LANGUAGES.values()], index=0, key="source_lang", help="Auto-detected, override if needed", label_visibility="hidden")
|
|
|
27 |
with col2:
|
28 |
+
target_lang_display = st.selectbox("Target Language", options=[v[0] for v in LANGUAGES.values()], index=list(LANGUAGES.values()).index(LANGUAGES["hi"]), key="target_lang", label_visibility="hidden")
|
29 |
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == target_lang_display), "hi")
|
30 |
|
31 |
# Input Section
|
|
|
33 |
st.session_state.user_text = ""
|
34 |
col_input = st.container()
|
35 |
with col_input:
|
36 |
+
input_type = st.radio("Input Method", ["Text", "File Upload"], horizontal=True, key="input_type", label_visibility="hidden")
|
37 |
if input_type == "Text":
|
38 |
+
user_text = st.text_area("Enter or paste text", height=200, key="user_text", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden")
|
39 |
else:
|
40 |
+
user_text = st.file_uploader("Upload File", type=["txt", "docx", "pdf"], key="doc_input", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden")
|
41 |
if user_text:
|
42 |
st.session_state.user_text = user_text.read().decode("utf-8").strip()
|
43 |
st.markdown(f"<small style='color: grey;'>Characters: {len(st.session_state.user_text)}/1000</small>", unsafe_allow_html=True)
|
44 |
+
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden")
|
45 |
|
46 |
# Output Section
|
47 |
col_output = st.container()
|
48 |
with col_output:
|
49 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
50 |
+
st.text_area("Translation", value=st.session_state.translated_text, height=200, key="output_area", disabled=True, label_visibility="hidden")
|
51 |
+
col_a = st.columns([1])[0] # Single column for speaker icon
|
52 |
+
if st.button("π", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Listen to translation", label_visibility="hidden"):
|
53 |
+
pass # Handled by play_audio function
|
|
|
|
|
|
|
54 |
|
55 |
def trigger_translation(translation, lang_detect, audio_processor):
|
56 |
user_text = st.session_state.user_text.strip()
|
|
|
77 |
finally:
|
78 |
spinner.empty()
|
79 |
|
|
|
|
|
|
|
|
|
80 |
def play_audio(audio_processor):
|
81 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
82 |
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "hi")
|