Update app.py
Browse files
app.py
CHANGED
@@ -1,131 +1,75 @@
|
|
1 |
import streamlit as st
|
2 |
-
import os
|
3 |
import importlib
|
|
|
4 |
|
5 |
-
|
6 |
-
st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="centered")
|
7 |
|
8 |
-
# Main app function
|
9 |
def main():
|
10 |
-
# Lazy import modules to avoid Streamlit initialization during import
|
11 |
translation = importlib.import_module("translation")
|
12 |
lang_detect = importlib.import_module("lang_detect")
|
13 |
audio_processor = importlib.import_module("audio_processor")
|
14 |
|
15 |
-
# Title and header with styling
|
16 |
st.markdown("<h1 style='text-align: center; color: #2E86C1;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
17 |
-
st.markdown("<p style='text-align: center; color: #666;'>Translate text
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
if 'translated_text' not in st.session_state:
|
24 |
-
st.session_state.translated_text = None
|
25 |
-
st.session_state.audio_path = None
|
26 |
-
st.session_state.source_lang = None
|
27 |
-
st.session_state.detected_options = []
|
28 |
|
29 |
-
with
|
30 |
-
# Text input
|
31 |
-
user_text = st.text_area("Enter Text", placeholder="Type or paste your text here...", height=150, key="text_input").strip()
|
32 |
if user_text:
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# Set initial source language to the top detected option
|
61 |
-
st.session_state.source_lang = detected_options[0][0] if detected_options else "English"
|
62 |
-
|
63 |
-
# Display native language names for detected options
|
64 |
-
native_lang_map = {
|
65 |
-
"English": "English",
|
66 |
-
"French": "Français",
|
67 |
-
"Spanish": "Español",
|
68 |
-
"German": "Deutsch",
|
69 |
-
"Hindi": "हिन्दी",
|
70 |
-
"Chinese": "中文",
|
71 |
-
"Arabic": "العربية",
|
72 |
-
"Russian": "Русский",
|
73 |
-
"Japanese": "日本語",
|
74 |
-
}
|
75 |
-
display_options = ["Auto"] + [f"{native_lang_map.get(lang, lang)} ({conf:.2f})" for lang, conf, _ in detected_options] + list(translation.LANGUAGES.keys())
|
76 |
-
override_lang = st.selectbox("Override Detected Language", display_options, index=0, key=f"override_lang_{input_type}")
|
77 |
-
|
78 |
-
# Map override selection to supported language
|
79 |
-
if override_lang != "Auto":
|
80 |
-
selected_lang = next((lang for lang, _, _ in detected_options if native_lang_map.get(lang, lang) in override_lang), override_lang)
|
81 |
-
st.session_state.source_lang = selected_lang
|
82 |
-
|
83 |
-
st.info(f"Selected Source Language: {native_lang_map.get(st.session_state.source_lang, st.session_state.source_lang)}")
|
84 |
-
|
85 |
-
# Target language selection with native names
|
86 |
-
target_options = {lang: native_lang_map.get(lang, lang) for lang in translation.LANGUAGES}
|
87 |
-
target_lang = st.selectbox("Target Language", [target_options[lang] for lang in translation.LANGUAGES],
|
88 |
-
index=list(translation.LANGUAGES.keys()).index("Hindi") if "Hindi" in translation.LANGUAGES else 1,
|
89 |
-
key=f"target_lang_{input_type}", format_func=lambda x: x)
|
90 |
-
target_lang = next(key for key, value in target_options.items() if value == target_lang)
|
91 |
-
|
92 |
-
# Translate button
|
93 |
-
if st.button("Translate", key=f"translate_button_{input_type}"):
|
94 |
-
with st.spinner("Translating..."):
|
95 |
-
try:
|
96 |
-
# Translate the text
|
97 |
-
st.session_state.translated_text = translation.translate(text, st.session_state.source_lang, target_lang)
|
98 |
-
st.write(f"Translated Text (Debug): {st.session_state.translated_text}") # Debug output
|
99 |
-
# Display results in a styled container
|
100 |
-
st.markdown("<h3 style='color: #2E86C1;'>Translation Result</h3>", unsafe_allow_html=True)
|
101 |
-
|
102 |
-
# Output options: Text and Audio
|
103 |
-
output_option = st.radio("Output Format", ["Text", "Audio"], key=f"output_option_{input_type}")
|
104 |
-
|
105 |
if output_option == "Text":
|
106 |
-
st.success("
|
107 |
st.write(st.session_state.translated_text)
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
if st.session_state.audio_path:
|
113 |
-
st.audio(st.session_state.audio_path)
|
114 |
else:
|
115 |
-
st.error("
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
""", unsafe_allow_html=True)
|
126 |
-
except Exception as e:
|
127 |
-
st.error(f"Translation failed: {str(e)}")
|
128 |
|
129 |
-
# Run the app
|
130 |
if __name__ == "__main__":
|
131 |
main()
|
|
|
1 |
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="wide")
|
|
|
6 |
|
|
|
7 |
def main():
|
|
|
8 |
translation = importlib.import_module("translation")
|
9 |
lang_detect = importlib.import_module("lang_detect")
|
10 |
audio_processor = importlib.import_module("audio_processor")
|
11 |
|
|
|
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 Method", ["Text", "File Upload"], horizontal=True)
|
19 |
+
if input_type == "Text":
|
20 |
+
user_text = st.text_area("Enter Text", height=200, key="text_input").strip()
|
21 |
+
else:
|
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"
|
31 |
+
|
32 |
+
native_lang_map = {
|
33 |
+
"English": "English", "French": "Français", "Spanish": "Español",
|
34 |
+
"German": "Deutsch", "Hindi": "हिन्दी", "Chinese": "中文",
|
35 |
+
"Arabic": "العربية", "Russian": "Русский", "Japanese": "日本語"
|
36 |
+
}
|
37 |
+
display_options = ["Auto"] + [f"{native_lang_map.get(lang, lang)} ({conf:.2f})" for lang, conf, _ in detected_options] + list(translation.LANGUAGES.keys())
|
38 |
+
source_lang = st.selectbox("Source Language", display_options, index=0, key="source_lang").replace("Auto", source_lang)
|
39 |
+
|
40 |
+
target_lang = st.selectbox("Target Language", [native_lang_map[lang] for lang in translation.LANGUAGES],
|
41 |
+
index=list(translation.LANGUAGES.keys()).index("Hindi") if "Hindi" in translation.LANGUAGES else 0,
|
42 |
+
key="target_lang", format_func=lambda x: x)
|
43 |
+
target_lang = next(key for key, value in native_lang_map.items() if value == target_lang)
|
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 text.")
|
52 |
+
st.session_state.translated_text = f"Translation not supported for {source_lang} to {target_lang}. Input: {user_text}"
|
53 |
+
|
54 |
+
output_option = st.radio("Output", ["Text", "Audio"], horizontal=True, key="output_option")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
if output_option == "Text":
|
56 |
+
st.success("Result:")
|
57 |
st.write(st.session_state.translated_text)
|
58 |
+
else:
|
59 |
+
audio_path = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
|
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 By: Krishna Prakash
|
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>
|
72 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
73 |
|
|
|
74 |
if __name__ == "__main__":
|
75 |
main()
|