Krishna086 commited on
Commit
8040f65
·
verified ·
1 Parent(s): 684aa3e

Input updated for app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -35
app.py CHANGED
@@ -1,45 +1,105 @@
1
  import streamlit as st
2
- from translation import translate, LANGUAGES
 
 
 
3
 
4
- # Main function for the app
 
 
 
5
  def main():
6
- # Set page title and description
7
- st.title("Multilingual Translation App")
8
- st.write("Translate text for multilingual support (e.g., chatbots or customer service).")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Dropdowns for selecting languages
11
- source_lang = st.selectbox("Source Language", list(LANGUAGES.keys()), index=0)
12
- target_lang = st.selectbox("Target Language", list(LANGUAGES.keys()), index=1)
13
-
14
- # Text input area
15
- user_input = st.text_area("Input Text", placeholder="Enter text to translate...", height=150)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  # Translate button
18
- if st.button("Translate"):
19
- if user_input:
20
- # Show a spinner while translating
21
- with st.spinner("Translating..."):
22
- try:
23
- # Call the translate function from translation.py
24
- result = translate(user_input, source_lang, target_lang)
 
 
 
 
 
 
25
  st.success("Translated Text:")
26
- st.write(result)
27
-
28
- # Show footer only after translation
29
- st.markdown("""
30
- <p style="font-size: small; color: grey; text-align: center;">
31
- Developed By: Krishna Prakash
32
- <a href="https://www.linkedin.com/in/krishnaprakash-profile/" target="_blank">
33
- <img src="https://img.icons8.com/ios-filled/30/0077b5/linkedin.png" alt="LinkedIn" style="vertical-align: middle; margin: 0 5px;"/>
34
- </a>
35
- </p>
36
- """, unsafe_allow_html=True)
37
- except Exception as e:
38
- # Show error if translation fails
39
- st.error(f"Translation failed: {str(e)}")
40
- else:
41
- # Warn if no text is entered
42
- st.warning("Please enter text to translate!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # Run the app
45
  if __name__ == "__main__":
 
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 for better UI
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)
15
+
16
+ # Tabs for input methods
17
+ tab1, tab2, tab3 = st.tabs(["Text Input", "Audio Input", "Document Upload"])
18
+
19
+ # Initialize session state for results
20
+ if 'translated_text' not in st.session_state:
21
+ st.session_state.translated_text = None
22
+ st.session_state.audio_path = None
23
+ st.session_state.source_lang = "English"
24
+
25
+ with tab1:
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
41
+ doc_file = st.file_uploader("Upload Document (TXT)", type=["txt"], key="doc_input")
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)
66
+
67
+ # Output options: Text and Audio
68
+ output_option = st.radio("Output Format", ["Text", "Audio"], key="output_option")
69
+
70
+ if output_option == "Text":
71
  st.success("Translated Text:")
72
+ st.write(st.session_state.translated_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
+ st.audio(st.session_state.audio_path)
78
+
79
+ # Show footer after translation
80
+ st.markdown("""
81
+ <p style='font-size: small; color: grey; text-align: center; margin-top: 20px;'>
82
+ Developed By: Krishna Prakash
83
+ <a href='https://www.linkedin.com/in/krishnaprakash-profile/' target='_blank'>
84
+ <img src='https://img.icons8.com/ios-filled/30/0077b5/linkedin.png' alt='LinkedIn' style='vertical-align: middle; margin: 0 5px;'/>
85
+ </a>
86
+ </p>
87
+ """, unsafe_allow_html=True)
88
+ except Exception as e:
89
+ st.error(f"Translation failed: {str(e)}")
90
+
91
+ # Language dictionary for UI
92
+ LANGUAGES = {
93
+ "English": "en",
94
+ "French": "fr",
95
+ "Spanish": "es",
96
+ "German": "de",
97
+ "Chinese": "zh",
98
+ "Arabic": "ar",
99
+ "Russian": "ru",
100
+ "Hindi": "hi",
101
+ "Japanese": "ja"
102
+ }
103
 
104
  # Run the app
105
  if __name__ == "__main__":