File size: 9,161 Bytes
1c2fdca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import streamlit as st 
from deep_translator import GoogleTranslator
from gtts import gTTS
from pydub import AudioSegment
import tempfile
import os
import speech_recognition as sr
from transformers import pipeline as pl
from streamlit_option_menu import option_menu
import css
from st_audiorec import st_audiorec
import streamlit.components.v1 as components
from voice import transcribe,text_to_speech
from tone import tone
import about

os.environ["STREAMLIT_WATCHER_TYPE"] = "none"
st.session_state.translate=False

st.set_page_config(page_title="VoiceBridge",layout="wide",page_icon="fevicon.png")
Languages = {'afrikaans':'af','albanian':'sq','amharic':'am','arabic':'ar','armenian':'hy','azerbaijani':'az',
             'basque':'eu','belarusian':'be','bengali':'bn','bosnian':'bs','bulgarian':'bg','catalan':'ca',
             'cebuano':'ceb','chichewa':'ny','chinese (simplified)':'zh-cn','chinese (traditional)':'zh-tw',
             'corsican':'co','croatian':'hr','czech':'cs','danish':'da','dutch':'nl','english':'en','esperanto':'eo',
             'estonian':'et','filipino':'tl','finnish':'fi','french':'fr','frisian':'fy','galician':'gl','georgian':'ka',
             'german':'de','greek':'el','gujarati':'gu','haitian creole':'ht','hausa':'ha','hawaiian':'haw','hebrew':'iw',
             'hebrew':'he','hindi':'hi','hmong':'hmn','hungarian':'hu','icelandic':'is','igbo':'ig','indonesian':'id',
             'irish':'ga','italian':'it','japanese':'ja','javanese':'jw','kannada':'kn','kazakh':'kk','khmer':'km',
             'korean':'ko','kurdish (kurmanji)':'ku','kyrgyz':'ky','lao':'lo','latin':'la','latvian':'lv','lithuanian':'lt',
             'luxembourgish':'lb','macedonian':'mk','malagasy':'mg','malay':'ms','malayalam':'ml','maltese':'mt','maori':'mi',
             'marathi':'mr','mongolian':'mn','myanmar (burmese)':'my','nepali':'ne','norwegian':'no','odia':'or','pashto':'ps',
             'persian':'fa','polish':'pl','portuguese':'pt','punjabi':'pa','romanian':'ro','russian':'ru','samoan':'sm',
             'scots gaelic':'gd','serbian':'sr','sesotho':'st','shona':'sn','sindhi':'sd','sinhala':'si','slovak':'sk',
             'slovenian':'sl','somali':'so','spanish':'es','sundanese':'su','swahili':'sw','swedish':'sv','tajik':'tg',
             'tamil':'ta','telugu':'te','thai':'th','turkish':'tr','turkmen':'tk','ukrainian':'uk','urdu':'ur','uyghur':'ug',
             'uzbek':'uz','vietnamese':'vi','welsh':'cy','xhosa':'xh','yiddish':'yi','yoruba':'yo','zulu':'zu'}



st.markdown(
    """

    <style>



    [alt="Logo"] {

        height: 90px;

        width: auto;  

        

    }

    .block-container {

            padding-top: 0rem;

        }

    header { visibility: hidden; }

    </style>

    """,
    unsafe_allow_html=True
)
col1, col2 = st.columns([1, 6])
with col1:
    st.markdown("""

    <style>

    @media only screen and (max-width: 959px) {

        img {

            max-width: 100px;

            height: auto;

        }

    }

    </style>

    """, unsafe_allow_html=True)
    st.logo("logo.png")
with col2:

    css.nev()
    st.markdown('<div class="custom-navbar">', unsafe_allow_html=True)
    selected = option_menu(
        menu_title=None,
        options=["Translate", "Tone", "About"],
        icons=["bi-people-fill", "bi-soundwave", "gear"],
        menu_icon="cast",
        default_index=0,
        orientation="horizontal",
        styles={
            "container": {"padding": "0!important", "background-color": "#0E1117"},
            "icon": {"color": "white"},
            "nav-link": {
                "text-align": "center",
                "margin": "0px",
                "--hover-color": "#204044",
            },
            "nav-link-selected": {"background-color": "#1f6f78"},
        }
    )
    st.markdown('</div>', unsafe_allow_html=True)
with st.container():
    st.markdown("""

    <style>

    .custom-container {

        margin-top: 70px;

    }



    @media (max-width: 767px) {

        .custom-container {

            margin-top: 0px;

        }

    }

    </style>

    <style>

    .middle {

        margin-top: 120px;

    }



    @media (max-width: 767px) {

        .middle {

            margin-top: 0px;

        }

    }

    </style>

""", unsafe_allow_html=True)
    st.markdown('<div class="custom-container">', unsafe_allow_html=True)

    left_col, st.session_state.mid_col, st.session_state.right_col = st.columns([3, 2, 3])
    # left user input
    # ------------------------------------------------------------------------------------------------------------------------------------------------
    if (selected == "Translate" or selected == "Tone"):
        with left_col:
            with st.popover("", icon=":material/tune:"):
                inp = st.selectbox('Choose Input Format', ("Text", "Audio_file", "MIC"))
                st.session_state.inp = inp
            with st.form("my_form"):
                st.markdown("### 🎙️ Input")
                if st.session_state.inp == "Text":
                    st.session_state.text = st.text_area("Enter Text:", help="Type your text here...")
                elif st.session_state.inp == "MIC":
                    st.session_state.uploaded_file = st.audio_input("Record a Voice Message")
                else:
                    st.session_state.uploaded_file = st.file_uploader("Upload an Audio File", type=["mp3", "wav", "m4a"])
                    
                submitted = st.form_submit_button("Submit")
    
        # ------------------------------------------------------------------------------------------------------------------------------------------------
    if selected == "Translate":            
        
        # ------------------------------------------------------------------------------------------------------------------------------------------------
        # center button
        st.markdown('<div class="middle">', unsafe_allow_html=True)
        with st.session_state.mid_col:
            
            css.cicle_button()
            
            if st.button("Translate"):
                st.session_state.translate=False
                st.session_state.translate=True
        st.markdown('</div>', unsafe_allow_html=True)


        # ------------------------------------------------------------------------------------------------------------------------------------------------
        # Right Output
        with st.session_state.right_col:
            
            with st.popover("", icon=":material/tune:"):
                out_type = st.selectbox('Choose Input Format', ("Text", "Voice", "Both"))
                st.session_state.out_type = out_type
            with st.form("output"):
                st.markdown("### 🔉 Voice Output")
                option2 = st.selectbox('Select Output Language', list(Languages.keys()))
                value2 = Languages[option2]
                
                if st.session_state.translate:
                    c1,c2=st.columns(2)
                    if st.session_state.inp != "Text":
                        st.session_state.text = transcribe(st.session_state.uploaded_file)
                        
                    translated_text = GoogleTranslator(target=value2).translate(st.session_state.text)
                    
                    if st.session_state.out_type == "Text":
                        st.text_area("Translated Text:", translated_text, height=100)

                    elif st.session_state.out_type == "Voice":
                        if translated_text.strip():
                            audio_file = text_to_speech(translated_text, value2)
                        else:
                            c2.warning("Please enter text before converting.")
                        st.audio(audio_file, format='audio/mp3', autoplay=True)

                    else:
                        if translated_text.strip():
                            audio_file = text_to_speech(translated_text, value2)
                        else:
                            c2.warning("Please enter text before converting.")
                        with c1.popover("", icon=":material/library_books:"):
                            st.text_area("Translated Text:", translated_text, height=100)
                        c2.audio(audio_file, format='audio/mp3', autoplay=True)

                reset = st.form_submit_button("Reset ↻ ")
                if reset:
                    st.session_state.translate= False
        # Optional: Add some styling
        st.markdown("""

                    

        <style>

        body {

            background-color: #0e1117;

            color: white;

        }

        .stButton>button {

            background-color: teal;

            color: white;

        }

        </style>

    """, unsafe_allow_html=True)
        st.markdown('</div>', unsafe_allow_html=True)


    if selected == "Tone":
        tone()
    if selected == "About":
        about.about_page()