shamik commited on
Commit
e61ddf0
1 Parent(s): fca9772

Added the required files to run the app.

Browse files
Files changed (5) hide show
  1. app.py +116 -0
  2. assets/sample_input.mp3 +0 -0
  3. assets/sample_input_2.mp3 +0 -0
  4. lang_list.py +255 -0
  5. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import torch
4
+ import torchaudio
5
+
6
+ from transformers import SeamlessM4Tv2Model, AutoProcessor
7
+ from lang_list import (
8
+ ASR_TARGET_LANGUAGE_NAMES,
9
+ LANGUAGE_NAME_TO_CODE,
10
+ S2ST_TARGET_LANGUAGE_NAMES,
11
+ S2TT_TARGET_LANGUAGE_NAMES,
12
+ T2ST_TARGET_LANGUAGE_NAMES,
13
+ T2TT_TARGET_LANGUAGE_NAMES,
14
+ TEXT_SOURCE_LANGUAGE_NAMES,
15
+ )
16
+
17
+ processor = AutoProcessor.from_pretrained("facebook/seamless-m4t-v2-large")
18
+ model = SeamlessM4Tv2Model.from_pretrained("facebook/seamless-m4t-v2-large")
19
+
20
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
21
+
22
+ AUDIO_SAMPLE_RATE = 16000.0
23
+ MAX_INPUT_AUDIO_LENGTH = 60 # in seconds
24
+ DEFAULT_TARGET_LANGUAGE = "French"
25
+
26
+ if torch.cuda.is_available():
27
+ device = torch.device("cuda:0")
28
+ dtype = torch.float16
29
+ else:
30
+ device = torch.device("cpu")
31
+ dtype = torch.float32
32
+
33
+ def preprocess_audio(input_audio: str) -> None:
34
+ arr, org_sr = torchaudio.load(input_audio)
35
+ new_arr = torchaudio.functional.resample(arr, orig_freq=org_sr, new_freq=AUDIO_SAMPLE_RATE)
36
+ max_length = int(MAX_INPUT_AUDIO_LENGTH * AUDIO_SAMPLE_RATE)
37
+ if new_arr.shape[1] > max_length:
38
+ new_arr = new_arr[:, :max_length]
39
+ gr.Warning(f"Input audio is too long. Only the first {MAX_INPUT_AUDIO_LENGTH} seconds is used.")
40
+ torchaudio.save(input_audio, new_arr, sample_rate=int(AUDIO_SAMPLE_RATE))
41
+
42
+
43
+ def run_s2st(
44
+ input_audio: str, source_language: str, target_language: str
45
+ ) -> tuple[tuple[int, np.ndarray] | None, str]:
46
+ preprocess_audio(input_audio)
47
+ source_language_code = LANGUAGE_NAME_TO_CODE[source_language]
48
+ target_language_code = LANGUAGE_NAME_TO_CODE[target_language]
49
+ arr, org_sr = torchaudio.load(input_audio)
50
+ audio_inputs = processor(audios=arr, return_tensors="pt",
51
+ sampling_rate=model.config.sampling_rate).to(device)
52
+ output = model.generate(**audio_inputs, return_intermediate_token_ids=True,
53
+ tgt_lang=target_language_code,)
54
+
55
+ audio_array_from_audio = output[0].cpu().numpy().squeeze()
56
+ text_tokens = output[2]
57
+ translated_text_from_text = processor.decode(text_tokens.tolist()[0], skip_special_tokens=True)
58
+ return (int(AUDIO_SAMPLE_RATE), audio_array_from_audio), translated_text_from_text
59
+
60
+ description = """
61
+ # Direct Speech to Speech Translation
62
+
63
+ This demo uses SeamlessM4T V2 to translate one speech directly into another.
64
+ The model being used here is [facebook/seamless-m4t-v2-large](https://huggingface.co/facebook/seamless-m4t-v2-large).
65
+
66
+ SeamlessM4T V2 is unified model enables multiple tasks like Speech-to-Speech (S2ST), Speech-to-Text (S2TT), Text-to-Speech (T2ST) translation and more, without relying on multiple separate models.
67
+ """
68
+
69
+ with gr.Blocks() as demo_s2st:
70
+ gr.Markdown(description)
71
+ with gr.Row():
72
+ with gr.Column():
73
+ with gr.Group():
74
+ input_audio = gr.Audio(label="Input speech", type="filepath")
75
+ source_language = gr.Dropdown(
76
+ label="Source language",
77
+ choices=ASR_TARGET_LANGUAGE_NAMES,
78
+ value="English",
79
+ )
80
+ target_language = gr.Dropdown(
81
+ label="Target language",
82
+ choices=S2ST_TARGET_LANGUAGE_NAMES,
83
+ value=DEFAULT_TARGET_LANGUAGE,
84
+ )
85
+ btn = gr.Button("Translate")
86
+ with gr.Column():
87
+ with gr.Group():
88
+ output_audio = gr.Audio(
89
+ label="Translated speech",
90
+ autoplay=False,
91
+ streaming=False,
92
+ type="numpy",
93
+ )
94
+ output_text = gr.Textbox(label="Translated text")
95
+
96
+ gr.Examples(
97
+ examples=[
98
+ ["assets/sample_input.mp3", "English", "French"],
99
+ ["assets/sample_input.mp3", "English", "Mandarin Chinese"],
100
+ ["assets/sample_input_2.mp3", "English", "Hindi"],
101
+ ["assets/sample_input_2.mp3", "English", "Spanish"],
102
+ ],
103
+ inputs=[input_audio, source_language, target_language],
104
+ outputs=[output_audio, output_text],
105
+ fn=run_s2st,
106
+ cache_examples=True,
107
+ allow_flagging="never",
108
+ )
109
+
110
+ btn.click(
111
+ fn=run_s2st,
112
+ inputs=[input_audio, source_language, target_language],
113
+ outputs=[output_audio, output_text],
114
+ )
115
+
116
+ demo_s2st.launch()
assets/sample_input.mp3 ADDED
Binary file (10.3 kB). View file
 
assets/sample_input_2.mp3 ADDED
Binary file (30.6 kB). View file
 
lang_list.py ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Language dict
2
+ language_code_to_name = {
3
+ "afr": "Afrikaans",
4
+ "amh": "Amharic",
5
+ "arb": "Modern Standard Arabic",
6
+ "ary": "Moroccan Arabic",
7
+ "arz": "Egyptian Arabic",
8
+ "asm": "Assamese",
9
+ "ast": "Asturian",
10
+ "azj": "North Azerbaijani",
11
+ "bel": "Belarusian",
12
+ "ben": "Bengali",
13
+ "bos": "Bosnian",
14
+ "bul": "Bulgarian",
15
+ "cat": "Catalan",
16
+ "ceb": "Cebuano",
17
+ "ces": "Czech",
18
+ "ckb": "Central Kurdish",
19
+ "cmn": "Mandarin Chinese",
20
+ "cym": "Welsh",
21
+ "dan": "Danish",
22
+ "deu": "German",
23
+ "ell": "Greek",
24
+ "eng": "English",
25
+ "est": "Estonian",
26
+ "eus": "Basque",
27
+ "fin": "Finnish",
28
+ "fra": "French",
29
+ "gaz": "West Central Oromo",
30
+ "gle": "Irish",
31
+ "glg": "Galician",
32
+ "guj": "Gujarati",
33
+ "heb": "Hebrew",
34
+ "hin": "Hindi",
35
+ "hrv": "Croatian",
36
+ "hun": "Hungarian",
37
+ "hye": "Armenian",
38
+ "ibo": "Igbo",
39
+ "ind": "Indonesian",
40
+ "isl": "Icelandic",
41
+ "ita": "Italian",
42
+ "jav": "Javanese",
43
+ "jpn": "Japanese",
44
+ "kam": "Kamba",
45
+ "kan": "Kannada",
46
+ "kat": "Georgian",
47
+ "kaz": "Kazakh",
48
+ "kea": "Kabuverdianu",
49
+ "khk": "Halh Mongolian",
50
+ "khm": "Khmer",
51
+ "kir": "Kyrgyz",
52
+ "kor": "Korean",
53
+ "lao": "Lao",
54
+ "lit": "Lithuanian",
55
+ "ltz": "Luxembourgish",
56
+ "lug": "Ganda",
57
+ "luo": "Luo",
58
+ "lvs": "Standard Latvian",
59
+ "mai": "Maithili",
60
+ "mal": "Malayalam",
61
+ "mar": "Marathi",
62
+ "mkd": "Macedonian",
63
+ "mlt": "Maltese",
64
+ "mni": "Meitei",
65
+ "mya": "Burmese",
66
+ "nld": "Dutch",
67
+ "nno": "Norwegian Nynorsk",
68
+ "nob": "Norwegian Bokm\u00e5l",
69
+ "npi": "Nepali",
70
+ "nya": "Nyanja",
71
+ "oci": "Occitan",
72
+ "ory": "Odia",
73
+ "pan": "Punjabi",
74
+ "pbt": "Southern Pashto",
75
+ "pes": "Western Persian",
76
+ "pol": "Polish",
77
+ "por": "Portuguese",
78
+ "ron": "Romanian",
79
+ "rus": "Russian",
80
+ "slk": "Slovak",
81
+ "slv": "Slovenian",
82
+ "sna": "Shona",
83
+ "snd": "Sindhi",
84
+ "som": "Somali",
85
+ "spa": "Spanish",
86
+ "srp": "Serbian",
87
+ "swe": "Swedish",
88
+ "swh": "Swahili",
89
+ "tam": "Tamil",
90
+ "tel": "Telugu",
91
+ "tgk": "Tajik",
92
+ "tgl": "Tagalog",
93
+ "tha": "Thai",
94
+ "tur": "Turkish",
95
+ "ukr": "Ukrainian",
96
+ "urd": "Urdu",
97
+ "uzn": "Northern Uzbek",
98
+ "vie": "Vietnamese",
99
+ "xho": "Xhosa",
100
+ "yor": "Yoruba",
101
+ "yue": "Cantonese",
102
+ "zlm": "Colloquial Malay",
103
+ "zsm": "Standard Malay",
104
+ "zul": "Zulu",
105
+ }
106
+ LANGUAGE_NAME_TO_CODE = {v: k for k, v in language_code_to_name.items()}
107
+
108
+ # Source langs: S2ST / S2TT / ASR don't need source lang
109
+ # T2TT / T2ST use this
110
+ text_source_language_codes = [
111
+ "afr",
112
+ "amh",
113
+ "arb",
114
+ "ary",
115
+ "arz",
116
+ "asm",
117
+ "azj",
118
+ "bel",
119
+ "ben",
120
+ "bos",
121
+ "bul",
122
+ "cat",
123
+ "ceb",
124
+ "ces",
125
+ "ckb",
126
+ "cmn",
127
+ "cym",
128
+ "dan",
129
+ "deu",
130
+ "ell",
131
+ "eng",
132
+ "est",
133
+ "eus",
134
+ "fin",
135
+ "fra",
136
+ "gaz",
137
+ "gle",
138
+ "glg",
139
+ "guj",
140
+ "heb",
141
+ "hin",
142
+ "hrv",
143
+ "hun",
144
+ "hye",
145
+ "ibo",
146
+ "ind",
147
+ "isl",
148
+ "ita",
149
+ "jav",
150
+ "jpn",
151
+ "kan",
152
+ "kat",
153
+ "kaz",
154
+ "khk",
155
+ "khm",
156
+ "kir",
157
+ "kor",
158
+ "lao",
159
+ "lit",
160
+ "lug",
161
+ "luo",
162
+ "lvs",
163
+ "mai",
164
+ "mal",
165
+ "mar",
166
+ "mkd",
167
+ "mlt",
168
+ "mni",
169
+ "mya",
170
+ "nld",
171
+ "nno",
172
+ "nob",
173
+ "npi",
174
+ "nya",
175
+ "ory",
176
+ "pan",
177
+ "pbt",
178
+ "pes",
179
+ "pol",
180
+ "por",
181
+ "ron",
182
+ "rus",
183
+ "slk",
184
+ "slv",
185
+ "sna",
186
+ "snd",
187
+ "som",
188
+ "spa",
189
+ "srp",
190
+ "swe",
191
+ "swh",
192
+ "tam",
193
+ "tel",
194
+ "tgk",
195
+ "tgl",
196
+ "tha",
197
+ "tur",
198
+ "ukr",
199
+ "urd",
200
+ "uzn",
201
+ "vie",
202
+ "yor",
203
+ "yue",
204
+ "zsm",
205
+ "zul",
206
+ ]
207
+ TEXT_SOURCE_LANGUAGE_NAMES = sorted([language_code_to_name[code] for code in text_source_language_codes])
208
+
209
+ # Target langs:
210
+ # S2ST / T2ST
211
+ s2st_target_language_codes = [
212
+ "eng",
213
+ "arb",
214
+ "ben",
215
+ "cat",
216
+ "ces",
217
+ "cmn",
218
+ "cym",
219
+ "dan",
220
+ "deu",
221
+ "est",
222
+ "fin",
223
+ "fra",
224
+ "hin",
225
+ "ind",
226
+ "ita",
227
+ "jpn",
228
+ "kor",
229
+ "mlt",
230
+ "nld",
231
+ "pes",
232
+ "pol",
233
+ "por",
234
+ "ron",
235
+ "rus",
236
+ "slk",
237
+ "spa",
238
+ "swe",
239
+ "swh",
240
+ "tel",
241
+ "tgl",
242
+ "tha",
243
+ "tur",
244
+ "ukr",
245
+ "urd",
246
+ "uzn",
247
+ "vie",
248
+ ]
249
+ S2ST_TARGET_LANGUAGE_NAMES = sorted([language_code_to_name[code] for code in s2st_target_language_codes])
250
+ T2ST_TARGET_LANGUAGE_NAMES = S2ST_TARGET_LANGUAGE_NAMES
251
+
252
+ # S2TT / T2TT / ASR
253
+ S2TT_TARGET_LANGUAGE_NAMES = TEXT_SOURCE_LANGUAGE_NAMES
254
+ T2TT_TARGET_LANGUAGE_NAMES = TEXT_SOURCE_LANGUAGE_NAMES
255
+ ASR_TARGET_LANGUAGE_NAMES = TEXT_SOURCE_LANGUAGE_NAMES
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ torch==2.1.0
3
+ torchaudio==2.1.0