Spaces:
Runtime error
Runtime error
Aboubacar OUATTARA - kaira
commited on
Commit
·
d72812e
1
Parent(s):
3c47b68
initial commit
Browse files- app.py +116 -0
- flore200_codes.py +211 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import concurrent
|
2 |
+
|
3 |
+
from transformers import pipeline
|
4 |
+
import gradio as gr
|
5 |
+
import torch
|
6 |
+
import torchaudio
|
7 |
+
from resemble_enhance.enhancer.inference import denoise, enhance
|
8 |
+
|
9 |
+
from flore200_codes import flores_codes
|
10 |
+
|
11 |
+
# Check if CUDA is available
|
12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
+
|
14 |
+
# Translation pipeline
|
15 |
+
translation_model = "oza75/nllb-600M-mt-french-bambara"
|
16 |
+
translator = pipeline("translation", model=translation_model, max_length=512)
|
17 |
+
|
18 |
+
# Text-to-Speech pipeline
|
19 |
+
tts_model = "oza75/bambara-tts-male-001"
|
20 |
+
tts = pipeline("text-to-speech", model=tts_model, device=device)
|
21 |
+
|
22 |
+
|
23 |
+
# Function to translate text to Bambara
|
24 |
+
def translate_to_bambara(text, src_lang):
|
25 |
+
translation = translator(text, src_lang=src_lang, tgt_lang="bam_Latn")
|
26 |
+
return translation[0]['translation_text']
|
27 |
+
|
28 |
+
|
29 |
+
# Function to convert text to speech
|
30 |
+
def text_to_speech(bambara_text):
|
31 |
+
speech = tts(bambara_text)
|
32 |
+
audio, sr = speech['audio'], speech['sampling_rate']
|
33 |
+
audio = torch.from_numpy(audio).mean(dim=0)
|
34 |
+
|
35 |
+
return audio, sr
|
36 |
+
|
37 |
+
|
38 |
+
# Function to enhance speech
|
39 |
+
def enhance_speech(audio_array, sampling_rate, solver, nfe, tau, denoise_before_enhancement):
|
40 |
+
solver = solver.lower()
|
41 |
+
nfe = int(nfe)
|
42 |
+
lambd = 0.9 if denoise_before_enhancement else 0.1
|
43 |
+
|
44 |
+
def denoise_audio():
|
45 |
+
return denoise(audio_array, sampling_rate, device)
|
46 |
+
|
47 |
+
def enhance_audio():
|
48 |
+
return enhance(audio_array, sampling_rate, device, nfe=nfe, solver=solver, lambd=lambd, tau=tau)
|
49 |
+
|
50 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
51 |
+
future_denoise = executor.submit(denoise_audio)
|
52 |
+
future_enhance = executor.submit(enhance_audio)
|
53 |
+
|
54 |
+
denoised_audio, new_sr1 = future_denoise.result()
|
55 |
+
enhanced_audio, new_sr2 = future_enhance.result()
|
56 |
+
|
57 |
+
# Convert to numpy and return
|
58 |
+
return (new_sr1, denoised_audio.cpu().numpy()), (new_sr2, enhanced_audio.cpu().numpy())
|
59 |
+
|
60 |
+
|
61 |
+
# Define the Gradio interface
|
62 |
+
def _fn(src_lang, text, solver="Midpoint", nfe=64, prior_temp=0.5, denoise_before_enhancement=False):
|
63 |
+
source_lang = flores_codes[src_lang]
|
64 |
+
|
65 |
+
# Step 1: Translate the text to Bambara
|
66 |
+
bambara_text = translate_to_bambara(text, source_lang)
|
67 |
+
|
68 |
+
# Step 2: Convert the translated text to speech
|
69 |
+
audio_array, sampling_rate = text_to_speech(bambara_text)
|
70 |
+
|
71 |
+
# Step 3: Enhance the audio
|
72 |
+
denoised_audio, enhanced_audio = enhance_speech(
|
73 |
+
audio_array,
|
74 |
+
sampling_rate,
|
75 |
+
solver,
|
76 |
+
nfe,
|
77 |
+
prior_temp,
|
78 |
+
denoise_before_enhancement
|
79 |
+
)
|
80 |
+
|
81 |
+
# Return all outputs
|
82 |
+
return bambara_text, (sampling_rate, audio_array.cpu().numpy()), denoised_audio, enhanced_audio
|
83 |
+
|
84 |
+
|
85 |
+
def main():
|
86 |
+
lang_codes = list(flores_codes.keys())
|
87 |
+
|
88 |
+
# Build Gradio app
|
89 |
+
app = gr.Interface(
|
90 |
+
fn=_fn,
|
91 |
+
inputs=[
|
92 |
+
gr.Dropdown(label="Source Language", choices=lang_codes, value='French'),
|
93 |
+
gr.Textbox(label="Text to Translate"),
|
94 |
+
gr.Dropdown(
|
95 |
+
choices=["Midpoint", "RK4", "Euler"], value="Midpoint",
|
96 |
+
label="ODE Solver (Midpoint is recommended)"
|
97 |
+
),
|
98 |
+
gr.Slider(minimum=1, maximum=128, value=64, step=1, label="Number of Function Evaluations"),
|
99 |
+
gr.Slider(minimum=0, maximum=1, value=0.5, step=0.01, label="Prior Temperature"),
|
100 |
+
gr.Checkbox(value=False, label="Denoise Before Enhancement")
|
101 |
+
],
|
102 |
+
outputs=[
|
103 |
+
gr.Textbox(label="Translated Text"),
|
104 |
+
gr.Audio(label="Original TTS Audio"),
|
105 |
+
gr.Audio(label="Denoised Audio"),
|
106 |
+
gr.Audio(label="Enhanced Audio")
|
107 |
+
],
|
108 |
+
title="Bambara Translation and Text to Speech with Audio Enhancement",
|
109 |
+
description="Translate text to Bambara and convert it to speech with options to enhance audio quality."
|
110 |
+
)
|
111 |
+
|
112 |
+
app.launch()
|
113 |
+
|
114 |
+
|
115 |
+
if __name__ == "__main__":
|
116 |
+
main()
|
flore200_codes.py
ADDED
@@ -0,0 +1,211 @@
|
|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arabic ajp_Arab
|
8 |
+
Akan aka_Latn
|
9 |
+
Amharic amh_Ethi
|
10 |
+
North Levantine Arabic apc_Arab
|
11 |
+
Modern Standard Arabic arb_Arab
|
12 |
+
Modern Standard Arabic (Romanized) arb_Latn
|
13 |
+
Najdi Arabic ars_Arab
|
14 |
+
Moroccan Arabic ary_Arab
|
15 |
+
Egyptian Arabic arz_Arab
|
16 |
+
Assamese asm_Beng
|
17 |
+
Asturian ast_Latn
|
18 |
+
Awadhi awa_Deva
|
19 |
+
Central Aymara ayr_Latn
|
20 |
+
South Azerbaijani azb_Arab
|
21 |
+
North Azerbaijani azj_Latn
|
22 |
+
Bashkir bak_Cyrl
|
23 |
+
Bambara bam_Latn
|
24 |
+
Balinese ban_Latn
|
25 |
+
Belarusian bel_Cyrl
|
26 |
+
Bemba bem_Latn
|
27 |
+
Bengali ben_Beng
|
28 |
+
Bhojpuri bho_Deva
|
29 |
+
Banjar (Arabic script) bjn_Arab
|
30 |
+
Banjar (Latin script) bjn_Latn
|
31 |
+
Standard Tibetan bod_Tibt
|
32 |
+
Bosnian bos_Latn
|
33 |
+
Buginese bug_Latn
|
34 |
+
Bulgarian bul_Cyrl
|
35 |
+
Catalan cat_Latn
|
36 |
+
Cebuano ceb_Latn
|
37 |
+
Czech ces_Latn
|
38 |
+
Chokwe cjk_Latn
|
39 |
+
Central Kurdish ckb_Arab
|
40 |
+
Crimean Tatar crh_Latn
|
41 |
+
Welsh cym_Latn
|
42 |
+
Danish dan_Latn
|
43 |
+
German deu_Latn
|
44 |
+
Southwestern Dinka dik_Latn
|
45 |
+
Dyula dyu_Latn
|
46 |
+
Dzongkha dzo_Tibt
|
47 |
+
Greek ell_Grek
|
48 |
+
English eng_Latn
|
49 |
+
Esperanto epo_Latn
|
50 |
+
Estonian est_Latn
|
51 |
+
Basque eus_Latn
|
52 |
+
Ewe ewe_Latn
|
53 |
+
Faroese fao_Latn
|
54 |
+
Fijian fij_Latn
|
55 |
+
Finnish fin_Latn
|
56 |
+
Fon fon_Latn
|
57 |
+
French fra_Latn
|
58 |
+
Friulian fur_Latn
|
59 |
+
Nigerian Fulfulde fuv_Latn
|
60 |
+
Scottish Gaelic gla_Latn
|
61 |
+
Irish gle_Latn
|
62 |
+
Galician glg_Latn
|
63 |
+
Guarani grn_Latn
|
64 |
+
Gujarati guj_Gujr
|
65 |
+
Haitian Creole hat_Latn
|
66 |
+
Hausa hau_Latn
|
67 |
+
Hebrew heb_Hebr
|
68 |
+
Hindi hin_Deva
|
69 |
+
Chhattisgarhi hne_Deva
|
70 |
+
Croatian hrv_Latn
|
71 |
+
Hungarian hun_Latn
|
72 |
+
Armenian hye_Armn
|
73 |
+
Igbo ibo_Latn
|
74 |
+
Ilocano ilo_Latn
|
75 |
+
Indonesian ind_Latn
|
76 |
+
Icelandic isl_Latn
|
77 |
+
Italian ita_Latn
|
78 |
+
Javanese jav_Latn
|
79 |
+
Japanese jpn_Jpan
|
80 |
+
Kabyle kab_Latn
|
81 |
+
Jingpho kac_Latn
|
82 |
+
Kamba kam_Latn
|
83 |
+
Kannada kan_Knda
|
84 |
+
Kashmiri (Arabic script) kas_Arab
|
85 |
+
Kashmiri (Devanagari script) kas_Deva
|
86 |
+
Georgian kat_Geor
|
87 |
+
Central Kanuri (Arabic script) knc_Arab
|
88 |
+
Central Kanuri (Latin script) knc_Latn
|
89 |
+
Kazakh kaz_Cyrl
|
90 |
+
Kabiyè kbp_Latn
|
91 |
+
Kabuverdianu kea_Latn
|
92 |
+
Khmer khm_Khmr
|
93 |
+
Kikuyu kik_Latn
|
94 |
+
Kinyarwanda kin_Latn
|
95 |
+
Kyrgyz kir_Cyrl
|
96 |
+
Kimbundu kmb_Latn
|
97 |
+
Northern Kurdish kmr_Latn
|
98 |
+
Kikongo kon_Latn
|
99 |
+
Korean kor_Hang
|
100 |
+
Lao lao_Laoo
|
101 |
+
Ligurian lij_Latn
|
102 |
+
Limburgish lim_Latn
|
103 |
+
Lingala lin_Latn
|
104 |
+
Lithuanian lit_Latn
|
105 |
+
Lombard lmo_Latn
|
106 |
+
Latgalian ltg_Latn
|
107 |
+
Luxembourgish ltz_Latn
|
108 |
+
Luba-Kasai lua_Latn
|
109 |
+
Ganda lug_Latn
|
110 |
+
Luo luo_Latn
|
111 |
+
Mizo lus_Latn
|
112 |
+
Standard Latvian lvs_Latn
|
113 |
+
Magahi mag_Deva
|
114 |
+
Maithili mai_Deva
|
115 |
+
Malayalam mal_Mlym
|
116 |
+
Marathi mar_Deva
|
117 |
+
Minangkabau (Arabic script) min_Arab
|
118 |
+
Minangkabau (Latin script) min_Latn
|
119 |
+
Macedonian mkd_Cyrl
|
120 |
+
Plateau Malagasy plt_Latn
|
121 |
+
Maltese mlt_Latn
|
122 |
+
Meitei (Bengali script) mni_Beng
|
123 |
+
Halh Mongolian khk_Cyrl
|
124 |
+
Mossi mos_Latn
|
125 |
+
Maori mri_Latn
|
126 |
+
Burmese mya_Mymr
|
127 |
+
Dutch nld_Latn
|
128 |
+
Norwegian Nynorsk nno_Latn
|
129 |
+
Norwegian Bokmål nob_Latn
|
130 |
+
Nepali npi_Deva
|
131 |
+
Northern Sotho nso_Latn
|
132 |
+
Nuer nus_Latn
|
133 |
+
Nyanja nya_Latn
|
134 |
+
Occitan oci_Latn
|
135 |
+
West Central Oromo gaz_Latn
|
136 |
+
Odia ory_Orya
|
137 |
+
Pangasinan pag_Latn
|
138 |
+
Eastern Panjabi pan_Guru
|
139 |
+
Papiamento pap_Latn
|
140 |
+
Western Persian pes_Arab
|
141 |
+
Polish pol_Latn
|
142 |
+
Portuguese por_Latn
|
143 |
+
Dari prs_Arab
|
144 |
+
Southern Pashto pbt_Arab
|
145 |
+
Ayacucho Quechua quy_Latn
|
146 |
+
Romanian ron_Latn
|
147 |
+
Rundi run_Latn
|
148 |
+
Russian rus_Cyrl
|
149 |
+
Sango sag_Latn
|
150 |
+
Sanskrit san_Deva
|
151 |
+
Santali sat_Olck
|
152 |
+
Sicilian scn_Latn
|
153 |
+
Shan shn_Mymr
|
154 |
+
Sinhala sin_Sinh
|
155 |
+
Slovak slk_Latn
|
156 |
+
Slovenian slv_Latn
|
157 |
+
Samoan smo_Latn
|
158 |
+
Shona sna_Latn
|
159 |
+
Sindhi snd_Arab
|
160 |
+
Somali som_Latn
|
161 |
+
Southern Sotho sot_Latn
|
162 |
+
Spanish spa_Latn
|
163 |
+
Tosk Albanian als_Latn
|
164 |
+
Sardinian srd_Latn
|
165 |
+
Serbian srp_Cyrl
|
166 |
+
Swati ssw_Latn
|
167 |
+
Sundanese sun_Latn
|
168 |
+
Swedish swe_Latn
|
169 |
+
Swahili swh_Latn
|
170 |
+
Silesian szl_Latn
|
171 |
+
Tamil tam_Taml
|
172 |
+
Tatar tat_Cyrl
|
173 |
+
Telugu tel_Telu
|
174 |
+
Tajik tgk_Cyrl
|
175 |
+
Tagalog tgl_Latn
|
176 |
+
Thai tha_Thai
|
177 |
+
Tigrinya tir_Ethi
|
178 |
+
Tamasheq (Latin script) taq_Latn
|
179 |
+
Tamasheq (Tifinagh script) taq_Tfng
|
180 |
+
Tok Pisin tpi_Latn
|
181 |
+
Tswana tsn_Latn
|
182 |
+
Tsonga tso_Latn
|
183 |
+
Turkmen tuk_Latn
|
184 |
+
Tumbuka tum_Latn
|
185 |
+
Turkish tur_Latn
|
186 |
+
Twi twi_Latn
|
187 |
+
Central Atlas Tamazight tzm_Tfng
|
188 |
+
Uyghur uig_Arab
|
189 |
+
Ukrainian ukr_Cyrl
|
190 |
+
Umbundu umb_Latn
|
191 |
+
Urdu urd_Arab
|
192 |
+
Northern Uzbek uzn_Latn
|
193 |
+
Venetian vec_Latn
|
194 |
+
Vietnamese vie_Latn
|
195 |
+
Waray war_Latn
|
196 |
+
Wolof wol_Latn
|
197 |
+
Xhosa xho_Latn
|
198 |
+
Eastern Yiddish ydd_Hebr
|
199 |
+
Yoruba yor_Latn
|
200 |
+
Yue Chinese yue_Hant
|
201 |
+
Chinese (Simplified) zho_Hans
|
202 |
+
Chinese (Traditional) zho_Hant
|
203 |
+
Standard Malay zsm_Latn
|
204 |
+
Zulu zul_Latn'''
|
205 |
+
|
206 |
+
codes_as_string = codes_as_string.split('\n')
|
207 |
+
|
208 |
+
flores_codes = {}
|
209 |
+
for code in codes_as_string:
|
210 |
+
lang, lang_code = code.split('\t')
|
211 |
+
flores_codes[lang] = lang_code
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
gradio
|
3 |
+
torch
|
4 |
+
torchaudio
|
5 |
+
resemble-enhance
|