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