|
import streamlit as st |
|
from translation import translate |
|
from lang_detect import detect_language |
|
from audio_processor import transcribe_audio, text_to_speech |
|
import os |
|
|
|
|
|
st.set_page_config(page_title="Multilingual Translator", page_icon="π", layout="centered") |
|
|
|
|
|
def main(): |
|
|
|
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 = "English" |
|
|
|
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) |
|
|
|
with tab2: |
|
|
|
audio_file = st.file_uploader("Upload Audio (MP3/WAV)", type=["mp3", "wav"], key="audio_input") |
|
if audio_file: |
|
user_text = transcribe_audio(audio_file) |
|
st.write(f"Transcribed Text: {user_text}") |
|
handle_input(user_text) |
|
|
|
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) |
|
|
|
|
|
def handle_input(text): |
|
|
|
detected_lang = detect_language(text) |
|
st.session_state.source_lang = detected_lang if detected_lang in LANGUAGES else "English" |
|
st.info(f"Detected Source Language: {st.session_state.source_lang}") |
|
|
|
|
|
target_lang = st.selectbox("Target Language", list(LANGUAGES.keys()), index=1, key="target_lang") |
|
|
|
|
|
if st.button("Translate", key="translate_button"): |
|
with st.spinner("Translating..."): |
|
try: |
|
|
|
st.session_state.translated_text = 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="output_option") |
|
|
|
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 = text_to_speech(st.session_state.translated_text, target_lang) |
|
st.audio(st.session_state.audio_path) |
|
|
|
|
|
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)}") |
|
|
|
|
|
LANGUAGES = { |
|
"English": "en", |
|
"French": "fr", |
|
"Spanish": "es", |
|
"German": "de", |
|
"Chinese": "zh", |
|
"Arabic": "ar", |
|
"Russian": "ru", |
|
"Hindi": "hi", |
|
"Japanese": "ja" |
|
} |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |