Krishna086 commited on
Commit
c4196eb
·
verified ·
1 Parent(s): e12cdaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -15,7 +15,7 @@ def main():
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_swap, col_target = st.columns([1, 0.2, 1])
19
  with col_lang:
20
  detected_options = lang_detect.detect_language(st.session_state.get("user_text", "")) if st.session_state.get("user_text", "").strip() and len(st.session_state.get("user_text", "").strip()) >= 10 else [("English", 1.0, "English")]
21
  source_lang = detected_options[0][0] if detected_options else "English"
@@ -25,10 +25,6 @@ def main():
25
  "Arabic": "العربية", "Russian": "Русский", "Japanese": "日本語"
26
  }
27
  source_lang_display = st.selectbox("Source Language (Auto-Detected)", [native_lang_map.get(source_lang, source_lang)] + list(native_lang_map.values()), index=0, key="source_lang", label_visibility="collapsed", help="Auto-detected language, override if needed")
28
- with col_swap:
29
- if st.button("↔", key="swap_btn"):
30
- st.session_state.target_lang = source_lang_display
31
- st.session_state.source_lang = st.session_state.get("target_lang_display", "हिन्दी")
32
  with col_target:
33
  target_options = list(native_lang_map.values())
34
  target_lang_display = st.selectbox("Target Language", target_options, index=target_options.index("हिन्दी") if "हिन्दी" in target_options else 0, key="target_lang", label_visibility="collapsed")
@@ -45,15 +41,18 @@ def main():
45
  user_text = doc_file.read().decode("utf-8").strip() if doc_file else st.session_state.get("user_text", "")
46
  char_count = len(user_text)
47
  st.markdown(f"<small style='color: grey;'>Characters: {char_count}/1000</small>", unsafe_allow_html=True)
48
- # Microphone (Optional, placeholder)
49
- st.button("🎤", key="mic_btn", help="Speech-to-text (not implemented)", disabled=True)
50
 
51
  if user_text and (st.session_state.get("last_text", "") != user_text or st.button("Translate", key="translate_btn")):
52
  st.session_state.last_text = user_text
53
  with st.spinner("Translating..."):
54
  start_time = time.time()
55
  try:
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:
@@ -65,27 +64,31 @@ def main():
65
  with col_output:
66
  st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
67
  if "translated_text" in st.session_state and st.session_state.translated_text:
 
68
  line_count = max(len(st.session_state.translated_text.splitlines()), len(user_text.splitlines()))
69
  output_height = max(200, line_count * 20 + 50)
70
  st.text_area("Translation:", value=st.session_state.translated_text, height=output_height, key="output_area")
71
  st.write(f"Translation time: {st.session_state.translation_time:.2f} seconds")
72
- col_text, col_audio = st.columns([0.5, 0.5])
73
- with col_text:
74
  if st.button("📋", key="copy_btn", help="Copy to clipboard"):
75
- st.clipboard(st.session_state.translated_text)
76
- st.success("Copied to clipboard!")
 
 
 
77
  with col_audio:
78
  output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, label_visibility="collapsed", key="output_option")
79
  if output_option == "Audio":
80
  audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
81
  if audio and audio.getbuffer().nbytes > 0:
82
- st.audio(audio, format="audio/mp3")
83
  st.success("Audio playing.")
84
  else:
85
  st.error("Audio generation failed. Retrying with English...")
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")
89
  st.success("Fallback audio in English playing.")
90
  else:
91
  st.error("Audio generation failed. Try again later.")
 
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])
19
  with col_lang:
20
  detected_options = lang_detect.detect_language(st.session_state.get("user_text", "")) if st.session_state.get("user_text", "").strip() and len(st.session_state.get("user_text", "").strip()) >= 10 else [("English", 1.0, "English")]
21
  source_lang = detected_options[0][0] if detected_options else "English"
 
25
  "Arabic": "العربية", "Russian": "Русский", "Japanese": "日本語"
26
  }
27
  source_lang_display = st.selectbox("Source Language (Auto-Detected)", [native_lang_map.get(source_lang, source_lang)] + list(native_lang_map.values()), index=0, key="source_lang", label_visibility="collapsed", help="Auto-detected language, override if needed")
 
 
 
 
28
  with col_target:
29
  target_options = list(native_lang_map.values())
30
  target_lang_display = st.selectbox("Target Language", target_options, index=target_options.index("हिन्दी") if "हिन्दी" in target_options else 0, key="target_lang", label_visibility="collapsed")
 
41
  user_text = doc_file.read().decode("utf-8").strip() if doc_file else st.session_state.get("user_text", "")
42
  char_count = len(user_text)
43
  st.markdown(f"<small style='color: grey;'>Characters: {char_count}/1000</small>", unsafe_allow_html=True)
 
 
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
51
+ if source_lang != "English" and target_lang != "English":
52
+ temp_translation = translation.translate(user_text, source_lang, "English")
53
+ translated_text = translation.translate(temp_translation, "English", target_lang)
54
+ else:
55
+ translated_text = translation.translate(user_text, source_lang, target_lang)
56
  st.session_state.translated_text = translated_text
57
  st.session_state.translation_time = time.time() - start_time
58
  except Exception as e:
 
64
  with col_output:
65
  st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
66
  if "translated_text" in st.session_state and st.session_state.translated_text:
67
+ # Match input height dynamically
68
  line_count = max(len(st.session_state.translated_text.splitlines()), len(user_text.splitlines()))
69
  output_height = max(200, line_count * 20 + 50)
70
  st.text_area("Translation:", value=st.session_state.translated_text, height=output_height, key="output_area")
71
  st.write(f"Translation time: {st.session_state.translation_time:.2f} seconds")
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
+ try:
76
+ st.experimental_set_query_params(text=st.session_state.translated_text)
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)
92
  st.success("Fallback audio in English playing.")
93
  else:
94
  st.error("Audio generation failed. Try again later.")