Update app.py
Browse files
app.py
CHANGED
@@ -6,42 +6,7 @@ from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Proce
|
|
6 |
|
7 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
8 |
|
9 |
-
|
10 |
-
translation_models = {
|
11 |
-
"en": "Helsinki-NLP/opus-mt-en-es", # Inglés a Español
|
12 |
-
"fr": "Helsinki-NLP/opus-mt-fr-es", # Francés a Español
|
13 |
-
"de": "Helsinki-NLP/opus-mt-de-es", # Alemán a Español
|
14 |
-
"it": "Helsinki-NLP/opus-mt-it-es", # Italiano a Español
|
15 |
-
"pt": "Helsinki-NLP/opus-mt-pt-es", # Portugués a Español
|
16 |
-
"nl": "Helsinki-NLP/opus-mt-nl-es", # Neerlandés (Holandés) a Español
|
17 |
-
"fi": "Helsinki-NLP/opus-mt-fi-es", # Finés a Español
|
18 |
-
"sv": "Helsinki-NLP/opus-mt-sv-es", # Sueco a Español
|
19 |
-
"da": "Helsinki-NLP/opus-mt-da-es", # Danés a Español
|
20 |
-
"no": "Helsinki-NLP/opus-mt-no-es", # Noruego a Español
|
21 |
-
"ru": "Helsinki-NLP/opus-mt-ru-es", # Ruso a Español
|
22 |
-
"pl": "Helsinki-NLP/opus-mt-pl-es", # Polaco a Español
|
23 |
-
"cs": "Helsinki-NLP/opus-mt-cs-es", # Checo a Español
|
24 |
-
"tr": "Helsinki-NLP/opus-mt-tr-es", # Turco a Español
|
25 |
-
"zh": "Helsinki-NLP/opus-mt-zh-es", # Chino a Español
|
26 |
-
"ja": "Helsinki-NLP/opus-mt-ja-es", # Japonés a Español
|
27 |
-
"ar": "Helsinki-NLP/opus-mt-ar-es", # Árabe a Español
|
28 |
-
"ro": "Helsinki-NLP/opus-mt-ro-es", # Rumano a Español
|
29 |
-
"el": "Helsinki-NLP/opus-mt-el-es", # Griego a Español
|
30 |
-
"bg": "Helsinki-NLP/opus-mt-bg-es", # Búlgaro a Español
|
31 |
-
"uk": "Helsinki-NLP/opus-mt-uk-es", # Ucraniano a Español
|
32 |
-
"he": "Helsinki-NLP/opus-mt-he-es", # Hebreo a Español
|
33 |
-
"lt": "Helsinki-NLP/opus-mt-lt-es", # Lituano a Español
|
34 |
-
"et": "Helsinki-NLP/opus-mt-et-es", # Estonio a Español
|
35 |
-
"hr": "Helsinki-NLP/opus-mt-hr-es", # Croata a Español
|
36 |
-
"hu": "Helsinki-NLP/opus-mt-hu-es", # Húngaro a Español
|
37 |
-
"lv": "Helsinki-NLP/opus-mt-lv-es", # Letón a Español
|
38 |
-
"sl": "Helsinki-NLP/opus-mt-sl-es", # Esloveno a Español
|
39 |
-
"sk": "Helsinki-NLP/opus-mt-sk-es", # Eslovaco a Español
|
40 |
-
"sr": "Helsinki-NLP/opus-mt-sr-es", # Serbio a Español
|
41 |
-
"fa": "Helsinki-NLP/opus-mt-fa-es", # Persa a Español
|
42 |
-
}
|
43 |
-
|
44 |
-
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
|
45 |
|
46 |
#vist_model = VitsModel.from_pretrained("facebook/mms-tts-spa")
|
47 |
#vist_tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-spa")
|
@@ -66,26 +31,10 @@ def language_detector(text):
|
|
66 |
return idioma_detectado
|
67 |
|
68 |
def translate(audio):
|
69 |
-
|
70 |
-
|
71 |
-
codigo_idioma = language_detector(transcribe['text'])
|
72 |
-
|
73 |
-
if codigo_idioma in translation_models:
|
74 |
-
translator = pipeline("translation", model=translation_models[codigo_idioma])
|
75 |
-
traduccion = translator(transcribe['text'])
|
76 |
-
else:
|
77 |
-
transcribe = transcribe['text']
|
78 |
-
print(f"No hay un modelo de traducción disponible para el idioma detectado {codigo_idioma}")
|
79 |
-
return transcribe
|
80 |
-
|
81 |
-
return traduccion
|
82 |
|
83 |
def synthesise(text):
|
84 |
-
if isinstance(text, list):
|
85 |
-
text = text[0]['translation_text']
|
86 |
-
else:
|
87 |
-
text = text
|
88 |
-
print(text)
|
89 |
inputs = processor(text=text, return_tensors="pt")
|
90 |
output = model.generate_speech(inputs["input_ids"], speaker_embeddings2, vocoder=vocoder)
|
91 |
return output
|
|
|
6 |
|
7 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
8 |
|
9 |
+
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2", device=device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
#vist_model = VitsModel.from_pretrained("facebook/mms-tts-spa")
|
12 |
#vist_tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-spa")
|
|
|
31 |
return idioma_detectado
|
32 |
|
33 |
def translate(audio):
|
34 |
+
outputs = pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "es"})
|
35 |
+
return outputs["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
def synthesise(text):
|
|
|
|
|
|
|
|
|
|
|
38 |
inputs = processor(text=text, return_tensors="pt")
|
39 |
output = model.generate_speech(inputs["input_ids"], speaker_embeddings2, vocoder=vocoder)
|
40 |
return output
|