Update app.py
Browse files
app.py
CHANGED
@@ -44,30 +44,33 @@ 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 |
-
spinner = st.empty()
|
48 |
-
spinner.info("Translating...")
|
49 |
start_time = time.time()
|
50 |
try:
|
51 |
-
# Use English as buffer for translation
|
52 |
if source_lang != "English" and target_lang != "English":
|
53 |
temp_translation = translation.translate(user_text, source_lang, "English")
|
|
|
|
|
54 |
translated_text = translation.translate(temp_translation, "English", target_lang)
|
55 |
else:
|
56 |
translated_text = translation.translate(user_text, source_lang, target_lang)
|
|
|
|
|
57 |
st.session_state.translated_text = translated_text
|
58 |
st.session_state.translation_time = time.time() - start_time
|
59 |
except Exception as e:
|
60 |
st.session_state.translated_text = user_text
|
61 |
st.session_state.translation_time = time.time() - start_time
|
62 |
-
st.warning(f"Translation
|
63 |
finally:
|
64 |
-
spinner.empty()
|
65 |
|
66 |
# Output Section
|
67 |
with col_output:
|
68 |
st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
|
69 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
70 |
-
# Match input height dynamically
|
71 |
line_count = max(len(st.session_state.translated_text.splitlines()), len(user_text.splitlines()))
|
72 |
output_height = max(200, line_count * 20 + 50)
|
73 |
st.text_area("Translation:", value=st.session_state.translated_text, height=output_height, key="output_area")
|
@@ -75,7 +78,7 @@ def main():
|
|
75 |
col_copy, col_audio = st.columns([0.5, 0.5])
|
76 |
with col_copy:
|
77 |
if st.button("📋", key="copy_btn", help="Copy to clipboard"):
|
78 |
-
st.
|
79 |
st.success("Copied to clipboard!")
|
80 |
with col_audio:
|
81 |
output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, label_visibility="collapsed", key="output_option")
|
|
|
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 |
+
spinner = st.empty()
|
48 |
+
spinner.info("Translating...")
|
49 |
start_time = time.time()
|
50 |
try:
|
51 |
+
# Use English as buffer for translation with validation
|
52 |
if source_lang != "English" and target_lang != "English":
|
53 |
temp_translation = translation.translate(user_text, source_lang, "English")
|
54 |
+
if not temp_translation or len(temp_translation.split()) < 2: # Validate intermediate result
|
55 |
+
raise ValueError("Intermediate translation failed")
|
56 |
translated_text = translation.translate(temp_translation, "English", target_lang)
|
57 |
else:
|
58 |
translated_text = translation.translate(user_text, source_lang, target_lang)
|
59 |
+
if not translated_text or len(translated_text.split()) < 2: # Validate final result
|
60 |
+
raise ValueError("Final translation failed")
|
61 |
st.session_state.translated_text = translated_text
|
62 |
st.session_state.translation_time = time.time() - start_time
|
63 |
except Exception as e:
|
64 |
st.session_state.translated_text = user_text
|
65 |
st.session_state.translation_time = time.time() - start_time
|
66 |
+
st.warning(f"Translation issue: {str(e)}. Using input as fallback.")
|
67 |
finally:
|
68 |
+
spinner.empty()
|
69 |
|
70 |
# Output Section
|
71 |
with col_output:
|
72 |
st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
|
73 |
if "translated_text" in st.session_state and st.session_state.translated_text:
|
|
|
74 |
line_count = max(len(st.session_state.translated_text.splitlines()), len(user_text.splitlines()))
|
75 |
output_height = max(200, line_count * 20 + 50)
|
76 |
st.text_area("Translation:", value=st.session_state.translated_text, height=output_height, key="output_area")
|
|
|
78 |
col_copy, col_audio = st.columns([0.5, 0.5])
|
79 |
with col_copy:
|
80 |
if st.button("📋", key="copy_btn", help="Copy to clipboard"):
|
81 |
+
st.clipboard(st.session_state.translated_text)
|
82 |
st.success("Copied to clipboard!")
|
83 |
with col_audio:
|
84 |
output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, label_visibility="collapsed", key="output_option")
|