ayushkanha commited on
Commit
a8445bf
·
verified ·
1 Parent(s): 35c8e34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +211 -215
app.py CHANGED
@@ -1,216 +1,212 @@
1
- import streamlit as st
2
- from deep_translator import GoogleTranslator
3
- from gtts import gTTS
4
- from pydub import AudioSegment
5
- import tempfile
6
- import os
7
- import speech_recognition as sr
8
- from transformers import pipeline as pl
9
- from streamlit_option_menu import option_menu
10
- import css
11
- from st_audiorec import st_audiorec
12
- import streamlit.components.v1 as components
13
- from voice import transcribe,text_to_speech
14
- from tone import tone
15
- import about
16
-
17
- os.environ["STREAMLIT_WATCHER_TYPE"] = "none"
18
- st.session_state.translate=False
19
-
20
- st.set_page_config(page_title="VoiceBridge",layout="wide",page_icon="fevicon.png")
21
- Languages = {'afrikaans':'af','albanian':'sq','amharic':'am','arabic':'ar','armenian':'hy','azerbaijani':'az',
22
- 'basque':'eu','belarusian':'be','bengali':'bn','bosnian':'bs','bulgarian':'bg','catalan':'ca',
23
- 'cebuano':'ceb','chichewa':'ny','chinese (simplified)':'zh-cn','chinese (traditional)':'zh-tw',
24
- 'corsican':'co','croatian':'hr','czech':'cs','danish':'da','dutch':'nl','english':'en','esperanto':'eo',
25
- 'estonian':'et','filipino':'tl','finnish':'fi','french':'fr','frisian':'fy','galician':'gl','georgian':'ka',
26
- 'german':'de','greek':'el','gujarati':'gu','haitian creole':'ht','hausa':'ha','hawaiian':'haw','hebrew':'iw',
27
- 'hebrew':'he','hindi':'hi','hmong':'hmn','hungarian':'hu','icelandic':'is','igbo':'ig','indonesian':'id',
28
- 'irish':'ga','italian':'it','japanese':'ja','javanese':'jw','kannada':'kn','kazakh':'kk','khmer':'km',
29
- 'korean':'ko','kurdish (kurmanji)':'ku','kyrgyz':'ky','lao':'lo','latin':'la','latvian':'lv','lithuanian':'lt',
30
- 'luxembourgish':'lb','macedonian':'mk','malagasy':'mg','malay':'ms','malayalam':'ml','maltese':'mt','maori':'mi',
31
- 'marathi':'mr','mongolian':'mn','myanmar (burmese)':'my','nepali':'ne','norwegian':'no','odia':'or','pashto':'ps',
32
- 'persian':'fa','polish':'pl','portuguese':'pt','punjabi':'pa','romanian':'ro','russian':'ru','samoan':'sm',
33
- 'scots gaelic':'gd','serbian':'sr','sesotho':'st','shona':'sn','sindhi':'sd','sinhala':'si','slovak':'sk',
34
- 'slovenian':'sl','somali':'so','spanish':'es','sundanese':'su','swahili':'sw','swedish':'sv','tajik':'tg',
35
- 'tamil':'ta','telugu':'te','thai':'th','turkish':'tr','turkmen':'tk','ukrainian':'uk','urdu':'ur','uyghur':'ug',
36
- 'uzbek':'uz','vietnamese':'vi','welsh':'cy','xhosa':'xh','yiddish':'yi','yoruba':'yo','zulu':'zu'}
37
-
38
-
39
-
40
- st.markdown(
41
- """
42
- <style>
43
-
44
- [alt="Logo"] {
45
- height: 90px;
46
- width: auto;
47
-
48
- }
49
- .block-container {
50
- padding-top: 0rem;
51
- }
52
- header { visibility: hidden; }
53
- </style>
54
- """,
55
- unsafe_allow_html=True
56
- )
57
- col1, col2 = st.columns([1, 6])
58
- with col1:
59
- st.markdown("""
60
- <style>
61
- @media only screen and (max-width: 959px) {
62
- img {
63
- max-width: 100px;
64
- height: auto;
65
- }
66
- }
67
- </style>
68
- """, unsafe_allow_html=True)
69
- st.logo("logo.png")
70
- with col2:
71
-
72
- css.nev()
73
- st.markdown('<div class="custom-navbar">', unsafe_allow_html=True)
74
- selected = option_menu(
75
- menu_title=None,
76
- options=["Translate", "Tone", "About"],
77
- icons=["bi-people-fill", "bi-soundwave", "gear"],
78
- menu_icon="cast",
79
- default_index=0,
80
- orientation="horizontal",
81
- styles={
82
- "container": {"padding": "0!important", "background-color": "#0E1117"},
83
- "icon": {"color": "white"},
84
- "nav-link": {
85
- "text-align": "center",
86
- "margin": "0px",
87
- "--hover-color": "#204044",
88
- },
89
- "nav-link-selected": {"background-color": "#1f6f78"},
90
- }
91
- )
92
- st.markdown('</div>', unsafe_allow_html=True)
93
- with st.container():
94
- st.markdown("""
95
- <style>
96
- .custom-container {
97
- margin-top: 70px;
98
- }
99
-
100
- @media (max-width: 767px) {
101
- .custom-container {
102
- margin-top: 0px;
103
- }
104
- }
105
- </style>
106
- <style>
107
- .middle {
108
- margin-top: 120px;
109
- }
110
-
111
- @media (max-width: 767px) {
112
- .middle {
113
- margin-top: 0px;
114
- }
115
- }
116
- </style>
117
- """, unsafe_allow_html=True)
118
- st.markdown('<div class="custom-container">', unsafe_allow_html=True)
119
-
120
- left_col, st.session_state.mid_col, st.session_state.right_col = st.columns([3, 2, 3])
121
- # left user input
122
- # ------------------------------------------------------------------------------------------------------------------------------------------------
123
- if (selected == "Translate" or selected == "Tone"):
124
- with left_col:
125
- with st.popover("", icon=":material/tune:"):
126
- inp = st.selectbox('Choose Input Format', ("Text", "Audio_file", "MIC"))
127
- st.session_state.inp = inp
128
- with st.form("my_form"):
129
- st.markdown("### 🎙️ Input")
130
- if st.session_state.inp == "Text":
131
- st.session_state.text = st.text_area("Enter Text:", help="Type your text here...")
132
- elif st.session_state.inp == "MIC":
133
- st.session_state.uploaded_file = st.audio_input("Record a Voice Message")
134
- else:
135
- st.session_state.uploaded_file = st.file_uploader("Upload an Audio File", type=["mp3", "wav", "m4a"])
136
-
137
- submitted = st.form_submit_button("Submit")
138
-
139
- # ------------------------------------------------------------------------------------------------------------------------------------------------
140
- if selected == "Translate":
141
-
142
- # ------------------------------------------------------------------------------------------------------------------------------------------------
143
- # center button
144
- st.markdown('<div class="middle">', unsafe_allow_html=True)
145
- with st.session_state.mid_col:
146
-
147
- css.cicle_button()
148
-
149
- if st.button("Translate"):
150
- st.session_state.translate=False
151
- st.session_state.translate=True
152
- st.markdown('</div>', unsafe_allow_html=True)
153
-
154
-
155
- # ------------------------------------------------------------------------------------------------------------------------------------------------
156
- # Right Output
157
- with st.session_state.right_col:
158
-
159
- with st.popover("", icon=":material/tune:"):
160
- out_type = st.selectbox('Choose Input Format', ("Text", "Voice", "Both"))
161
- st.session_state.out_type = out_type
162
- with st.form("output"):
163
- st.markdown("### 🔉 Voice Output")
164
- option2 = st.selectbox('Select Output Language', list(Languages.keys()))
165
- value2 = Languages[option2]
166
-
167
- if st.session_state.translate:
168
- c1,c2=st.columns(2)
169
- if st.session_state.inp != "Text":
170
- st.session_state.text = transcribe(st.session_state.uploaded_file)
171
-
172
- translated_text = GoogleTranslator(target=value2).translate(st.session_state.text)
173
-
174
- if st.session_state.out_type == "Text":
175
- st.text_area("Translated Text:", translated_text, height=100)
176
-
177
- elif st.session_state.out_type == "Voice":
178
- if translated_text.strip():
179
- audio_file = text_to_speech(translated_text, value2)
180
- else:
181
- c2.warning("Please enter text before converting.")
182
- st.audio(audio_file, format='audio/mp3', autoplay=True)
183
-
184
- else:
185
- if translated_text.strip():
186
- audio_file = text_to_speech(translated_text, value2)
187
- else:
188
- c2.warning("Please enter text before converting.")
189
- with c1.popover("", icon=":material/library_books:"):
190
- st.text_area("Translated Text:", translated_text, height=100)
191
- c2.audio(audio_file, format='audio/mp3', autoplay=True)
192
-
193
- reset = st.form_submit_button("Reset ↻ ")
194
- if reset:
195
- st.session_state.translate= False
196
- # Optional: Add some styling
197
- st.markdown("""
198
-
199
- <style>
200
- body {
201
- background-color: #0e1117;
202
- color: white;
203
- }
204
- .stButton>button {
205
- background-color: teal;
206
- color: white;
207
- }
208
- </style>
209
- """, unsafe_allow_html=True)
210
- st.markdown('</div>', unsafe_allow_html=True)
211
-
212
-
213
- if selected == "Tone":
214
- tone()
215
- if selected == "About":
216
  about.about_page()
 
1
+ import streamlit as st
2
+ from deep_translator import GoogleTranslator
3
+
4
+ import os
5
+
6
+ from streamlit_option_menu import option_menu
7
+ import css
8
+
9
+ from voice import transcribe,text_to_speech
10
+ from tone import tone
11
+ import about
12
+
13
+ os.environ["STREAMLIT_WATCHER_TYPE"] = "none"
14
+ st.session_state.translate=False
15
+
16
+ st.set_page_config(page_title="VoiceBridge",layout="wide",page_icon="fevicon.png")
17
+ Languages = {'afrikaans':'af','albanian':'sq','amharic':'am','arabic':'ar','armenian':'hy','azerbaijani':'az',
18
+ 'basque':'eu','belarusian':'be','bengali':'bn','bosnian':'bs','bulgarian':'bg','catalan':'ca',
19
+ 'cebuano':'ceb','chichewa':'ny','chinese (simplified)':'zh-cn','chinese (traditional)':'zh-tw',
20
+ 'corsican':'co','croatian':'hr','czech':'cs','danish':'da','dutch':'nl','english':'en','esperanto':'eo',
21
+ 'estonian':'et','filipino':'tl','finnish':'fi','french':'fr','frisian':'fy','galician':'gl','georgian':'ka',
22
+ 'german':'de','greek':'el','gujarati':'gu','haitian creole':'ht','hausa':'ha','hawaiian':'haw','hebrew':'iw',
23
+ 'hebrew':'he','hindi':'hi','hmong':'hmn','hungarian':'hu','icelandic':'is','igbo':'ig','indonesian':'id',
24
+ 'irish':'ga','italian':'it','japanese':'ja','javanese':'jw','kannada':'kn','kazakh':'kk','khmer':'km',
25
+ 'korean':'ko','kurdish (kurmanji)':'ku','kyrgyz':'ky','lao':'lo','latin':'la','latvian':'lv','lithuanian':'lt',
26
+ 'luxembourgish':'lb','macedonian':'mk','malagasy':'mg','malay':'ms','malayalam':'ml','maltese':'mt','maori':'mi',
27
+ 'marathi':'mr','mongolian':'mn','myanmar (burmese)':'my','nepali':'ne','norwegian':'no','odia':'or','pashto':'ps',
28
+ 'persian':'fa','polish':'pl','portuguese':'pt','punjabi':'pa','romanian':'ro','russian':'ru','samoan':'sm',
29
+ 'scots gaelic':'gd','serbian':'sr','sesotho':'st','shona':'sn','sindhi':'sd','sinhala':'si','slovak':'sk',
30
+ 'slovenian':'sl','somali':'so','spanish':'es','sundanese':'su','swahili':'sw','swedish':'sv','tajik':'tg',
31
+ 'tamil':'ta','telugu':'te','thai':'th','turkish':'tr','turkmen':'tk','ukrainian':'uk','urdu':'ur','uyghur':'ug',
32
+ 'uzbek':'uz','vietnamese':'vi','welsh':'cy','xhosa':'xh','yiddish':'yi','yoruba':'yo','zulu':'zu'}
33
+
34
+
35
+
36
+ st.markdown(
37
+ """
38
+ <style>
39
+
40
+ [alt="Logo"] {
41
+ height: 90px;
42
+ width: auto;
43
+
44
+ }
45
+ .block-container {
46
+ padding-top: 0rem;
47
+ }
48
+ header { visibility: hidden; }
49
+ </style>
50
+ """,
51
+ unsafe_allow_html=True
52
+ )
53
+ col1, col2 = st.columns([1, 6])
54
+ with col1:
55
+ st.markdown("""
56
+ <style>
57
+ @media only screen and (max-width: 959px) {
58
+ img {
59
+ max-width: 100px;
60
+ height: auto;
61
+ }
62
+ }
63
+ </style>
64
+ """, unsafe_allow_html=True)
65
+ st.logo("logo.png")
66
+ with col2:
67
+
68
+ css.nev()
69
+ st.markdown('<div class="custom-navbar">', unsafe_allow_html=True)
70
+ selected = option_menu(
71
+ menu_title=None,
72
+ options=["Translate", "Tone", "About"],
73
+ icons=["bi-people-fill", "bi-soundwave", "gear"],
74
+ menu_icon="cast",
75
+ default_index=0,
76
+ orientation="horizontal",
77
+ styles={
78
+ "container": {"padding": "0!important", "background-color": "#0E1117"},
79
+ "icon": {"color": "white"},
80
+ "nav-link": {
81
+ "text-align": "center",
82
+ "margin": "0px",
83
+ "--hover-color": "#204044",
84
+ },
85
+ "nav-link-selected": {"background-color": "#1f6f78"},
86
+ }
87
+ )
88
+ st.markdown('</div>', unsafe_allow_html=True)
89
+ with st.container():
90
+ st.markdown("""
91
+ <style>
92
+ .custom-container {
93
+ margin-top: 70px;
94
+ }
95
+
96
+ @media (max-width: 767px) {
97
+ .custom-container {
98
+ margin-top: 0px;
99
+ }
100
+ }
101
+ </style>
102
+ <style>
103
+ .middle {
104
+ margin-top: 120px;
105
+ }
106
+
107
+ @media (max-width: 767px) {
108
+ .middle {
109
+ margin-top: 0px;
110
+ }
111
+ }
112
+ </style>
113
+ """, unsafe_allow_html=True)
114
+ st.markdown('<div class="custom-container">', unsafe_allow_html=True)
115
+
116
+ left_col, st.session_state.mid_col, st.session_state.right_col = st.columns([3, 2, 3])
117
+ # left user input
118
+ # ------------------------------------------------------------------------------------------------------------------------------------------------
119
+ if (selected == "Translate" or selected == "Tone"):
120
+ with left_col:
121
+ with st.popover("", icon=":material/tune:"):
122
+ inp = st.selectbox('Choose Input Format', ("Text", "Audio_file", "MIC"))
123
+ st.session_state.inp = inp
124
+ with st.form("my_form"):
125
+ st.markdown("### 🎙️ Input")
126
+ if st.session_state.inp == "Text":
127
+ st.session_state.text = st.text_area("Enter Text:", help="Type your text here...")
128
+ elif st.session_state.inp == "MIC":
129
+ st.session_state.uploaded_file = st.audio_input("Record a Voice Message")
130
+ else:
131
+ st.session_state.uploaded_file = st.file_uploader("Upload an Audio File", type=["mp3", "wav", "m4a"])
132
+
133
+ submitted = st.form_submit_button("Submit")
134
+
135
+ # ------------------------------------------------------------------------------------------------------------------------------------------------
136
+ if selected == "Translate":
137
+
138
+ # ------------------------------------------------------------------------------------------------------------------------------------------------
139
+ # center button
140
+ st.markdown('<div class="middle">', unsafe_allow_html=True)
141
+ with st.session_state.mid_col:
142
+
143
+ css.cicle_button()
144
+
145
+ if st.button("Translate"):
146
+ st.session_state.translate=False
147
+ st.session_state.translate=True
148
+ st.markdown('</div>', unsafe_allow_html=True)
149
+
150
+
151
+ # ------------------------------------------------------------------------------------------------------------------------------------------------
152
+ # Right Output
153
+ with st.session_state.right_col:
154
+
155
+ with st.popover("", icon=":material/tune:"):
156
+ out_type = st.selectbox('Choose Input Format', ("Text", "Voice", "Both"))
157
+ st.session_state.out_type = out_type
158
+ with st.form("output"):
159
+ st.markdown("### 🔉 Voice Output")
160
+ option2 = st.selectbox('Select Output Language', list(Languages.keys()))
161
+ value2 = Languages[option2]
162
+
163
+ if st.session_state.translate:
164
+ c1,c2=st.columns(2)
165
+ if st.session_state.inp != "Text":
166
+ st.session_state.text = transcribe(st.session_state.uploaded_file)
167
+
168
+ translated_text = GoogleTranslator(target=value2).translate(st.session_state.text)
169
+
170
+ if st.session_state.out_type == "Text":
171
+ st.text_area("Translated Text:", translated_text, height=100)
172
+
173
+ elif st.session_state.out_type == "Voice":
174
+ if translated_text.strip():
175
+ audio_file = text_to_speech(translated_text, value2)
176
+ else:
177
+ c2.warning("Please enter text before converting.")
178
+ st.audio(audio_file, format='audio/mp3', autoplay=True)
179
+
180
+ else:
181
+ if translated_text.strip():
182
+ audio_file = text_to_speech(translated_text, value2)
183
+ else:
184
+ c2.warning("Please enter text before converting.")
185
+ with c1.popover("", icon=":material/library_books:"):
186
+ st.text_area("Translated Text:", translated_text, height=100)
187
+ c2.audio(audio_file, format='audio/mp3', autoplay=True)
188
+
189
+ reset = st.form_submit_button("Reset ")
190
+ if reset:
191
+ st.session_state.translate= False
192
+ # Optional: Add some styling
193
+ st.markdown("""
194
+
195
+ <style>
196
+ body {
197
+ background-color: #0e1117;
198
+ color: white;
199
+ }
200
+ .stButton>button {
201
+ background-color: teal;
202
+ color: white;
203
+ }
204
+ </style>
205
+ """, unsafe_allow_html=True)
206
+ st.markdown('</div>', unsafe_allow_html=True)
207
+
208
+
209
+ if selected == "Tone":
210
+ tone()
211
+ if selected == "About":
 
 
 
 
212
  about.about_page()