harupurito commited on
Commit
749930a
1 Parent(s): 2a4fdd8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +340 -0
app.py ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import speech_recognition as sr
3
+ from googletrans import Translator
4
+ from gtts import gTTS
5
+ from playsound import playsound
6
+ from PIL import Image
7
+ from streamlit_mic_recorder import mic_recorder,speech_to_text
8
+ from transformers import AutoTokenizer, M2M100ForConditionalGeneration
9
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
10
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
11
+ # import logging
12
+ # import logging.handlers
13
+ # import queue
14
+ # import threading
15
+ # import time
16
+ # import urllib.request
17
+ # import os
18
+ # from collections import deque
19
+ # from pathlib import Path
20
+ # from typing import List
21
+
22
+ # import av
23
+ # import numpy as np
24
+ # import pydub
25
+ # import streamlit as st
26
+ # from twilio.rest import Client
27
+
28
+ # from streamlit_webrtc import WebRtcMode, webrtc_streamer
29
+ #from streamlit_mic_recorder import mic_recorder,speech_to_text
30
+ # Initialize the recognizer
31
+ recognizer = sr.Recognizer()
32
+
33
+ # Initialize the translator
34
+ translator = Translator()
35
+
36
+ # Language dictionary
37
+ language_dict = {
38
+ 'bengali': 'bn',
39
+ 'english': 'en',
40
+ 'gujarati': 'gu',
41
+ 'hindi': 'hi',
42
+ 'malayalam': 'ml',
43
+ 'marathi': 'mr',
44
+ 'nepali': 'ne',
45
+ 'odia': 'or',
46
+ 'punjabi': 'pa',
47
+ 'tamil': 'ta',
48
+ 'telugu': 'te',
49
+ 'urdu': 'ur',
50
+ }
51
+
52
+ # language_dict = {
53
+ # 'afrikaans': 'af',
54
+ # 'albanian': 'sq',
55
+ # 'amharic': 'am',
56
+ # 'arabic': 'ar',
57
+ # 'armenian': 'hy',
58
+ # 'azerbaijani': 'az',
59
+ # 'basque': 'eu',
60
+ # 'belarusian': 'be',
61
+ # 'bengali': 'bn',
62
+ # 'bosnian': 'bs',
63
+ # 'bulgarian': 'bg',
64
+ # 'catalan': 'ca',
65
+ # 'cebuano': 'ceb',
66
+ # 'chichewa': 'ny',
67
+ # 'chinese (simplified)': 'zh',
68
+ # 'chinese (traditional)': 'zh-TW',
69
+ # 'corsican': 'co',
70
+ # 'croatian': 'hr',
71
+ # 'czech': 'cs',
72
+ # 'danish': 'da',
73
+ # 'dutch': 'nl',
74
+ # 'english': 'en',
75
+ # 'esperanto': 'eo',
76
+ # 'estonian': 'et',
77
+ # 'filipino': 'tl',
78
+ # 'finnish': 'fi',
79
+ # 'french': 'fr',
80
+ # 'frisian': 'fy',
81
+ # 'galician': 'gl',
82
+ # 'georgian': 'ka',
83
+ # 'german': 'de',
84
+ # 'greek': 'el',
85
+ # 'gujarati': 'gu',
86
+ # 'haitian creole': 'ht',
87
+ # 'hausa': 'ha',
88
+ # 'hawaiian': 'haw',
89
+ # 'hebrew': 'he',
90
+ # 'hindi': 'hi',
91
+ # 'hmong': 'hmn',
92
+ # 'hungarian': 'hu',
93
+ # 'icelandic': 'is',
94
+ # 'igbo': 'ig',
95
+ # 'indonesian': 'id',
96
+ # 'irish': 'ga',
97
+ # 'italian': 'it',
98
+ # 'japanese': 'ja',
99
+ # 'javanese': 'jv',
100
+ # 'kannada': 'kn',
101
+ # 'kazakh': 'kk',
102
+ # 'khmer': 'km',
103
+ # 'kinyarwanda': 'rw',
104
+ # 'korean': 'ko',
105
+ # 'kurdish': 'ku',
106
+ # 'kyrgyz': 'ky',
107
+ # 'lao': 'lo',
108
+ # 'latin': 'la',
109
+ # 'latvian': 'lv',
110
+ # 'lithuanian': 'lt',
111
+ # 'luxembourgish': 'lb',
112
+ # 'macedonian': 'mk',
113
+ # 'malagasy': 'mg',
114
+ # 'malay': 'ms',
115
+ # 'malayalam': 'ml',
116
+ # 'maltese': 'mt',
117
+ # 'maori': 'mi',
118
+ # 'marathi': 'mr',
119
+ # 'mongolian': 'mn',
120
+ # 'myanmar (burmese)': 'my',
121
+ # 'nepali': 'ne',
122
+ # 'norwegian': 'no',
123
+ # 'odia': 'or',
124
+ # 'pashto': 'ps',
125
+ # 'persian': 'fa',
126
+ # 'polish': 'pl',
127
+ # 'portuguese': 'pt',
128
+ # 'punjabi': 'pa',
129
+ # 'romanian': 'ro',
130
+ # 'russian': 'ru',
131
+ # 'samoan': 'sm',
132
+ # 'scots gaelic': 'gd',
133
+ # 'serbian': 'sr',
134
+ # 'sesotho': 'st',
135
+ # 'shona': 'sn',
136
+ # 'sindhi': 'sd',
137
+ # 'sinhala': 'si',
138
+ # 'slovak': 'sk',
139
+ # 'slovenian': 'sl',
140
+ # 'somali': 'so',
141
+ # 'spanish': 'es',
142
+ # 'sundanese': 'su',
143
+ # 'swahili': 'sw',
144
+ # 'swedish': 'sv',
145
+ # 'tajik': 'tg',
146
+ # 'tamil': 'ta',
147
+ # 'telugu': 'te',
148
+ # 'thai': 'th',
149
+ # 'turkish': 'tr',
150
+ # 'ukrainian': 'uk',
151
+ # 'urdu': 'ur',
152
+ # 'uyghur': 'ug',
153
+ # 'uzbek': 'uz',
154
+ # 'vietnamese': 'vi',
155
+ # 'welsh': 'cy',
156
+ # 'xhosa': 'xh',
157
+ # 'yiddish': 'yi',
158
+ # 'yoruba': 'yo',
159
+ # 'zulu': 'zu',
160
+ # }
161
+
162
+
163
+ #img=Image.open('finallogo.jpg')
164
+
165
+ # col1, col2 = st.columns([1,3])
166
+
167
+ # #with col1:
168
+ # #st.image(img, width=220)
169
+
170
+ # with col2:
171
+ # custom_theme = {
172
+ # "theme": {
173
+ # "primaryColor": "#000000",
174
+ # "backgroundColor": "#89939E",
175
+ # "secondaryBackgroundColor": "#262730",
176
+ # "textColor": "#FFFFFF",
177
+ # "font": "Serif"
178
+ # }
179
+ # }
180
+
181
+ # # Apply custom theme to Streamlit
182
+ # st.markdown(
183
+ # f"""
184
+ # <style>
185
+ # :root {{
186
+ # --primary-color: {custom_theme["theme"]["primaryColor"]};
187
+ # --background-color: {custom_theme["theme"]["backgroundColor"]};
188
+ # --secondary-background-color: {custom_theme["theme"]["secondaryBackgroundColor"]};
189
+ # --text-color: {custom_theme["theme"]["textColor"]};
190
+ # --font: {custom_theme["theme"]["font"]};
191
+ # }}
192
+ # </style>
193
+ # """,
194
+ # unsafe_allow_html=True
195
+ # )
196
+
197
+
198
+
199
+ # st.title("AMPYtranslate")
200
+
201
+ # st.header("")
202
+ # hide_st_style = """
203
+ # <style>
204
+ # #Mainmenu{visibility: hidden;}
205
+ # footer{visibility:hidden; }
206
+ # </style>
207
+ # """
208
+
209
+ # st.markdown(hide_st_style, unsafe_allow_html=True)
210
+
211
+
212
+
213
+
214
+ # @st.cache_data # type: ignore
215
+ # def get_ice_servers():
216
+ # """Use Twilio's TURN server because Streamlit Community Cloud has changed
217
+ # its infrastructure and WebRTC connection cannot be established without TURN server now. # noqa: E501
218
+ # We considered Open Relay Project (https://www.metered.ca/tools/openrelay/) too,
219
+ # but it is not stable and hardly works as some people reported like https://github.com/aiortc/aiortc/issues/832#issuecomment-1482420656 # noqa: E501
220
+ # See https://github.com/whitphx/streamlit-webrtc/issues/1213
221
+ # """
222
+
223
+ # # Ref: https://www.twilio.com/docs/stun-turn/api
224
+ # try:
225
+ # account_sid = "ACbea2776671d07a28bfa473b522b609fb"
226
+ # auth_token = "f9d226593c16124f8eaed4b8d2d5397b"
227
+ # except KeyError:
228
+ # logger.warning(
229
+ # "Twilio credentials are not set. Fallback to a free STUN server from Google." # noqa: E501
230
+ # )
231
+ # return [{"urls": ["stun:stun.l.google.com:19302"]}]
232
+
233
+ # client = Client(account_sid, auth_token)
234
+
235
+ # token = client.tokens.create()
236
+
237
+ # return token.ice_servers
238
+
239
+ # Function to recognize speech
240
+ # def recognize_speech(prompt, language='en'):
241
+ # try:
242
+ # # audio_source = mic_recorder(
243
+ # # start_prompt="Start recording",
244
+ # # stop_prompt="Stop recording",
245
+ # # just_once=False,
246
+ # # use_container_width=False,
247
+ # # key='recorder')
248
+ # with sr.Microphone() as source:
249
+ # st.write(prompt)
250
+ # # Use recognizer to adjust to ambient noise
251
+ # recognizer.adjust_for_ambient_noise(source, duration=1)
252
+ # audio = recognizer.listen(source)
253
+ # st.write("Recognizing...")
254
+
255
+ # # Recognize the speech in the specified language
256
+ # spoken_text = recognizer.recognize_google(audio, language=language)
257
+ # return spoken_text
258
+ # except sr.UnknownValueError:
259
+ # st.write("Sorry, I couldn't understand your speech.")
260
+
261
+
262
+
263
+ # Function to translate speech
264
+ def translate_speech():
265
+ # Get source language name and convert it to code
266
+ #source_language_name = recognize_speech("Please speak the source language name (e.g., 'English'): ")
267
+
268
+
269
+
270
+ # st.write("Record your voice, and play the recorded audio:")
271
+ # audio=mic_recorder(start_prompt="⏺️",stop_prompt="⏹️",key='recorder')
272
+
273
+ # if audio:
274
+ # st.audio(audio['bytes'])
275
+
276
+ source_language_name = st.selectbox('Please input the source language',language_dict)
277
+ source_language = language_dict[source_language_name]
278
+ target_language_name = st.selectbox('Please input the target language',language_dict)
279
+ target_language = language_dict[target_language_name]
280
+ #state=st.session_state
281
+
282
+ # if 'text_received' not in state:
283
+ # state.text_received=[]
284
+
285
+ c1,c2=st.columns(2)
286
+ with c1:
287
+ st.write("Convert speech to text:")
288
+ with c2:
289
+ text=speech_to_text(language=source_language,use_container_width=True,just_once=True,key='STT')
290
+
291
+ # if text:
292
+ # state.text_received.append(text)
293
+
294
+ # for text in state.text_received:
295
+ # st.text(text)
296
+ # Get sentence to translate
297
+ sentence = text
298
+ # while sentence is None:
299
+ # sentence = recognize_speech("Please speak the sentence to translate:", language=source_language)
300
+
301
+ # Get destination language name and convert it to code
302
+ # destination_language_name = recognize_speech("Please speak the destination language name (e.g., 'French'): ")
303
+ # destination_language = language_dict.get(destination_language_name.lower(), 'en')
304
+
305
+ # Translate the text to the desired language
306
+ #translated_text = translator.translate(sentence, src=source_language, dest=target_language)
307
+
308
+ nllb_langs = {'hindi':'hin_Deva',
309
+ 'english':'eng_Latn',
310
+ 'punjabi':'pan_Guru',
311
+ 'odia':'ory_Orya',
312
+ 'bengali':'ben_Beng',
313
+ 'telugu':'tel_Tulu',
314
+ 'tamil':'tam_Taml',
315
+ 'nepali':'npi_Deva',
316
+ 'marathi':'mar_Deva',
317
+ 'malayalam':'mal_Mlym',
318
+ 'kannada':'kan_Knda',
319
+ 'gujarati':'guj_Gujr',
320
+ 'afrikaans':'hin_Deva'
321
+ }
322
+
323
+ translator = pipeline('translation', model=AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M"), tokenizer=AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M"), src_lang=nllb_langs[source_language_name], tgt_lang=nllb_langs[target_language_name], max_length = 4000)
324
+ text_to_translate = text
325
+ translated_text = translator(text_to_translate)[0]['translation_text']
326
+
327
+ st.write(f"Source Language: {source_language_name}")
328
+ st.write(f"Sentence: {sentence}")
329
+ st.write(f"Destination Language: {target_language_name}")
330
+ st.write(f"Translated Text: {translated_text}")
331
+
332
+ # Using Google-Text-to-Speech to speak the translated text
333
+ speak = gTTS(text=translated_text, lang=target_language, slow=False)
334
+ speak.save("translated_voice.mp3")
335
+
336
+ # Play the translated voice
337
+ playsound('translated_voice.mp3')
338
+
339
+ #if st.button(" CLICK HERE TO TRANSLATE "):
340
+ translate_speech()