Update app.py
Browse files
app.py
CHANGED
@@ -23,24 +23,23 @@ def main():
|
|
23 |
# Language and Input/Output Layout
|
24 |
col1, col2 = st.columns(2)
|
25 |
with col1:
|
26 |
-
detected_options = lang_detect.detect_language(st.session_state.get("input_text", "")) if st.session_state.get("input_text", "").strip() and len(st.session_state.get("input_text", "").strip()) >= 10 else [("
|
27 |
-
source_lang = detected_options[0][0] if detected_options else "
|
28 |
-
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "
|
29 |
-
|
30 |
-
input_type = st.radio("Input", ["Text", "File"], horizontal=True, key="input_type")
|
31 |
if input_type == "Text":
|
32 |
-
input_text = st.text_area("", height=200, key="input_text", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,))
|
33 |
else:
|
34 |
-
input_text = st.file_uploader("", type=["txt", "docx", "pdf"], key="file_input", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,))
|
35 |
if input_text:
|
36 |
st.session_state.input_text = input_text.read().decode("utf-8").strip()
|
37 |
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,))
|
38 |
with col2:
|
39 |
-
|
40 |
-
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == target_lang_display), "hi")
|
41 |
if "translated_text" in st.session_state:
|
42 |
-
st.text_area("", value=st.session_state.translated_text, height=200, key="output_text", disabled=True)
|
43 |
-
if st.button("", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio"):
|
44 |
pass # Handled by play_audio
|
45 |
|
46 |
except Exception as e:
|
@@ -50,8 +49,8 @@ def main():
|
|
50 |
def trigger_translation(translation, lang_detect, audio_processor):
|
51 |
text = st.session_state.input_text.strip()
|
52 |
if text:
|
53 |
-
source_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.source_lang), "
|
54 |
-
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "
|
55 |
if source_lang != "en" and target_lang != "en":
|
56 |
temp = translation.translate(text, source_lang, "en")
|
57 |
if temp and len(temp.split()) >= 2:
|
@@ -63,7 +62,7 @@ def trigger_translation(translation, lang_detect, audio_processor):
|
|
63 |
|
64 |
def play_audio(audio_processor):
|
65 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
66 |
-
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "
|
67 |
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
|
68 |
if audio and audio.getbuffer().nbytes > 0:
|
69 |
st.audio(audio, format="audio/mp3")
|
|
|
23 |
# Language and Input/Output Layout
|
24 |
col1, col2 = st.columns(2)
|
25 |
with col1:
|
26 |
+
detected_options = lang_detect.detect_language(st.session_state.get("input_text", "")) if st.session_state.get("input_text", "").strip() and len(st.session_state.get("input_text", "").strip()) >= 10 else [("Hindi", 1.0, "हिन्दी")]
|
27 |
+
source_lang = detected_options[0][0] if detected_options else "Hindi"
|
28 |
+
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "hi")
|
29 |
+
st.selectbox("Source", options=[LANGUAGES[source_lang_code][0]] + [v[0] for v in LANGUAGES.values()], index=0, key="source_lang")
|
30 |
+
input_type = st.radio("Input", ["Text", "File"], horizontal=True, key="input_type", label_visibility="hidden")
|
31 |
if input_type == "Text":
|
32 |
+
input_text = st.text_area("Input", height=200, key="input_text", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden")
|
33 |
else:
|
34 |
+
input_text = st.file_uploader("Input", type=["txt", "docx", "pdf"], key="file_input", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden")
|
35 |
if input_text:
|
36 |
st.session_state.input_text = input_text.read().decode("utf-8").strip()
|
37 |
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,))
|
38 |
with col2:
|
39 |
+
st.selectbox("Target", options=[v[0] for v in LANGUAGES.values()], index=list(LANGUAGES.values()).index(LANGUAGES["en"]), key="target_lang")
|
|
|
40 |
if "translated_text" in st.session_state:
|
41 |
+
st.text_area("Output", value=st.session_state.translated_text, height=200, key="output_text", disabled=True, label_visibility="hidden")
|
42 |
+
if st.button("🔊", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio"):
|
43 |
pass # Handled by play_audio
|
44 |
|
45 |
except Exception as e:
|
|
|
49 |
def trigger_translation(translation, lang_detect, audio_processor):
|
50 |
text = st.session_state.input_text.strip()
|
51 |
if text:
|
52 |
+
source_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.source_lang), "hi")
|
53 |
+
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "en")
|
54 |
if source_lang != "en" and target_lang != "en":
|
55 |
temp = translation.translate(text, source_lang, "en")
|
56 |
if temp and len(temp.split()) >= 2:
|
|
|
62 |
|
63 |
def play_audio(audio_processor):
|
64 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
65 |
+
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "en")
|
66 |
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
|
67 |
if audio and audio.getbuffer().nbytes > 0:
|
68 |
st.audio(audio, format="audio/mp3")
|