Spaces:
Runtime error
Runtime error
AlexanderMoshnov
commited on
Commit
·
fa5b21a
1
Parent(s):
dbfdf1a
Upload app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import torch
|
4 |
from datasets import load_dataset
|
5 |
|
6 |
-
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
7 |
-
|
8 |
|
9 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
# load speech translation checkpoint
|
12 |
-
asr_pipe = pipeline("automatic-speech-recognition", model="
|
13 |
|
|
|
14 |
# load text-to-speech checkpoint and speaker embeddings
|
15 |
-
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def translate(audio):
|
25 |
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
|
@@ -27,23 +51,31 @@ def translate(audio):
|
|
27 |
|
28 |
|
29 |
def synthesise(text):
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
return speech.cpu()
|
33 |
|
34 |
|
35 |
def speech_to_speech_translation(audio):
|
36 |
translated_text = translate(audio)
|
|
|
37 |
synthesised_speech = synthesise(translated_text)
|
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 |
|
@@ -61,12 +93,12 @@ file_translate = gr.Interface(
|
|
61 |
fn=speech_to_speech_translation,
|
62 |
inputs=gr.Audio(source="upload", type="filepath"),
|
63 |
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
64 |
-
examples=[["./example.wav"]],
|
65 |
title=title,
|
66 |
description=description,
|
67 |
)
|
68 |
|
69 |
with demo:
|
70 |
-
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "
|
71 |
|
72 |
demo.launch()
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""HW3_ml.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1z4ht7K9pttbgWmDDnrQhqoZ6SYAiaeUe
|
8 |
+
"""
|
9 |
+
|
10 |
+
# !pip -q uninstall gradio -y
|
11 |
+
# !pip -q install gradio==3.50.2
|
12 |
+
|
13 |
+
# !pip -q install datasets
|
14 |
+
|
15 |
import gradio as gr
|
16 |
import numpy as np
|
17 |
import torch
|
18 |
from datasets import load_dataset
|
19 |
|
20 |
+
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline, WhisperProcessor
|
|
|
21 |
|
22 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
23 |
|
24 |
# load speech translation checkpoint
|
25 |
+
asr_pipe = pipeline("automatic-speech-recognition", model="voidful/wav2vec2-xlsr-multilingual-56", device=device)
|
26 |
|
27 |
+
# !pip -q install sentencepiece
|
28 |
# load text-to-speech checkpoint and speaker embeddings
|
29 |
+
# processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
30 |
+
processor = WhisperProcessor.from_pretrained(
|
31 |
+
"openai/whisper-small")
|
32 |
|
33 |
+
translator1 = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
34 |
+
translator2 = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ru")
|
35 |
|
36 |
+
from transformers import VitsModel, VitsTokenizer
|
|
|
37 |
|
38 |
+
# model = pipeline("text-to-speech", model="suno/bark-small")
|
39 |
+
|
40 |
+
model = VitsModel.from_pretrained("facebook/mms-tts-rus")
|
41 |
+
tokenizer = VitsTokenizer.from_pretrained("facebook/mms-tts-rus")
|
42 |
+
|
43 |
+
def translator_mul_ru(text):
|
44 |
+
|
45 |
+
translation = translator2(translator1(text)[0]['translation_text'])
|
46 |
+
return translation[0]['translation_text']
|
47 |
|
48 |
def translate(audio):
|
49 |
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
|
|
|
51 |
|
52 |
|
53 |
def synthesise(text):
|
54 |
+
translated_text = translator_mul_ru(text)
|
55 |
+
inputs = tokenizer(translated_text, return_tensors="pt")
|
56 |
+
input_ids = inputs["input_ids"]
|
57 |
+
|
58 |
+
with torch.no_grad():
|
59 |
+
outputs = model(input_ids)
|
60 |
+
speech = outputs["waveform"]
|
61 |
return speech.cpu()
|
62 |
|
63 |
|
64 |
def speech_to_speech_translation(audio):
|
65 |
translated_text = translate(audio)
|
66 |
+
print(translated_text)
|
67 |
synthesised_speech = synthesise(translated_text)
|
68 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
69 |
+
return 16000, synthesised_speech[0]
|
|
|
70 |
|
71 |
title = "Cascaded STST"
|
72 |
description = """
|
73 |
+
* Сначала модель распознает речь с помощью voidful/wav2vec2-xlsr-multilingual-56 и возвращает текст на любом из 56 языков.
|
74 |
+
* Далее происходит перевод текста с любого на английский с помощью Helsinki-NLP/opus-mt-mul-en, а затем с английского на русский также с помощью Helsinki-NLP/opus-mt-en-ru
|
75 |
+
* В конце осуществляется воспроизведение русского текста моделью facebook/mms-tts-rus
|
76 |
+
|
77 |
|
78 |
+
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:
|
79 |
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
80 |
"""
|
81 |
|
|
|
93 |
fn=speech_to_speech_translation,
|
94 |
inputs=gr.Audio(source="upload", type="filepath"),
|
95 |
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
|
|
96 |
title=title,
|
97 |
description=description,
|
98 |
)
|
99 |
|
100 |
with demo:
|
101 |
+
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "File"])
|
102 |
|
103 |
demo.launch()
|
104 |
+
|