Spaces:
Runtime error
Runtime error
Update for Russian app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import torch
|
|
|
|
|
4 |
from datasets import load_dataset
|
5 |
|
6 |
-
from transformers import
|
7 |
-
|
8 |
|
9 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
# load text-to-speech checkpoint and speaker embeddings
|
15 |
-
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
outputs =
|
26 |
-
|
|
|
27 |
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def synthesise(text):
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
return speech.cpu()
|
33 |
|
34 |
|
35 |
def speech_to_speech_translation(audio):
|
36 |
-
|
37 |
-
|
|
|
38 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
39 |
-
return 16000, synthesised_speech
|
40 |
|
41 |
|
42 |
title = "Cascaded STST"
|
43 |
description = """
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
47 |
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
48 |
"""
|
49 |
|
|
|
1 |
+
"Задание 3"
|
2 |
+
|
3 |
+
!pip install datasets #>=2.6.1
|
4 |
+
!pip install git+https://github.com/huggingface/transformers
|
5 |
+
!pip -q install gradio==3.28.3
|
6 |
+
!pip install phonemizer
|
7 |
+
!pip install py-espeak-ng
|
8 |
+
|
9 |
+
%pip install sentencepiece
|
10 |
+
%pip install --pre ekorpkit
|
11 |
+
|
12 |
import gradio as gr
|
13 |
import numpy as np
|
14 |
import torch
|
15 |
+
import espeakng
|
16 |
+
import sentencepiece
|
17 |
from datasets import load_dataset
|
18 |
|
19 |
+
from transformers import pipeline, MarianMTModel, MarianTokenizer, VitsModel, VitsTokenizer
|
|
|
20 |
|
21 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
22 |
|
23 |
+
import phonemizer
|
24 |
+
model_wav2vec = 'facebook/wav2vec2-lv-60-espeak-cv-ft'
|
25 |
+
asr_pipe = pipeline("automatic-speech-recognition", model=model_wav2vec, device=device)
|
|
|
|
|
26 |
|
27 |
+
# from speech to text
|
28 |
+
def translate_audio(audio):
|
29 |
+
outputs = asr_pipe(audio, max_new_tokens=256,
|
30 |
+
generate_kwargs={"task": "translate"})
|
31 |
+
return outputs["text"]
|
32 |
|
33 |
+
# translation
|
34 |
+
def translate_text(text, from_language, target_language): #to English -mul en, to Russian - en ru
|
35 |
+
model_name = f'Helsinki-NLP/opus-mt-{from_language}-{target_language}'
|
36 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
37 |
+
model = MarianMTModel.from_pretrained(model_name)
|
38 |
|
39 |
+
inputs = tokenizer.encode(text, return_tensors="pt")
|
40 |
+
outputs = model.generate(inputs, num_beams=4, max_length=50, early_stopping=True)
|
41 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
42 |
+
return translated_text
|
43 |
|
44 |
+
# load text-to-speech checkpoint
|
45 |
+
#model = pipeline("text-to-speech", model="voxxer/speecht5_finetuned_commonvoice_ru_translit")
|
46 |
+
model = VitsModel.from_pretrained("voxxer/speecht5_finetuned_commonvoice_ru_translit")
|
47 |
+
tokenizer = VitsTokenizer.from_pretrained("facebook/mms-tts-rus")
|
48 |
|
49 |
def synthesise(text):
|
50 |
+
translated_text = translate_text(text, 'mul', 'en')
|
51 |
+
translated_text = translate_text(translate_text, 'en', 'ru')
|
52 |
+
inputs = tokenizer(translated_text, return_tensors="pt")
|
53 |
+
input_ids = inputs["input_ids"]
|
54 |
+
with torch.no_grad():
|
55 |
+
outputs = model(input_ids)
|
56 |
+
speech = outputs["waveform"]
|
57 |
return speech.cpu()
|
58 |
|
59 |
|
60 |
def speech_to_speech_translation(audio):
|
61 |
+
text_from_audio = translate_audio(audio)
|
62 |
+
print(translated_text)
|
63 |
+
synthesised_speech = synthesise(text_from_audio)
|
64 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
65 |
+
return 16000, synthesised_speech[0]
|
66 |
|
67 |
|
68 |
title = "Cascaded STST"
|
69 |
description = """
|
70 |
+
* В начале происходит распознавание речи с помощью модели facebook/wav2vec2-lv-60-espeak-cv-ft и на выходе получается текст на любом из 60 языков.
|
71 |
+
* Затем полученный текст переводится сначала на английский с помощью Helsinki-NLP/opus-mt-mul-en, а потом на русский с помощью Helsinki-NLP/opus-mt-en-ru
|
72 |
+
* На последнем шаге полученный текст озвучивается с помощью fine-tune-говой версии microsoft/speecht5_tts - voxxer/speecht5_finetuned_commonvoice_ru_translit
|
73 |
+
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in Russian. Demo uses facebook/mms-tts-rus model for text-to-speech:
|
74 |
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
75 |
"""
|
76 |
|