|
import streamlit as st |
|
import os |
|
import importlib |
|
|
|
|
|
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 or audio for multilingual support.</p>", unsafe_allow_html=True) |
|
|
|
|
|
tab1, tab2, tab3 = st.tabs(["Text Input", "Audio Input", "Document Upload"]) |
|
|
|
|
|
if 'translated_text' not in st.session_state: |
|
st.session_state.translated_text = None |
|
st.session_state.audio_path = None |
|
st.session_state.source_lang = None |
|
st.session_state.detected_options = [] |
|
|
|
with tab1: |
|
|
|
user_text = st.text_area("Enter Text", placeholder="Type or paste your text here...", height=150, key="text_input") |
|
if user_text: |
|
handle_input(user_text, "text", translation, lang_detect, audio_processor) |
|
|
|
with tab2: |
|
|
|
audio_file = st.file_uploader("Upload Audio (MP3/WAV)", type=["mp3", "wav"], key="audio_input") |
|
if audio_file: |
|
user_text = audio_processor.transcribe_audio(audio_file) |
|
st.write(f"Transcribed Text: {user_text}") |
|
handle_input(user_text, "audio", translation, lang_detect, audio_processor) |
|
|
|
with tab3: |
|
|
|
doc_file = st.file_uploader("Upload Document (TXT)", type=["txt"], key="doc_input") |
|
if doc_file: |
|
user_text = doc_file.read().decode("utf-8") |
|
st.write(f"Document Text: {user_text}") |
|
handle_input(user_text, "doc", translation, lang_detect, audio_processor) |
|
|
|
|
|
def handle_input(text, input_type, translation, lang_detect, audio_processor): |
|
|
|
if len(text) < 10: |
|
detected_options = [("English", 1.0, "English")] |
|
else: |
|
detected_options = lang_detect.detect_language(text) |
|
st.session_state.detected_options = detected_options |
|
|
|
|
|
st.session_state.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": "日本語", |
|
} |
|
display_options = ["Auto"] + [f"{native_lang_map.get(lang, lang)} ({conf:.2f})" for lang, conf, _ in detected_options] + list(translation.LANGUAGES.keys()) |
|
override_lang = st.selectbox("Override Detected Language", display_options, index=0, key=f"override_lang_{input_type}") |
|
|
|
|
|
if override_lang != "Auto": |
|
selected_lang = next((lang for lang, _, _ in detected_options if native_lang_map.get(lang, lang) in override_lang), override_lang) |
|
st.session_state.source_lang = selected_lang |
|
if selected_lang not in translation.SUPPORTED_PAIRS.get(st.session_state.source_lang, []): |
|
st.session_state.source_lang = "English" |
|
st.warning(f"Translation from {native_lang_map.get(selected_lang, selected_lang)} is not supported. Using English instead.") |
|
|
|
st.info(f"Selected Source Language: {native_lang_map.get(st.session_state.source_lang, st.session_state.source_lang)}") |
|
|
|
|
|
target_options = {lang: native_lang_map.get(lang, lang) for lang in translation.LANGUAGES} |
|
target_lang = st.selectbox("Target Language", [target_options[lang] for lang in translation.LANGUAGES], |
|
index=1, key=f"target_lang_{input_type}", format_func=lambda x: x) |
|
target_lang = next(key for key, value in target_options.items() if value == target_lang) |
|
|
|
|
|
if st.button("Translate", key=f"translate_button_{input_type}"): |
|
with st.spinner("Translating..."): |
|
try: |
|
|
|
st.session_state.translated_text = translation.translate(text, st.session_state.source_lang, target_lang) |
|
|
|
|
|
st.markdown("<h3 style='color: #2E86C1;'>Translation Result</h3>", unsafe_allow_html=True) |
|
|
|
|
|
output_option = st.radio("Output Format", ["Text", "Audio"], key=f"output_option_{input_type}") |
|
|
|
if output_option == "Text": |
|
st.success("Translated Text:") |
|
st.write(st.session_state.translated_text) |
|
|
|
elif output_option == "Audio": |
|
st.success("Translated Audio:") |
|
st.session_state.audio_path = audio_processor.text_to_speech(st.session_state.translated_text, target_lang) |
|
if st.session_state.audio_path: |
|
st.audio(st.session_state.audio_path) |
|
else: |
|
st.error("Failed to generate audio. Please try again or check the input.") |
|
|
|
|
|
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) |
|
except Exception as e: |
|
st.error(f"Translation failed: {str(e)}") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |