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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -47
app.py CHANGED
@@ -3,74 +3,94 @@ import importlib
3
  from io import BytesIO
4
  import time
5
 
6
- st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="centered")
7
 
8
  def main():
9
  translation = importlib.import_module("translation")
10
  lang_detect = importlib.import_module("lang_detect")
11
  audio_processor = importlib.import_module("audio_processor")
12
 
 
13
  st.markdown("<h1 style='text-align: center; color: #2E86C1;'>Multilingual Translator</h1>", unsafe_allow_html=True)
14
  st.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
15
 
16
- col1, col2 = st.columns([1, 1])
17
- with col1:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
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
- # Auto-detect source language
27
- detected_options = lang_detect.detect_language(user_text) if len(user_text) >= 10 else [("English", 1.0, "English")]
28
- source_lang = detected_options[0][0] if detected_options else "English"
29
-
30
- native_lang_map = {
31
- "English": "English", "French": "Français", "Spanish": "Español",
32
- "German": "Deutsch", "Hindi": "हिन्दी", "Chinese": "中文",
33
- "Arabic": "العربية", "Russian": "Русский", "Japanese": "日本語"
34
- }
35
- target_options = list(native_lang_map.values())
36
- target_lang_display = st.selectbox("To", target_options, index=target_options.index("हिन्दी") if "हिन्दी" in target_options else 0, key="target_lang")
37
- target_lang = next((k for k, v in native_lang_map.items() if v == target_lang_display), "Hindi")
38
 
39
- # Trigger translation only on button press, no Ctrl+Enter
40
- if st.button("Translate", key="translate_btn"):
41
- with st.spinner("Translating..."):
42
- start_time = time.time()
43
- try:
44
- translated_text = translation.translate(user_text, source_lang, target_lang)
45
- st.session_state.translated_text = translated_text
46
- st.session_state.translation_time = time.time() - start_time
47
- except Exception as e:
48
- st.session_state.translated_text = user_text
49
- st.session_state.translation_time = time.time() - start_time
50
- st.warning(f"Translation error: {str(e)}. Using input as fallback.")
51
 
52
- with col2:
 
53
  st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
54
  if "translated_text" in st.session_state and st.session_state.translated_text:
55
  line_count = max(len(st.session_state.translated_text.splitlines()), len(user_text.splitlines()))
56
  output_height = max(200, line_count * 20 + 50)
57
- st.text_area("Translation:", value=st.session_state.translated_text, height=output_height, key="output_area", disabled=True) # Disabled to avoid editing
58
  st.write(f"Translation time: {st.session_state.translation_time:.2f} seconds")
59
- output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, label_visibility="collapsed", key="output_option")
60
- if output_option == "Audio":
61
- audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
62
- if audio and audio.getbuffer().nbytes > 0:
63
- st.audio(audio, format="audio/mp3")
64
- st.success("Audio playing.")
65
- else:
66
- st.error("Audio generation failed. Retrying with English...")
67
- audio_fallback = audio_processor.text_to_speech(st.session_state.translated_text, "English")
68
- if audio_fallback and audio_fallback.getbuffer().nbytes > 0:
69
- st.audio(audio_fallback, format="audio/mp3")
70
- st.success("Fallback audio in English playing.")
71
  else:
72
- st.error("Audio generation failed. Try again later.")
 
 
 
 
 
 
73
 
 
74
  st.markdown("""
75
  <p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>
76
  Developed by: Krishna Prakash <a href='https://www.linkedin.com/in/krishnaprakash-profile/' target='_blank'>
 
3
  from io import BytesIO
4
  import time
5
 
6
+ st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="wide")
7
 
8
  def main():
9
  translation = importlib.import_module("translation")
10
  lang_detect = importlib.import_module("lang_detect")
11
  audio_processor = importlib.import_module("audio_processor")
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_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"
22
+ native_lang_map = {
23
+ "English": "English", "French": "Français", "Spanish": "Español",
24
+ "German": "Deutsch", "Hindi": "हिन्दी", "Chinese": "中文",
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")
35
+ target_lang = next((k for k, v in native_lang_map.items() if v == target_lang_display), "Hindi")
36
+
37
+ # Input Section
38
+ col_input, col_output = st.columns([1, 1])
39
+ with col_input:
40
  st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
41
  input_type = st.radio("Input", ["Text", "File Upload"], horizontal=True, label_visibility="collapsed", key="input_type")
42
+ user_text = st.text_area("Enter or paste text", height=200, key="user_text").strip() if input_type == "Text" else ""
43
  if input_type == "File Upload":
44
+ doc_file = st.file_uploader("Upload File", type=["txt", "docx", "pdf"], key="doc_input", accept_multiple_files=False, label_visibility="collapsed", help="Drag and drop or browse files")
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:
60
+ st.session_state.translated_text = user_text
61
+ st.session_state.translation_time = time.time() - start_time
62
+ st.warning(f"Translation error: {str(e)}. Using input as fallback.")
63
 
64
+ # Output Section
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.")
92
 
93
+ # Footer
94
  st.markdown("""
95
  <p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>
96
  Developed by: Krishna Prakash <a href='https://www.linkedin.com/in/krishnaprakash-profile/' target='_blank'>