Update app.py
Browse files
app.py
CHANGED
@@ -31,6 +31,13 @@ def extract_text_from_file(file):
|
|
31 |
except Exception:
|
32 |
return ""
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def main():
|
35 |
try:
|
36 |
translation = importlib.import_module("translation")
|
@@ -48,23 +55,21 @@ def main():
|
|
48 |
source_lang = detected_options[0][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
|
49 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "hi") if source_lang != "Auto-detect" else "auto"
|
50 |
source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
|
51 |
-
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")
|
52 |
-
input_text = st.text_area("", height=300, key="input_text", label_visibility="hidden")
|
53 |
-
input_type = st.radio("", ["Text", "File"], horizontal=True, label_visibility="hidden")
|
54 |
if input_type == "File":
|
55 |
-
|
56 |
-
if
|
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:
|
@@ -92,7 +97,7 @@ def play_audio(audio_processor):
|
|
92 |
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang.split(" (")[0]), "en")
|
93 |
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
|
94 |
if audio and audio.getbuffer().nbytes > 0:
|
95 |
-
st.audio(audio, format="audio/mp3")
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
main()
|
|
|
31 |
except Exception:
|
32 |
return ""
|
33 |
|
34 |
+
def on_file_upload():
|
35 |
+
uploaded_file = st.session_state.file_input
|
36 |
+
if uploaded_file and uploaded_file.size < 1024*1024:
|
37 |
+
st.session_state.input_text = extract_text_from_file(uploaded_file)
|
38 |
+
elif uploaded_file and uploaded_file.size >= 1024*1024:
|
39 |
+
st.error("File size must be less than 1 MB")
|
40 |
+
|
41 |
def main():
|
42 |
try:
|
43 |
translation = importlib.import_module("translation")
|
|
|
55 |
source_lang = detected_options[0][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
|
56 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "hi") if source_lang != "Auto-detect" else "auto"
|
57 |
source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
|
58 |
+
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", label="Source Language")
|
59 |
+
input_text = st.text_area("Input Text", height=300, key="input_text", label_visibility="hidden")
|
60 |
+
input_type = st.radio("Input Type", ["Text", "File"], horizontal=True, label_visibility="hidden", key="input_type")
|
61 |
if input_type == "File":
|
62 |
+
st.file_uploader("Upload File", type=["txt", "docx", "pdf"], key="file_input", on_change=on_file_upload, label_visibility="hidden")
|
63 |
+
if st.session_state.get("file_input") and st.session_state.get("file_input").size >= 1024*1024:
|
|
|
|
|
64 |
st.error("File size must be less than 1 MB")
|
65 |
+
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor), label="Translate Button")
|
66 |
with col2:
|
67 |
source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
68 |
target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display]
|
69 |
+
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", label="Target Language")
|
70 |
if "translated_text" in st.session_state:
|
71 |
+
st.text_area("Output Text", value=st.session_state.translated_text, height=300, key="output_text", disabled=True, label_visibility="hidden")
|
72 |
+
if st.button("🔊", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False, label="Play Audio"):
|
73 |
pass
|
74 |
# Footer
|
75 |
if "translated_text" in st.session_state:
|
|
|
97 |
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang.split(" (")[0]), "en")
|
98 |
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
|
99 |
if audio and audio.getbuffer().nbytes > 0:
|
100 |
+
st.audio(audio, format="audio/mp3", key=f"audio_{id(st.session_state)}")
|
101 |
|
102 |
if __name__ == "__main__":
|
103 |
main()
|