Krishna086 commited on
Commit
e656608
·
verified ·
1 Parent(s): 8886a34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -27
app.py CHANGED
@@ -1,14 +1,17 @@
1
  import streamlit as st
2
- from translation import translate
3
- from lang_detect import detect_language
4
- from audio_processor import transcribe_audio, text_to_speech
5
  import os
 
6
 
7
- # Set page config as the first Streamlit command
8
  st.set_page_config(page_title="Multilingual Translator", page_icon="🌐", layout="centered")
9
 
10
  # Main app function
11
  def main():
 
 
 
 
 
12
  # Title and header with styling
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 or audio for multilingual support.</p>", unsafe_allow_html=True)
@@ -26,15 +29,15 @@ def main():
26
  # Text input
27
  user_text = st.text_area("Enter Text", placeholder="Type or paste your text here...", height=150, key="text_input")
28
  if user_text:
29
- handle_input(user_text)
30
 
31
  with tab2:
32
  # Audio input
33
  audio_file = st.file_uploader("Upload Audio (MP3/WAV)", type=["mp3", "wav"], key="audio_input")
34
  if audio_file:
35
- user_text = transcribe_audio(audio_file)
36
  st.write(f"Transcribed Text: {user_text}")
37
- handle_input(user_text)
38
 
39
  with tab3:
40
  # Document input
@@ -42,24 +45,24 @@ def main():
42
  if doc_file:
43
  user_text = doc_file.read().decode("utf-8")
44
  st.write(f"Document Text: {user_text}")
45
- handle_input(user_text)
46
 
47
  # Handle input processing
48
- def handle_input(text):
49
  # Auto-detect source language
50
- detected_lang = detect_language(text)
51
- st.session_state.source_lang = detected_lang if detected_lang in LANGUAGES else "English"
52
  st.info(f"Detected Source Language: {st.session_state.source_lang}")
53
 
54
  # Target language selection
55
- target_lang = st.selectbox("Target Language", list(LANGUAGES.keys()), index=1, key="target_lang")
56
 
57
  # Translate button
58
  if st.button("Translate", key="translate_button"):
59
  with st.spinner("Translating..."):
60
  try:
61
  # Translate the text
62
- st.session_state.translated_text = translate(text, st.session_state.source_lang, target_lang)
63
 
64
  # Display results in a styled container
65
  st.markdown("<h3 style='color: #2E86C1;'>Translation Result</h3>", unsafe_allow_html=True)
@@ -73,7 +76,7 @@ def handle_input(text):
73
 
74
  elif output_option == "Audio":
75
  st.success("Translated Audio:")
76
- st.session_state.audio_path = text_to_speech(st.session_state.translated_text, target_lang)
77
  if st.session_state.audio_path:
78
  st.audio(st.session_state.audio_path)
79
 
@@ -89,19 +92,6 @@ def handle_input(text):
89
  except Exception as e:
90
  st.error(f"Translation failed: {str(e)}")
91
 
92
- # Language dictionary for UI
93
- LANGUAGES = {
94
- "English": "en",
95
- "French": "fr",
96
- "Spanish": "es",
97
- "German": "de",
98
- "Chinese": "zh",
99
- "Arabic": "ar",
100
- "Russian": "ru",
101
- "Hindi": "hi",
102
- "Japanese": "ja"
103
- }
104
-
105
  # Run the app
106
  if __name__ == "__main__":
107
  main()
 
1
  import streamlit as st
 
 
 
2
  import os
3
+ import importlib
4
 
5
+ # Set page config as the absolute first Streamlit command
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 or audio for multilingual support.</p>", unsafe_allow_html=True)
 
29
  # Text input
30
  user_text = st.text_area("Enter Text", placeholder="Type or paste your text here...", height=150, key="text_input")
31
  if user_text:
32
+ handle_input(user_text, translation, lang_detect, audio_processor)
33
 
34
  with tab2:
35
  # Audio input
36
  audio_file = st.file_uploader("Upload Audio (MP3/WAV)", type=["mp3", "wav"], key="audio_input")
37
  if audio_file:
38
+ user_text = audio_processor.transcribe_audio(audio_file)
39
  st.write(f"Transcribed Text: {user_text}")
40
+ handle_input(user_text, translation, lang_detect, audio_processor)
41
 
42
  with tab3:
43
  # Document input
 
45
  if doc_file:
46
  user_text = doc_file.read().decode("utf-8")
47
  st.write(f"Document Text: {user_text}")
48
+ handle_input(user_text, translation, lang_detect, audio_processor)
49
 
50
  # Handle input processing
51
+ def handle_input(text, translation, lang_detect, audio_processor):
52
  # Auto-detect source language
53
+ detected_lang = lang_detect.detect_language(text)
54
+ st.session_state.source_lang = detected_lang if detected_lang in translation.LANGUAGES else "English"
55
  st.info(f"Detected Source Language: {st.session_state.source_lang}")
56
 
57
  # Target language selection
58
+ target_lang = st.selectbox("Target Language", list(translation.LANGUAGES.keys()), index=1, key="target_lang")
59
 
60
  # Translate button
61
  if st.button("Translate", key="translate_button"):
62
  with st.spinner("Translating..."):
63
  try:
64
  # Translate the text
65
+ st.session_state.translated_text = translation.translate(text, st.session_state.source_lang, target_lang)
66
 
67
  # Display results in a styled container
68
  st.markdown("<h3 style='color: #2E86C1;'>Translation Result</h3>", unsafe_allow_html=True)
 
76
 
77
  elif output_option == "Audio":
78
  st.success("Translated Audio:")
79
+ st.session_state.audio_path = audio_processor.text_to_speech(st.session_state.translated_text, target_lang)
80
  if st.session_state.audio_path:
81
  st.audio(st.session_state.audio_path)
82
 
 
92
  except Exception as e:
93
  st.error(f"Translation failed: {str(e)}")
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # Run the app
96
  if __name__ == "__main__":
97
  main()