Krishna086 commited on
Commit
1761548
·
verified ·
1 Parent(s): 122790b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -15,12 +15,13 @@ def main():
15
 
16
  col1, col2 = st.columns([1, 1])
17
  with col1:
18
- input_type = st.radio("Input", ["Text", "File Upload"], horizontal=True, key="input_type")
19
- user_text = st.text_area("Enter or paste text", height=150, key="text_input").strip() if input_type == "Text" else ""
 
20
  if input_type == "File Upload":
21
- doc_file = st.file_uploader("Upload TXT File", type=["txt"], key="doc_input", accept_multiple_files=False)
22
  user_text = doc_file.read().decode("utf-8").strip() if doc_file else ""
23
-
24
  if user_text:
25
  start_time = time.time()
26
  detected_options = lang_detect.detect_language(user_text) if len(user_text) >= 10 else [("English", 1.0, "English")]
@@ -47,15 +48,16 @@ def main():
47
  st.session_state.translated_text = translated_text
48
  st.session_state.translation_time = time.time() - start_time
49
  except Exception as e:
50
- st.error(f"Translation failed: {str(e)}. Using input as fallback.")
51
- st.session_state.translated_text = user_text
52
  st.session_state.translation_time = time.time() - start_time
 
53
 
54
  with col2:
 
55
  if "translated_text" in st.session_state and st.session_state.translated_text:
56
- st.text_area("Translation:", value=st.session_state.translated_text, height=len(st.session_state.translated_text.splitlines()) * 20 + 50, key="output_area")
57
  st.write(f"Translation time: {st.session_state.translation_time:.2f} seconds")
58
- output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, key="output_option")
59
  if output_option == "Audio":
60
  audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
61
  if audio and audio.getbuffer().nbytes > 0:
@@ -68,7 +70,7 @@ def main():
68
  st.audio(audio_fallback, format="audio/mp3")
69
  st.success("Fallback audio in English playing.")
70
  else:
71
- st.error("Audio generation failed. Try again later or use English/French.")
72
 
73
  st.markdown("""
74
  <p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>
 
15
 
16
  col1, col2 = st.columns([1, 1])
17
  with col1:
18
+ st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True) # Spacing
19
+ input_type = st.radio("Input", ["Text", "File Upload"], horizontal=True, label_visibility="collapsed", key="input_type")
20
+ user_text = st.text_area("Enter or paste text", height=200, key="text_input").strip() if input_type == "Text" else ""
21
  if input_type == "File Upload":
22
+ doc_file = st.file_uploader("Upload TXT File", type=["txt"], key="doc_input", accept_multiple_files=False, label_visibility="collapsed")
23
  user_text = doc_file.read().decode("utf-8").strip() if doc_file else ""
24
+
25
  if user_text:
26
  start_time = time.time()
27
  detected_options = lang_detect.detect_language(user_text) if len(user_text) >= 10 else [("English", 1.0, "English")]
 
48
  st.session_state.translated_text = translated_text
49
  st.session_state.translation_time = time.time() - start_time
50
  except Exception as e:
51
+ st.session_state.translated_text = user_text # Fallback to input
 
52
  st.session_state.translation_time = time.time() - start_time
53
+ st.warning(f"Translation error: {str(e)}. Using input as fallback.")
54
 
55
  with col2:
56
+ st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True) # Matching spacing
57
  if "translated_text" in st.session_state and st.session_state.translated_text:
58
+ st.text_area("Translation:", value=st.session_state.translated_text, height=200, key="output_area")
59
  st.write(f"Translation time: {st.session_state.translation_time:.2f} seconds")
60
+ output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, label_visibility="collapsed", key="output_option")
61
  if output_option == "Audio":
62
  audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
63
  if audio and audio.getbuffer().nbytes > 0:
 
70
  st.audio(audio_fallback, format="audio/mp3")
71
  st.success("Fallback audio in English playing.")
72
  else:
73
+ st.error("Audio generation failed. Try again later.")
74
 
75
  st.markdown("""
76
  <p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>