Update app.py
Browse files
app.py
CHANGED
@@ -30,7 +30,7 @@ def main():
|
|
30 |
# Text input
|
31 |
user_text = st.text_area("Enter Text", placeholder="Type or paste your text here...", height=150, key="text_input")
|
32 |
if user_text:
|
33 |
-
handle_input(user_text, translation, lang_detect, audio_processor)
|
34 |
|
35 |
with tab2:
|
36 |
# Audio input
|
@@ -38,7 +38,7 @@ def main():
|
|
38 |
if audio_file:
|
39 |
user_text = audio_processor.transcribe_audio(audio_file)
|
40 |
st.write(f"Transcribed Text: {user_text}")
|
41 |
-
handle_input(user_text, translation, lang_detect, audio_processor)
|
42 |
|
43 |
with tab3:
|
44 |
# Document input
|
@@ -46,10 +46,10 @@ def main():
|
|
46 |
if doc_file:
|
47 |
user_text = doc_file.read().decode("utf-8")
|
48 |
st.write(f"Document Text: {user_text}")
|
49 |
-
handle_input(user_text, translation, lang_detect, audio_processor)
|
50 |
|
51 |
# Handle input processing
|
52 |
-
def handle_input(text, translation, lang_detect, audio_processor):
|
53 |
# Auto-detect source language with options
|
54 |
if len(text) < 10: # Minimum length for reliable detection
|
55 |
detected_options = [("English", 1.0, "English")]
|
@@ -73,7 +73,7 @@ def handle_input(text, translation, lang_detect, audio_processor):
|
|
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="
|
77 |
|
78 |
# Map override selection to supported language
|
79 |
if override_lang != "Auto":
|
@@ -88,11 +88,11 @@ def handle_input(text, translation, lang_detect, audio_processor):
|
|
88 |
# Target language selection with native names
|
89 |
target_options = {lang: native_lang_map.get(lang, lang) for lang in translation.LANGUAGES}
|
90 |
target_lang = st.selectbox("Target Language", [target_options[lang] for lang in translation.LANGUAGES],
|
91 |
-
index=1, key="
|
92 |
target_lang = next(key for key, value in target_options.items() if value == target_lang)
|
93 |
|
94 |
# Translate button
|
95 |
-
if st.button("Translate", key="
|
96 |
with st.spinner("Translating..."):
|
97 |
try:
|
98 |
# Translate the text
|
@@ -102,7 +102,7 @@ def handle_input(text, translation, lang_detect, audio_processor):
|
|
102 |
st.markdown("<h3 style='color: #2E86C1;'>Translation Result</h3>", unsafe_allow_html=True)
|
103 |
|
104 |
# Output options: Text and Audio
|
105 |
-
output_option = st.radio("Output Format", ["Text", "Audio"], key="
|
106 |
|
107 |
if output_option == "Text":
|
108 |
st.success("Translated Text:")
|
|
|
30 |
# Text input
|
31 |
user_text = st.text_area("Enter Text", placeholder="Type or paste your text here...", height=150, key="text_input")
|
32 |
if user_text:
|
33 |
+
handle_input(user_text, "text", translation, lang_detect, audio_processor)
|
34 |
|
35 |
with tab2:
|
36 |
# Audio input
|
|
|
38 |
if audio_file:
|
39 |
user_text = audio_processor.transcribe_audio(audio_file)
|
40 |
st.write(f"Transcribed Text: {user_text}")
|
41 |
+
handle_input(user_text, "audio", translation, lang_detect, audio_processor)
|
42 |
|
43 |
with tab3:
|
44 |
# Document input
|
|
|
46 |
if doc_file:
|
47 |
user_text = doc_file.read().decode("utf-8")
|
48 |
st.write(f"Document Text: {user_text}")
|
49 |
+
handle_input(user_text, "doc", translation, lang_detect, audio_processor)
|
50 |
|
51 |
# Handle input processing
|
52 |
+
def handle_input(text, input_type, translation, lang_detect, audio_processor):
|
53 |
# Auto-detect source language with options
|
54 |
if len(text) < 10: # Minimum length for reliable detection
|
55 |
detected_options = [("English", 1.0, "English")]
|
|
|
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":
|
|
|
88 |
# Target language selection with native names
|
89 |
target_options = {lang: native_lang_map.get(lang, lang) for lang in translation.LANGUAGES}
|
90 |
target_lang = st.selectbox("Target Language", [target_options[lang] for lang in translation.LANGUAGES],
|
91 |
+
index=1, key=f"target_lang_{input_type}", format_func=lambda x: x)
|
92 |
target_lang = next(key for key, value in target_options.items() if value == target_lang)
|
93 |
|
94 |
# Translate button
|
95 |
+
if st.button("Translate", key=f"translate_button_{input_type}"):
|
96 |
with st.spinner("Translating..."):
|
97 |
try:
|
98 |
# Translate the text
|
|
|
102 |
st.markdown("<h3 style='color: #2E86C1;'>Translation Result</h3>", unsafe_allow_html=True)
|
103 |
|
104 |
# Output options: Text and Audio
|
105 |
+
output_option = st.radio("Output Format", ["Text", "Audio"], key=f"output_option_{input_type}")
|
106 |
|
107 |
if output_option == "Text":
|
108 |
st.success("Translated Text:")
|