Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import importlib
|
|
3 |
from io import BytesIO
|
4 |
import docx
|
5 |
from PyPDF2 import PdfReader
|
|
|
6 |
|
7 |
st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="wide")
|
8 |
|
@@ -129,11 +130,12 @@ def main():
|
|
129 |
except Exception as e:
|
130 |
st.error(f"App error: {e}")
|
131 |
|
132 |
-
# Function to trigger translation process with progress indicator
|
133 |
def trigger_translation(translation_module, language_detector, audio_processor_module):
|
134 |
user_input_text = st.session_state.get("user_input_text", "").strip()
|
135 |
if user_input_text:
|
136 |
with st.spinner("Translating..."):
|
|
|
137 |
source_lang = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
138 |
target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang
|
139 |
if source_lang == "Auto-detect":
|
@@ -143,6 +145,9 @@ def trigger_translation(translation_module, language_detector, audio_processor_m
|
|
143 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "en")
|
144 |
target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "hi")
|
145 |
translated_text = translation_module.translate(user_input_text, source_lang_code, target_lang_code)
|
|
|
|
|
|
|
146 |
if translated_text and len(translated_text.split()) > 2: # Basic validation
|
147 |
st.session_state.translated_text = translated_text
|
148 |
else:
|
|
|
3 |
from io import BytesIO
|
4 |
import docx
|
5 |
from PyPDF2 import PdfReader
|
6 |
+
import time
|
7 |
|
8 |
st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="wide")
|
9 |
|
|
|
130 |
except Exception as e:
|
131 |
st.error(f"App error: {e}")
|
132 |
|
133 |
+
# Function to trigger translation process with progress indicator and timeout
|
134 |
def trigger_translation(translation_module, language_detector, audio_processor_module):
|
135 |
user_input_text = st.session_state.get("user_input_text", "").strip()
|
136 |
if user_input_text:
|
137 |
with st.spinner("Translating..."):
|
138 |
+
start_time = time.time()
|
139 |
source_lang = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang
|
140 |
target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang
|
141 |
if source_lang == "Auto-detect":
|
|
|
145 |
source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "en")
|
146 |
target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "hi")
|
147 |
translated_text = translation_module.translate(user_input_text, source_lang_code, target_lang_code)
|
148 |
+
if time.time() - start_time > 10: # Timeout after 10 seconds
|
149 |
+
st.error("Translation took too long, reverting to input.")
|
150 |
+
translated_text = user_input_text
|
151 |
if translated_text and len(translated_text.split()) > 2: # Basic validation
|
152 |
st.session_state.translated_text = translated_text
|
153 |
else:
|