Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,25 @@
|
|
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:
|
@@ -35,44 +35,47 @@ def main():
|
|
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 =
|
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 =
|
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 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
if output_option == "Audio":
|
62 |
-
audio = audio_processor.text_to_speech(
|
63 |
if audio and audio.getbuffer().nbytes > 0:
|
64 |
-
|
65 |
-
|
66 |
else:
|
67 |
-
|
68 |
-
audio_fallback = audio_processor.text_to_speech(
|
69 |
if audio_fallback and audio_fallback.getbuffer().nbytes > 0:
|
70 |
-
|
71 |
-
|
72 |
else:
|
73 |
-
|
74 |
|
75 |
-
|
76 |
<p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>
|
77 |
Developed by: Krishna Prakash <a href='https://www.linkedin.com/in/krishnaprakash-profile/' target='_blank'>
|
78 |
<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 streamlit
|
2 |
import importlib
|
3 |
from io import BytesIO
|
4 |
import time
|
5 |
|
6 |
+
streamlit.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 |
+
streamlit.markdown("<h1 style='text-align: center; color: #2E86C1;'>Multilingual Translator</h1>", unsafe_allow_html=True)
|
14 |
+
streamlit.markdown("<p style='text-align: center; color: #666;'>Translate text like Google Translate</p>", unsafe_allow_html=True)
|
15 |
|
16 |
+
col1, col2 = streamlit.columns([1, 1])
|
17 |
with col1:
|
18 |
+
streamlit.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
|
19 |
+
input_type = streamlit.radio("Input", ["Text", "File Upload"], horizontal=True, label_visibility="collapsed", key="input_type")
|
20 |
+
user_text = streamlit.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 = streamlit.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:
|
|
|
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 streamlit.button("Translate", key="translate_btn"):
|
45 |
+
with streamlit.spinner("Translating..."):
|
46 |
try:
|
47 |
translated_text = translation.translate(user_text, source_lang, target_lang)
|
48 |
+
streamlit.session_state.translated_text = translated_text
|
49 |
+
streamlit.session_state.translation_time = time.time() - start_time
|
50 |
except Exception as e:
|
51 |
+
streamlit.session_state.translated_text = user_text
|
52 |
+
streamlit.session_state.translation_time = time.time() - start_time
|
53 |
+
streamlit.warning(f"Translation error: {str(e)}. Using input as fallback.")
|
54 |
|
55 |
with col2:
|
56 |
+
streamlit.markdown("<div style='margin-bottom: 10px;'></div>", unsafe_allow_html=True)
|
57 |
+
if "translated_text" in streamlit.session_state and streamlit.session_state.translated_text:
|
58 |
+
# Dynamic height based on content, with minimum 200px
|
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 |
+
streamlit.text_area("Translation:", value=streamlit.session_state.translated_text, height=output_height, key="output_area")
|
62 |
+
streamlit.write(f"Translation time: {streamlit.session_state.translation_time:.2f} seconds")
|
63 |
+
output_option = streamlit.radio("Output", ["Text", "Audio"], horizontal=True, label_visibility="collapsed", key="output_option")
|
64 |
if output_option == "Audio":
|
65 |
+
audio = audio_processor.text_to_speech(streamlit.session_state.translated_text, target_lang)
|
66 |
if audio and audio.getbuffer().nbytes > 0:
|
67 |
+
streamlit.audio(audio, format="audio/mp3")
|
68 |
+
streamlit.success("Audio playing.")
|
69 |
else:
|
70 |
+
streamlit.error("Audio generation failed. Retrying with English...")
|
71 |
+
audio_fallback = audio_processor.text_to_speech(streamlit.session_state.translated_text, "English")
|
72 |
if audio_fallback and audio_fallback.getbuffer().nbytes > 0:
|
73 |
+
streamlit.audio(audio_fallback, format="audio/mp3")
|
74 |
+
streamlit.success("Fallback audio in English playing.")
|
75 |
else:
|
76 |
+
streamlit.error("Audio generation failed. Try again later.")
|
77 |
|
78 |
+
streamlit.markdown("""
|
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;'/>
|