Krishna086 commited on
Commit
9878bca
·
verified ·
1 Parent(s): 700ac54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -25
app.py CHANGED
@@ -19,46 +19,45 @@ def main():
19
 
20
  # Header
21
  st.markdown("<h1 style='text-align: center; color: #4285F4;'>Multilingual Translator</h1>", unsafe_allow_html=True)
 
22
 
23
  # Language and Input/Output Layout
24
  col1, col2 = st.columns(2)
25
  with col1:
26
- detected_options = lang_detect.detect_language(st.session_state.get("input_text", "")) if st.session_state.get("input_text", "").strip() and len(st.session_state.get("input_text", "").strip()) >= 10 else [("Hindi", 1.0, "हिन्दी")]
27
- source_lang = detected_options[0][0] if detected_options else "Hindi"
28
- source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "hi")
29
- st.selectbox("Source", options=[LANGUAGES[source_lang_code][0]] + [v[0] for v in LANGUAGES.values()], index=0, key="source_lang")
30
- input_type = st.radio("Input", ["Text", "File"], horizontal=True, key="input_type", label_visibility="hidden")
 
31
  if input_type == "Text":
32
- input_text = st.text_area("Input", height=200, key="input_text", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden")
33
  else:
34
- input_text = st.file_uploader("Input", type=["txt", "docx", "pdf"], key="file_input", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden")
35
- if input_text:
36
- st.session_state.input_text = input_text.read().decode("utf-8").strip()
37
  st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,))
38
  with col2:
39
  st.selectbox("Target", options=[v[0] for v in LANGUAGES.values()], index=list(LANGUAGES.values()).index(LANGUAGES["en"]), key="target_lang")
40
  if "translated_text" in st.session_state:
41
- st.text_area("Output", value=st.session_state.translated_text, height=200, key="output_text", disabled=True, label_visibility="hidden")
42
- if st.button("🔊", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio"):
43
- pass # Handled by play_audio
44
 
45
  except Exception as e:
46
  st.error(f"App error: {e}")
47
  st.stop()
48
 
49
  def trigger_translation(translation, lang_detect, audio_processor):
50
- text = st.session_state.input_text.strip()
51
  if text:
52
- source_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.source_lang), "hi")
53
  target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "en")
54
- if source_lang != "en" and target_lang != "en":
55
- temp = translation.translate(text, source_lang, "en")
56
- if temp and len(temp.split()) >= 2:
57
- st.session_state.translated_text = translation.translate(temp, "en", target_lang)
58
- else:
59
- st.session_state.translated_text = text
60
  else:
61
- st.session_state.translated_text = translation.translate(text, source_lang, target_lang) or text
 
62
 
63
  def play_audio(audio_processor):
64
  if "translated_text" in st.session_state and st.session_state.translated_text:
@@ -66,10 +65,6 @@ def play_audio(audio_processor):
66
  audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
67
  if audio and audio.getbuffer().nbytes > 0:
68
  st.audio(audio, format="audio/mp3")
69
- else:
70
- audio_fallback = audio_processor.text_to_speech(st.session_state.translated_text, "en")
71
- if audio_fallback and audio_fallback.getbuffer().nbytes > 0:
72
- st.audio(audio_fallback, format="audio/mp3")
73
 
74
  if __name__ == "__main__":
75
  main()
 
19
 
20
  # Header
21
  st.markdown("<h1 style='text-align: center; color: #4285F4;'>Multilingual Translator</h1>", unsafe_allow_html=True)
22
+ st.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
23
 
24
  # Language and Input/Output Layout
25
  col1, col2 = st.columns(2)
26
  with col1:
27
+ 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")]
28
+ source_lang = detected_options[0][0] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
29
+ source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "hi") if source_lang != "Auto-detect" else "auto"
30
+ source_options = ["Auto-detect"] + [v[0] for v in LANGUAGES.values()]
31
+ st.selectbox("Source", options=source_options, index=0 if source_lang == "Auto-detect" else source_options.index(source_lang), key="source_lang")
32
+ input_type = st.radio("", ["Text", "File"], horizontal=True, label_visibility="hidden")
33
  if input_type == "Text":
34
+ st.text_area("", height=200, key="input_text", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden")
35
  else:
36
+ uploaded_file = st.file_uploader("", type=["txt", "docx", "pdf"], key="file_input", on_change=trigger_translation, args=(translation, lang_detect, audio_processor,), label_visibility="hidden")
37
+ if uploaded_file:
38
+ st.session_state.input_text = uploaded_file.read().decode("utf-8").strip()
39
  st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,))
40
  with col2:
41
  st.selectbox("Target", options=[v[0] for v in LANGUAGES.values()], index=list(LANGUAGES.values()).index(LANGUAGES["en"]), key="target_lang")
42
  if "translated_text" in st.session_state:
43
+ st.text_area("", value=st.session_state.translated_text, height=200, key="output_text", disabled=True, label_visibility="hidden")
44
+ st.button("🔊", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False)
 
45
 
46
  except Exception as e:
47
  st.error(f"App error: {e}")
48
  st.stop()
49
 
50
  def trigger_translation(translation, lang_detect, audio_processor):
51
+ text = st.session_state.get("input_text", "").strip()
52
  if text:
53
+ source_lang = st.session_state.source_lang
54
  target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang), "en")
55
+ if source_lang == "Auto-detect":
56
+ detected_options = lang_detect.detect_language(text)
57
+ source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "hi")
 
 
 
58
  else:
59
+ source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "hi")
60
+ st.session_state.translated_text = translation.translate(text, source_lang_code, target_lang) or text
61
 
62
  def play_audio(audio_processor):
63
  if "translated_text" in st.session_state and st.session_state.translated_text:
 
65
  audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
66
  if audio and audio.getbuffer().nbytes > 0:
67
  st.audio(audio, format="audio/mp3")
 
 
 
 
68
 
69
  if __name__ == "__main__":
70
  main()