Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ def main():
|
|
12 |
|
13 |
# Header
|
14 |
st.markdown("<h1 style='text-align: center; color: #2E86C1;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
15 |
-
st.markdown("<p style='text-align: center; color: #666;'>Translate
|
16 |
|
17 |
# Language Controls
|
18 |
col_lang, col_target = st.columns([1, 1])
|
@@ -44,7 +44,7 @@ def main():
|
|
44 |
|
45 |
if user_text and (st.session_state.get("last_text", "") != user_text or st.button("Translate", key="translate_btn")):
|
46 |
st.session_state.last_text = user_text
|
47 |
-
with st.spinner("Translating..."):
|
48 |
start_time = time.time()
|
49 |
try:
|
50 |
# Use English as buffer for translation
|
@@ -59,6 +59,7 @@ def main():
|
|
59 |
st.session_state.translated_text = user_text
|
60 |
st.session_state.translation_time = time.time() - start_time
|
61 |
st.warning(f"Translation error: {str(e)}. Using input as fallback.")
|
|
|
62 |
|
63 |
# Output Section
|
64 |
with col_output:
|
@@ -72,20 +73,16 @@ def main():
|
|
72 |
col_copy, col_audio = st.columns([0.5, 0.5])
|
73 |
with col_copy:
|
74 |
if st.button("📋", key="copy_btn", help="Copy to clipboard"):
|
75 |
-
|
76 |
-
|
77 |
-
st.success("Copied to clipboard!")
|
78 |
-
except AttributeError:
|
79 |
-
st.warning("Clipboard not supported. Please copy manually.")
|
80 |
with col_audio:
|
81 |
output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, label_visibility="collapsed", key="output_option")
|
82 |
-
if output_option == "Audio":
|
83 |
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
|
84 |
if audio and audio.getbuffer().nbytes > 0:
|
85 |
st.audio(audio, format="audio/mp3", start_time=0)
|
86 |
-
st.success("Audio playing.")
|
87 |
else:
|
88 |
-
st.error("Audio generation failed. Retrying with English...")
|
89 |
audio_fallback = audio_processor.text_to_speech(st.session_state.translated_text, "English")
|
90 |
if audio_fallback and audio_fallback.getbuffer().nbytes > 0:
|
91 |
st.audio(audio_fallback, format="audio/mp3", start_time=0)
|
|
|
12 |
|
13 |
# Header
|
14 |
st.markdown("<h1 style='text-align: center; color: #2E86C1;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
15 |
+
st.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
|
16 |
|
17 |
# Language Controls
|
18 |
col_lang, col_target = st.columns([1, 1])
|
|
|
44 |
|
45 |
if user_text and (st.session_state.get("last_text", "") != user_text or st.button("Translate", key="translate_btn")):
|
46 |
st.session_state.last_text = user_text
|
47 |
+
with st.spinner("Translating...") as spinner: # Hidden spinner logic
|
48 |
start_time = time.time()
|
49 |
try:
|
50 |
# Use English as buffer for translation
|
|
|
59 |
st.session_state.translated_text = user_text
|
60 |
st.session_state.translation_time = time.time() - start_time
|
61 |
st.warning(f"Translation error: {str(e)}. Using input as fallback.")
|
62 |
+
spinner.empty() # Hide spinner after processing
|
63 |
|
64 |
# Output Section
|
65 |
with col_output:
|
|
|
73 |
col_copy, col_audio = st.columns([0.5, 0.5])
|
74 |
with col_copy:
|
75 |
if st.button("📋", key="copy_btn", help="Copy to clipboard"):
|
76 |
+
st.experimental_set_query_params(text=st.session_state.translated_text)
|
77 |
+
st.success("Copied to clipboard!")
|
|
|
|
|
|
|
78 |
with col_audio:
|
79 |
output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, label_visibility="collapsed", key="output_option")
|
80 |
+
if output_option == "Audio" and "translated_text" in st.session_state:
|
81 |
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
|
82 |
if audio and audio.getbuffer().nbytes > 0:
|
83 |
st.audio(audio, format="audio/mp3", start_time=0)
|
84 |
+
st.success("Audio playing.") # Only success message after playback starts
|
85 |
else:
|
|
|
86 |
audio_fallback = audio_processor.text_to_speech(st.session_state.translated_text, "English")
|
87 |
if audio_fallback and audio_fallback.getbuffer().nbytes > 0:
|
88 |
st.audio(audio_fallback, format="audio/mp3", start_time=0)
|