|
import streamlit as st |
|
import importlib |
|
from io import BytesIO |
|
|
|
st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="centered") |
|
|
|
def main(): |
|
translation = importlib.import_module("translation") |
|
lang_detect = importlib.import_module("lang_detect") |
|
audio_processor = importlib.import_module("audio_processor") |
|
|
|
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) |
|
|
|
col1, col2 = st.columns([1, 1]) |
|
with col1: |
|
input_type = st.radio("Input", ["Text", "File Upload"], horizontal=True, key="input_type") |
|
user_text = st.text_area("Enter or paste text", height=150, key="text_input").strip() if input_type == "Text" else "" |
|
if input_type == "File Upload": |
|
doc_file = st.file_uploader("Upload TXT File", type=["txt"], key="doc_input", accept_multiple_files=False) |
|
user_text = doc_file.read().decode("utf-8").strip() if doc_file else "" |
|
|
|
if user_text: |
|
detected_options = lang_detect.detect_language(user_text) if len(user_text) >= 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_options = ["Detect"] + [native_lang_map.get(lang, lang) for lang, _, _ in detected_options] + list(native_lang_map.values()) |
|
target_options = list(native_lang_map.values()) |
|
|
|
source_lang_display = st.selectbox("From", source_options, index=0, key="source_lang").replace("Detect", native_lang_map.get(source_lang, source_lang)) |
|
source_lang = next((k for k, v in native_lang_map.items() if v == source_lang_display), source_lang) |
|
|
|
target_lang_display = st.selectbox("To", target_options, index=target_options.index("हिन्दी") if "हिन्दी" in target_options else 0, key="target_lang") |
|
target_lang = next((k for k, v in native_lang_map.items() if v == target_lang_display), "Hindi") |
|
|
|
if st.button("Translate", key="translate_btn"): |
|
try: |
|
translated_text = translation.translate(user_text, source_lang, target_lang) |
|
st.session_state.translated_text = translated_text |
|
except Exception as e: |
|
st.error(f"Translation failed: {str(e)}. Using input as fallback.") |
|
st.session_state.translated_text = user_text |
|
|
|
with col2: |
|
if "translated_text" in st.session_state and st.session_state.translated_text: |
|
st.write("Translation:") |
|
st.write(st.session_state.translated_text) |
|
output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, 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. Try English or French.") |
|
|
|
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() |