Krishna086 commited on
Commit
0787c83
Β·
verified Β·
1 Parent(s): b5c7107

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -77
app.py CHANGED
@@ -8,42 +8,40 @@ import time
8
  # Configure Streamlit page settings
9
  st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="wide")
10
 
11
- # Import language definitions from translation module
12
  try:
13
  from translation import LANGUAGES
14
  except ImportError as e:
15
  st.error(f"Failed to import translation module: {e}")
16
  st.stop()
17
 
18
- # Extract text from uploaded files (PDF, DOCX, TXT)
19
  def extract_text_from_file(uploaded_file):
20
  try:
21
  if uploaded_file.type == "application/pdf":
22
  pdf_reader = PdfReader(uploaded_file)
23
  text = "".join(page.extract_text() or "" for page in pdf_reader.pages)
24
- return text.encode().decode("utf-8", errors="ignore").strip()
25
  elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
26
  doc = docx.Document(uploaded_file)
27
  text = "\n".join(para.text for para in doc.paragraphs)
28
- return text.encode().decode("utf-8", errors="ignore").strip()
29
  elif uploaded_file.type == "text/plain":
30
- return uploaded_file.read().decode("utf-8", errors="ignore").strip()
31
  return ""
32
  except Exception as e:
33
- st.error(f"Error extracting text from file: {e}")
34
  return ""
35
 
36
  # Update input text when a file is uploaded
37
  def on_file_upload():
38
- try:
39
- uploaded_file = st.session_state.get("file_input")
40
- if uploaded_file and uploaded_file.size < 1024 * 1024:
41
  st.session_state.user_input_text = extract_text_from_file(uploaded_file)
42
  st.success(f"File '{uploaded_file.name}' uploaded successfully!")
43
- elif uploaded_file and uploaded_file.size >= 1024 * 1024:
44
  st.error("File size must be less than 1 MB")
45
- except Exception as e:
46
- st.error(f"Error processing file upload: {e}")
47
 
48
  # Main application function
49
  def main():
@@ -53,41 +51,15 @@ def main():
53
  language_detector = importlib.import_module("lang_detect")
54
  audio_processor_module = importlib.import_module("audio_processor")
55
 
56
- # Display application header
57
  st.markdown("<h1 style='text-align: center; color: #4285F4;'>Multilingual Translator</h1>", unsafe_allow_html=True)
58
  st.markdown("<p style='text-align: center; color: #666;'>Effortless Multilingual Translation</p>", unsafe_allow_html=True)
59
 
60
- # Apply custom CSS for UI enhancements
61
- st.markdown(
62
- """
63
- <style>
64
- .stFileUploader > div > div > div[role="button"] { display: none !important; }
65
- .stFileUploader label { display: none !important; }
66
- .stFileUploader [data-testid="stFileUploaderDropzone"] {
67
- border: 2px dashed #ccc !important; padding: 10px !important;
68
- text-align: center !important; font-size: 0 !important;
69
- }
70
- .stFileUploader [data-testid="stFileUploaderDropzone"]::after {
71
- content: 'Drag and drop TXT, DOCX, or PDF here or ' !important;
72
- font-size: 1em !important; color: #666 !important;
73
- }
74
- .stFileUploader [data-testid="stFileUploaderButton"] {
75
- font-size: 1em !important; color: #1E90FF !important;
76
- background: none !important; border: none !important; padding: 0 !important;
77
- }
78
- .stFileUploader [data-testid="stFileUploader"] { background: none !important; }
79
- .stRadio > div { margin-bottom: 0 !important; }
80
- .stTextArea { margin-top: 0 !important; }
81
- </style>
82
- """,
83
- unsafe_allow_html=True
84
- )
85
-
86
  # Create symmetric layout with two columns
87
  left_col, right_col = st.columns([1, 1])
88
  with left_col:
89
- # Detect language of input text if available
90
- detected_options = language_detector.detect_language(st.session_state.get("user_input_text", "").strip()) if st.session_state.get("user_input_text", "").strip() else [("Auto-detect", 1.0, "Auto-detect")]
91
  source_language = detected_options[0][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
92
  source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_language), "en") if source_language != "Auto-detect" else "auto"
93
  source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
@@ -97,17 +69,18 @@ def main():
97
  if input_type == "File":
98
  st.file_uploader("Upload File", type=["txt", "docx", "pdf"], key="file_input", on_change=on_file_upload, label_visibility="hidden")
99
  st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation_module, language_detector, audio_processor_module))
 
100
  with right_col:
101
- # Set target language options based on source language
102
  source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
103
  target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display]
104
  st.selectbox("Target Language", options=target_options, index=target_options.index(f"{LANGUAGES['en'][0]} ({LANGUAGES['en'][1]})") if "English" not in source_lang_display else 0, key="target_lang")
105
  if "translated_text" in st.session_state:
106
  st.text_area("Output Text", value=st.session_state.translated_text, height=200, key="output_text", disabled=True, label_visibility="hidden")
107
- # Trigger audio playback on button click
108
  if st.button("πŸ”Š", key="audio_btn", on_click=play_audio, args=(audio_processor_module,), help="Play audio", use_container_width=False):
109
  pass
110
- # Display application footer
 
111
  st.markdown("""
112
  <p style="font-size: small; color: grey; text-align: center;">
113
  Developed By: Krishna Prakash
@@ -116,48 +89,54 @@ def main():
116
  </a>
117
  </p>
118
  """, unsafe_allow_html=True)
 
119
  except Exception as e:
120
  st.error(f"Application error occurred: {e}")
 
121
 
122
  # Trigger translation process with timeout handling
123
  def trigger_translation(translation_module, language_detector, audio_processor_module):
124
- try:
125
- user_input_text = st.session_state.get("user_input_text", "").strip()
126
- if user_input_text:
127
- with st.spinner("Translating..."):
128
- start_time = time.time()
129
- source_lang = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
130
- target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang
131
- if source_lang == "Auto-detect":
132
- detected_options = language_detector.detect_language(user_input_text)
133
- source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "en")
134
- else:
135
- source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "en")
136
- target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "en")
137
- translated_text = translation_module.translate(user_input_text, source_lang_code, target_lang_code)
138
- if time.time() - start_time > 20: # Check if translation exceeds 20-second timeout
139
- st.error("Translation took too long, reverting to input.")
140
- translated_text = user_input_text
141
- if translated_text and len(translated_text.split()) > 2: # Validate translation result
142
- st.session_state.translated_text = translated_text
143
- else:
144
- st.session_state.translated_text = user_input_text
145
- except Exception as e:
146
- st.error(f"Translation process failed: {e}")
147
- st.session_state.translated_text = user_input_text
 
 
148
 
149
- # Handle audio playback with error checking
150
  def play_audio(audio_processor_module):
 
 
 
151
  try:
152
- if "translated_text" in st.session_state and st.session_state.translated_text:
153
- target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang.split(" (")[0]), "en")
154
- audio_data = audio_processor_module.text_to_speech(st.session_state.translated_text, target_lang)
155
- if audio_data and audio_data.getbuffer().nbytes > 0:
156
- st.audio(audio_data, format="audio/mp3")
157
- else:
158
- st.error("Failed to generate audio. Please try again.")
159
  except Exception as e:
160
- st.error(f"Audio playback failed: {e}")
161
 
162
  if __name__ == "__main__":
163
  main()
 
8
  # Configure Streamlit page settings
9
  st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="wide")
10
 
11
+ # Import LANGUAGES from translation.py with error handling
12
  try:
13
  from translation import LANGUAGES
14
  except ImportError as e:
15
  st.error(f"Failed to import translation module: {e}")
16
  st.stop()
17
 
18
+ # Extract text from uploaded files (PDF, DOCX, TXT) with error handling
19
  def extract_text_from_file(uploaded_file):
20
  try:
21
  if uploaded_file.type == "application/pdf":
22
  pdf_reader = PdfReader(uploaded_file)
23
  text = "".join(page.extract_text() or "" for page in pdf_reader.pages)
24
+ return text.encode().decode('utf-8', errors='ignore').strip()
25
  elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
26
  doc = docx.Document(uploaded_file)
27
  text = "\n".join(para.text for para in doc.paragraphs)
28
+ return text.encode().decode('utf-8', errors='ignore').strip()
29
  elif uploaded_file.type == "text/plain":
30
+ return uploaded_file.read().decode('utf-8', errors='ignore').strip()
31
  return ""
32
  except Exception as e:
33
+ st.error(f"Error processing file: {e}")
34
  return ""
35
 
36
  # Update input text when a file is uploaded
37
  def on_file_upload():
38
+ uploaded_file = st.session_state.get("file_input")
39
+ if uploaded_file:
40
+ if uploaded_file.size < 1024 * 1024: # Check file size limit (1MB)
41
  st.session_state.user_input_text = extract_text_from_file(uploaded_file)
42
  st.success(f"File '{uploaded_file.name}' uploaded successfully!")
43
+ else:
44
  st.error("File size must be less than 1 MB")
 
 
45
 
46
  # Main application function
47
  def main():
 
51
  language_detector = importlib.import_module("lang_detect")
52
  audio_processor_module = importlib.import_module("audio_processor")
53
 
54
+ # Display header
55
  st.markdown("<h1 style='text-align: center; color: #4285F4;'>Multilingual Translator</h1>", unsafe_allow_html=True)
56
  st.markdown("<p style='text-align: center; color: #666;'>Effortless Multilingual Translation</p>", unsafe_allow_html=True)
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  # Create symmetric layout with two columns
59
  left_col, right_col = st.columns([1, 1])
60
  with left_col:
61
+ # Detect language of input text
62
+ detected_options = language_detector.detect_language(st.session_state.get("user_input_text", "")) if st.session_state.get("user_input_text", "").strip() else [("Auto-detect", 1.0, "Auto-detect")]
63
  source_language = detected_options[0][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect"
64
  source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_language), "en") if source_language != "Auto-detect" else "auto"
65
  source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()]
 
69
  if input_type == "File":
70
  st.file_uploader("Upload File", type=["txt", "docx", "pdf"], key="file_input", on_change=on_file_upload, label_visibility="hidden")
71
  st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation_module, language_detector, audio_processor_module))
72
+
73
  with right_col:
74
+ # Set target language options
75
  source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
76
  target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display]
77
  st.selectbox("Target Language", options=target_options, index=target_options.index(f"{LANGUAGES['en'][0]} ({LANGUAGES['en'][1]})") if "English" not in source_lang_display else 0, key="target_lang")
78
  if "translated_text" in st.session_state:
79
  st.text_area("Output Text", value=st.session_state.translated_text, height=200, key="output_text", disabled=True, label_visibility="hidden")
 
80
  if st.button("πŸ”Š", key="audio_btn", on_click=play_audio, args=(audio_processor_module,), help="Play audio", use_container_width=False):
81
  pass
82
+
83
+ # Display footer
84
  st.markdown("""
85
  <p style="font-size: small; color: grey; text-align: center;">
86
  Developed By: Krishna Prakash
 
89
  </a>
90
  </p>
91
  """, unsafe_allow_html=True)
92
+
93
  except Exception as e:
94
  st.error(f"Application error occurred: {e}")
95
+ raise # Re-raise exception for further handling if needed
96
 
97
  # Trigger translation process with timeout handling
98
  def trigger_translation(translation_module, language_detector, audio_processor_module):
99
+ user_input_text = st.session_state.get("user_input_text", "").strip()
100
+ if not user_input_text:
101
+ st.error("No input text provided")
102
+ return
103
+ with st.spinner("Translating..."):
104
+ start_time = time.time()
105
+ source_lang = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
106
+ target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang
107
+ if source_lang == "Auto-detect":
108
+ detected_options = language_detector.detect_language(user_input_text)
109
+ source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "en")
110
+ else:
111
+ source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "en")
112
+ target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "en")
113
+ try:
114
+ translated_text = translation_module.translate(user_input_text, source_lang_code, target_lang_code)
115
+ if time.time() - start_time > 20: # Timeout after 20 seconds
116
+ st.error("Translation took too long, reverting to input.")
117
+ translated_text = user_input_text
118
+ if translated_text and len(translated_text.split()) > 2:
119
+ st.session_state.translated_text = translated_text
120
+ else:
121
+ st.session_state.translated_text = user_input_text
122
+ except Exception as e:
123
+ st.error(f"Translation failed: {e}")
124
+ st.session_state.translated_text = user_input_text
125
 
126
+ # Handle audio playback with error handling
127
  def play_audio(audio_processor_module):
128
+ if "translated_text" not in st.session_state or not st.session_state.translated_text:
129
+ st.error("No translated text available for audio")
130
+ return
131
  try:
132
+ target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang.split(" (")[0]), "en")
133
+ audio_data = audio_processor_module.text_to_speech(st.session_state.translated_text, target_lang)
134
+ if audio_data and audio_data.getbuffer().nbytes > 0:
135
+ st.audio(audio_data, format="audio/mp3")
136
+ else:
137
+ st.error("Failed to generate audio")
 
138
  except Exception as e:
139
+ st.error(f"Audio playback error: {e}")
140
 
141
  if __name__ == "__main__":
142
  main()