File size: 6,771 Bytes
737c030 e656608 da34b5e 1c122b7 891f160 e12cdaf 8040f65 891f160 e656608 e12cdaf 737c030 115098a 8040f65 e12cdaf c4196eb e12cdaf 737c030 e12cdaf d60f4d4 e12cdaf da34b5e e12cdaf 115098a e12cdaf c4196eb e12cdaf 115098a da34b5e e12cdaf 737c030 c4196eb 737c030 7a23dcf e12cdaf 737c030 c4196eb e12cdaf 115098a e12cdaf 115098a e12cdaf c4196eb 115098a 1c122b7 e12cdaf c4196eb e12cdaf da34b5e e12cdaf 737c030 da34b5e d60f4d4 da34b5e 8040f65 891f160 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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
with st.spinner("Translating...") as spinner: # Hidden spinner logic
start_time = time.time()
try:
# Use English as buffer for translation
if source_lang != "English" and target_lang != "English":
temp_translation = translation.translate(user_text, source_lang, "English")
translated_text = translation.translate(temp_translation, "English", target_lang)
else:
translated_text = translation.translate(user_text, source_lang, target_lang)
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 error: {str(e)}. Using input as fallback.")
spinner.empty() # Hide spinner after processing
# 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:
# Match input height dynamically
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.experimental_set_query_params(text=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.") # Only success message after playback starts
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() |