Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
|
|
2 |
import importlib
|
3 |
from io import BytesIO
|
4 |
|
5 |
-
st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="
|
6 |
|
7 |
def main():
|
8 |
translation = importlib.import_module("translation")
|
@@ -12,19 +12,14 @@ def main():
|
|
12 |
st.markdown("<h1 style='text-align: center; color: #2E86C1;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
13 |
st.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
|
14 |
|
15 |
-
# Single-page layout
|
16 |
col1, col2 = st.columns([1, 1])
|
17 |
with col1:
|
18 |
-
input_type = st.radio("Input
|
19 |
-
if input_type == "Text"
|
20 |
-
|
21 |
-
|
22 |
-
doc_file = st.file_uploader("Upload TXT File", type=["txt"], key="doc_input")
|
23 |
user_text = doc_file.read().decode("utf-8").strip() if doc_file else ""
|
24 |
|
25 |
-
st.write(f"Raw Input: {user_text}") # Debug
|
26 |
-
|
27 |
-
with col2:
|
28 |
if user_text:
|
29 |
detected_options = lang_detect.detect_language(user_text) if len(user_text) >= 10 else [("English", 1.0, "English")]
|
30 |
source_lang = detected_options[0][0] if detected_options else "English"
|
@@ -34,38 +29,39 @@ def main():
|
|
34 |
"German": "Deutsch", "Hindi": "हिन्दी", "Chinese": "中文",
|
35 |
"Arabic": "العربية", "Russian": "Русский", "Japanese": "日本語"
|
36 |
}
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
|
45 |
if st.button("Translate", key="translate_btn"):
|
46 |
try:
|
47 |
translated_text = translation.translate(user_text, source_lang, target_lang)
|
48 |
st.session_state.translated_text = translated_text
|
49 |
-
st.write(f"Translated Text (Debug): {translated_text}")
|
50 |
except Exception as e:
|
51 |
-
st.error(f"Translation failed: {str(e)}. Using fallback
|
52 |
-
st.session_state.translated_text =
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
else:
|
59 |
-
|
60 |
-
if audio_path:
|
61 |
-
st.audio(audio_path, format="audio/mp3")
|
62 |
-
else:
|
63 |
-
st.error("Audio generation failed. Try a supported language like English or French.")
|
64 |
|
65 |
st.markdown("""
|
66 |
<p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>
|
67 |
-
Developed
|
68 |
-
<a href='https://www.linkedin.com/in/krishnaprakash-profile/' target='_blank'>
|
69 |
<img src='https://img.icons8.com/ios-filled/30/0077b5/linkedin.png' alt='LinkedIn' style='vertical-align: middle; margin: 0 5px;'/>
|
70 |
</a>
|
71 |
</p>
|
|
|
2 |
import importlib
|
3 |
from io import BytesIO
|
4 |
|
5 |
+
st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="centered")
|
6 |
|
7 |
def main():
|
8 |
translation = importlib.import_module("translation")
|
|
|
12 |
st.markdown("<h1 style='text-align: center; color: #2E86C1;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
13 |
st.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
|
14 |
|
|
|
15 |
col1, col2 = st.columns([1, 1])
|
16 |
with col1:
|
17 |
+
input_type = st.radio("Input", ["Text", "File Upload"], horizontal=True, key="input_type")
|
18 |
+
user_text = st.text_area("Enter or paste text", height=150, key="text_input").strip() if input_type == "Text" else ""
|
19 |
+
if input_type == "File Upload":
|
20 |
+
doc_file = st.file_uploader("Upload TXT File", type=["txt"], key="doc_input", accept_multiple_files=False)
|
|
|
21 |
user_text = doc_file.read().decode("utf-8").strip() if doc_file else ""
|
22 |
|
|
|
|
|
|
|
23 |
if user_text:
|
24 |
detected_options = lang_detect.detect_language(user_text) if len(user_text) >= 10 else [("English", 1.0, "English")]
|
25 |
source_lang = detected_options[0][0] if detected_options else "English"
|
|
|
29 |
"German": "Deutsch", "Hindi": "हिन्दी", "Chinese": "中文",
|
30 |
"Arabic": "العربية", "Russian": "Русский", "Japanese": "日本語"
|
31 |
}
|
32 |
+
source_options = ["Detect"] + [native_lang_map.get(lang, lang) for lang, _, _ in detected_options] + list(native_lang_map.values())
|
33 |
+
target_options = list(native_lang_map.values())
|
34 |
|
35 |
+
source_lang_display = st.selectbox("From", source_options, index=0, key="source_lang").replace("Detect", native_lang_map.get(source_lang, source_lang))
|
36 |
+
source_lang = next((k for k, v in native_lang_map.items() if v == source_lang_display), source_lang)
|
37 |
+
|
38 |
+
target_lang_display = st.selectbox("To", target_options, index=target_options.index("हिन्दी") if "हिन्दी" in target_options else 0, key="target_lang")
|
39 |
+
target_lang = next((k for k, v in native_lang_map.items() if v == target_lang_display), "Hindi")
|
40 |
|
41 |
if st.button("Translate", key="translate_btn"):
|
42 |
try:
|
43 |
translated_text = translation.translate(user_text, source_lang, target_lang)
|
44 |
st.session_state.translated_text = translated_text
|
|
|
45 |
except Exception as e:
|
46 |
+
st.error(f"Translation failed: {str(e)}. Using input as fallback.")
|
47 |
+
st.session_state.translated_text = user_text
|
48 |
|
49 |
+
with col2:
|
50 |
+
if "translated_text" in st.session_state and st.session_state.translated_text:
|
51 |
+
st.write("Translation:")
|
52 |
+
st.write(st.session_state.translated_text)
|
53 |
+
output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, key="output_option")
|
54 |
+
if output_option == "Audio":
|
55 |
+
audio = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
|
56 |
+
if audio and audio.getbuffer().nbytes > 0:
|
57 |
+
st.audio(audio, format="audio/mp3")
|
58 |
+
st.success("Audio playing.")
|
59 |
else:
|
60 |
+
st.error("Audio generation failed. Try English or French.")
|
|
|
|
|
|
|
|
|
61 |
|
62 |
st.markdown("""
|
63 |
<p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>
|
64 |
+
Developed by: Krishna Prakash <a href='https://www.linkedin.com/in/krishnaprakash-profile/' target='_blank'>
|
|
|
65 |
<img src='https://img.icons8.com/ios-filled/30/0077b5/linkedin.png' alt='LinkedIn' style='vertical-align: middle; margin: 0 5px;'/>
|
66 |
</a>
|
67 |
</p>
|