|
import streamlit as st |
|
import importlib |
|
from io import BytesIO |
|
import docx |
|
from PyPDF2 import PdfReader |
|
|
|
st.set_page_config(page_title="Multilingual Translator", page_icon="π", layout="centered") |
|
|
|
|
|
try: |
|
from translation import LANGUAGES |
|
except ImportError as e: |
|
st.error(f"Failed to import translation module: {e}") |
|
st.stop() |
|
|
|
def extract_text_from_file(file): |
|
try: |
|
if file.type == "application/pdf": |
|
pdf_reader = PdfReader(file) |
|
text = "" |
|
for page in pdf_reader.pages: |
|
text += page.extract_text() or "" |
|
return text.encode().decode('utf-8', errors='ignore').strip() |
|
elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document": |
|
doc = docx.Document(file) |
|
text = "\n".join([para.text for para in doc.paragraphs]) |
|
return text.encode().decode('utf-8', errors='ignore').strip() |
|
elif file.type == "text/plain": |
|
return file.read().decode('utf-8', errors='ignore').strip() |
|
return "" |
|
except Exception: |
|
return "" |
|
|
|
def main(): |
|
try: |
|
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: #4285F4;'>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(2) |
|
with col1: |
|
detected_options = lang_detect.detect_language(st.session_state.get("input_text", "")) if st.session_state.get("input_text", "").strip() else [("Auto-detect", 1.0, "Auto-detect")] |
|
source_lang = detected_options[0][2] if detected_options[0][0] != "Auto-detect" else "Auto-detect" |
|
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == source_lang), "hi") if source_lang != "Auto-detect" else "auto" |
|
source_options = ["Auto-detect"] + [f"{v[0]} ({v[1]})" for v in LANGUAGES.values()] |
|
st.selectbox("Source", options=source_options, index=0 if source_lang == "Auto-detect" else source_options.index(f"{LANGUAGES[source_lang_code][0]} ({source_lang})"), key="source_lang") |
|
input_text = st.text_area("", height=300, key="input_text", label_visibility="hidden") |
|
input_type = st.radio("", ["Text", "File"], horizontal=True, label_visibility="hidden") |
|
if input_type == "File": |
|
uploaded_file = st.file_uploader("", type=["txt", "docx", "pdf"], key="file_input", label_visibility="hidden") |
|
if uploaded_file and uploaded_file.size < 1024*1024: |
|
st.session_state.input_text = extract_text_from_file(uploaded_file) |
|
elif uploaded_file and uploaded_file.size >= 1024*1024: |
|
st.error("File size must be less than 1 MB") |
|
st.button("Translate", key="translate_btn", on_click=trigger_translation, args=(translation, lang_detect, audio_processor,)) |
|
with col2: |
|
source_lang_display = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang |
|
target_options = [f"{v[0]} ({v[1]})" for v in LANGUAGES.values() if v[0] != source_lang_display and v[1] != source_lang_display] |
|
st.selectbox("Target", options=target_options, index=target_options.index(f"{LANGUAGES['en'][0]} ({LANGUAGES['en'][1]})") if "English" not in source_lang_display else 0, key="target_lang") |
|
if "translated_text" in st.session_state: |
|
st.text_area("", value=st.session_state.translated_text, height=300, key="output_text", disabled=True, label_visibility="hidden") |
|
if st.button("π", key="audio_btn", on_click=play_audio, args=(audio_processor,), help="Play audio", use_container_width=False): |
|
pass |
|
|
|
if "translated_text" in st.session_state: |
|
st.markdown("<p style='text-align: center; color: #666;'>Developed by Krishna Prakash <a href='https://www.linkedin.com/in/krishna-prakash-123456' target='_blank'>LinkedIn</a></p>", unsafe_allow_html=True) |
|
|
|
except Exception as e: |
|
st.error(f"App error: {e}") |
|
|
|
def trigger_translation(translation, lang_detect, audio_processor): |
|
text = st.session_state.get("input_text", "").strip() |
|
if text: |
|
source_lang = st.session_state.source_lang.split(" (")[0] if " (" in st.session_state.source_lang else st.session_state.source_lang |
|
target_lang = st.session_state.target_lang.split(" (")[0] if " (" in st.session_state.target_lang else st.session_state.target_lang |
|
if source_lang == "Auto-detect": |
|
detected_options = lang_detect.detect_language(text) |
|
source_lang_code = next((k for k, v in LANGUAGES.items() if v[1] == detected_options[0][0]), "hi") |
|
else: |
|
source_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == source_lang), "hi") |
|
target_lang_code = next((k for k, v in LANGUAGES.items() if v[0] == target_lang), "en") |
|
translated_text = translation.translate(text, source_lang_code, target_lang_code) |
|
st.session_state.translated_text = translated_text or text |
|
|
|
def play_audio(audio_processor): |
|
if "translated_text" in st.session_state and st.session_state.translated_text: |
|
target_lang = next((k for k, v in LANGUAGES.items() if v[0] == st.session_state.target_lang.split(" (")[0]), "en") |
|
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") |
|
|
|
if __name__ == "__main__": |
|
main() |