Update app.py
Browse files
app.py
CHANGED
@@ -49,14 +49,14 @@ def main():
|
|
49 |
st.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
|
50 |
|
51 |
# Language and Input/Output Layout
|
52 |
-
col1, col2 = st.columns(
|
53 |
with col1:
|
54 |
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")]
|
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 Language", 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")
|
59 |
-
input_text = st.text_area("Input Text", height=
|
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")
|
@@ -68,12 +68,19 @@ def main():
|
|
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 Language", 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")
|
70 |
if "translated_text" in st.session_state:
|
71 |
-
st.text_area("Output Text", value=st.session_state.translated_text, height=
|
72 |
if st.button("🔊", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False):
|
73 |
pass
|
74 |
# Footer
|
75 |
if "translated_text" in st.session_state:
|
76 |
-
st.markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
except Exception as e:
|
79 |
st.error(f"App error: {e}")
|
@@ -97,7 +104,7 @@ def play_audio(audio_processor):
|
|
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"
|
101 |
|
102 |
if __name__ == "__main__":
|
103 |
main()
|
|
|
49 |
st.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
|
50 |
|
51 |
# Language and Input/Output Layout
|
52 |
+
col1, col2 = st.columns([1, 1]) # Adjust column widths for larger boxes
|
53 |
with col1:
|
54 |
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")]
|
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 Language", 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")
|
59 |
+
input_text = st.text_area("Input Text", height=400, key="input_text", placeholder="Enter text here", 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")
|
|
|
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 Language", 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")
|
70 |
if "translated_text" in st.session_state:
|
71 |
+
st.text_area("Output Text", value=st.session_state.translated_text, height=400, 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):
|
73 |
pass
|
74 |
# Footer
|
75 |
if "translated_text" in st.session_state:
|
76 |
+
st.markdown("""
|
77 |
+
<p style="font-size: small; color: grey; text-align: center;">
|
78 |
+
Developed By: Krishna Prakash
|
79 |
+
<a href="https://www.linkedin.com/in/krishnaprakash-profile/" target="_blank">
|
80 |
+
<img src="https://img.icons8.com/ios-filled/30/0077b5/linkedin.png" alt="LinkedIn" style="vertical-align: middle; margin: 0 5px;"/>
|
81 |
+
</a>
|
82 |
+
</p>
|
83 |
+
""", unsafe_allow_html=True)
|
84 |
|
85 |
except Exception as e:
|
86 |
st.error(f"App error: {e}")
|
|
|
104 |
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang.split(" (")[0]), "en")
|
105 |
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
|
106 |
if audio and audio.getbuffer().nbytes > 0:
|
107 |
+
st.audio(audio, format="audio/mp3")
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
main()
|