Krishna086's picture
Update app.py
395199d verified
raw
history blame
6.95 kB
import streamlit as st
import importlib
from io import BytesIO
import time
st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="wide")
def main():
translation = importlib.import_module("translation")
lang_detect = importlib.import_module("lang_detect")
audio_processor = importlib.import_module("audio_processor")
# Header
st.markdown("<h1 style='text-align: center; color: #2E86C1;'>Multilingual Translator</h1>", unsafe_allow_html=True)
st.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
# Language Controls
col_lang, col_target = st.columns([1, 1])
with col_lang:
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")]
source_lang = detected_options[0][0] if detected_options else "English"
native_lang_map = {
"English": "English", "French": "Français", "Spanish": "Español",
"German": "Deutsch", "Hindi": "हिन्दी", "Chinese": "中文",
"Arabic": "العربية", "Russian": "Русский", "Japanese": "日本語"
}
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")
with col_target:
target_options = list(native_lang_map.values())
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")
target_lang = next((k for k, v in native_lang_map.items() if v == target_lang_display), "Hindi")
# Input Section
col_input, col_output = st.columns([1, 1])
with col_input:
st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
input_type = st.radio("Input", ["Text", "File Upload"], horizontal=True, label_visibility="collapsed", key="input_type")
user_text = st.text_area("Enter or paste text", height=200, key="user_text").strip() if input_type == "Text" else ""
if input_type == "File Upload":
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")
user_text = doc_file.read().decode("utf-8").strip() if doc_file else st.session_state.get("user_text", "")
char_count = len(user_text)
st.markdown(f"<small style='color: grey;'>Characters: {char_count}/1000</small>", unsafe_allow_html=True)
if user_text and (st.session_state.get("last_text", "") != user_text or st.button("Translate", key="translate_btn")):
st.session_state.last_text = user_text
spinner = st.empty()
spinner.info("Translating...")
start_time = time.time()
try:
# Use English as buffer for translation with validation
if source_lang != "English" and target_lang != "English":
temp_translation = translation.translate(user_text, source_lang, "English")
if not temp_translation or len(temp_translation.split()) < 2: # Validate intermediate result
raise ValueError("Intermediate translation failed")
translated_text = translation.translate(temp_translation, "English", target_lang)
else:
translated_text = translation.translate(user_text, source_lang, target_lang)
if not translated_text or len(translated_text.split()) < 2: # Validate final result
raise ValueError("Final translation failed")
st.session_state.translated_text = translated_text
st.session_state.translation_time = time.time() - start_time
except Exception as e:
st.session_state.translated_text = user_text
st.session_state.translation_time = time.time() - start_time
st.warning(f"Translation issue: {str(e)}. Using input as fallback.")
finally:
spinner.empty()
# Output Section
with col_output:
st.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
if "translated_text" in st.session_state and st.session_state.translated_text:
line_count = max(len(st.session_state.translated_text.splitlines()), len(user_text.splitlines()))
output_height = max(200, line_count * 20 + 50)
st.text_area("Translation:", value=st.session_state.translated_text, height=output_height, key="output_area")
st.write(f"Translation time: {st.session_state.translation_time:.2f} seconds")
col_copy, col_audio = st.columns([0.5, 0.5])
with col_copy:
if st.button("📋", key="copy_btn", help="Copy to clipboard"):
st.clipboard(st.session_state.translated_text)
st.success("Copied to clipboard!")
with col_audio:
output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, label_visibility="collapsed", key="output_option")
if output_option == "Audio" and "translated_text" in st.session_state:
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
if audio and audio.getbuffer().nbytes > 0:
st.audio(audio, format="audio/mp3", start_time=0)
st.success("Audio playing.")
else:
audio_fallback = audio_processor.text_to_speech(st.session_state.translated_text, "English")
if audio_fallback and audio_fallback.getbuffer().nbytes > 0:
st.audio(audio_fallback, format="audio/mp3", start_time=0)
st.success("Fallback audio in English playing.")
else:
st.error("Audio generation failed. Try again later.")
# Footer
st.markdown("""
<p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>
Developed by: Krishna Prakash <a href='https://www.linkedin.com/in/krishnaprakash-profile/' target='_blank'>
<img src='https://img.icons8.com/ios-filled/30/0077b5/linkedin.png' alt='LinkedIn' style='vertical-align: middle; margin: 0 5px;'/>
</a>
</p>
""", unsafe_allow_html=True)
if __name__ == "__main__":
main()