File size: 6,590 Bytes
737c030 e656608 da34b5e 1c122b7 891f160 e12cdaf 8040f65 891f160 e656608 e12cdaf 737c030 8040f65 e12cdaf 737c030 e12cdaf d60f4d4 e12cdaf da34b5e e12cdaf da34b5e e12cdaf 737c030 7a23dcf e12cdaf 737c030 e12cdaf 1c122b7 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_swap, col_target = st.columns([1, 0.2, 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_swap:
if st.button("↔", key="swap_btn"):
st.session_state.target_lang = source_lang_display
st.session_state.source_lang = st.session_state.get("target_lang_display", "हिन्दी")
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)
# Microphone (Optional, placeholder)
st.button("🎤", key="mic_btn", help="Speech-to-text (not implemented)", disabled=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..."):
start_time = time.time()
try:
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.")
# 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_text, col_audio = st.columns([0.5, 0.5])
with col_text:
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":
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")
st.success("Audio playing.")
else:
st.error("Audio generation failed. Retrying with English...")
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")
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() |